-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from libsese/refactor-pimpl
Refactor: pimpl (Plausibility check)
- Loading branch information
Showing
12 changed files
with
252 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2025 libsese | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <sese/system/Process.h> | ||
#include <sese/Util.h> | ||
|
||
#include <csignal> | ||
#include <unistd.h> | ||
#include <sys/wait.h> | ||
|
||
using namespace sese::system; | ||
|
||
class Process::ProcessImpl { | ||
public: | ||
|
||
static std::unique_ptr<ProcessImpl> createEx(const std::string &exec, const std::vector<std::string> &args) noexcept { | ||
auto pid = fork(); | ||
if (pid > 0) { | ||
// parent process | ||
return std::make_unique<ProcessImpl>(pid); | ||
} else if (pid == 0) { | ||
// client process | ||
auto c_args = new char *[args.size() + 2]; | ||
auto exec_string = new char[exec.size() + 1]; | ||
memcpy(exec_string, exec.c_str(), exec.size() + 1); | ||
c_args[0] = exec_string; | ||
int i = 1; | ||
for (auto &arg: args) { | ||
auto arg_string = new char[arg.size() + 1]; | ||
memcpy(arg_string, arg.c_str(), arg.size() + 1); | ||
c_args[i] = arg_string; | ||
i++; | ||
} | ||
c_args[args.size() + 1] = nullptr; | ||
|
||
if (execvp(exec.c_str(), c_args) == -1) { | ||
for (int i = 0; i < args.size() + 2; ++i) { | ||
delete[] c_args[i]; | ||
} | ||
delete[] c_args; | ||
exit(errno); | ||
return nullptr; // GCOVR_EXCL_LINE | ||
} else { | ||
// never reach | ||
return nullptr; // GCOVR_EXCL_LINE | ||
} | ||
} else { | ||
// failed to create | ||
return nullptr; // GCOVR_EXCL_LINE | ||
} | ||
} | ||
|
||
ProcessImpl(const sese::pid_t &pid) : pid(pid) { | ||
} | ||
|
||
static sese::pid_t getCurrentProcessId() noexcept { | ||
return getpid(); | ||
} | ||
|
||
int wait() const noexcept { | ||
int status; | ||
::waitpid(pid, &status, 0); | ||
if (!WIFEXITED(status)) { | ||
return WEXITSTATUS(status); // GCOVR_EXCL_LINE | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
sese::pid_t getProcessId() const noexcept { | ||
return pid; | ||
} | ||
|
||
bool kill() const noexcept { | ||
return ::kill(pid, SIGKILL) == 0; | ||
} | ||
|
||
private: | ||
sese::pid_t pid; | ||
}; | ||
|
||
Process::~Process() { | ||
} | ||
|
||
sese::Result<Process::Ptr, sese::ErrorCode> Process::createEx(const std::string &exec, const std::vector<std::string> &args) noexcept { | ||
if (auto impl = ProcessImpl::createEx(exec, args)) { | ||
auto result = MAKE_UNIQUE_PRIVATE(Process); | ||
result->process_impl = std::move(impl); | ||
return Result<Ptr, ErrorCode>::success(std::move(result)); | ||
} | ||
return Result<Ptr, ErrorCode>::error({getErrorCode(), getErrorString()}); | ||
} | ||
|
||
sese::pid_t Process::getCurrentProcessId() noexcept { | ||
return ProcessImpl::getCurrentProcessId(); | ||
} | ||
|
||
int Process::wait() const noexcept { | ||
return process_impl->wait(); | ||
} | ||
|
||
bool Process::kill() const noexcept { | ||
return process_impl->kill(); | ||
} | ||
|
||
sese::pid_t Process::getProcessId() const noexcept { | ||
return process_impl->getProcessId(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2025 libsese | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <sese/system/Process.h> | ||
#include <sese/text/StringBuilder.h> | ||
#include <sese/Util.h> | ||
|
||
#include <windows.h> | ||
|
||
using namespace sese::system; | ||
|
||
class Process::ProcessImpl { | ||
public: | ||
using StartupInfo = std::unique_ptr<STARTUPINFO>; | ||
using ProcessInfo = std::unique_ptr<PROCESS_INFORMATION>; | ||
StartupInfo startupinfo; | ||
ProcessInfo process_information; | ||
|
||
static std::unique_ptr<ProcessImpl> createEx(const std::string &exec, const std::vector<std::string> &args) noexcept { | ||
auto startup_info = std::make_unique<STARTUPINFO>(); | ||
auto process_info = std::make_unique<PROCESS_INFORMATION>(); | ||
|
||
text::StringBuilder builder; | ||
builder << exec; | ||
for (auto &arg: args) { | ||
builder << " " << arg; | ||
} | ||
builder << '\0'; | ||
auto cmd_line = std::make_unique<char[]>(builder.size()); | ||
memcpy(cmd_line.get(), builder.buf(), builder.size()); | ||
|
||
bool rt = CreateProcessA( | ||
nullptr, | ||
cmd_line.get(), | ||
nullptr, | ||
nullptr, | ||
false, | ||
0, | ||
nullptr, | ||
nullptr, | ||
startup_info.get(), | ||
process_info.get() | ||
); | ||
|
||
if (rt) { | ||
return std::make_unique<ProcessImpl>(std::move(startup_info), std::move(process_info)); | ||
} | ||
return {}; | ||
} | ||
|
||
static pid_t getCurrentProcessId() noexcept { | ||
return GetCurrentProcessId(); | ||
} | ||
|
||
int wait() const noexcept { | ||
DWORD exit_code; | ||
WaitForSingleObject(process_information->hProcess, INFINITE); | ||
GetExitCodeProcess(process_information->hProcess, &exit_code); | ||
return static_cast<int>(exit_code); | ||
} | ||
|
||
bool kill() const noexcept { | ||
return TerminateProcess(process_information->hProcess, -1) != 0; | ||
} | ||
|
||
pid_t getProcessId() const noexcept { | ||
return GetProcessId(process_information->hProcess); | ||
} | ||
|
||
ProcessImpl(StartupInfo startupinfo, ProcessInfo process_info) | ||
: startupinfo(std::move(startupinfo)), process_information(std::move(process_info)) { | ||
} | ||
}; | ||
|
||
Process::~Process() { | ||
} | ||
|
||
sese::Result<Process::Ptr, sese::ErrorCode> Process::createEx(const std::string &exec, const std::vector<std::string> &args) noexcept { | ||
if (auto impl = ProcessImpl::createEx(exec, args)) { | ||
auto result = MAKE_UNIQUE_PRIVATE(Process); | ||
result->process_impl = std::move(impl); | ||
return Result<Ptr, ErrorCode>::success(std::move(result)); | ||
} | ||
return Result<Ptr, ErrorCode>::error({getErrorCode(), getErrorString()}); | ||
} | ||
|
||
sese::pid_t Process::getCurrentProcessId() noexcept { | ||
return ProcessImpl::getCurrentProcessId(); | ||
} | ||
|
||
int Process::wait() const noexcept { | ||
return process_impl->wait(); | ||
} | ||
|
||
bool Process::kill() const noexcept { | ||
return process_impl->kill(); | ||
} | ||
|
||
sese::pid_t Process::getProcessId() const noexcept { | ||
return process_impl->getProcessId(); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.