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
can::Settings::get_optional
T get_optional(const std::string &n, const T &def) const
Definition: settings.h:14
can::NoSettings::create
static SettingsConstSharedPtr create()
Definition: settings.h:36
can::SettingsMap::getRepr
virtual bool getRepr(const std::string &n, std::string &repr) const
Definition: settings.h:43
can::Settings::getRepr
virtual bool getRepr(const std::string &n, std::string &repr) const =0
can::SettingsMap
Definition: settings.h:41
can::Settings
Definition: settings.h:11
can::SettingsMap::set
void set(const std::string &n, const T &val)
Definition: settings.h:50
can::NoSettings::getRepr
virtual bool getRepr(const std::string &n, std::string &repr) const
Definition: settings.h:38
can::SettingsMap::create
static std::shared_ptr< SettingsMap > create()
Definition: settings.h:53
can::SettingsMap::settings_
std::map< std::string, std::string > settings_
Definition: settings.h:42
can::SettingsConstSharedPtr
std::shared_ptr< const Settings > SettingsConstSharedPtr
Definition: settings.h:31
can::Settings::get
bool get(const std::string &n, T &val) const
Definition: settings.h:21
can
Definition: asio_base.h:11
can::NoSettings
Definition: settings.h:34
can::SettingsSharedPtr
std::shared_ptr< Settings > SettingsSharedPtr
Definition: settings.h:32
can::Settings::~Settings
virtual ~Settings()
Definition: settings.h:27


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Wed Mar 2 2022 00:52:25