Flow123d  release_2.1.0-84-g6a13a75
storage.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file storage.cc
15  * @brief
16  */
17 
18 #include "system/global_defs.h"
19 #include "system/system.hh"
20 #include "input/storage.hh"
21 
22 namespace Input {
23 using namespace std;
24 
25 /**********************************************************************
26  * Implements StorageBase
27  */
28 
29 std::int64_t StorageBase::get_int() const {
30  THROW( ExcStorageTypeMismatch() << EI_RequestedType("int") << EI_StoredType( typeid(*this).name()) );
31  return 0;
32 }
33 
34 
35 
36 double StorageBase::get_double() const {
37  THROW( ExcStorageTypeMismatch() << EI_RequestedType("double") << EI_StoredType( typeid(*this).name()) );
38  return 0;
39 }
40 
41 
42 
43 bool StorageBase::get_bool() const {
44  THROW( ExcStorageTypeMismatch() << EI_RequestedType("bool") << EI_StoredType( typeid(*this).name()) );
45  return false;
46 }
47 
48 
49 
50 const std::string & StorageBase::get_string() const {
51  THROW( ExcStorageTypeMismatch() << EI_RequestedType("string") << EI_StoredType( typeid(*this).name()) );
52 #pragma GCC diagnostic push
53 #pragma GCC diagnostic ignored "-Wreturn-local-addr"
54  return 0; // Return reference to temporary, but we throw anyway.
55 #pragma GCC diagnostic pop
56 }
57 
58 
59 
60 StorageBase * StorageBase::get_item(const unsigned int index) const {
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_LT(index, array_.size()).error("Index is out of array.");
103  ASSERT( array_[index] == NULL )(index).error("Can not replace non NULL pointer.");
104  array_[index] = item;
105  }
106 
107 
108  void StorageArray::set_item(unsigned int index, StorageBase* item) {
109  ASSERT_LT(index, array_.size()).error("Index is out of array.");
110  ASSERT( array_[index] == NULL || typeid(*array_[index]) == typeid(StorageNull) )(index)
111  .error("Can not replace non NULL pointer.");
112 
113  if ( typeid(*array_[index]) == typeid(StorageNull) ) delete array_[index];
114  array_[index] = item;
115  }
116 
117 
118 
119 StorageBase * StorageArray::get_item(const unsigned int index) const {
120  ASSERT_LT(index, array_.size()).error("Index is out of array.");
121  ASSERT_PTR(array_[index])(index).error();
122  return array_[index];
123 }
124 
125 
126 
127 unsigned int StorageArray::get_array_size() const {
128  return array_.size();
129 }
130 
131 
132 
133 bool StorageArray::is_null() const {
134  return false;
135 }
136 
137 
138 void StorageArray::print(ostream &stream, int pad) const {
139  stream << setw(pad) << "" << "array(" << this->get_array_size() << ")" << std::endl;
140  for(unsigned int i=0;i<get_array_size();++i) get_item(i)->print(stream, pad+2);
141 }
142 
143 
144 
146  for( vector<StorageBase *>::iterator it = array_.begin(); it != array_.end(); ++it)
147  if (*it != NULL) delete (*it);
148 }
149 
150 /**********************************************
151  * Implementation of StorageBool
152  */
153 
155 : value_(value)
156 {}
157 
158 
159 
160 bool StorageBool::get_bool() const {
161  return value_;
162 }
163 
164 
165 
166 bool StorageBool::is_null() const {
167  return false;
168 }
169 
170 
171 
173  return new StorageBool(value_);
174 }
175 
176 void StorageBool::print(ostream &stream, int pad) const {
177  stream << setw(pad) << "" << "bool(" << value_ << ")"<<std::endl;
178 }
179 
181 {}
182 
183 
184 
185 /**********************************************
186  * Implementation of StorageInt
187  */
188 
190 : value_(value)
191 {}
192 
193 
194 
195 std::int64_t StorageInt::get_int() const {
196  return value_;
197 }
198 
199 
200 
201 bool StorageInt::is_null() const {
202  return false;
203 }
204 
206  return new StorageInt(value_);
207 }
208 
209 
210 void StorageInt::print(ostream &stream, int pad) const {
211  stream << setw(pad) << "" << "int(" << value_ << ")"<<std::endl;
212 }
213 
214 
216 {}
217 
218 
219 
220 /**********************************************
221  * Implementation of StorageDouble
222  */
223 
225 : value_(value)
226 {}
227 
228 
229 
231  return value_;
232 }
233 
234 
235 
237  return false;
238 }
239 
241  return new StorageDouble(value_);
242 }
243 
244 
245 void StorageDouble::print(ostream &stream, int pad) const {
246  stream << setw(pad) << "" << "double(" << value_ << ")"<<std::endl;
247 }
248 
249 
251 {}
252 
253 
254 
255 /**********************************************
256  * Implementation of StorageString
257  */
258 
260 : value_(value)
261 {}
262 
263 
264 
265 const string& StorageString::get_string() const {
266  return value_;
267 }
268 
269 
270 
272  return false;
273 }
274 
275 
277  return new StorageString(value_);
278 }
279 
280 
281 void StorageString::print(ostream &stream, int pad) const {
282  stream << setw(pad) << "" << "string(" << value_ << ")"<<std::endl;
283 }
284 
285 
287 {}
288 
289 
290 /**********************************************
291  * Implementation of StorageNull
292  */
293 
294 
295 bool StorageNull::is_null() const {
296  return true;
297 }
298 
299 
300 
302  return new StorageNull();
303 }
304 
305 
306 
307 void StorageNull::print(ostream &stream, int pad) const{
308  stream << setw(pad) << "" << "null()"<<std::endl;
309 }
310 
311 
313 {}
314 
315 
316 } // namespace Input
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:138
Base class for nodes of a data storage tree.
Definition: storage.hh:68
virtual std::int64_t get_int() const
Definition: storage.cc:29
virtual StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:119
StorageArray()
Forbids default constructor to have array set to NULL.
virtual StorageBase * deep_copy() const
Definition: storage.cc:240
virtual ~StorageString()
Definition: storage.cc:286
virtual StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:60
std::vector< StorageBase * > array_
Definition: storage.hh:104
virtual const std::string & get_string() const
Definition: storage.cc:50
IntFormatSpec< int, AlignTypeSpec< TYPE_CODE >, Char > pad(int value, unsigned width, Char fill= ' ')
virtual StorageBase * deep_copy() const
Definition: storage.cc:205
std::string value_
Definition: storage.hh:156
virtual bool is_null() const =0
Definition: storage.cc:67
virtual const std::string & get_string() const
Definition: storage.cc:265
std::int64_t value_
Definition: storage.hh:130
void set_item(unsigned int index, StorageBase *item)
Definition: storage.cc:108
virtual bool is_null() const
Definition: storage.cc:271
virtual bool is_null() const
Definition: storage.cc:166
virtual std::int64_t get_int() const
Definition: storage.cc:195
virtual void print(std::ostream &stream, int pad=0) const =0
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
virtual ~StorageBool()
Definition: storage.cc:180
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:176
static constexpr bool value
Definition: json.hpp:87
StorageDouble(double value)
Definition: storage.cc:224
virtual StorageBase * deep_copy() const
Definition: storage.cc:92
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:281
virtual double get_double() const
Definition: storage.cc:36
Global macros to enhance readability and debugging, general constants.
virtual bool is_null() const
Definition: storage.cc:133
virtual StorageBase * deep_copy() const
Definition: storage.cc:276
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:307
virtual ~StorageBase()
Definition: storage.cc:78
virtual StorageBase * deep_copy() const
Definition: storage.cc:301
virtual bool is_null() const
Definition: storage.cc:201
virtual ~StorageNull()
Definition: storage.cc:312
void new_item(unsigned int index, StorageBase *item)
Definition: storage.cc:101
virtual unsigned int get_array_size() const
Definition: storage.cc:73
virtual double get_double() const
Definition: storage.cc:230
virtual bool get_bool() const
Definition: storage.cc:43
virtual bool get_bool() const
Definition: storage.cc:160
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR)
Definition: asserts.hh:336
StorageInt(int value)
Definition: storage.cc:189
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than)
Definition: asserts.hh:296
virtual ~StorageInt()
Definition: storage.cc:215
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:210
StorageBool(bool value)
Definition: storage.cc:154
virtual ~StorageArray()
Definition: storage.cc:145
virtual unsigned int get_array_size() const
Definition: storage.cc:127
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:245
virtual bool is_null() const
Definition: storage.cc:236
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:45
virtual ~StorageDouble()
Definition: storage.cc:250
virtual StorageBase * deep_copy() const
Definition: storage.cc:172
StorageString(const std::string &value)
Definition: storage.cc:259
virtual bool is_null() const
Definition: storage.cc:295