libqi-api  release-2.5.3-2016-11-18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
application.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright (c) 2012, 2013 Aldebaran Robotics. All rights reserved.
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the COPYING file.
6  */
7 
8 #ifndef _QI_APPLICATION_HPP_
9 # define _QI_APPLICATION_HPP_
10 
11 # include <functional>
12 # include <vector>
13 # include <string>
14 # include <qi/api.hpp>
15 
16 namespace qi {
17 
31  {
32  public:
42  Application(int& argc, char** &argv, const std::string& name = "", const std::string& path = "");
52  QI_API_DEPRECATED_MSG(Use 'Application(int, char**, string, string)' instead)
53  Application(const std::string &name, int& argc, char** &argv);
59  ~Application();
60 
73  static void run();
77  static void stop();
78 
83  static const std::vector<std::string>& arguments();
89  static int argc();
94  static const char** argv();
99  static void setName(const std::string &name);
105  static std::string name();
112  static void setArguments(int argc, char** argv);
117  static void setArguments(const std::vector<std::string>& arguments);
118 
130  static void* loadModule(const std::string& name, int flags=-1);
135  static void unloadModule(void* handle);
140  static bool terminated();
145  static bool initialized();
146 
153  static const char* program();
154 
178  static const char* realProgram();
179 
185  static const char* _suggestedSdkPath();
186 
192  static bool atEnter(std::function<void()> func);
193 
199  static bool atExit(std::function<void()> func);
200 
207  static bool atRun(std::function<void()> func);
208 
215  static bool atStop(std::function<void()> func);
216 
227  static bool atSignal(std::function<void(int)> func, int signal);
228  };
229 }
230 
235 #define QI_AT_ENTER(func) \
236  static bool QI_UNIQ_DEF(_qi_atenter) = ::qi::Application::atEnter(func);
237 
243 #define QI_AT_EXIT(func) \
244  static bool QI_UNIQ_DEF(_qi_atexit) = ::qi::Application::atExit(func);
245 
246 //THIS IS INTERNAL
247 //API is not maintained for this function
248 //The user need to include <boost/program_options.hpp> and <boost/bind.hpp>
249 //Use like this:
250 //namespace {
251 // _QI_COMMAND_LINE_OPTIONS(
252 // "Name of category",
253 // (option1)
254 // (option2)
255 // )
256 //}
257 #define _QI_COMMAND_LINE_OPTIONS(desc, opts) \
258  static void QI_UNIQ_DEF(_qi_opt_func)() { \
259  namespace po = boost::program_options; \
260  po::variables_map vm; \
261  po::command_line_parser p(::qi::Application::arguments()); \
262  po::options_description options(desc); \
263  { \
264  using namespace boost::program_options; \
265  options.add_options() opts; \
266  } \
267  po::parsed_options res = p.options(options) \
268  .allow_unregistered() \
269  .run(); \
270  po::store(res, vm); \
271  /* Invoke notify callbacks*/ \
272  po::notify(vm); \
273  { \
274  po::options_description descTmp; \
275  descTmp.add_options() \
276  ("help,h", ""); \
277  po::variables_map vmTmp; \
278  po::store(po::command_line_parser(qi::Application::arguments()) \
279  .options(descTmp).allow_unregistered().run(), vmTmp); \
280  if (vmTmp.count("help")) \
281  std::cout << options << std::endl; \
282  } \
283  std::vector<std::string> args \
284  = po::collect_unrecognized(res.options, po::include_positional); \
285  /* Set arguments to what was not used */ \
286  ::qi::Application::setArguments(args); \
287  } \
288  QI_AT_ENTER(boost::bind(&(QI_UNIQ_DEF(_qi_opt_func))))
289 
290 #endif // _QI_APPLICATION_HPP_
#define QI_API
Definition: api.hpp:33
dll import/export and compiler message
Class handling startup and teardown of an application.
Definition: application.hpp:30
#define QI_API_DEPRECATED_MSG(msg__)
Compiler flags to mark a function as deprecated. It will generate a compiler warning.
Definition: macro.hpp:53