VPTissue Reference Manual
AttributeContainer.h
Go to the documentation of this file.
1 #ifndef ATTRIBUTE_CONTAINER_H_INCLUDED
2 #define ATTRIBUTE_CONTAINER_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 // WARNING: FILE ALSO GETS PROCESSED BY SWIG. SWIG (VERSIONS EARLY 2013) //
24 // DOES NOT ACCEPT TWO CONSECUTIVE >> TEMPLATE TYPE PARAMETERLIST ENDING //
25 // BRACKETS WITHOUT INTERVENING BLANK (EVEN THOUGH C++11 DOES ACCPT THIS) //
27 
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <boost/property_tree/ptree.hpp>
32 
33 namespace SimPT_Sim {
34 
42 {
43 public:
47  AttributeContainer(size_t n = 0)
48  : m_num(n)
49  {
50  SetNum(n);
51  }
52 
56  template<typename T> void Add(const std::string& name);
57 
61  template<typename T> T Get(size_t index, const std::string& name) const;
62 
66  template<typename T> std::vector<T> GetAll(const std::string& name) const;
67 
71  template<typename T> std::vector<std::string> GetNames() const;
72 
76  size_t GetNum() const
77  {
78  return m_num;
79  }
80 
84  template<typename T> bool IsName(const std::string& name) const;
85 
89  void Print(std::ostream& out) const;
90 
94  template<typename T> void Set(size_t index, const std::string& name, T value);
95 
99  template<typename T> void SetAll(const std::string& name, const std::vector<T>& values);
100 
104  void SetNum(size_t n)
105  {
106  m_num = n;
107  for (auto attr : m_attri) {
108  attr.second.resize(n);
109  }
110  for (auto attr : m_attrd) {
111  attr.second.resize(n);
112  }
113  for (auto attr : m_attrs) {
114  attr.second.resize(n);
115  }
116  }
117 
118 private:
119  size_t m_num;
120  template<typename T> std::map<std::string, std::vector<T> >& Attributes();
121  template<typename T> const std::map<std::string, std::vector<T> >& Attributes() const;
122 
123 private:
124  std::map<std::string, std::vector<int> > m_attri;
125  std::map<std::string, std::vector<double> > m_attrd;
126  std::map<std::string, std::vector<std::string> > m_attrs;
127 };
128 
129 
131 // Implementation.
133 template<> inline
134 std::map<std::string, std::vector<int> >& AttributeContainer::Attributes<int>()
135 {
136  return m_attri;
137 }
138 
139 template<> inline
140 const std::map<std::string, std::vector<int> >& AttributeContainer::Attributes<int>() const
141 {
142  return m_attri;
143 }
144 
145 template<> inline
146 std::map<std::string, std::vector<double> >& AttributeContainer::Attributes<double>()
147 {
148  return m_attrd;
149 }
150 
151 template<> inline
152 const std::map<std::string, std::vector<double> >& AttributeContainer::Attributes<double>() const
153 {
154  return m_attrd;
155 }
156 
157 template<> inline
158 std::map<std::string, std::vector<std::string> >& AttributeContainer::Attributes<std::string>()
159 {
160  return m_attrs;
161 }
162 
163 template<> inline
164 const std::map<std::string, std::vector<std::string> >& AttributeContainer::Attributes<std::string>() const
165 {
166  return m_attrs;
167 }
168 
169 template<typename T>
170 inline void AttributeContainer::Add(const std::string& name)
171 {
172  Attributes<T>()[name] = std::vector<T>(m_num);
173 }
174 
175 template<typename T>
176 inline T AttributeContainer::Get(size_t node_idx, const std::string& name) const
177 {
178  auto attr_kv = Attributes<T>().find(name);
179  if (attr_kv != Attributes<T>().end()) {
180  return attr_kv->second.at(node_idx);
181  } else {
182  throw std::runtime_error("AttributeContainer::Get(): attribute not found: " + name);
183  }
184 }
185 template<typename T>
186 inline std::vector<std::string> AttributeContainer::GetNames() const
187 {
188  std::vector<std::string> names;
189  for (auto attr : Attributes<T>()) {
190  names.push_back(attr.first);
191  }
192  return names;
193 }
194 
195 template<typename T>
196 inline bool AttributeContainer::IsName(const std::string& name) const
197 {
198  return Attributes<T>().find(name) != Attributes<T>().end();
199 }
200 
201 template<typename T>
202 inline void AttributeContainer::Set(size_t node_idx, const std::string& name, T value)
203 {
204  auto attr_kv = Attributes<T>().find(name);
205  if (attr_kv != Attributes<T>().end()) {
206  attr_kv->second.at(node_idx) = value;
207  } else {
208  throw std::runtime_error("AttributeContainer::Set(): attribute not found: " + name);
209  }
210 }
211 
212 template<typename T>
213 inline std::vector<T> AttributeContainer::GetAll(const std::string& name) const
214 {
215  auto attr_kv = Attributes<T>().find(name);
216  if (attr_kv != Attributes<T>().end()) {
217  return attr_kv->second;
218  } else {
219  throw std::runtime_error("AttributeContainer::GetAll(): attribute not found: " + name);
220  }
221 }
222 
223 template<typename T>
224 inline void AttributeContainer::SetAll(const std::string& name, const std::vector<T>& values)
225 {
226  if (values.size() != GetNum()) {
227  throw std::runtime_error("AttributeContainer::SetAll(): Number attribute values doesn't match number of values.");
228  } else {
229  auto attr_kv = Attributes<T>().find(name);
230  if (attr_kv != Attributes<T>().end()) {
231  attr_kv->second = values;
232  } else {
233  throw std::runtime_error("AttributeContainer::SetAll(): attribute not found: " + name);
234  }
235  }
236 }
237 
238 } // namespace
239 
240 #endif // include guard
241 
bool IsName(const std::string &name) const
Returns true if there is an attribute of type T with a specific name.
void SetNum(size_t n)
Set size of each of the attribute containers.
std::vector< T > GetAll(const std::string &name) const
Returns all values of a named attribute.
void Add(const std::string &name)
Adds a named attribute of type T.
size_t GetNum() const
Returns size of each of the attribute containers.
T Get(size_t index, const std::string &name) const
Returns the value of a named attribute.
Namespace for the core simulator.
void SetAll(const std::string &name, const std::vector< T > &values)
Sets all values of a named attribute.
void Set(size_t index, const std::string &name, T value)
Sets the value of a named attribute.
AttributeContainer(size_t n=0)
Creates an empty AttributeContainer.
std::vector< std::string > GetNames() const
Returns a vector containing the names of attributes of type T.
void Print(std::ostream &out) const
Prints a textual representation of this AttributeContainer.