Goby v2
application_base.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef MINIMALAPPLICATIONBASE20110413H
24 #define MINIMALAPPLICATIONBASE20110413H
25 
26 #include <iostream>
27 
28 #include <boost/shared_ptr.hpp>
29 
30 #include "goby/common/configuration_reader.h"
31 #include "goby/common/exception.h"
32 
33 #include "goby/common/protobuf/app_base_config.pb.h"
34 
35 #include "goby/common/logger.h"
36 
37 namespace goby
38 {
44 
45 template <typename App, typename Config> int run(int argc, char* argv[], Config* cfg);
46 
47 namespace common
48 {
50 {
51  public:
53  virtual ~ApplicationBase();
54 
56  void quit() { alive_ = false; }
57 
58  virtual void iterate() = 0;
59 
61  std::string application_name() { return base_cfg_->app_name(); }
63  std::string platform_name() { return base_cfg_->platform_name(); }
64 
65  template <typename App, typename Config>
66  friend int ::goby::run(int argc, char* argv[], Config* cfg);
67 
68  const AppBaseConfig& base_cfg() { return *base_cfg_; }
69 
70  private:
71  // main loop that exits on disconnect. called by goby::run()
72  void __run();
73 
74  void __set_application_name(const std::string& s) { base_cfg_->set_app_name(s); }
75  void __set_platform_name(const std::string& s) { base_cfg_->set_platform_name(s); }
76 
77  private:
78  // copies of the "real" argc, argv that are used
79  // to give ApplicationBase access without requiring the subclasses of
80  // ApplicationBase to pass them through their constructors
81  static int argc_;
82  static char** argv_;
83  google::protobuf::Message* all_cfg_;
84 
85  // configuration relevant to all applications (loop frequency, for example)
86  // defined in #include "proto/app_base_config.pb.h"
87  AppBaseConfig* base_cfg_;
88  bool own_base_cfg_;
89 
90  bool alive_;
91  std::vector<boost::shared_ptr<std::ofstream> > fout_;
92 };
93 } // namespace common
94 } // namespace goby
95 
96 template <typename App, typename Config> int goby::run(int argc, char* argv[], Config* cfg)
97 {
98  // avoid making the user pass these through their Ctor...
99  App::argc_ = argc;
100  App::argv_ = argv;
101 
102  try
103  {
104  App app(cfg);
105  app.__run();
106  }
108  {
109  // no further warning as the ApplicationBase Ctor handles this
110  return 1;
111  }
112  catch (std::exception& e)
113  {
114  // some other exception
115  std::cerr << "uncaught exception: " << e.what() << std::endl;
116  return 2;
117  }
118 
119  return 0;
120 }
121 
122 #endif
indicates a problem with the runtime command line or .cfg file configuration (or –help was given) ...
std::string platform_name()
name of this platform (from AppBaseConfig::platform_name). E.g. "AUV-23" or "unicorn" ...
int run(int argc, char *argv[], Config *cfg)
Run a Goby application derived from MinimalApplicationBase. blocks caller until MinimalApplicationBas...
std::string application_name()
name of this application (from AppBaseConfig::app_name). E.g. "garmin_gps_g"
void quit()
Requests a clean (return 0) exit.
The global namespace for the Goby project.