libqi-api  2.0.6.8
/home/opennao/work/master/sdk/libqi/qi/application.hpp
Go to the documentation of this file.
00001 #pragma once
00002 /*
00003  * Copyright (c) 2012, 2013 Aldebaran Robotics. All rights reserved.
00004  * Use of this source code is governed by a BSD-style license that can be
00005  * found in the COPYING file.
00006  */
00007 
00008 #ifndef _QI_APPLICATION_HPP_
00009 #define _QI_APPLICATION_HPP_
00010 
00011 #include <boost/function.hpp>
00012 #include <qi/api.hpp>
00013 #include <vector>
00014 
00015 namespace qi {
00016 
00017   class QI_API Application
00018   {
00019   public:
00020     Application(int& argc, char** &argv);
00021     Application(const std::string &name, int& argc, char** &argv);
00022     ~Application();
00023 
00024     static void run();
00025     static void stop();
00026 
00027     static const std::vector<std::string>& arguments();
00028     static int argc();
00029     static const char** argv();
00030     static void setName(const std::string &name);
00031     static std::string name();
00032     static void setArguments(int argc, char** argv);
00033     static void setArguments(const std::vector<std::string>& arguments);
00034 
00035     static void* loadModule(const std::string& name, int flags=-1);
00036     static void unloadModule(void* handle);
00037     static bool terminated();
00038     static bool initialized();
00039 
00040     static const char* program();
00041 
00042     static bool atEnter(boost::function<void()> func);
00043     static bool atExit(boost::function<void()> func);
00044     static bool atStop(boost::function<void()> func);
00045     static bool atSignal(boost::function<void(int)> func, int signal);
00046   };
00047 }
00048 
00049 #define QI_AT_ENTER(func) \
00050 static bool QI_UNIQ_DEF(_qi_atenter) = ::qi::Application::atEnter(func);
00051 
00052 #define QI_AT_EXIT(func) \
00053 static bool QI_UNIQ_DEF(_qi_atexit) = ::qi::Application::atExit(func);
00054 
00055 //THIS IS INTERNAL
00056 //API is not maintained for this function
00057 //The user need to include <boost/program_options.hpp> and <boost/bind.hpp>
00058 //Use like this:
00059 //namespace {
00060 //  _QI_COMMAND_LINE_OPTIONS(
00061 //    "Name of category",
00062 //    (option1)
00063 //    (option2)
00064 //  )
00065 //}
00066 #define _QI_COMMAND_LINE_OPTIONS(desc, opts)                          \
00067 static void QI_UNIQ_DEF(_qi_opt_func)() {                             \
00068   namespace po = boost::program_options;                              \
00069   po::variables_map vm;                                               \
00070   po::command_line_parser p(::qi::Application::arguments());          \
00071   po::options_description options(desc);                              \
00072   {                                                                   \
00073     using namespace boost::program_options;                           \
00074     options.add_options() opts;                                       \
00075   }                                                                   \
00076   po::parsed_options res = p.options(options)                         \
00077     .allow_unregistered()                                             \
00078     .run();                                                           \
00079   po::store(res, vm);                                                 \
00080   /* Invoke notify callbacks*/                                        \
00081   po::notify(vm);                                                     \
00082   {                                                                   \
00083     po::options_description descTmp;                                  \
00084     descTmp.add_options()                                             \
00085     ("help,h", "");                                                   \
00086     po::variables_map vmTmp;                                          \
00087     po::store(po::command_line_parser(qi::Application::arguments())   \
00088       .options(descTmp).allow_unregistered().run(), vmTmp);           \
00089     if (vmTmp.count("help"))                                          \
00090       std::cout << options << std::endl;                              \
00091   }                                                                   \
00092   std::vector<std::string> args                                       \
00093     = po::collect_unrecognized(res.options, po::include_positional);  \
00094   /* Set arguments to what was not used */                            \
00095   ::qi::Application::setArguments(args);                              \
00096 }                                                                     \
00097 QI_AT_ENTER(boost::bind(&(QI_UNIQ_DEF(_qi_opt_func))))
00098 
00099 
00100 #endif  // _QI_APPLICATION_HPP_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines