Flow123d  jenkins-Flow123d-windows32-release-multijob-28
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()=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  virtual const StorageBase * get_item(const unsigned int index) const;
84  virtual unsigned int get_array_size() const;
85  virtual bool is_null() const;
86  virtual StorageBase *deep_copy();
87  virtual void print(std::ostream &stream, int pad=0) const;
88  virtual ~StorageArray();
89 private:
90  /// Forbids default constructor to have array set to NULL.
91  StorageArray();
93 };
94 
95 
96 class StorageBool : public StorageBase {
97 public:
98  StorageBool(bool value);
99  virtual bool get_bool() const;
100  virtual bool is_null() const;
101  virtual StorageBase *deep_copy();
102  virtual void print(std::ostream &stream, int pad=0) const;
103  virtual ~StorageBool();
104 private:
105  bool value_;
106 };
107 
108 class StorageInt : public StorageBase {
109 public:
110  StorageInt(int value);
111  virtual int get_int() const;
112  virtual bool is_null() const;
113  virtual StorageBase *deep_copy();
114  virtual void print(std::ostream &stream, int pad=0) const;
115  virtual ~StorageInt();
116 
117 private:
118  int value_;
119 };
120 
121 class StorageDouble : public StorageBase {
122 public:
123  StorageDouble(double value);
124  virtual double get_double() const;
125  virtual bool is_null() const;
126  virtual StorageBase *deep_copy();
127  virtual void print(std::ostream &stream, int pad=0) const;
128  virtual ~StorageDouble();
129 
130 private:
131  double value_;
132 };
133 
134 class StorageString : public StorageBase {
135 public:
136  StorageString(const std::string & value);
137  virtual const std::string & get_string() const;
138  virtual bool is_null() const;
139  virtual StorageBase *deep_copy();
140  virtual void print(std::ostream &stream, int pad=0) const;
141  virtual ~StorageString();
142 
143 private:
144  std::string value_;
145 };
146 
147 class StorageNull : public StorageBase {
148  virtual bool is_null() const;
149  virtual StorageBase *deep_copy();
150  virtual void print(std::ostream &stream, int pad=0) const;
151  virtual ~StorageNull();
152 
153 };
154 
155 } // namespace Input
156 
157 #endif /* STORAGE_HH_ */
158 
159 
virtual StorageBase * deep_copy()
Definition: storage.cc:163
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:129
virtual StorageBase * deep_copy()
Definition: storage.cc:267
Base class for nodes of a data storage tree.
Definition: storage.hh:57
virtual StorageBase * deep_copy()=0
StorageArray()
Forbids default constructor to have array set to NULL.
virtual ~StorageString()
Definition: storage.cc:277
std::vector< StorageBase * > array_
Definition: storage.hh:92
virtual const std::string & get_string() const
Definition: storage.cc:46
std::string value_
Definition: storage.hh:144
virtual bool is_null() const =0
Definition: storage.cc:67
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:256
virtual bool is_null() const
Definition: storage.cc:262
virtual bool is_null() const
Definition: storage.cc:157
virtual int get_int() const
Definition: storage.cc:186
virtual void print(std::ostream &stream, int pad=0) const =0
virtual ~StorageBool()
Definition: storage.cc:171
virtual StorageBase * deep_copy()
Definition: storage.cc:292
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:167
StorageDouble(double value)
Definition: storage.cc:215
virtual const StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:58
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:272
virtual double get_double() const
Definition: storage.cc:28
virtual int get_int() const
Definition: storage.cc:19
virtual bool is_null() const
Definition: storage.cc:124
virtual StorageBase * deep_copy()
Definition: storage.cc:92
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:298
virtual ~StorageBase()
Definition: storage.cc:78
virtual bool is_null() const
Definition: storage.cc:192
virtual ~StorageNull()
Definition: storage.cc:303
void new_item(unsigned int index, StorageBase *item)
Definition: storage.cc:101
TYPEDEF_ERR_INFO(EI_Address, const std::string)
virtual const StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:109
virtual unsigned int get_array_size() const
Definition: storage.cc:73
virtual StorageBase * deep_copy()
Definition: storage.cc:196
virtual double get_double() const
Definition: storage.cc:221
virtual bool get_bool() const
Definition: storage.cc:37
virtual bool get_bool() const
Definition: storage.cc:151
StorageInt(int value)
Definition: storage.cc:180
virtual ~StorageInt()
Definition: storage.cc:206
virtual StorageBase * deep_copy()
Definition: storage.cc:231
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:201
StorageBool(bool value)
Definition: storage.cc:145
virtual ~StorageArray()
Definition: storage.cc:136
virtual unsigned int get_array_size() const
Definition: storage.cc:118
virtual void print(std::ostream &stream, int pad=0) const
Definition: storage.cc:236
virtual bool is_null() const
Definition: storage.cc:227
virtual ~StorageDouble()
Definition: storage.cc:241
StorageString(const std::string &value)
Definition: storage.cc:250
virtual bool is_null() const
Definition: storage.cc:286