VPTissue Reference Manual
AttributeStore.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "AttributeStore.h"
21 #include "util/misc/Exception.h"
22 
23 #include <cassert>
24 #include <iostream>
25 
26 using namespace std;
27 using boost::property_tree::ptree;
29 
30 
31 namespace SimPT_Sim {
32 
33 AttributeStore::AttributeStore(const boost::property_tree::ptree& pt) : m_ptree(pt)
34 {
35  const string xcpt = "AttributeStoreIndex::AttributeStoreIndex(const ptree&) : for type ";
36 
37  for(auto it = m_ptree.begin(); it != m_ptree.end(); it++){
38  const auto pt2 = it->second;
39  const auto name = pt2.get<string>("name");
40  const auto type = pt2.get<string>("type");
41 
42  if( type == "bool" ){
43  if ( IsAttribute<bool>(name) ) {
44  throw std::runtime_error(xcpt + "bool, name already in index: " + name);
45  } else{
46  m_map_b[name] = make_pair(std::vector<bool>(), pt2.get<bool>("default"));
47  }
48  } else if( type == "int" ){
49  if ( IsAttribute<int>(name) ) {
50  throw std::runtime_error(xcpt + "int, name already in index: " + name);
51  } else{
52  m_map_i[name] = make_pair(std::vector<int>(), pt2.get<int>("default"));
53  }
54  } else if( type == "double" ) {
55  if ( IsAttribute<int>(name) ) {
56  throw std::runtime_error(xcpt + "double, name already in index: " + name);
57  } else{
58  m_map_d[name] = make_pair(std::vector<double>(), pt2.get<double>("default"));
59  }
60  } else if ( type == "string" ) {
61  if ( IsAttribute<string>(name) ) {
62  throw std::runtime_error(xcpt + "string, name already in index: " + name);
63  } else{
64  m_map_s[name] = make_pair(std::vector<string>(), pt2.get<string>("default"));
65  }
66  } else {
67  throw Exception( xcpt + type + " is not a supported attribute type.");
68  }
69  }
70 }
71 
72 ptree AttributeStore::GetAttributeValues(size_t pos) const
73 {
74  ptree result;
75 
76  for(const auto& name : GetAttributeNames<bool>()) {
77  result.put(name, Container<bool>(name).at(pos));
78  }
79  for(const auto& name : GetAttributeNames<int>()) {
80  result.put(name, Container<int>(name).at(pos));
81  }
82  for(const auto& name : GetAttributeNames<double>()) {
83  result.put(name, Container<double>(name).at(pos));
84  }
85  for(const auto& name : GetAttributeNames<string>()) {
86  result.put(name, Container<string>(name).at(pos));
87  }
88 
89  return result;
90 }
91 
93 {
94  ptree result;
95 
96  for(const auto& name : GetAttributeNames<bool>()) {
97  ptree child;
98  child.put("name", name);
99  child.put("type", "bool");
100  child.put("default", GetDefaultValue<bool>(name));
101  result.add_child("attribute", child);
102  }
103  for(const auto& name : GetAttributeNames<int>()) {
104  ptree child;
105  child.put("name", name);
106  child.put("type", "int");
107  child.put("default", GetDefaultValue<int>(name));
108  result.add_child("attribute", child);
109  }
110  for(const auto& name : GetAttributeNames<double>()) {
111  ptree child;
112  child.put("name", name);
113  child.put("type", "double");
114  child.put("default", GetDefaultValue<double>(name));
115  result.add_child("attribute", child);
116  }
117  for(const auto& name : GetAttributeNames<string>()) {
118  ptree child;
119  child.put("name", name);
120  child.put("type", "string");
121  child.put("default", GetDefaultValue<double>(name));
122  result.add_child("attribute", child);
123  }
124 
125  return result;
126 }
127 
128 ostream& operator<<(ostream& os, const AttributeStore& a)
129 {
130  os << "AttributeStore index:" << endl;
131  const ptree pt = a.GetIndexPtree();
132  for(auto it = pt.begin(); it != pt.end(); it++){
133  const auto pt2 = it->second;
134  const auto name = pt2.get<string>("name");
135  const auto type = pt2.get<string>("type");
136 
137  if( type == "bool" ){
138  os << "name: " << name << " type: bool " << " default value: " << pt2.get<bool>("default") << endl;
139  } else if( type == "int" ){
140  os << "name: " << name << " type: int " << " default value: " << pt2.get<int>("default") << endl;
141  } else if( type == "double" ) {
142  os << "name: " << name << " type: double" << " default value: " << pt2.get<double>("default") << endl;
143  } else if ( type == "string" ) {
144  os << "name: " << name << " type: string" << " default value: " << pt2.get<string>("default") << endl;
145  } else {
146  throw Exception( string("operator<<(ostream& os, const AttributeStore& a)") + type + " is not a supported attribute type.");
147  }
148  }
149 
150  os << "AttributeStore values for bool attributes:" << endl;
151  for (const auto& name : a.GetAttributeNames<bool>()) {
152  os << "name: " << name << endl;
153  for (const auto e : a.Container<bool>(name)) {
154  os << e << endl;
155  }
156  os << endl;
157  }
158 
159  os << "AttributeStore values for int attributes:" << endl;
160  for (const auto& name : a.GetAttributeNames<int>()) {
161  os << "name: " << name << endl;
162  for (const auto e : a.Container<int>(name)) {
163  os << e << endl;
164  }
165  os << endl;
166  }
167 
168  os << "AttributeStore values for double attributes:" << endl;
169  for (const auto& name : a.GetAttributeNames<double>()) {
170  os << "name: " << name << endl;
171  for (const auto e : a.Container<double>(name)) {
172  os << e << endl;
173  }
174  os << endl;
175  }
176 
177  os << "AttributeStore values for string attributes:" << endl;
178  for (const auto& name : a.GetAttributeNames<string>()) {
179  os << "name: " << name << endl;
180  for (const auto e : a.Container<string>(name)) {
181  os << e << endl;
182  }
183  os << endl;
184  }
185 
186  return os;
187 }
188 
189 } // namespace
Interface of AttributeStore.
STL namespace.
boost::property_tree::ptree GetIndexPtree() const
Return original attribute ptree the AttributeStore was created from.
boost::property_tree::ptree GetAttributeValues(std::size_t pos) const
Return all attribute values at position i in the container in property tree format.
std::vector< T > & Container(const std::string &name)
Reference to the appropriate (type and name) attribute container.
Extremely simple Exception root class.
Definition: Exception.h:28
Namespace for the core simulator.
boost::property_tree::ptree GetIndexDump() const
Produces representation of index structure in a ptree descriptor.
Store and manage attributes.
std::vector< std::string > GetAttributeNames() const
Return a vector containing the names of attributes of type T.
Header file for Exception class.