Flow123d  release_2.1.0-84-g6a13a75
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 StorageBase * get_item(const unsigned int index) const;
75  virtual bool is_null() const =0;
76  virtual unsigned int get_array_size() const;
77 
78  virtual StorageBase *deep_copy() const =0;
79  virtual void print(std::ostream &stream, int pad=0) const =0;
80 
81  virtual ~StorageBase();
82 
83 };
84 
85 /**
86  * Simple array of heterogeneous values. The values are inserted as pointers
87  * (no copies) that is possibly dangerous, but don't care as the Storage is meant for internal usage only.
88  */
89 class StorageArray : public StorageBase {
90 public:
91  StorageArray(unsigned int size);
92  StorageArray(const StorageArray &); // deep copy for test purpose
93  void new_item(unsigned int index, StorageBase* item);
94  void set_item(unsigned int index, StorageBase* item);
95  virtual StorageBase * get_item(const unsigned int index) const;
96  virtual unsigned int get_array_size() const;
97  virtual bool is_null() const;
98  virtual StorageBase *deep_copy() const;
99  virtual void print(std::ostream &stream, int pad=0) const;
100  virtual ~StorageArray();
101 private:
102  /// Forbids default constructor to have array set to NULL.
103  StorageArray();
105 };
106 
107 
108 class StorageBool : public StorageBase {
109 public:
110  StorageBool(bool value);
111  virtual bool get_bool() const;
112  virtual bool is_null() const;
113  virtual StorageBase *deep_copy() const;
114  virtual void print(std::ostream &stream, int pad=0) const;
115  virtual ~StorageBool();
116 private:
117  bool value_;
118 };
119 
120 class StorageInt : public StorageBase {
121 public:
122  StorageInt(int value);
123  virtual std::int64_t get_int() const;
124  virtual bool is_null() const;
125  virtual StorageBase *deep_copy() const;
126  virtual void print(std::ostream &stream, int pad=0) const;
127  virtual ~StorageInt();
128 
129 private:
130  std::int64_t value_;
131 };
132 
133 class StorageDouble : public StorageBase {
134 public:
135  StorageDouble(double value);
136  virtual double get_double() const;
137  virtual bool is_null() const;
138  virtual StorageBase *deep_copy() const;
139  virtual void print(std::ostream &stream, int pad=0) const;
140  virtual ~StorageDouble();
141 
142 private:
143  double value_;
144 };
145 
146 class StorageString : public StorageBase {
147 public:
148  StorageString(const std::string & value);
149  virtual const std::string & get_string() const;
150  virtual bool is_null() const;
151  virtual StorageBase *deep_copy() const;
152  virtual void print(std::ostream &stream, int pad=0) const;
153  virtual ~StorageString();
154 
155 private:
156  std::string value_;
157 };
158 
159 class StorageNull : public StorageBase {
160  virtual bool is_null() const;
161  virtual StorageBase *deep_copy() const;
162  virtual void print(std::ostream &stream, int pad=0) const;
163  virtual ~StorageNull();
164 
165 };
166 
167 } // namespace Input
168 
169 #endif /* STORAGE_HH_ */
170 
171 
Base class for nodes of a data storage tree.
Definition: storage.hh:68
virtual std::int64_t get_int() const
Definition: storage.cc:29
virtual StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:60
std::vector< StorageBase * > array_
Definition: storage.hh:104
virtual const std::string & get_string() const
Definition: storage.cc:50
IntFormatSpec< int, AlignTypeSpec< TYPE_CODE >, Char > pad(int value, unsigned width, Char fill= ' ')
std::string value_
Definition: storage.hh:156
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)
std::int64_t value_
Definition: storage.hh:130
virtual void print(std::ostream &stream, int pad=0) const =0
static constexpr bool value
Definition: json.hpp:87
virtual double get_double() const
Definition: storage.cc:36
virtual StorageBase * deep_copy() const =0
virtual ~StorageBase()
Definition: storage.cc:78
static struct Ini_item * new_item(struct Ini_item *prev, char *section, char *key, char *value)
Definition: read_ini.cc:205
virtual unsigned int get_array_size() const
Definition: storage.cc:73
virtual bool get_bool() const
Definition: storage.cc:43
TYPEDEF_ERR_INFO(EI_InputType, const string)