Elements  6.2
A C++ base framework for the Euclid Software.
Program.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 
49 namespace Elements {
50 namespace Examples {
51 
60 
61  auto logger = Logging::getLogger();
62  logger.info("Test of Message");
63 
64  auto logger2 = Logging::getLogger(__func__);
65  logger2.info("Test2 of Message");
66 
67  auto logger3 = Logging::getLogger(BOOST_CURRENT_FUNCTION);
68  logger3.info("Test3 of Message");
69 }
70 
79 class Program : public Elements::Program {
80 
81 public:
92 
93  bool flag = false;
94 
95  OptionsDescription config_options{"Example program options"};
96 
97  config_options.add_options()("int-option", value<int>()->default_value(int{111}), "An example int option")(
98  "int-option-with-default-and-default-in-conf", value<int>()->default_value(int{222}), "An example int option")(
99  "int-option-with-default-no-default-in-conf", value<int>()->default_value(int{444}),
100  "An example int option")("int-option-no-default-not-defined-in-conf", value<int>(), "An example int option")(
101  "int-option-with-no-defaults-anywhere", value<int>(),
102  "An example int option")("string-option", value<string>()->default_value(string{}), "An example string option")(
103  "boolean-option", value<bool>()->default_value(false), "An example boolean option")(
104  "flag,f", bool_switch(&flag), "An option to set to true")("string-option-no-default", value<string>(),
105  "A string option without default value")(
106  "long-long-option", value<int64_t>()->default_value(int64_t{}), "An example long long option")(
107  "double-option", value<double>()->default_value(double{}), "An example double option")(
108  "int-vector-option", value<vector<int>>()->multitoken()->default_value(vector<int>{}, "Empty"),
109  "An example vector option")("threshold,t", value<double>()->default_value(double{0.5}),
110  "An example double option");
111 
112  return config_options;
113  }
114 
127 
128  auto log = Logging::getLogger("Program");
129  log.info("Entering mainMethod()");
130  log.info("#");
131  /*
132  * Check availability of mandatory program arguments (or options)
133  *
134  * Arguments values may come from
135  * 1) the default value provided in above defineSpecificProgramOptions()
136  * 2) the configuration file
137  * 3) the command line
138  *
139  * If an none of the three options provide any values for a mandatory
140  * argument, you should check if your option has any values following the
141  * below example. Note that this may happen for all options without default
142  * values.
143  */
144  if (args["string-option-no-default"].empty()) {
145  log.info() << "No value are available for string-option-no-default";
146  /*
147  * An exception may be thrown her if the above option is mandatory and there
148  * is no way to continue without value
149  */
150  }
151  /*
152  * Get and log one of the program arguments (or options)
153  *
154  * The string-option has a default empty string value, so that it can always be
155  * printed event as an empty string
156  */
157  string string_example{args["string-option"].as<string>()};
158  log.info() << "String option value: " << string_example;
159 
160  log.info() << "The int-option value is " << args["int-option"].as<int>();
161  log.info() << "The threshold value is " << args["threshold"].as<double>();
162 
163  // Some initialization
164  double input_variable = 3.4756;
165  int64_t source_id = 12345;
166  double ra = 45.637;
167 
168  // Factory method example
169  ClassExample example_class_object = ClassExample::factoryMethod(source_id, ra);
170 
171  /*
172  * All fundamental type variables can be copied forth and back without significant
173  * cost in (almost) all cases
174  */
175  double method_result = example_class_object.fundamentalTypeMethod(input_variable);
176  log.info() << "Some result: " << method_result;
177 
178  double first = 1.0;
179  double division_result{};
180  try {
181  log.info("#");
182  log.info("# Calling a method throwing an exception ");
183  log.info("#");
184  double second = 0.0;
185  division_result = example_class_object.divideNumbers(first, second);
186  //
187  } catch (const Exception& e) {
188  log.info("#");
189  log.info() << e.what();
190  log.info("#");
191  log.info("# In this silly example we continue with a fake fix ");
192  log.info("#");
193  division_result = example_class_object.divideNumbers(first, 0.000001);
194  }
195  log.info() << "Second result is: " << division_result;
196 
197  /*
198  * Illustration on how best to use smart pointer (regular pointer should not
199  * be used anymore). The move() indicate that the ownership of the pointer is given to the
200  * method called. The vector_unique_ptr cannot be used in this method anymore after the
201  * call.
202  */
203  std::unique_ptr<vector<double>> vector_unique_ptr{new vector<double>{1.0, 2.3, 4.5}};
204  example_class_object.passingUniquePointer(std::move(vector_unique_ptr));
205 
206  /*
207  * Illustration on how best to pass any object. The passingObjectInGeneral() is taking
208  * a reference to this object.
209  */
210  vector<double> object_example{vector<double>{1.0, 2.3, 4.5}};
211  example_class_object.passingObjectInGeneral(object_example);
212 
213  log.info() << "Function Example: " << functionExample(3);
214 
215  log.info() << "This executable name: " << Elements::System::getThisExecutableInfo().name();
216 
218 
219  printProject();
220 
221  log.info() << Project();
222  log.info() << "Project Name: " << Project::name();
223  log.info() << "Project Version: " << Project::versionString();
224  log.info() << "Module Name: " << Module::name();
225  log.info() << "Module Version: " << Module::versionString();
226 
227  log.info("#");
228  log.info("Exiting mainMethod()");
229  return ExitCode::OK;
230  }
231 };
232 
233 } // namespace Examples
234 } // namespace Elements
235 
Elements::Examples::myLocalLogTestFunc
void myLocalLogTestFunc()
test function to demonstrate the logger
Definition: Program.cpp:59
Elements::ExitCode::OK
@ OK
Everything is OK.
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::Examples::ClassExample::factoryMethod
static ClassExample factoryMethod(const std::int64_t source_id, const double ra)
Example factory method.
Definition: ClassExample.cpp:63
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.
Elements::Program::OptionsDescription
options_description OptionsDescription
Definition: Program.h:62
Elements::Kernel::Units::second
constexpr double second
Definition: SystemOfUnits.h:107
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::Examples::Program
Simple example of an Elements program.
Definition: Program.cpp:79
Elements::Project::name
static std::string name()
Definition: Project.h:42
functionExample.h
Elements::Examples::Program::defineSpecificProgramOptions
OptionsDescription defineSpecificProgramOptions() override
Allows to define the (command line and configuration file) options specific to this program.
Definition: Program.cpp:91
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::ClassExample::divideNumbers
double divideNumbers(const double first, const double second) const
Divide two double variables.
Definition: ClassExample.cpp:47
Elements::Examples::printProject
ELEMENTS_API void printProject()
Definition: printProject.cpp:31
Elements::Project::versionString
static std::string versionString()
Definition: Project.h:46
Elements::Examples::ClassExample::passingUniquePointer
void passingUniquePointer(std::unique_ptr< std::vector< double >> vector_unique_ptr) const
Example method with a unique pointer argument.
Definition: ClassExample.cpp:55
Elements::Program
Abstract class for all Elements programs.
Definition: Program.h:52
MAIN_FOR
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:113
Elements::Examples::ClassExample::fundamentalTypeMethod
double fundamentalTypeMethod(const double input_variable) const
Simple method example.
Definition: ClassExample.cpp:40
Elements::Examples::ClassExample::passingObjectInGeneral
void passingObjectInGeneral(const std::vector< double > &input_object) const
Example method taking an object in input.
Definition: ClassExample.cpp:59
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
Elements::Examples::Program::mainMethod
ExitCode mainMethod(map< string, VariableValue > &args) override
The "main" method.
Definition: Program.cpp:126
Elements
Definition: callBackExample.h:35