Flow123d  3.9.0-c09ffe475
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
Input::StorageInt::value_
std::int64_t value_
Definition: storage.hh:131
Input::StorageBase
Base class for nodes of a data storage tree.
Definition: storage.hh:68
Input::StorageBool::StorageBool
StorageBool(bool value)
Definition: storage.cc:158
Input::StorageString::deep_copy
virtual StorageBase * deep_copy() const
Definition: storage.cc:280
Input::StorageBool::~StorageBool
virtual ~StorageBool()
Definition: storage.cc:184
Input::StorageBool::get_bool
virtual bool get_bool() const
Definition: storage.cc:164
Input::StorageDouble::is_null
virtual bool is_null() const
Definition: storage.cc:240
Input::StorageDouble::deep_copy
virtual StorageBase * deep_copy() const
Definition: storage.cc:244
Input
Abstract linear system class.
Definition: balance.hh:40
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
Input::StorageInt::deep_copy
virtual StorageBase * deep_copy() const
Definition: storage.cc:209
Input::StorageBool::is_null
virtual bool is_null() const
Definition: storage.cc:170
Input::StorageArray::new_item
void new_item(unsigned int index, StorageBase *item)
Definition: storage.cc:107
Input::StorageDouble::get_double
virtual double get_double() const
Definition: storage.cc:234
value
static constexpr bool value
Definition: json.hpp:87
Input::StorageBase::get_array_size
virtual unsigned int get_array_size() const
Definition: storage.cc:79
Input::StorageDouble::value_
double value_
Definition: storage.hh:144
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
Input::StorageArray::array_
std::vector< StorageBase * > array_
Definition: storage.hh:105
std::vector
Definition: doxy_dummy_defs.hh:7
system.hh
Input::StorageArray::get_item
virtual StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:123
Input::StorageNull::deep_copy
virtual StorageBase * deep_copy() const
Definition: storage.cc:305
Input::StorageArray
Definition: storage.hh:90
Input::StorageNull::print
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:311
Input::StorageArray::get_array_size
virtual unsigned int get_array_size() const
Definition: storage.cc:131
storage.hh
Input::StorageArray::StorageArray
StorageArray()
Forbids default constructor to have array set to NULL.
ASSERT_LT
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:301
Input::StorageNull
Definition: storage.hh:160
Input::StorageNull::~StorageNull
virtual ~StorageNull()
Definition: storage.cc:316
Input::StorageInt::is_null
virtual bool is_null() const
Definition: storage.cc:205
Input::StorageString::~StorageString
virtual ~StorageString()
Definition: storage.cc:290
Input::StorageString::is_null
virtual bool is_null() const
Definition: storage.cc:275
Input::StorageDouble::print
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:249
Input::StorageBase::~StorageBase
virtual ~StorageBase()
Definition: storage.cc:84
Input::StorageArray::is_null
virtual bool is_null() const
Definition: storage.cc:137
Input::StorageString::StorageString
StorageString(const std::string &value)
Definition: storage.cc:263
Input::StorageInt::get_int
virtual std::int64_t get_int() const
Definition: storage.cc:199
Input::Type
Definition: balance.hh:41
Input::StorageArray::print
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:142
Input::StorageString::print
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:285
Input::StorageString::get_string
virtual const std::string & get_string() const
Definition: storage.cc:269
Input::StorageInt::print
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:214
Input::StorageArray::deep_copy
virtual StorageBase * deep_copy() const
Definition: storage.cc:98
Input::StorageBase::print
virtual void print(std::ostream &stream, int pad=0) const =0
Input::StorageBase::get_double
virtual double get_double() const
Definition: storage.cc:36
global_defs.h
Global macros to enhance readability and debugging, general constants.
Input::StorageBool::value_
bool value_
Definition: storage.hh:118
Input::StorageBase::get_string
virtual const std::string & get_string() const
Definition: storage.cc:50
std
Definition: doxy_dummy_defs.hh:5
Input::StorageInt::~StorageInt
virtual ~StorageInt()
Definition: storage.cc:219
Input::StorageString::value_
std::string value_
Definition: storage.hh:157
Input::StorageNull::is_null
virtual bool is_null() const
Definition: storage.cc:299
Input::StorageBase::is_null
virtual bool is_null() const =0
Definition: storage.cc:73
Input::StorageBase::set_item
virtual void set_item(unsigned int index, StorageBase *item)
Definition: storage.cc:60
Input::StorageInt::StorageInt
StorageInt(int value)
Definition: storage.cc:193
Input::StorageBase::get_item
virtual StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:66
ASSERT_PTR
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
Input::StorageBool::deep_copy
virtual StorageBase * deep_copy() const
Definition: storage.cc:176
fmt::pad
IntFormatSpec< int, AlignTypeSpec< TYPE_CODE >, Char > pad(int value, unsigned width, Char fill=' ')
Input::StorageArray::set_item
virtual void set_item(unsigned int index, StorageBase *item)
Definition: storage.cc:114
Input::StorageDouble::StorageDouble
StorageDouble(double value)
Definition: storage.cc:228
Input::StorageDouble::~StorageDouble
virtual ~StorageDouble()
Definition: storage.cc:254
Input::StorageBase::get_int
virtual std::int64_t get_int() const
Definition: storage.cc:29
Input::StorageBool::print
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:180
Input::StorageBase::get_bool
virtual bool get_bool() const
Definition: storage.cc:43
FMT_UNUSED
#define FMT_UNUSED
Definition: posix.h:75
Input::StorageArray::~StorageArray
virtual ~StorageArray()
Definition: storage.cc:149