Flow123d  jenkins-Flow123d-linux-release-multijob-282
storage.cc
Go to the documentation of this file.
1 /*
2  * storage.cc
3  *
4  * Created on: May 5, 2012
5  * Author: jb
6  */
7 
8 #include "system/global_defs.h"
9 #include "system/system.hh"
10 #include "input/storage.hh"
11 
12 namespace Input {
13 using namespace std;
14 
15 /**********************************************************************
16  * Implements StorageBase
17  */
18 
19 int StorageBase::get_int() const {
20  THROW( ExcStorageTypeMismatch() << EI_RequestedType("int") << EI_StoredType( typeid(*this).name()) );
21  return 0;
22 }
23 
24 
25 
26 double StorageBase::get_double() const {
27  THROW( ExcStorageTypeMismatch() << EI_RequestedType("double") << EI_StoredType( typeid(*this).name()) );
28  return 0;
29 }
30 
31 
32 
33 bool StorageBase::get_bool() const {
34  THROW( ExcStorageTypeMismatch() << EI_RequestedType("bool") << EI_StoredType( typeid(*this).name()) );
35  return false;
36 }
37 
38 
39 
40 const std::string & StorageBase::get_string() const {
41  THROW( ExcStorageTypeMismatch() << EI_RequestedType("string") << EI_StoredType( typeid(*this).name()) );
42 #pragma GCC diagnostic push
43 #pragma GCC diagnostic ignored "-Wreturn-local-addr"
44  return 0; // Return reference to temporary, but we throw anyway.
45 #pragma GCC diagnostic pop
46 }
47 
48 
49 
50 const StorageBase * StorageBase::get_item(const unsigned int index) const {
51  THROW( ExcStorageTypeMismatch() << EI_RequestedType("array") << EI_StoredType( typeid(*this).name()) );
52  return 0;
53 }
54 
55 
56 
57 bool StorageBase::is_null() const {
58  return false;
59 }
60 
61 
62 
63 unsigned int StorageBase::get_array_size() const {
64  THROW( ExcStorageTypeMismatch() << EI_RequestedType("array") << EI_StoredType( typeid(*this).name()) );
65  return 0;
66 }
67 
69 {}
70 
71 /*****************************************************************
72  * Implementation of StorageArray
73  */
74 
75 StorageArray::StorageArray(unsigned int size)
76 : array_(size)
77 {
78  for( vector<StorageBase *>::iterator it = array_.begin(); it != array_.end(); ++it)
79  *it=NULL;
80 }
81 
83  StorageArray *copy = new StorageArray(this->get_array_size());
84 
85  for(unsigned int i=0; i< array_.size(); i++)
86  if (array_[i] != NULL) copy->new_item(i, array_[i]->deep_copy() );
87 
88  return copy;
89 }
90 
91  void StorageArray::new_item(unsigned int index, StorageBase* item) {
92  ASSERT( index < array_.size() , "Index %d out of array of size: %d", index, array_.size());
93  if (array_[index] == NULL) array_[index] = item;
94  else xprintf(PrgErr, "Can not replace non NULL pointer.");
95  }
96 
97 
98  void StorageArray::set_item(unsigned int index, StorageBase* item) {
99  ASSERT( index < array_.size() , "Index %d out of array of size: %d", index, array_.size());
100  if (array_[index] == NULL) array_[index] = item;
101  else if ( typeid(*array_[index]) == typeid(StorageNull) ) {
102  delete array_[index];
103  array_[index] = item;
104  } else xprintf(PrgErr, "Can not replace non NULL pointer.");
105 
106  }
107 
108 
109 
110 const StorageBase * StorageArray::get_item(const unsigned int index) const {
111  if ( index >= array_.size() )
112  xprintf(Err, "Index %d out of array of size: %d", index, array_.size());
113  ASSERT( array_[index] != NULL, "Null pointer in storage.");
114  return array_[index];
115 }
116 
117 
118 
119 unsigned int StorageArray::get_array_size() const {
120  return array_.size();
121 }
122 
123 
124 
125 bool StorageArray::is_null() const {
126  return false;
127 }
128 
129 
130 void StorageArray::print(ostream &stream, int pad) const {
131  stream << setw(pad) << "" << "array(" << this->get_array_size() << ")" << std::endl;
132  for(unsigned int i=0;i<get_array_size();++i) get_item(i)->print(stream, pad+2);
133 }
134 
135 
136 
138  for( vector<StorageBase *>::iterator it = array_.begin(); it != array_.end(); ++it)
139  if (*it != NULL) delete (*it);
140 }
141 
142 /**********************************************
143  * Implementation of StorageBool
144  */
145 
147 : value_(value)
148 {}
149 
150 
151 
152 bool StorageBool::get_bool() const {
153  return value_;
154 }
155 
156 
157 
158 bool StorageBool::is_null() const {
159  return false;
160 }
161 
162 
163 
165  return new StorageBool(value_);
166 }
167 
168 void StorageBool::print(ostream &stream, int pad) const {
169  stream << setw(pad) << "" << "bool(" << value_ << ")"<<std::endl;
170 }
171 
173 {}
174 
175 
176 
177 /**********************************************
178  * Implementation of StorageInt
179  */
180 
182 : value_(value)
183 {}
184 
185 
186 
187 int StorageInt::get_int() const {
188  return value_;
189 }
190 
191 
192 
193 bool StorageInt::is_null() const {
194  return false;
195 }
196 
198  return new StorageInt(value_);
199 }
200 
201 
202 void StorageInt::print(ostream &stream, int pad) const {
203  stream << setw(pad) << "" << "int(" << value_ << ")"<<std::endl;
204 }
205 
206 
208 {}
209 
210 
211 
212 /**********************************************
213  * Implementation of StorageDouble
214  */
215 
217 : value_(value)
218 {}
219 
220 
221 
223  return value_;
224 }
225 
226 
227 
229  return false;
230 }
231 
233  return new StorageDouble(value_);
234 }
235 
236 
237 void StorageDouble::print(ostream &stream, int pad) const {
238  stream << setw(pad) << "" << "double(" << value_ << ")"<<std::endl;
239 }
240 
241 
243 {}
244 
245 
246 
247 /**********************************************
248  * Implementation of StorageString
249  */
250 
251 StorageString::StorageString(const string& value)
252 : value_(value)
253 {}
254 
255 
256 
257 const string& StorageString::get_string() const {
258  return value_;
259 }
260 
261 
262 
264  return false;
265 }
266 
267 
269  return new StorageString(value_);
270 }
271 
272 
273 void StorageString::print(ostream &stream, int pad) const {
274  stream << setw(pad) << "" << "string(" << value_ << ")"<<std::endl;
275 }
276 
277 
279 {}
280 
281 
282 /**********************************************
283  * Implementation of StorageNull
284  */
285 
286 
287 bool StorageNull::is_null() const {
288  return true;
289 }
290 
291 
292 
294  return new StorageNull();
295 }
296 
297 
298 
299 void StorageNull::print(ostream &stream, int pad) const{
300  stream << setw(pad) << "" << "null()"<<std::endl;
301 }
302 
303 
305 {}
306 
307 
308 } // namespace Input
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:130
Base class for nodes of a data storage tree.
Definition: storage.hh:57
StorageArray()
Forbids default constructor to have array set to NULL.
virtual StorageBase * deep_copy() const
Definition: storage.cc:232
virtual ~StorageString()
Definition: storage.cc:278
std::vector< StorageBase * > array_
Definition: storage.hh:93
virtual const std::string & get_string() const
Definition: storage.cc:40
virtual StorageBase * deep_copy() const
Definition: storage.cc:197
std::string value_
Definition: storage.hh:145
virtual bool is_null() const =0
Definition: storage.cc:57
virtual const std::string & get_string() const
Definition: storage.cc:257
void set_item(unsigned int index, StorageBase *item)
Definition: storage.cc:98
virtual bool is_null() const
Definition: storage.cc:263
virtual bool is_null() const
Definition: storage.cc:158
virtual int get_int() const
Definition: storage.cc:187
virtual void print(std::ostream &stream, int pad=0) const =0
virtual ~StorageBool()
Definition: storage.cc:172
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:168
StorageDouble(double value)
Definition: storage.cc:216
virtual StorageBase * deep_copy() const
Definition: storage.cc:82
virtual const StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:50
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:273
virtual double get_double() const
Definition: storage.cc:26
virtual int get_int() const
Definition: storage.cc:19
Global macros to enhance readability and debugging, general constants.
virtual bool is_null() const
Definition: storage.cc:125
#define ASSERT(...)
Definition: global_defs.h:121
#define xprintf(...)
Definition: system.hh:100
virtual StorageBase * deep_copy() const
Definition: storage.cc:268
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:299
virtual ~StorageBase()
Definition: storage.cc:68
virtual StorageBase * deep_copy() const
Definition: storage.cc:293
virtual bool is_null() const
Definition: storage.cc:193
virtual ~StorageNull()
Definition: storage.cc:304
void new_item(unsigned int index, StorageBase *item)
Definition: storage.cc:91
virtual const StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:110
virtual unsigned int get_array_size() const
Definition: storage.cc:63
virtual double get_double() const
Definition: storage.cc:222
virtual bool get_bool() const
Definition: storage.cc:33
virtual bool get_bool() const
Definition: storage.cc:152
Definition: system.hh:72
StorageInt(int value)
Definition: storage.cc:181
virtual ~StorageInt()
Definition: storage.cc:207
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:202
Definition: system.hh:72
StorageBool(bool value)
Definition: storage.cc:146
virtual ~StorageArray()
Definition: storage.cc:137
virtual unsigned int get_array_size() const
Definition: storage.cc:119
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:237
virtual bool is_null() const
Definition: storage.cc:228
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34
virtual ~StorageDouble()
Definition: storage.cc:242
virtual StorageBase * deep_copy() const
Definition: storage.cc:164
StorageString(const std::string &value)
Definition: storage.cc:251
virtual bool is_null() const
Definition: storage.cc:287