Goby3  3.1.5a
2024.05.23
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 
34 namespace goby
35 {
36 namespace 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 
100 template <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 
118 template <typename App, typename Configurator>
119 void 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 
131 template <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 
139 template <typename App, typename Configurator>
140 void 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
google::protobuf::RepeatedPtrField
Definition: message_lite.h:52
goby::GobyEnumValueOptions_ConfigurationOptions::has_external_command
bool has_external_command() const
Definition: option_extensions.pb.h:2139
goby::ev
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::EnumValueOptions, ::google::protobuf::internal::MessageTypeTraits< ::goby::GobyEnumValueOptions >, 11, false > ev
Definition: option_extensions.pb.h:1331
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::ToolHelper::perform_action
bool perform_action(Action action)
Definition: tool.h:100
goby::middleware::ToolHelper::ToolHelper
ToolHelper(const std::string &name, const goby::middleware::protobuf::AppConfig::Tool &tool_cfg, const google::protobuf::EnumDescriptor *action_enum_desc)
Definition: tool.h:41
goby::middleware::ProtobufConfigurator
Implementation of ConfiguratorInterface for Google Protocol buffers.
Definition: configurator.h:77
goby::middleware::ToolHelper::run_subtool
void run_subtool()
Definition: tool.h:131
goby::middleware::protobuf::AppConfig_Tool
Definition: app_config.pb.h:640
goby::GobyEnumValueOptions
Definition: option_extensions.pb.h:1199
goby::middleware::ToolSharedLibraryLoader::~ToolSharedLibraryLoader
~ToolSharedLibraryLoader()
goby::middleware::ToolSharedLibraryLoader::ToolSharedLibraryLoader
ToolSharedLibraryLoader(const std::string &lib)
Definition: tool.h:87
interface.h
goby::GobyEnumValueOptions_ConfigurationOptions::external_help_param
const ::std::string & external_help_param() const
Definition: option_extensions.pb.h:2218
goby::GobyEnumValueOptions_ConfigurationOptions::external_command
const ::std::string & external_command() const
Definition: option_extensions.pb.h:2152
goby::middleware::ToolSharedLibraryLoader
Definition: tool.h:74
goby::middleware::protobuf::AppConfig_Tool::extra_cli_param
const ::std::string & extra_cli_param(int index) const
Definition: app_config.pb.h:1231
goby::GobyEnumValueOptions::cfg
const ::goby::GobyEnumValueOptions_ConfigurationOptions & cfg() const
Definition: option_extensions.pb.h:2447
configurator.h
option_extensions.pb.h
goby::middleware::ToolSharedLibraryLoader::ToolSharedLibraryLoader
ToolSharedLibraryLoader(const google::protobuf::RepeatedPtrField< std::string > &load_libs)
Definition: tool.h:82
app_config.pb.h
goby::middleware::ToolHelper::help
bool help(int *action_for_help)
goby::middleware::ToolSharedLibraryLoader::ToolSharedLibraryLoader
ToolSharedLibraryLoader(const std::vector< std::string > &load_libs)
Definition: tool.h:77
goby::middleware::ToolHelper
Definition: tool.h:38