trajopt
 All Classes Namespaces Files Functions Variables Typedefs Pages
config.hpp
1 #pragma once
2 
3 #include <vector>
4 #include <string>
5 #include <iostream>
6 #include <boost/program_options.hpp>
7 #include <boost/shared_ptr.hpp>
8 #include "utils/stl_to_string.hpp"
9 
10 namespace util {
11 
12 
13 namespace po = boost::program_options;
14 
15 struct ParameterBase {
16  std::string m_name;
17  std::string m_desc;
18  ParameterBase(const std::string& name, const std::string& desc) : m_name(name), m_desc(desc) {}
19  virtual void addToBoost(po::options_description&) = 0;
20  virtual ~ParameterBase() {}
21 };
22 typedef boost::shared_ptr<ParameterBase> ParameterBasePtr;
23 
24 template <typename T>
26  std::vector<T>* m_value;
27  ParameterVec(std::string name, std::vector<T>* value, std::string desc) :
28  ParameterBase(name, desc),
29  m_value(value) {}
30  void addToBoost(po::options_description& od) {
31  od.add_options()(m_name.c_str(), po::value(m_value)->default_value(*m_value, Str(*m_value))->multitoken(), m_desc.c_str());
32  }
33 };
34 
35 template <typename T>
37  T* m_value;
38  Parameter(std::string name, T* value, std::string desc) :
39  ParameterBase(name, desc),
40  m_value(value) {}
41  void addToBoost(po::options_description& od) {
42  od.add_options()(m_name.c_str(), po::value(m_value)->default_value(*m_value, Str(*m_value)), m_desc.c_str());
43  }
44 };
45 
46 struct Config {
47  std::vector< ParameterBasePtr > params;
48  void add(ParameterBase* param) {params.push_back(ParameterBasePtr(param));}
49 };
50 
51 struct CommandParser {
52  std::vector<Config> configs;
53  void addGroup(const Config& config) {
54  configs.push_back(config);
55  }
56  CommandParser(const Config& config) {
57  addGroup(config);
58  }
59  void read(int argc, char* argv[]);
60 };
61 
62 }