Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
process_error.h
1#ifndef SOURCEMETA_CORE_PROCESS_ERROR_H_
2#define SOURCEMETA_CORE_PROCESS_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_PROCESS_EXPORT
5#include <sourcemeta/core/process_export.h>
6#endif
7
8#include <exception> // std::exception
9#include <initializer_list> // std::initializer_list
10#include <span> // std::span
11#include <string> // std::string
12#include <string_view> // std::string_view
13#include <utility> // std::move
14#include <vector> // std::vector
15
16namespace sourcemeta::core {
17
18// Exporting symbols that depends on the standard C++ library is considered
19// safe.
20// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
21#if defined(_MSC_VER)
22#pragma warning(disable : 4251 4275)
23#endif
24
27class SOURCEMETA_CORE_PROCESS_EXPORT ProcessProgramNotFoundError
28 : public std::exception {
29public:
31 ProcessProgramNotFoundError(const std::string_view program)
32 : program_{program} {}
33
34 [[nodiscard]] auto what() const noexcept -> const char * override {
35 return "Could not locate the requested program";
36 }
37
39 [[nodiscard]] auto program() const noexcept -> std::string_view {
40 return this->program_;
41 }
42
43private:
44 std::string program_;
45};
46
49class SOURCEMETA_CORE_PROCESS_EXPORT ProcessSpawnError : public std::exception {
50public:
52 ProcessSpawnError(const std::string_view program,
53 std::initializer_list<std::string_view> arguments)
54 : program_{program}, arguments_{arguments.begin(), arguments.end()} {}
55
57 ProcessSpawnError(const std::string_view program,
58 std::span<const std::string_view> arguments)
59 : program_{program}, arguments_{arguments.begin(), arguments.end()} {}
60
61 [[nodiscard]] auto what() const noexcept -> const char * override {
62 return "Process terminated abnormally";
63 }
64
66 [[nodiscard]] auto program() const noexcept -> std::string_view {
67 return this->program_;
68 }
69
71 [[nodiscard]] auto arguments() const noexcept
72 -> const std::vector<std::string> & {
73 return this->arguments_;
74 }
75
76private:
77 std::string program_;
78 std::vector<std::string> arguments_;
79};
80
81#if defined(_MSC_VER)
82#pragma warning(default : 4251 4275)
83#endif
84
85} // namespace sourcemeta::core
86
87#endif
ProcessSpawnError(const std::string_view program, std::span< const std::string_view > arguments)
Construct the error given the offending program and its arguments.
Definition process_error.h:57
auto arguments() const noexcept -> const std::vector< std::string > &
The arguments the program was spawned with.
Definition process_error.h:71
auto program() const noexcept -> std::string_view
The offending program.
Definition process_error.h:39
ProcessProgramNotFoundError(const std::string_view program)
Construct the error given the offending program.
Definition process_error.h:31
ProcessSpawnError(const std::string_view program, std::initializer_list< std::string_view > arguments)
Construct the error given the offending program and its arguments.
Definition process_error.h:52
auto program() const noexcept -> std::string_view
The offending program.
Definition process_error.h:66