Flow123d  3.9.1-8f37ba8
storage.hh
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.hh
15  * @brief
16  */
17 
18 #ifndef STORAGE_HH_
19 #define STORAGE_HH_
20 
21 /**
22  * Iterator - like intermediate class for access to the stored values. The storage is a tree where
23  * one node can hold:
24  * - int, double, bool
25  * - pointer to string
26  * - pointer to array of storages
27  * - special state: NULL (no data)
28  * INCLUDE (have to read another file to provide the value, this may be possible only through particular readers)
29  * ...
30  *
31  * json_spirit use boost::variant for storing JSON tree and arrays are stored as vectors of these variants
32  * not pointers to variants. This is probably more effective, but do not allow effective modifications of the
33  * tree and also construction not involving copies is not very intuitive. Therefore we use our own storage and
34  * copy the json_spirit tree into it.
35  */
36 
37 #include <iostream>
38 #include <string>
39 #include <vector>
40 #include <cstdint>
41 #include "system/exceptions.hh"
42 
43 
44 namespace Input {
45 
46 TYPEDEF_ERR_INFO( EI_RequestedType, const std::string);
47 TYPEDEF_ERR_INFO( EI_StoredType, const std::string);
48 DECLARE_EXCEPTION(ExcStorageTypeMismatch, << "Storage type mismatch. You want value of type "
49  << EI_RequestedType::qval << " but stored is value of type "
50  << EI_StoredType::qval);
51 
52 
53 /**
54  * @brief Base class for nodes of a data storage tree.
55  *
56  * This class as well as its descendants is meant for internal usage only as part of the implementation of the input interface.
57  *
58  * The leave nodes of the data storage tree can be of types \p StorageBool, \p StorageInt, \p StorageDouble, and StorageNull.
59  * The branching nodes of the tree are of type StorageArray. The data storage tree serves to store data with structure described
60  * by Input::Type classes. Therefore it provides no way to ask for the type of stored data and an exception \p ExcStorageTypeMismatch
61  * is thrown if you use
62  * getter that do not match actual type of the node. Moreover, the tree can be only created using bottom-up approach and than can
63  * not be modified ( this can change if we want to use same storage for buffered reading of large data). However, we provide a method for
64  * deep copy of any subtree.
65  *
66  * @ingroup input
67  */
68 class StorageBase {
69 public:
70  virtual std::int64_t get_int() const;
71  virtual double get_double() const;
72  virtual bool get_bool() const;
73  virtual const std::string &get_string() const;
74  virtual void set_item(unsigned int index, StorageBase* item);
75  virtual StorageBase * get_item(const unsigned int index) const;
76  virtual bool is_null() const =0;
77  virtual unsigned int get_array_size() const;
78 
79  virtual StorageBase *deep_copy() const =0;
80  virtual void print(std::ostream &stream, int pad=0) const =0;
81 
82  virtual ~StorageBase();
83 
84 };
85 
86 /**
87  * Simple array of heterogeneous values. The values are inserted as pointers
88  * (no copies) that is possibly dangerous, but don't care as the Storage is meant for internal usage only.
89  */
90 class StorageArray : public StorageBase {
91 public:
92  StorageArray(unsigned int size);
93  StorageArray(const StorageArray &); // deep copy for test purpose
94  void new_item(unsigned int index, StorageBase* item);
95  virtual void set_item(unsigned int index, StorageBase* item);
96  virtual StorageBase * get_item(const unsigned int index) const;
97  virtual unsigned int get_array_size() const;
98  virtual bool is_null() const;
99  virtual StorageBase *deep_copy() const;
100  virtual void print(std::ostream &stream, int pad=0) const;
101  virtual ~StorageArray();
102 private:
103  /// Forbids default constructor to have array set to NULL.
104  StorageArray();
106 };
107 
108 
109 class StorageBool : public StorageBase {
110 public:
111  StorageBool(bool value);
112  virtual bool get_bool() const;
113  virtual bool is_null() const;
114  virtual StorageBase *deep_copy() const;
115  virtual void print(std::ostream &stream, int pad=0) const;
116  virtual ~StorageBool();
117 private:
118  bool value_;
119 };
120 
121 class StorageInt : public StorageBase {
122 public:
123  StorageInt(int value);
124  virtual std::int64_t get_int() const;
125  virtual bool is_null() const;
126  virtual StorageBase *deep_copy() const;
127  virtual void print(std::ostream &stream, int pad=0) const;
128  virtual ~StorageInt();
129 
130 private:
131  std::int64_t value_;
132 };
133 
134 class StorageDouble : public StorageBase {
135 public:
136  StorageDouble(double value);
137  virtual double get_double() const;
138  virtual bool is_null() const;
139  virtual StorageBase *deep_copy() const;
140  virtual void print(std::ostream &stream, int pad=0) const;
141  virtual ~StorageDouble();
142 
143 private:
144  double value_;
145 };
146 
147 class StorageString : public StorageBase {
148 public:
149  StorageString(const std::string & value);
150  virtual const std::string & get_string() const;
151  virtual bool is_null() const;
152  virtual StorageBase *deep_copy() const;
153  virtual void print(std::ostream &stream, int pad=0) const;
154  virtual ~StorageString();
155 
156 private:
157  std::string value_;
158 };
159 
160 class StorageNull : public StorageBase {
161  virtual bool is_null() const;
162  virtual StorageBase *deep_copy() const;
163  virtual void print(std::ostream &stream, int pad=0) const;
164  virtual ~StorageNull();
165 
166 };
167 
168 } // namespace Input
169 
170 #endif /* STORAGE_HH_ */
171 
172 
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
Input::DECLARE_EXCEPTION
DECLARE_EXCEPTION(ExcTypeMismatch,<< "Key:"<< EI_KeyName::qval<< ". Can not construct Iterator<T> with C++ type T="<< EI_CPPRequiredType::qval<< ";\n"<< "can not convert Type: "<< EI_InputType::qval<< " to: "<< EI_RequiredType::qval)
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::TYPEDEF_ERR_INFO
TYPEDEF_ERR_INFO(EI_InputType, const string)
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
Input::StorageArray::array_
std::vector< StorageBase * > array_
Definition: storage.hh:105
std::vector
Definition: doxy_dummy_defs.hh:7
Input::StorageArray::get_item
virtual StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:123
Input::StorageString
Definition: storage.hh:147
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
exceptions.hh
Input::StorageArray::StorageArray
StorageArray()
Forbids default constructor to have array set to NULL.
Input::StorageNull
Definition: storage.hh:160
Input::StorageNull::~StorageNull
virtual ~StorageNull()
Definition: storage.cc:316
Input::StorageInt
Definition: storage.hh:121
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::StorageBool
Definition: storage.hh:109
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::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
Input::StorageBool::value_
bool value_
Definition: storage.hh:118
Input::StorageBase::get_string
virtual const std::string & get_string() const
Definition: storage.cc:50
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
Input::StorageBase::deep_copy
virtual StorageBase * deep_copy() const =0
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::StorageDouble
Definition: storage.hh:134
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
Input::StorageArray::~StorageArray
virtual ~StorageArray()
Definition: storage.cc:149