Util--  1.1
configset.hh
Go to the documentation of this file.
1 #ifndef UTILMM_CONFIG_SET_HH
2 #define UTILMM_CONFIG_SET_HH
3 
4 #include <map>
5 #include <string>
6 #include <boost/noncopyable.hpp>
7 #include <boost/lexical_cast.hpp>
8 #include <boost/utility/enable_if.hpp>
9 
10 #include <list>
11 
12 namespace utilmm
13 {
14  /* To discriminate between list and non-list get<> */
15  namespace details
16  {
17  template<typename T> struct is_list
18  { static const bool value = false; };
19  template<typename T> struct is_list< std::list<T> >
20  { static const bool value = true; };
21  }
22 
24  class config_set
25  : private boost::noncopyable
26  {
27  friend class ConfigFile;
28 
29  private:
30  typedef std::list<std::string> stringlist;
31  template<typename T>
32  static T convert(std::string const& value);
33 
34  protected:
36  typedef std::multimap<std::string, std::string> ValueMap;
38  typedef std::multimap<std::string, const config_set*> ChildMap;
40 
41  protected:
43  void clear();
44 
45  public:
46  explicit config_set(config_set* parent = 0);
47  ~config_set();
48 
49  typedef std::list<const config_set*> subsets;
50 
52  bool empty() const;
53 
55  bool exists (const std::string& attribute) const;
56 
60  const config_set* parent() const;
61  config_set* parent();
62 
64  std::list<const config_set*> children(const std::string& name) const;
66  config_set const& child(std::string const& name) const;
67 
74  template<typename T>
75  T get(std::string const& name, T const& defval = T(),
76  typename boost::enable_if< details::is_list<T> >::type *enabler = 0) const;
84  template<typename T>
85  T get(std::string const& name, T const& defval = T(),
86  typename boost::disable_if< details::is_list<T> >::type *enabler = 0) const;
87 
91  void set(std::string const& name, std::string const& value);
96  void set(std::string const& name, std::list<std::string> const& value);
98  void insert(std::string const& name, std::string const& value);
100  void insert(std::string const& name, std::list<std::string> const& value);
102  void insert(std::string const& name, config_set const* value);
104  void erase(std::string const& name);
105  };
106 
107  namespace details {
108  typedef std::list<std::string> stringlist;
109  }
110 
111  template<> bool config_set::convert(std::string const& value);
112  template<typename T>
113  T config_set::convert(const std::string& value)
114  { return boost::lexical_cast<T>(value); }
115 
116 
117  template<>
118  config_set::stringlist config_set::get(std::string const& name,
119  config_set::stringlist const& defval,
120  boost::enable_if< details::is_list<config_set::stringlist> >::type *enabler) const;
121 
122  template<typename T>
123  T config_set::get(std::string const& name, T const& defval,
124  typename boost::enable_if< details::is_list<T> >::type *enabler) const
125  {
126  stringlist values = get< stringlist >(name);
127  if (values.empty())
128  return defval;
129 
130  T result;
131  for (stringlist::const_iterator it = values.begin(); it != values.end(); ++it)
132  result.push_back(convert<typename T::value_type>(*it));
133 
134  return result;
135  }
136 
137  template<typename T>
138  T config_set::get(std::string const& name, T const& defval,
139  typename boost::disable_if< details::is_list<T> >::type *enabler) const
140  {
141  std::list<T> deflist;
142  deflist.push_back(defval);
143  return get< std::list<T> >(name, deflist).front();
144  }
145 }
146 
147 #endif
148 
149 
150 
ChildMap m_children
Definition: configset.hh:39
bool empty() const
void insert(std::string const &name, std::string const &value)
T get(std::string const &name, T const &defval=T(), typename boost::enable_if< details::is_list< T > >::type *enabler=0) const
Definition: configset.hh:123
config_set const & child(std::string const &name) const
const config_set * parent() const
bool exists(const std::string &attribute) const
std::multimap< std::string, const config_set * > ChildMap
Definition: configset.hh:38
std::list< std::string > stringlist
Definition: configset.hh:108
Definition: configset.hh:24
config_set(config_set *parent=0)
ValueMap m_values
Definition: configset.hh:37
void erase(std::string const &name)
std::list< const config_set * > subsets
Definition: configset.hh:49
std::list< const config_set * > children(const std::string &name) const
static const bool value
Definition: configset.hh:18
std::multimap< std::string, std::string > ValueMap
Definition: configset.hh:36
void set(std::string const &name, std::string const &value)
config_set * m_parent
Definition: configset.hh:35
friend class ConfigFile
Definition: configset.hh:27
Definition: configset.hh:17

Generated on Tue Oct 9 2018 10:12:45 for Util-- by doxygen 1.8.6
SourceForge.net Project Page