Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
tool.h
Go to the documentation of this file.
1// Copyright 2024:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_MIDDLEWARE_APPLICATION_TOOL_H
25#define GOBY_MIDDLEWARE_APPLICATION_TOOL_H
26
27#include <google/protobuf/descriptor.h>
28
33
34namespace goby
35{
36namespace middleware
37{
39{
40 public:
41 ToolHelper(const std::string& name, const goby::middleware::protobuf::AppConfig::Tool& tool_cfg,
42 const google::protobuf::EnumDescriptor* action_enum_desc)
43 : name_(name), tool_cfg_(tool_cfg), action_enum_desc_(action_enum_desc)
44 {
45 }
46
47 // return true if handled by this function, false otherwise
48 template <typename Action> bool perform_action(Action action);
49
50 // return true if handled by this function, false otherwise
51 bool help(int* action_for_help);
52
53 template <typename App,
55 void run_subtool();
56
57 template <typename App,
59 void help(int action_for_help);
60
61 private:
62 void exec_external(std::string app, std::vector<std::string> args,
63 const goby::GobyEnumValueOptions& ev_options);
64 template <typename App, typename Configurator>
65 void exec_internal(std::string app, std::vector<std::string> args);
66
67 private:
68 std::string name_;
70 std::string action_;
71 const google::protobuf::EnumDescriptor* action_enum_desc_;
72};
73
75{
76 public:
77 ToolSharedLibraryLoader(const std::vector<std::string>& load_libs)
78 {
79 for (const auto& lib : load_libs) load_lib(lib);
80 }
81
83 {
84 for (const auto& lib : load_libs) load_lib(lib);
85 }
86
87 ToolSharedLibraryLoader(const std::string& lib) { load_lib(lib); }
89
90 private:
91 void load_lib(const std::string& lib);
92
93 private:
94 std::vector<void*> dl_handles_;
95};
96
97} // namespace middleware
98} // namespace goby
99
100template <typename Action> bool goby::middleware::ToolHelper::perform_action(Action action)
101{
102 const google::protobuf::EnumValueDescriptor* value_desc =
103 action_enum_desc_->FindValueByNumber(action);
104 // Get the extended option for this enum value
105 const goby::GobyEnumValueOptions& ev_options = value_desc->options().GetExtension(goby::ev);
106 action_ = value_desc->name();
107
108 if (ev_options.cfg().has_external_command())
109 {
110 const std::string& external_command = ev_options.cfg().external_command();
111 std::vector<std::string> args;
112 for (const auto& cli_extra : tool_cfg_.extra_cli_param()) args.push_back(cli_extra);
113 exec_external(external_command, args, ev_options);
114 }
115 return false;
116}
117
118template <typename App, typename Configurator>
119void goby::middleware::ToolHelper::exec_internal(std::string app, std::vector<std::string> args)
120{
121 std::vector<std::string> bin_args;
122 bin_args.push_back("--binary=" + name_ + " " + action_);
123
124 std::vector<char*> c_args;
125 c_args.push_back(const_cast<char*>(app.c_str()));
126 for (const auto& arg : bin_args) { c_args.push_back(const_cast<char*>(arg.c_str())); }
127 for (const auto& arg : args) { c_args.push_back(const_cast<char*>(arg.c_str())); }
128 goby::run<App>(Configurator(c_args.size(), c_args.data()));
129}
130
131template <typename App, typename Configurator> void goby::middleware::ToolHelper::run_subtool()
132
133{
134 std::vector<std::string> args;
135 for (const auto& cli_extra : tool_cfg_.extra_cli_param()) args.push_back(cli_extra);
136 exec_internal<App, Configurator>(name_, args);
137}
138
139template <typename App, typename Configurator>
140void goby::middleware::ToolHelper::help(int action_for_help)
141
142{
143 const google::protobuf::EnumValueDescriptor* help_action_value_desc =
144 action_enum_desc_->FindValueByNumber(action_for_help);
145
146 const goby::GobyEnumValueOptions& ev_options =
147 help_action_value_desc->options().GetExtension(goby::ev);
148
149 action_ = help_action_value_desc->name();
150
151 std::vector<std::string> args;
152 if (tool_cfg_.extra_cli_param_size() > 1)
153 {
154 for (int i = 0, n = tool_cfg_.extra_cli_param_size() - 1; i < n; ++i)
155 args.push_back(tool_cfg_.extra_cli_param(i));
156 }
157 else
158 {
159 args.push_back(ev_options.cfg().external_help_param());
160 }
161
162 exec_internal<App, Configurator>(name_, args);
163}
164
165#endif
const ::goby::GobyEnumValueOptions_ConfigurationOptions & cfg() const
Implementation of ConfiguratorInterface for Google Protocol buffers.
bool help(int *action_for_help)
ToolHelper(const std::string &name, const goby::middleware::protobuf::AppConfig::Tool &tool_cfg, const google::protobuf::EnumDescriptor *action_enum_desc)
Definition tool.h:41
bool perform_action(Action action)
Definition tool.h:100
ToolSharedLibraryLoader(const std::string &lib)
Definition tool.h:87
ToolSharedLibraryLoader(const std::vector< std::string > &load_libs)
Definition tool.h:77
ToolSharedLibraryLoader(const google::protobuf::RepeatedPtrField< std::string > &load_libs)
Definition tool.h:82
const std::string & extra_cli_param(int index) const
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::EnumValueOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyEnumValueOptions >, 11, false > ev