Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
app_exec.cpp
Go to the documentation of this file.
1 
7 #include "demos.h"
8 #include "tclap/CmdLine.h"
9 #include "util/Exception.h"
10 #include <g3log/g3log.hpp>
11 #include <g3log/logworker.hpp>
12 
13 using namespace std;
15 
16 namespace {
17 
18 using Function = std::function<int()>;
19 
20 class FunctionRegister : public map<string, Function>
21 {
22 public:
24  bool is_valid(string const& fname) const { return find(fname) != end(); }
25 
27  void list(ostream& os) const
28  {
29  map<string, Function>::const_iterator itor;
30  os << " The available tests are:" << endl << endl;
31  for (itor = begin(); itor != end(); itor++) {
32  os << (*itor).first << endl;
33  }
34  os << endl;
35  }
36 };
37 } // namespace
38 
39 int main(int argc, char* argv[])
40 {
41  try {
42  //----------------------------------------------------------------------
43  // Initialize logging library .
44  //----------------------------------------------------------------------
45  string logPath("./");
46  std::unique_ptr<g3::LogWorker> g3log{g3::LogWorker::createLogWorker()};
47  g3log->addDefaultLogger(argv[0], logPath);
48  g3::initializeLogging(g3log.get());
49  cout << "Process the log with \"cut -f 2 -d ] <logfile>\" to get a clean output." << endl;
50 
51  //----------------------------------------------------------------------
52  // Command line definition and parsing.
53  //----------------------------------------------------------------------
54  TCLAP::CmdLine cmd("Driver for object tracer demos", ' ', "1.0");
55  TCLAP::ValueArg<string> exec_arg("", "exec", "Demo to execute", false, "", "string", cmd);
56  cmd.parse(argc, static_cast<const char* const*>(argv));
57 
58  LOG(INFO) << "Starting main program ...";
59 
60  //----------------------------------------------------------------------
61  // Setting up the register with available tests
62  //----------------------------------------------------------------------
63  FunctionRegister f_reg;
64  f_reg["app_copy"] = app_copy;
65  f_reg["app_essentialops"] = app_essentialops;
66  f_reg["app_exception"] = app_exception;
67  f_reg["app_fcalls"] = app_fcalls;
68  f_reg["app_hierarchies"] = app_hierarchies;
69  f_reg["app_poly1"] = app_poly1;
70  f_reg["app_poly2"] = app_poly2;
71 
72  //----------------------------------------------------------------------
73  // Set demo to execute
74  //----------------------------------------------------------------------
75  string choice;
76  if (exec_arg.isSet()) {
77  choice = exec_arg.getValue();
78  } else {
79  f_reg.list(cout);
80  bool valid = false;
81  ios_base::iostate old_state = cin.exceptions();
82  cin.exceptions(ios_base::eofbit);
83  while (!valid) {
84  cout << "Select a demo by name: ";
85  cin >> choice;
86  if (f_reg.is_valid(choice)) {
87  valid = true;
88  } else {
89  cout << "error on input: invalid demo name" << endl;
90  }
91  }
92  cin.exceptions(old_state);
93  }
94 
95  //----------------------------------------------------------------------
96  // Execute it.
97  //----------------------------------------------------------------------
98  if (!f_reg.is_valid(choice)) {
99  throw UA_CoMP::Util::Exception("error: unknown demo name cannot be executed");
100  }
101  f_reg[choice]();
102 
103  //----------------------------------------------------------------------
104  // Wrapping up and exiting
105  //----------------------------------------------------------------------
106  cout << endl << "Exiting ... " << endl;
107  return (0);
108  } catch (TCLAP::ArgException& e) {
109  cerr << "error: " << e.error() << " for arg " << e.argId() << " " << endl;
110  } catch (exception& e) {
111  cerr << endl << "Caught std exception" << endl;
112  cerr << e.what() << endl << "Exiting ..." << endl;
113  } catch (...) {
114  cerr << endl << "Caught unknown exception. Exiting ..." << endl;
115  }
116 
117  return 0;
118 }
int app_essentialops()
Demo prog for features of ctor/dtor.
Header for object tracer demo routines.
Implementation for exception.
int app_exception()
Demo prog for ctor/dtor features in class derivation.
int app_poly1()
Demonstrates late binding.
Definition: app_poly1.cpp:37
int app_copy()
Demo prog to demonstrate deep & shallow copy operations.
Definition: app_copy.cpp:19
Extremely simple Exception root class.
int app_poly2()
Demonstrates late binding.
Definition: app_poly2.cpp:37
int app_fcalls()
Demo prog for features of essential ops.
Definition: app_fcalls.cpp:50
int app_hierarchies()
Demo prog for ctor/dtor features in class derivation.
int main(int argc, char *argv[])
Definition: app_exec.cpp:39