VPTissue Reference Manual
mode_manager.h
Go to the documentation of this file.
1 #ifndef SIMPT_MODE_MANAGER_H_
2 #define SIMPT_MODE_MANAGER_H_
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include "parex_client_mode.h"
23 #include "parex_node_mode.h"
24 #include "parex_server_mode.h"
25 #include "sim_cli_mode.h"
26 #include "sim_gui_mode.h"
27 #include "util/misc/FunctionMap.h"
28 
29 #include <tclap/CmdLine.h>
30 #include <boost/functional/value_factory.hpp>
31 #include <cstring>
32 #include <stdexcept>
33 #include <string>
34 #include <vector>
35 
36 namespace Modes {
37 
41 using Function = std::function<int(int argc, char** argv)>;
42 
46 template<typename executable_type>
48 {
49 public:
53  static Function Create(const std::string& application)
54  {
55  if (!IsValid(application)) {
56  const std::string msg = std::string("Invalid selection:\n");
57  throw std::logic_error(msg + application);
58  }
59  return executable_type::modes.Get(application)();
60  }
61 
65  static bool IsValid(const std::string& selection)
66  {
67  return (selection != "") && executable_type::modes.IsValid(selection);
68  }
69 
73  static std::vector<std::string> List()
74  {
75  auto list = executable_type::modes.List();
76  return std::vector<std::string> (std::begin(list),std::end(list) );
77  }
78 
82  inline static std::string DefaultApplication()
83  {
84  return executable_type::default_mode;
85  }
86 
87  static int main(int argc, char** argv)
88  {
89  using namespace std;
90  using namespace TCLAP;
91 
92  int exit_status = EXIT_SUCCESS;
93  try {
94  // Parse command line and create task to be executed.
95  CmdLine cmd(executable_type::application_name, ' ', "", false);
96  vector<string> apps(List());
97  ValuesConstraint<string> allowedApps(apps);
98  ValueArg<string> application_Arg("m", "mode", "The application mode", false, "compound", &allowedApps, cmd);
99  SwitchArg list_applications("l", "list-modes", "List the available modes", cmd, false);
100  SwitchArg help("h", "help", "Displays usage information and exits.", cmd, false);
101 
102  // The unlabeled multi args will be used to get the arguments of the "sub" application.
103  UnlabeledMultiArg<string> multi("params","application arguments", false,"");
104  cmd.add( multi );
105  cmd.parse(argc, argv);
106 
107  if (list_applications.getValue()) {
108  std::cout << "Available modes:"<<std::endl;
109  for (std::string app: apps ) {
110  cout <<" -"<<app<<std::endl;
111  }
112  } else if (help.getValue() && !application_Arg.isSet()) {
113  cmd.getOutput()->usage(cmd);
114  } else {
115  const std::string default_app = DefaultApplication();
116  vector<string> params = multi.getValue();
117 
118  //Add the -m <mode> to the command line argument (otherwise the --help is wrong for the app.)
119  std::string appArg = " -m " + (application_Arg.isSet() ? application_Arg.getValue() : default_app);
120  params.insert(params.begin(), std::string(argv[0])+appArg);
121 
122  // If the help flag is set together with an application, show the help of the application.
123  if (help.getValue()) {
124  params.insert(params.begin() + 1, "--help");
125  }
126 
127  // Convert the arguments back to a plain old char* array
128  std::vector<char *> argv_new(params.size() + 1);
129  for(std::size_t i=0; i < params.size(); ++i) {
130  argv_new[i] = new char[params[i].length() + 1];
131  std::strcpy(argv_new[i], params[i].c_str());
132  }
133  int argc_new = params.size();
134 
135  // Start the application, if no application is set, use the default.
136  if (application_Arg.isSet()) {
137  exit_status = Create(application_Arg.getValue())(argc_new, argv_new.data());
138  } else {
139  exit_status = Create(default_app)(argc_new, argv_new.data());
140  }
141 
142  for(int i = 0; i < argc_new; ++i) {
143  if (argv_new[i]!=0) {
144  delete [] argv_new[i];
145  }
146  }
147  }
148  }
149  catch (exception& e) {
150  exit_status = EXIT_FAILURE;
151  cerr << e.what() << endl;
152  }
153  catch (...) {
154  exit_status = EXIT_FAILURE;
155  cerr << "Unknown exception." << endl;
156  }
157 
158  return exit_status;
159  }
160 
165 
166 };
167 
168 } // namespace Modes
169 
170 #endif // end_of_include_guard
STL namespace.
Interface/Implementation for the exec modes.
Interface/Implementation for the exec modes.
Namespace for startup modes for simPT tools.
Definition: mode_manager.h:36
Interface/Implementation for the exec modes.
static Function Create(const std::string &application)
Create a function.
Definition: mode_manager.h:53
static std::vector< std::string > List()
Lists all the registered applications.
Definition: mode_manager.h:73
std::function< int(int argc, char **argv)> Function
Function representing a certain mode.
Definition: mode_manager.h:41
static bool IsValid(const std::string &selection)
Check whether selection is valid i.e.
Definition: mode_manager.h:65
A map to hold std::functions.
Definition: FunctionMap.h:36
Interface/Implementation for the exec modes.
Interface/Implementation FunctionMap.
static std::string DefaultApplication()
Returns the default application.
Definition: mode_manager.h:82
Factory produces functions that start the different modes of the various executables.
Definition: mode_manager.h:47
Interface/Implementation for the exec modes.