KrisLibrary  1.0.0
PropertyMap.h
1 #ifndef UTILS_PROPERTY_MAP_H
2 #define UTILS_PROPERTY_MAP_H
3 
4 
5 #include <map>
6 #include <vector>
7 #include <string>
8 #include <iostream>
9 #include <sstream>
10 class TiXmlElement;
11 
27 class PropertyMap : public std::map<std::string,std::string>
28 {
29  public:
30  PropertyMap() {}
31  PropertyMap(const std::map<std::string,std::string>& );
32  template <class T>
33  PropertyMap(const std::map<std::string,T>& );
34  template <class T>
35  PropertyMap(const std::vector<std::string>& keys,const std::vector<T>& values);
36  inline bool contains(const std::string& key) const { return count(key) != 0; }
37  bool remove(const std::string& key);
38  void set(const std::string& key,const std::string& value);
39  bool get(const std::string& key,std::string& value) const;
40  std::string as(const std::string& key) const;
41  void setArray(const std::string& key,const std::vector<std::string>& values);
42  bool getArray(const std::string& key,std::vector<std::string>& values) const;
43  std::vector<std::string> asArray(const std::string& key) const;
44  template <class T>
45  void set(const std::string& key,const T& value);
46  template <class T>
47  bool get(const std::string& key,T& value) const;
48  template <class T>
49  T as(const std::string& key) const;
50  template <class T>
51  T getDefault(const std::string& key,T defaultValue=T()) const;
52  template <class T>
53  void setArray(const std::string& key,const std::vector<T>& value);
54  template <class T>
55  bool getArray(const std::string& key,std::vector<T>& values) const;
56  template <class T>
57  std::vector<T> asArray(const std::string& key) const;
58 
59  bool Load(TiXmlElement* node);
60  bool Save(TiXmlElement* node) const;
61  bool LoadJSON(std::istream& in);
62  bool SaveJSON(std::ostream& out) const;
63  bool Load(const char* fn);
64  bool Save(const char* fn) const;
65  void Print(std::ostream& out) const;
66 };
67 
68 inline std::ostream& operator << (std::ostream& out,const PropertyMap& pmap)
69 {
70  pmap.SaveJSON(out);
71  return out;
72 }
73 
74 inline std::istream& operator >> (std::istream& in,PropertyMap& pmap)
75 {
76  if(!pmap.LoadJSON(in))
77  in.setstate(std::ios::failbit);
78  return in;
79 }
80 
81 
82 template <class T>
83 PropertyMap::PropertyMap(const std::map<std::string,T>& rhs)
84 {
85  for(const auto& i=rhs.begin();i!=rhs.end();++i)
86  set(i->first,i->second);
87 }
88 
89 template <class T>
90 PropertyMap::PropertyMap(const std::vector<std::string>& keys,const std::vector<T>& values)
91 {
92  for(size_t i=0;i<keys.size();i++)
93  set(keys[i],values[i]);
94 }
95 
96 template <class T>
97 T PropertyMap::as(const std::string& key) const
98 {
99  T res;
100  get(key,res);
101  return res;
102 }
103 
104 template <class T>
105 T PropertyMap::getDefault(const std::string& key,T defaultValue) const
106 {
107  T res;
108  if(!get(key,res))
109  return defaultValue;
110  return res;
111 }
112 
113 template <class T>
114 bool PropertyMap::get(const std::string& key,T& value) const
115 {
116  const_iterator i=find(key);
117  if(i==end()) return false;
118  std::stringstream ss(i->second);
119  ss>>value;
120  if(!ss) return false;
121  return true;
122 }
123 
124 template <class T>
125 void PropertyMap::set(const std::string& key,const T& value)
126 {
127  std::stringstream ss;
128  ss<<value;
129  (*this)[key] = ss.str();
130 }
131 
132 template <class T>
133 bool PropertyMap::getArray(const std::string& key,std::vector<T>& values) const
134 {
135  const_iterator i=find(key);
136  if(i==end()) return false;
137  std::stringstream ss(i->second);
138  T temp;
139  values.resize(0);
140  while(ss) {
141  ss>>temp;
142  if(ss)
143  values.push_back(temp);
144  }
145  return true;
146 }
147 
148 
149 template <class T>
150 void PropertyMap::setArray(const std::string& key,const std::vector<T>& values)
151 {
152  std::stringstream ss;
153  for(size_t i=0;i<values.size();i++) {
154  if(i > 0) ss<<" ";
155  ss<<values[i];
156  }
157  set(key,ss.str());
158 }
159 
160 
161 
162 template <class T>
163 std::vector<T> PropertyMap::asArray(const std::string& key) const
164 {
165  std::vector<T> res;
166  getArray(key,res);
167  return res;
168 }
169 
170 
171 #endif
A simple map from keys to values.
Definition: PropertyMap.h:27
Definition: PropertyMap.cpp:9