Flow123d  master-f44eb46
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 void StorageBase::set_item(FMT_UNUSED unsigned int index, FMT_UNUSED StorageBase* item) {
61  THROW( ExcStorageTypeMismatch() << EI_RequestedType("array") << EI_StoredType( typeid(*this).name()) );
62 }
63 
64 
65 
66 StorageBase * StorageBase::get_item(FMT_UNUSED const unsigned int index) const {
67  THROW( ExcStorageTypeMismatch() << EI_RequestedType("array") << EI_StoredType( typeid(*this).name()) );
68  return 0;
69 }
70 
71 
72 
73 bool StorageBase::is_null() const {
74  return false;
75 }
76 
77 
78 
79 unsigned int StorageBase::get_array_size() const {
80  THROW( ExcStorageTypeMismatch() << EI_RequestedType("array") << EI_StoredType( typeid(*this).name()) );
81  return 0;
82 }
83 
85 {}
86 
87 /*****************************************************************
88  * Implementation of StorageArray
89  */
90 
91 StorageArray::StorageArray(unsigned int size)
92 : array_(size)
93 {
94  for( vector<StorageBase *>::iterator it = array_.begin(); it != array_.end(); ++it)
95  *it=NULL;
96 }
97 
99  StorageArray *copy = new StorageArray(this->get_array_size());
100 
101  for(unsigned int i=0; i< array_.size(); i++)
102  if (array_[i] != NULL) copy->new_item(i, array_[i]->deep_copy() );
103 
104  return copy;
105 }
106 
107  void StorageArray::new_item(unsigned int index, StorageBase* item) {
108  ASSERT_LT(index, array_.size()).error("Index is out of array.");
109  ASSERT( array_[index] == NULL )(index).error("Can not replace non NULL pointer.");
110  array_[index] = item;
111  }
112 
113 
114  void StorageArray::set_item(unsigned int index, StorageBase* item) {
115  ASSERT_LT(index, array_.size()).error("Index is out of array.");
116 
117  if ( array_[index] != NULL ) delete array_[index];
118  array_[index] = item;
119  }
120 
121 
122 
123 StorageBase * StorageArray::get_item(const unsigned int index) const {
124  ASSERT_LT(index, array_.size()).error("Index is out of array.");
125  ASSERT_PTR(array_[index])(index).error();
126  return array_[index];
127 }
128 
129 
130 
131 unsigned int StorageArray::get_array_size() const {
132  return array_.size();
133 }
134 
135 
136 
137 bool StorageArray::is_null() const {
138  return false;
139 }
140 
141 
142 void StorageArray::print(ostream &stream, int pad) const {
143  stream << setw(pad) << "" << "array(" << this->get_array_size() << ")" << std::endl;
144  for(unsigned int i=0;i<get_array_size();++i) get_item(i)->print(stream, pad+2);
145 }
146 
147 
148 
150  for( vector<StorageBase *>::iterator it = array_.begin(); it != array_.end(); ++it)
151  if (*it != NULL) delete (*it);
152 }
153 
154 /**********************************************
155  * Implementation of StorageBool
156  */
157 
159 : value_(value)
160 {}
161 
162 
163 
164 bool StorageBool::get_bool() const {
165  return value_;
166 }
167 
168 
169 
170 bool StorageBool::is_null() const {
171  return false;
172 }
173 
174 
175 
177  return new StorageBool(value_);
178 }
179 
180 void StorageBool::print(ostream &stream, int pad) const {
181  stream << setw(pad) << "" << "bool(" << value_ << ")"<<std::endl;
182 }
183 
185 {}
186 
187 
188 
189 /**********************************************
190  * Implementation of StorageInt
191  */
192 
194 : value_(value)
195 {}
196 
197 
198 
199 std::int64_t StorageInt::get_int() const {
200  return value_;
201 }
202 
203 
204 
205 bool StorageInt::is_null() const {
206  return false;
207 }
208 
210  return new StorageInt(value_);
211 }
212 
213 
214 void StorageInt::print(ostream &stream, int pad) const {
215  stream << setw(pad) << "" << "int(" << value_ << ")"<<std::endl;
216 }
217 
218 
220 {}
221 
222 
223 
224 /**********************************************
225  * Implementation of StorageDouble
226  */
227 
229 : value_(value)
230 {}
231 
232 
233 
235  return value_;
236 }
237 
238 
239 
241  return false;
242 }
243 
245  return new StorageDouble(value_);
246 }
247 
248 
249 void StorageDouble::print(ostream &stream, int pad) const {
250  stream << setw(pad) << "" << "double(" << value_ << ")"<<std::endl;
251 }
252 
253 
255 {}
256 
257 
258 
259 /**********************************************
260  * Implementation of StorageString
261  */
262 
264 : value_(value)
265 {}
266 
267 
268 
269 const string& StorageString::get_string() const {
270  return value_;
271 }
272 
273 
274 
276  return false;
277 }
278 
279 
281  return new StorageString(value_);
282 }
283 
284 
285 void StorageString::print(ostream &stream, int pad) const {
286  stream << setw(pad) << "" << "string(" << value_ << ")"<<std::endl;
287 }
288 
289 
291 {}
292 
293 
294 /**********************************************
295  * Implementation of StorageNull
296  */
297 
298 
299 bool StorageNull::is_null() const {
300  return true;
301 }
302 
303 
304 
306  return new StorageNull();
307 }
308 
309 
310 
311 void StorageNull::print(ostream &stream, int pad) const{
312  stream << setw(pad) << "" << "null()"<<std::endl;
313 }
314 
315 
317 {}
318 
319 
320 } // namespace Input
#define ASSERT(expr)
Definition: asserts.hh:351
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:301
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
virtual bool is_null() const
Definition: storage.cc:137
virtual StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:123
virtual StorageBase * deep_copy() const
Definition: storage.cc:98
std::vector< StorageBase * > array_
Definition: storage.hh:105
virtual unsigned int get_array_size() const
Definition: storage.cc:131
virtual void set_item(unsigned int index, StorageBase *item)
Definition: storage.cc:114
StorageArray()
Forbids default constructor to have array set to NULL.
void new_item(unsigned int index, StorageBase *item)
Definition: storage.cc:107
virtual ~StorageArray()
Definition: storage.cc:149
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:142
Base class for nodes of a data storage tree.
Definition: storage.hh:68
virtual bool get_bool() const
Definition: storage.cc:43
virtual double get_double() const
Definition: storage.cc:36
virtual StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:66
virtual void print(std::ostream &stream, int pad=0) const =0
virtual std::int64_t get_int() const
Definition: storage.cc:29
virtual const std::string & get_string() const
Definition: storage.cc:50
virtual void set_item(unsigned int index, StorageBase *item)
Definition: storage.cc:60
virtual unsigned int get_array_size() const
Definition: storage.cc:79
virtual bool is_null() const =0
Definition: storage.cc:73
virtual ~StorageBase()
Definition: storage.cc:84
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:180
StorageBool(bool value)
Definition: storage.cc:158
virtual StorageBase * deep_copy() const
Definition: storage.cc:176
virtual bool is_null() const
Definition: storage.cc:170
virtual ~StorageBool()
Definition: storage.cc:184
virtual bool get_bool() const
Definition: storage.cc:164
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:249
virtual bool is_null() const
Definition: storage.cc:240
virtual StorageBase * deep_copy() const
Definition: storage.cc:244
StorageDouble(double value)
Definition: storage.cc:228
virtual ~StorageDouble()
Definition: storage.cc:254
virtual double get_double() const
Definition: storage.cc:234
StorageInt(int value)
Definition: storage.cc:193
virtual bool is_null() const
Definition: storage.cc:205
virtual StorageBase * deep_copy() const
Definition: storage.cc:209
std::int64_t value_
Definition: storage.hh:131
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:214
virtual std::int64_t get_int() const
Definition: storage.cc:199
virtual ~StorageInt()
Definition: storage.cc:219
virtual StorageBase * deep_copy() const
Definition: storage.cc:305
virtual ~StorageNull()
Definition: storage.cc:316
virtual bool is_null() const
Definition: storage.cc:299
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:311
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:285
StorageString(const std::string &value)
Definition: storage.cc:263
std::string value_
Definition: storage.hh:157
virtual ~StorageString()
Definition: storage.cc:290
virtual StorageBase * deep_copy() const
Definition: storage.cc:280
virtual const std::string & get_string() const
Definition: storage.cc:269
virtual bool is_null() const
Definition: storage.cc:275
Global macros to enhance readability and debugging, general constants.
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
static constexpr bool value
Definition: json.hpp:87
Abstract linear system class.
Definition: balance.hh:40
IntFormatSpec< int, AlignTypeSpec< TYPE_CODE >, Char > pad(int value, unsigned width, Char fill=' ')
#define FMT_UNUSED
Definition: posix.h:75