Flow123d  master-f44eb46
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.
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 
virtual bool is_null() const
Definition: storage.cc:137
StorageArray(const StorageArray &)
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 StorageBase * deep_copy() const =0
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
static constexpr bool value
Definition: json.hpp:87
Abstract linear system class.
Definition: balance.hh:40
TYPEDEF_ERR_INFO(EI_InputType, const string)
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)
IntFormatSpec< int, AlignTypeSpec< TYPE_CODE >, Char > pad(int value, unsigned width, Char fill=' ')