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