Flow123d  jenkins-Flow123d-windows32-release-multijob-51
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 
99 const StorageBase * StorageArray::get_item(const unsigned int index) const {
100  if ( index >= array_.size() )
101  xprintf(Err, "Index %d out of array of size: %d", index, array_.size());
102  ASSERT( array_[index] != NULL, "Null pointer in storage.");
103  return array_[index];
104 }
105 
106 
107 
108 unsigned int StorageArray::get_array_size() const {
109  return array_.size();
110 }
111 
112 
113 
114 bool StorageArray::is_null() const {
115  return false;
116 }
117 
118 
119 void StorageArray::print(ostream &stream, int pad) const {
120  stream << setw(pad) << "" << "array(" << this->get_array_size() << ")" << std::endl;
121  for(unsigned int i=0;i<get_array_size();++i) get_item(i)->print(stream, pad+2);
122 }
123 
124 
125 
127  for( vector<StorageBase *>::iterator it = array_.begin(); it != array_.end(); ++it)
128  if (*it != NULL) delete (*it);
129 }
130 
131 /**********************************************
132  * Implementation of StorageBool
133  */
134 
136 : value_(value)
137 {}
138 
139 
140 
141 bool StorageBool::get_bool() const {
142  return value_;
143 }
144 
145 
146 
147 bool StorageBool::is_null() const {
148  return false;
149 }
150 
151 
152 
154  return new StorageBool(value_);
155 }
156 
157 void StorageBool::print(ostream &stream, int pad) const {
158  stream << setw(pad) << "" << "bool(" << value_ << ")"<<std::endl;
159 }
160 
162 {}
163 
164 
165 
166 /**********************************************
167  * Implementation of StorageInt
168  */
169 
171 : value_(value)
172 {}
173 
174 
175 
176 int StorageInt::get_int() const {
177  return value_;
178 }
179 
180 
181 
182 bool StorageInt::is_null() const {
183  return false;
184 }
185 
187  return new StorageInt(value_);
188 }
189 
190 
191 void StorageInt::print(ostream &stream, int pad) const {
192  stream << setw(pad) << "" << "int(" << value_ << ")"<<std::endl;
193 }
194 
195 
197 {}
198 
199 
200 
201 /**********************************************
202  * Implementation of StorageDouble
203  */
204 
206 : value_(value)
207 {}
208 
209 
210 
212  return value_;
213 }
214 
215 
216 
218  return false;
219 }
220 
222  return new StorageDouble(value_);
223 }
224 
225 
226 void StorageDouble::print(ostream &stream, int pad) const {
227  stream << setw(pad) << "" << "double(" << value_ << ")"<<std::endl;
228 }
229 
230 
232 {}
233 
234 
235 
236 /**********************************************
237  * Implementation of StorageString
238  */
239 
240 StorageString::StorageString(const string& value)
241 : value_(value)
242 {}
243 
244 
245 
246 const string& StorageString::get_string() const {
247  return value_;
248 }
249 
250 
251 
253  return false;
254 }
255 
256 
258  return new StorageString(value_);
259 }
260 
261 
262 void StorageString::print(ostream &stream, int pad) const {
263  stream << setw(pad) << "" << "string(" << value_ << ")"<<std::endl;
264 }
265 
266 
268 {}
269 
270 
271 /**********************************************
272  * Implementation of StorageNull
273  */
274 
275 
276 bool StorageNull::is_null() const {
277  return true;
278 }
279 
280 
281 
283  return new StorageNull();
284 }
285 
286 
287 
288 void StorageNull::print(ostream &stream, int pad) const{
289  stream << setw(pad) << "" << "null()"<<std::endl;
290 }
291 
292 
294 {}
295 
296 
297 } // namespace Input
virtual StorageBase * deep_copy()
Definition: storage.cc:153
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:119
virtual StorageBase * deep_copy()
Definition: storage.cc:257
Base class for nodes of a data storage tree.
Definition: storage.hh:57
StorageArray()
Forbids default constructor to have array set to NULL.
virtual ~StorageString()
Definition: storage.cc:267
std::vector< StorageBase * > array_
Definition: storage.hh:92
virtual const std::string & get_string() const
Definition: storage.cc:40
std::string value_
Definition: storage.hh:144
virtual bool is_null() const =0
Definition: storage.cc:57
virtual const std::string & get_string() const
Definition: storage.cc:246
virtual bool is_null() const
Definition: storage.cc:252
virtual bool is_null() const
Definition: storage.cc:147
virtual int get_int() const
Definition: storage.cc:176
virtual void print(std::ostream &stream, int pad=0) const =0
virtual ~StorageBool()
Definition: storage.cc:161
virtual StorageBase * deep_copy()
Definition: storage.cc:282
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:157
StorageDouble(double value)
Definition: storage.cc:205
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:262
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:114
#define ASSERT(...)
Definition: global_defs.h:121
virtual StorageBase * deep_copy()
Definition: storage.cc:82
#define xprintf(...)
Definition: system.hh:100
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:288
virtual ~StorageBase()
Definition: storage.cc:68
virtual bool is_null() const
Definition: storage.cc:182
virtual ~StorageNull()
Definition: storage.cc:293
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:99
virtual unsigned int get_array_size() const
Definition: storage.cc:63
virtual StorageBase * deep_copy()
Definition: storage.cc:186
virtual double get_double() const
Definition: storage.cc:211
virtual bool get_bool() const
Definition: storage.cc:33
virtual bool get_bool() const
Definition: storage.cc:141
Definition: system.hh:72
StorageInt(int value)
Definition: storage.cc:170
virtual ~StorageInt()
Definition: storage.cc:196
virtual StorageBase * deep_copy()
Definition: storage.cc:221
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:191
Definition: system.hh:72
StorageBool(bool value)
Definition: storage.cc:135
virtual ~StorageArray()
Definition: storage.cc:126
virtual unsigned int get_array_size() const
Definition: storage.cc:108
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:226
virtual bool is_null() const
Definition: storage.cc:217
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34
virtual ~StorageDouble()
Definition: storage.cc:231
StorageString(const std::string &value)
Definition: storage.cc:240
virtual bool is_null() const
Definition: storage.cc:276