settings.h
Go to the documentation of this file.
1 #ifndef SOCKETCAN_INTERFACE_SETTINGS_H
2 #define SOCKETCAN_INTERFACE_SETTINGS_H
3 
4 #include <map>
5 #include <string>
6 #include <memory>
7 
8 #include <boost/lexical_cast.hpp>
9 
10 namespace can {
11  class Settings
12  {
13  public:
14  template <typename T> T get_optional(const std::string &n, const T& def) const {
15  std::string repr;
16  if(!getRepr(n, repr)){
17  return def;
18  }
19  return boost::lexical_cast<T>(repr);
20  }
21  template <typename T> bool get(const std::string &n, T& val) const {
22  std::string repr;
23  if(!getRepr(n, repr)) return false;
24  val = boost::lexical_cast<T>(repr);
25  return true;
26  }
27  virtual ~Settings() {}
28  private:
29  virtual bool getRepr(const std::string &n, std::string & repr) const = 0;
30  };
31  using SettingsConstSharedPtr = std::shared_ptr<const Settings>;
32  using SettingsSharedPtr = std::shared_ptr<Settings>;
33 
34  class NoSettings : public Settings {
35  public:
37  private:
38  virtual bool getRepr(const std::string &n, std::string & repr) const { return false; }
39  };
40 
41  class SettingsMap : public Settings {
42  std::map<std::string, std::string> settings_;
43  virtual bool getRepr(const std::string &n, std::string & repr) const {
44  std::map<std::string, std::string>::const_iterator it = settings_.find(n);
45  if (it == settings_.cend()) return false;
46  repr = it->second;
47  return true;
48  }
49  public:
50  template <typename T> void set(const std::string &n, const T& val) {
51  settings_[n] = boost::lexical_cast<std::string>(val);
52  }
53  static std::shared_ptr<SettingsMap> create() { return std::shared_ptr<SettingsMap>(new SettingsMap); }
54  };
55 
56 
57 } // can
58 
59 #endif
virtual ~Settings()
Definition: settings.h:27
Definition: asio_base.h:11
std::shared_ptr< Settings > SettingsSharedPtr
Definition: settings.h:32
virtual bool getRepr(const std::string &n, std::string &repr) const
Definition: settings.h:38
virtual bool getRepr(const std::string &n, std::string &repr) const
Definition: settings.h:43
std::shared_ptr< const Settings > SettingsConstSharedPtr
Definition: settings.h:31
std::map< std::string, std::string > settings_
Definition: settings.h:42
virtual bool getRepr(const std::string &n, std::string &repr) const =0
T get_optional(const std::string &n, const T &def) const
Definition: settings.h:14
static SettingsConstSharedPtr create()
Definition: settings.h:36
static std::shared_ptr< SettingsMap > create()
Definition: settings.h:53


socketcan_interface
Author(s): Mathias L├╝dtke
autogenerated on Tue May 4 2021 02:50:18