1 #ifndef ATTRIBUTE_CONTAINER_H_INCLUDED
2 #define ATTRIBUTE_CONTAINER_H_INCLUDED
31 #include <boost/property_tree/ptree.hpp>
56 template<
typename T>
void Add(
const std::string& name);
61 template<
typename T> T
Get(
size_t index,
const std::string& name)
const;
66 template<
typename T> std::vector<T>
GetAll(
const std::string& name)
const;
71 template<
typename T> std::vector<std::string>
GetNames()
const;
84 template<
typename T>
bool IsName(
const std::string& name)
const;
89 void Print(std::ostream& out)
const;
94 template<
typename T>
void Set(
size_t index,
const std::string& name, T value);
99 template<
typename T>
void SetAll(
const std::string& name,
const std::vector<T>& values);
107 for (
auto attr : m_attri) {
108 attr.second.resize(n);
110 for (
auto attr : m_attrd) {
111 attr.second.resize(n);
113 for (
auto attr : m_attrs) {
114 attr.second.resize(n);
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;
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;
134 std::map<std::string, std::vector<int> >& AttributeContainer::Attributes<int>()
140 const std::map<std::string, std::vector<int> >& AttributeContainer::Attributes<int>()
const
146 std::map<std::string, std::vector<double> >& AttributeContainer::Attributes<double>()
152 const std::map<std::string, std::vector<double> >& AttributeContainer::Attributes<double>()
const
158 std::map<std::string, std::vector<std::string> >& AttributeContainer::Attributes<std::string>()
164 const std::map<std::string, std::vector<std::string> >& AttributeContainer::Attributes<std::string>()
const
172 Attributes<T>()[name] = std::vector<T>(m_num);
178 auto attr_kv = Attributes<T>().find(name);
179 if (attr_kv != Attributes<T>().end()) {
180 return attr_kv->second.at(node_idx);
182 throw std::runtime_error(
"AttributeContainer::Get(): attribute not found: " + name);
188 std::vector<std::string> names;
189 for (
auto attr : Attributes<T>()) {
190 names.push_back(attr.first);
198 return Attributes<T>().find(name) != Attributes<T>().end();
204 auto attr_kv = Attributes<T>().find(name);
205 if (attr_kv != Attributes<T>().end()) {
206 attr_kv->second.at(node_idx) = value;
208 throw std::runtime_error(
"AttributeContainer::Set(): attribute not found: " + name);
215 auto attr_kv = Attributes<T>().find(name);
216 if (attr_kv != Attributes<T>().end()) {
217 return attr_kv->second;
219 throw std::runtime_error(
"AttributeContainer::GetAll(): attribute not found: " + name);
226 if (values.size() !=
GetNum()) {
227 throw std::runtime_error(
"AttributeContainer::SetAll(): Number attribute values doesn't match number of values.");
229 auto attr_kv = Attributes<T>().find(name);
230 if (attr_kv != Attributes<T>().end()) {
231 attr_kv->second = values;
233 throw std::runtime_error(
"AttributeContainer::SetAll(): attribute not found: " + name);
240 #endif // include guard
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.