Elements  6.2
A C++ base framework for the Euclid Software.
ExtProgram.cpp
Go to the documentation of this file.
1 
21 #include <cstdint> // for int64_t
22 #include <map> // for map
23 #include <memory> // for unique_ptr
24 #include <string> // for string
25 #include <utility> // for move
26 #include <vector> // for vector
27 
29 #include <boost/current_function.hpp> // for BOOST_CURRENT_FUNCTION
30 #include <boost/program_options.hpp> // for program options from configuration file of command line arguments
31 
32 #include "ElementsKernel/Module.h" // for Module
33 #include "ElementsKernel/ProgramHeaders.h" // for including all Program/related headers
34 #include "ElementsKernel/Project.h" // for Project
35 #include "ElementsKernel/ThisModule.h" // for getThisExecutableInfo
36 
39 
40 using std::map;
41 using std::string;
42 using std::vector;
43 
44 using boost::program_options::bool_switch;
45 using boost::program_options::value;
46 
47 using std::int64_t;
48 
57 class ExtProgram : public Elements::Program {
58 
59 public:
70 
71  OptionsDescription config_options{"Example program options"};
72  auto add = config_options.add_options();
73 
74  bool flag = false;
75 
76  // Add the specific program options
77  add("int-option", value<int>()->default_value(int{111}), "An example int option");
78  add("int-option-with-default-and-default-in-conf", value<int>()->default_value(int{222}), "An example int option");
79  add("int-option-with-default-no-default-in-conf", value<int>()->default_value(int{444}), "An example int option");
80  add("int-option-no-default-not-defined-in-conf", value<int>(), "An example int option");
81  add("int-option-with-no-defaults-anywhere", value<int>(), "An example int option");
82  add("string-option", value<string>()->default_value(string{}), "An example string option");
83  add("boolean-option", value<bool>()->default_value(false), "An example boolean option");
84  add("flag,f", bool_switch(&flag), "An option to set to true");
85  add("string-option-no-default", value<string>(), "A string option without default value");
86  add("long-long-option", value<int64_t>()->default_value(int64_t{}), "An example long long option");
87  add("double-option", value<double>()->default_value(double{}), "An example double option");
88  add("int-vector-option", value<vector<int>>()->multitoken()->default_value(vector<int>{}, "Empty"),
89  "An example vector option");
90  add("threshold,t", value<double>()->default_value(double{0.5}), "An example double option");
91 
92  return config_options;
93  }
94 
107 
109 
110  auto log = Logging::getLogger("ExtProgram");
111  log.info("Entering mainMethod()");
112  log.info("#");
113  /*
114  * Check availability of mandatory program arguments (or options)
115  *
116  * Arguments values may come from
117  * 1) the default value provided in above defineSpecificProgramOptions()
118  * 2) the configuration file
119  * 3) the command line
120  *
121  * If an none of the three options provide any values for a mandatory
122  * argument, you should check if your option has any values following the
123  * below example. Note that this may happen for all options without default
124  * values.
125  */
126  if (args["string-option-no-default"].empty()) {
127  log.info() << "No value are available for string-option-no-default";
128  /*
129  * An exception may be thrown her if the above option is mandatory and there
130  * is no way to continue without value
131  */
132  }
133  /*
134  * Get and log one of the program arguments (or options)
135  *
136  * The string-option has a default empty string value, so that it can always be
137  * printed event as an empty string
138  */
139  string string_example{args["string-option"].as<string>()};
140  log.info() << "String option value: " << string_example;
141 
142  log.info() << "The int-option value is " << args["int-option"].as<int>();
143  log.info() << "The threshold value is " << args["threshold"].as<double>();
144 
145  // Some initialization
146  double input_variable = 3.4756;
147  int64_t source_id = 12345;
148  double ra = 45.637;
149 
150  // Factory method example
151  ClassExample example_class_object = ClassExample::factoryMethod(source_id, ra);
152 
153  /*
154  * All fundamental type variables can be copied forth and back without significant
155  * cost in (almost) all cases
156  */
157  double method_result = example_class_object.fundamentalTypeMethod(input_variable);
158  log.info() << "Some result: " << method_result;
159 
160  double first = 1.0;
161  double division_result{};
162  try {
163  log.info("#");
164  log.info("# Calling a method throwing an exception ");
165  log.info("#");
166  double second = 0.0;
167  division_result = example_class_object.divideNumbers(first, second);
168  //
169  } catch (const Elements::Exception& e) {
170  log.info("#");
171  log.info() << e.what();
172  log.info("#");
173  log.info("# In this silly example we continue with a fake fix ");
174  log.info("#");
175  division_result = example_class_object.divideNumbers(first, 0.000001);
176  }
177  log.info() << "Second result is: " << division_result;
178 
179  /*
180  * Illustration on how best to use smart pointer (regular pointer should not
181  * be used anymore). The move() indicate that the ownership of the pointer is given to the
182  * method called. The vector_unique_ptr cannot be used in this method anymore after the
183  * call.
184  */
185  std::unique_ptr<vector<double>> vector_unique_ptr{new vector<double>{1.0, 2.3, 4.5}};
186  example_class_object.passingUniquePointer(std::move(vector_unique_ptr));
187 
188  /*
189  * Illustration on how best to pass any object. The passingObjectInGeneral() is taking
190  * a reference to this object.
191  */
192  vector<double> object_example{vector<double>{1.0, 2.3, 4.5}};
193  example_class_object.passingObjectInGeneral(object_example);
194 
195  log.info() << "Function Example: " << Elements::Examples::functionExample(3);
196 
197  log.info() << "This executable name: " << Elements::System::getThisExecutableInfo().name();
198 
200 
201  log.info() << Elements::Project();
202  log.info() << "Project Name: " << Elements::Project::name();
203  log.info() << "Project Version: " << Elements::Project::versionString();
204  log.info() << "Module Name: " << Elements::Module::name();
205  log.info() << "Module Version: " << Elements::Module::versionString();
206 
207  log.info("#");
208  log.info("Exiting mainMethod()");
209  return ExitCode::OK;
210  }
211 };
212 
Elements::System::ModuleInfo::name
const std::string name() const
Definition: ModuleInfo.cpp:71
std::string
STL class.
printProject.h
std::move
T move(T... args)
Elements::Project
Definition: Project.h:41
Project.h
Defines tools to describe the current project.
std::vector
STL class.
Module.h
Defines tools to describe the current Elmeents module.
ExtProgram::mainMethod
ExitCode mainMethod(map< string, VariableValue > &args) override
The "main" method.
Definition: ExtProgram.cpp:106
Elements::Program::OptionsDescription
options_description OptionsDescription
Definition: Program.h:62
Elements::Kernel::Units::second
constexpr double second
Definition: SystemOfUnits.h:107
ExtProgram
Simple example of an Elements program outside of the Elements namespace.
Definition: ExtProgram.cpp:57
Elements::Module::versionString
static std::string versionString()
Definition: Module.h:45
Elements::Examples::functionExample
ELEMENTS_API int functionExample(const int j)
Definition: functionExample.cpp:27
Elements::ExitCode
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
Elements::Project::name
static std::string name()
Definition: Project.h:42
functionExample.h
Elements::Exception
Elements base exception class.
Definition: Exception.h:47
Elements::Module::name
static std::string name()
Definition: Module.h:42
Elements::System::getThisExecutableInfo
const ELEMENTS_API ModuleInfo & getThisExecutableInfo()
Definition: ThisModule.cpp:33
std::int64_t
std::map
STL class.
ProgramHeaders.h
ClassExample.h
Elements::Examples::log
auto log
Definition: BackTrace.cpp:36
Elements::Logging::getLogger
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
Elements::Examples::printProject
ELEMENTS_API void printProject()
Definition: printProject.cpp:31
Elements::Project::versionString
static std::string versionString()
Definition: Project.h:46
Elements::Program
Abstract class for all Elements programs.
Definition: Program.h:52
MAIN_FOR
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:113
ExtProgram::defineSpecificProgramOptions
OptionsDescription defineSpecificProgramOptions() override
Allows to define the (command line and configuration file) options specific to this program.
Definition: ExtProgram.cpp:69
std::unique_ptr
STL class.
ThisModule.h
header to get the module info statically
Elements::Examples::ClassExample
An class example.
Definition: ClassExample.h:59
Elements::Kernel::Units::e
constexpr double e
The base of the natural logarithm .
Definition: MathConstants.h:51