/src/Fast-DDS/src/cpp/utils/SystemCommandBuilder.hpp
Line | Count | Source |
1 | | // Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef UTILS__SYSTEMCOMMANDBUILDER_HPP_ |
16 | | #define UTILS__SYSTEMCOMMANDBUILDER_HPP_ |
17 | | |
18 | | #include <cstdlib> |
19 | | #include <sstream> |
20 | | |
21 | | namespace eprosima { |
22 | | |
23 | | namespace fastdds { |
24 | | |
25 | | static constexpr const char* FAST_DDS_DEFAULT_CLI_SCRIPT_NAME = "fastdds"; |
26 | | static constexpr const char* FAST_DDS_DEFAULT_CLI_DISCOVERY_VERB = "discovery"; |
27 | | static constexpr const char* FAST_DDS_DEFAULT_CLI_AUTO_VERB = "auto"; |
28 | | |
29 | | /** |
30 | | * @brief Class to build and execute system commands. |
31 | | */ |
32 | | class SystemCommandBuilder |
33 | | { |
34 | | public: |
35 | | |
36 | | enum SystemCommandResult |
37 | | { |
38 | | SUCCESS = 0, |
39 | | FAILURE, |
40 | | BAD_PARAM, |
41 | | INVALID |
42 | | }; |
43 | | |
44 | 0 | SystemCommandBuilder() = default; |
45 | | |
46 | | SystemCommandBuilder& executable( |
47 | | const std::string& executable) |
48 | 0 | { |
49 | 0 | command_ << executable; |
50 | 0 | return *this; |
51 | 0 | } |
52 | | |
53 | | SystemCommandBuilder& verb( |
54 | | const std::string& verb) |
55 | 0 | { |
56 | 0 | command_ << " " << verb; |
57 | 0 | return *this; |
58 | 0 | } |
59 | | |
60 | | SystemCommandBuilder& arg( |
61 | | const std::string& arg) |
62 | 0 | { |
63 | 0 | command_ << " " << arg; |
64 | 0 | return *this; |
65 | 0 | } |
66 | | |
67 | | SystemCommandBuilder& value( |
68 | | const std::string& value) |
69 | 0 | { |
70 | 0 | command_ << " " << value; |
71 | 0 | return *this; |
72 | 0 | } |
73 | | |
74 | | int build_and_call() |
75 | 0 | { |
76 | 0 | return std::system(command_.str().c_str()); |
77 | 0 | } |
78 | | |
79 | | private: |
80 | | |
81 | | std::stringstream command_; |
82 | | }; |
83 | | |
84 | | } // namespace fastdds |
85 | | } // namespace eprosima |
86 | | |
87 | | #endif // UTILS__SYSTEMCOMMANDBUILDER_HPP_ |