libqi-api  2.1.4.13
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 <boost/function.hpp>
12 #include <qi/api.hpp>
13 #include <vector>
14 
15 namespace qi {
16 
18  {
19  public:
20  Application(int& argc, char** &argv);
21  Application(const std::string &name, int& argc, char** &argv);
22  ~Application();
23 
24  static void run();
25  static void stop();
26 
27  static const std::vector<std::string>& arguments();
28  static int argc();
29  static const char** argv();
30  static void setName(const std::string &name);
31  static std::string name();
32  static void setArguments(int argc, char** argv);
33  static void setArguments(const std::vector<std::string>& arguments);
34 
35  static void* loadModule(const std::string& name, int flags=-1);
36  static void unloadModule(void* handle);
37  static bool terminated();
38  static bool initialized();
39 
40  static const char* program();
41 
42  static bool atEnter(boost::function<void()> func);
43  static bool atExit(boost::function<void()> func);
44  static bool atStop(boost::function<void()> func);
45  static bool atSignal(boost::function<void(int)> func, int signal);
46  };
47 }
48 
49 #define QI_AT_ENTER(func) \
50 static bool QI_UNIQ_DEF(_qi_atenter) = ::qi::Application::atEnter(func);
51 
52 #define QI_AT_EXIT(func) \
53 static bool QI_UNIQ_DEF(_qi_atexit) = ::qi::Application::atExit(func);
54 
55 //THIS IS INTERNAL
56 //API is not maintained for this function
57 //The user need to include <boost/program_options.hpp> and <boost/bind.hpp>
58 //Use like this:
59 //namespace {
60 // _QI_COMMAND_LINE_OPTIONS(
61 // "Name of category",
62 // (option1)
63 // (option2)
64 // )
65 //}
66 #define _QI_COMMAND_LINE_OPTIONS(desc, opts) \
67 static void QI_UNIQ_DEF(_qi_opt_func)() { \
68  namespace po = boost::program_options; \
69  po::variables_map vm; \
70  po::command_line_parser p(::qi::Application::arguments()); \
71  po::options_description options(desc); \
72  { \
73  using namespace boost::program_options; \
74  options.add_options() opts; \
75  } \
76  po::parsed_options res = p.options(options) \
77  .allow_unregistered() \
78  .run(); \
79  po::store(res, vm); \
80  /* Invoke notify callbacks*/ \
81  po::notify(vm); \
82  { \
83  po::options_description descTmp; \
84  descTmp.add_options() \
85  ("help,h", ""); \
86  po::variables_map vmTmp; \
87  po::store(po::command_line_parser(qi::Application::arguments()) \
88  .options(descTmp).allow_unregistered().run(), vmTmp); \
89  if (vmTmp.count("help")) \
90  std::cout << options << std::endl; \
91  } \
92  std::vector<std::string> args \
93  = po::collect_unrecognized(res.options, po::include_positional); \
94  /* Set arguments to what was not used */ \
95  ::qi::Application::setArguments(args); \
96 } \
97 QI_AT_ENTER(boost::bind(&(QI_UNIQ_DEF(_qi_opt_func))))
98 
99 
100 #endif // _QI_APPLICATION_HPP_
QI_API_DEPRECATED const char ** argv()
Deprecated, see detailed description.
QI_API_DEPRECATED int argc()
Deprecated, see detailed description.
Class handling startup and teardown of an application.
Definition: application.hpp:17
dll import/export and compiler message
QI_API_DEPRECATED const char * program()
Deprecated, see detailed description.
#define QI_API
Definition: api.hpp:24