RTC Toolkit  2.0.0
rtcComponent.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_DETAIL_RTCCOMPONENT_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_DETAIL_RTCCOMPONENT_HPP
14 
16 #include <boost/core/demangle.hpp>
17 #include <boost/program_options.hpp>
18 #include <mal/Cii.hpp>
19 
20 #include <iostream>
21 
23 
25 public:
26  HelpOnly(const std::string& help_text) : rtctk::componentFramework::RtctkException(help_text) {
27  }
28 };
29 
31 public:
32  ArgNotSet(const std::string& arg_name)
33  : rtctk::componentFramework::RtctkException("Mandatory argument '" + arg_name +
34  "' not set, use -h for help") {
35  }
36 };
37 
38 class Args {
39 public:
40  Args(int argc, char** argv) : m_argc(argc), m_argv(argv) {
41  Parse();
42  }
43 
44  std::string GetComponentName() const {
45  return vm["cid"].as<std::string>();
46  }
47 
48  elt::mal::Uri GetServiceDiscEndpoint() const {
49  return vm["sde"].as<elt::mal::Uri>();
50  }
51 
52  std::optional<elt::mal::Uri> GetLogPropsFile() const {
53  if (vm.count("lpf")) {
54  return {vm["lpf"].as<elt::mal::Uri>()};
55  } else {
56  return std::nullopt;
57  }
58  }
59 
60  std::optional<elt::mal::Uri> GetModelExportFile() const {
61  if (vm.count("emf")) {
62  return {vm["emf"].as<elt::mal::Uri>()};
63  } else {
64  return std::nullopt;
65  }
66  }
67 
68 private:
69  void Parse() {
70  namespace bpo = boost::program_options;
71  using ::elt::mal::Uri;
72 
73  bpo::positional_options_description pos_opts_desc;
74  pos_opts_desc.add("cid", 1);
75  pos_opts_desc.add("sde", 1);
76 
77  bpo::options_description opts_desc("Options");
78  opts_desc.add_options()
79  ("help,h", "print help messages")
80  ("cid,i", bpo::value<std::string>(), "component identity")
81  ("sde,s", bpo::value<Uri>(), "service discovery endpoint (URI)")
82  ("lpf,l", bpo::value<Uri>(), "log4cplus properties file (URI)")
83  ("emf,e", bpo::value<Uri>(), "export state machine model to file (URI)");
84 
85  bpo::store(bpo::command_line_parser(m_argc, m_argv)
86  .options(opts_desc)
87  .positional(pos_opts_desc)
88  .run(),
89  vm);
90 
91  bpo::notify(vm);
92 
93  if (vm.count("help")) {
94  std::stringstream ss;
95  ss << opts_desc << std::endl;
96  CII_THROW(HelpOnly, ss.str());
97  }
98 
99  if (!vm.count("cid")) {
100  CII_THROW(ArgNotSet, "component identity");
101  }
102 
103  if (!vm.count("sde")) {
104  CII_THROW(ArgNotSet, "service discovery endpoint");
105  }
106  }
107 
108  int m_argc;
109  char** m_argv;
110 
111  boost::program_options::variables_map vm;
112 };
113 
114 std::string GetTypeName(const std::type_info& tid) {
115  return boost::core::demangle(tid.name());
116 }
117 
118 } // namespace rtctk::componentFramework::detail
119 
120 #endif // RTCTK_COMPONENTFRAMEWORK_DETAIL_RTCCOMPONENT_HPP
rtctk::componentFramework::detail::GetTypeName
std::string GetTypeName(const std::type_info &tid)
Definition: rtcComponent.hpp:114
rtctk::componentFramework::detail::HelpOnly
Definition: rtcComponent.hpp:24
exceptions.hpp
Provides macros and utilities for exception handling.
rtctk::componentFramework::detail::ArgNotSet
Definition: rtcComponent.hpp:30
rtctk::componentFramework::detail
Definition: rtcComponent.hpp:22
rtctk::componentFramework::detail::Args
Definition: rtcComponent.hpp:38
rtctk::componentFramework::RtctkException
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
rtctk::componentFramework::detail::ArgNotSet::ArgNotSet
ArgNotSet(const std::string &arg_name)
Definition: rtcComponent.hpp:32
rtctk::componentFramework::detail::Args::GetComponentName
std::string GetComponentName() const
Definition: rtcComponent.hpp:44
rtctk::componentFramework::detail::HelpOnly::HelpOnly
HelpOnly(const std::string &help_text)
Definition: rtcComponent.hpp:26
rtctk::componentFramework::detail::Args::GetServiceDiscEndpoint
elt::mal::Uri GetServiceDiscEndpoint() const
Definition: rtcComponent.hpp:48
rtctk::componentFramework::detail::Args::GetModelExportFile
std::optional< elt::mal::Uri > GetModelExportFile() const
Definition: rtcComponent.hpp:60
rtctk::componentFramework::detail::Args::Args
Args(int argc, char **argv)
Definition: rtcComponent.hpp:40
rtctk
Definition: commandReplier.cpp:20
rtctk::componentFramework::detail::Args::GetLogPropsFile
std::optional< elt::mal::Uri > GetLogPropsFile() const
Definition: rtcComponent.hpp:52