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