Flow123d
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  return 0; // Return reference to temporary, but we throw anyway.
51 }
52 
53 
54 
55 const StorageBase * StorageBase::get_item(const unsigned int index) const {
56  //cout << "Fatal Error at:" << std::endl;
57  //print(cout,0);
58  THROW( ExcStorageTypeMismatch() << EI_RequestedType("array") << EI_StoredType( typeid(*this).name()) );
59  return 0;
60 }
61 
62 
63 
64 bool StorageBase::is_null() const {
65  return false;
66 }
67 
68 
69 
70 unsigned int StorageBase::get_array_size() const {
71  THROW( ExcStorageTypeMismatch() << EI_RequestedType("array") << EI_StoredType( typeid(*this).name()) );
72  return 0;
73 }
74 
76 {}
77 
78 /*****************************************************************
79  * Implementation of StorageArray
80  */
81 
82 StorageArray::StorageArray(unsigned int size)
83 : array_(size)
84 {
85  for( vector<StorageBase *>::iterator it = array_.begin(); it != array_.end(); ++it)
86  *it=NULL;
87 }
88 
90  StorageArray *copy = new StorageArray(this->get_array_size());
91 
92  for(unsigned int i=0; i< array_.size(); i++)
93  if (array_[i] != NULL) copy->new_item(i, array_[i]->deep_copy() );
94 
95  return copy;
96 }
97 
98 void StorageArray::new_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 xprintf(PrgErr, "Can not replace non NULL pointer.");
102 }
103 
104 
105 
106 const StorageBase * StorageArray::get_item(const unsigned int index) const {
107  if ( index >= array_.size() )
108  xprintf(Err, "Index %d out of array of size: %d", index, array_.size());
109  ASSERT( array_[index] != NULL, "Null pointer in storage.");
110  return array_[index];
111 }
112 
113 
114 
115 unsigned int StorageArray::get_array_size() const {
116  return array_.size();
117 }
118 
119 
120 
121 bool StorageArray::is_null() const {
122  return false;
123 }
124 
125 
126 void StorageArray::print(ostream &stream, int pad) const {
127  stream << setw(pad) << "" << "array(" << this->get_array_size() << ")" << std::endl;
128  for(unsigned int i=0;i<get_array_size();++i) get_item(i)->print(stream, pad+2);
129 }
130 
131 
132 
134  for( vector<StorageBase *>::iterator it = array_.begin(); it != array_.end(); ++it)
135  if (*it != NULL) delete (*it);
136 }
137 
138 /**********************************************
139  * Implementation of StorageBool
140  */
141 
143 : value_(value)
144 {}
145 
146 
147 
148 bool StorageBool::get_bool() const {
149  return value_;
150 }
151 
152 
153 
154 bool StorageBool::is_null() const {
155  return false;
156 }
157 
158 
159 
161  return new StorageBool(value_);
162 }
163 
164 void StorageBool::print(ostream &stream, int pad) const {
165  stream << setw(pad) << "" << "bool(" << value_ << ")"<<std::endl;
166 }
167 
169 {}
170 
171 
172 
173 /**********************************************
174  * Implementation of StorageInt
175  */
176 
178 : value_(value)
179 {}
180 
181 
182 
183 int StorageInt::get_int() const {
184  return value_;
185 }
186 
187 
188 
189 bool StorageInt::is_null() const {
190  return false;
191 }
192 
194  return new StorageInt(value_);
195 }
196 
197 
198 void StorageInt::print(ostream &stream, int pad) const {
199  stream << setw(pad) << "" << "int(" << value_ << ")"<<std::endl;
200 }
201 
202 
204 {}
205 
206 
207 
208 /**********************************************
209  * Implementation of StorageDouble
210  */
211 
213 : value_(value)
214 {}
215 
216 
217 
219  return value_;
220 }
221 
222 
223 
225  return false;
226 }
227 
229  return new StorageDouble(value_);
230 }
231 
232 
233 void StorageDouble::print(ostream &stream, int pad) const {
234  stream << setw(pad) << "" << "double(" << value_ << ")"<<std::endl;
235 }
236 
237 
239 {}
240 
241 
242 
243 /**********************************************
244  * Implementation of StorageString
245  */
246 
247 StorageString::StorageString(const string& value)
248 : value_(value)
249 {}
250 
251 
252 
253 const string& StorageString::get_string() const {
254  return value_;
255 }
256 
257 
258 
260  return false;
261 }
262 
263 
265  return new StorageString(value_);
266 }
267 
268 
269 void StorageString::print(ostream &stream, int pad) const {
270  stream << setw(pad) << "" << "string(" << value_ << ")"<<std::endl;
271 }
272 
273 
275 {}
276 
277 
278 /**********************************************
279  * Implementation of StorageNull
280  */
281 
282 
283 bool StorageNull::is_null() const {
284  return true;
285 }
286 
287 
288 
290  return new StorageNull();
291 }
292 
293 
294 
295 void StorageNull::print(ostream &stream, int pad) const{
296  stream << setw(pad) << "" << "null()"<<std::endl;
297 }
298 
299 
301 {}
302 
303 
304 } // namespace Input