Flow123d  release_3.0.0-1150-g11be08a
reader_internal_base.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 reader_internal_base.hh
15  * @brief
16  */
17 
18 #ifndef READER_INTERNAL_BASE_HH_
19 #define READER_INTERNAL_BASE_HH_
20 
21 
23 
24 #include "input/input_exception.hh"
25 #include "input/storage.hh"
26 #include "input/path_base.hh"
28 
29 
30 namespace Input {
31 
32 using namespace std;
33 
34 /**
35  * @brief Internal part of IST reader.
36  *
37  * This class defines interface for creating storage. Dispatching of storage is ensured by the non-virtual
38  * method read_storage, different for individual descendants.
39  *
40  * @ingroup input
41  */
43 public:
44  /*
45  * Exceptions.
46  */
47  TYPEDEF_ERR_INFO(EI_InputType, string );
48  TYPEDEF_ERR_INFO(EI_File, const string);
49  TYPEDEF_ERR_INFO(EI_Specification, const string);
50  TYPEDEF_ERR_INFO(EI_Format, const string);
51  TYPEDEF_ERR_INFO(EI_JSON_Type, const string);
52  TYPEDEF_ERR_INFO(EI_ErrorAddress, string);
53  TYPEDEF_ERR_INFO(EI_TransposeIndex, unsigned int);
54  TYPEDEF_ERR_INFO(EI_TransposeAddress, string);
55  TYPEDEF_ERR_INFO(EI_JSONLine, unsigned int);
56  TYPEDEF_ERR_INFO(EI_JSONColumn, unsigned int);
57  TYPEDEF_ERR_INFO(EI_JSONReason, string);
58  TYPEDEF_ERR_INFO(EI_InputErrorMessage, const string);
59  TYPEDEF_ERR_INFO(EI_RecordName, const string);
60  TYPEDEF_ERR_INFO(EI_Tag, string);
61  TYPEDEF_ERR_INFO(EI_TokenizerMsg, std::string);
62  TYPEDEF_ERR_INFO(EI_ColumnIndex, unsigned int);
63 
64  /// General exception during conversion from JSON/YAML tree to storage.
65  DECLARE_INPUT_EXCEPTION( ExcInputError, << "Error in input file: " << EI_File::qval << " at address: " << EI_ErrorAddress::qval << "\n"
66  << EI_Specification::val << "\n"
67  << EI_Format::val << " type: " << EI_JSON_Type::qval << "\n"
68  << "Expected type:\n" << EI_InputType::val );
69 
70  DECLARE_INPUT_EXCEPTION( ExcNotJSONFormat, << "Not valid JSON file " << EI_File::qval << ". Error at line "
71  << EI_JSONLine::val << " : col " << EI_JSONColumn::val
72  << " ; reason: " << EI_JSONReason::val << "\n" );
73  DECLARE_INPUT_EXCEPTION( ExcAutomaticConversionError, << "Error during automatic conversion of "
74  << EI_RecordName::val << " record.\n " << EI_InputErrorMessage::val << "\n" );
75  DECLARE_INPUT_EXCEPTION( ExcForbiddenTag, << "Tag " << EI_Tag::qval << " " << EI_Specification::val << "\n" );
76  DECLARE_INPUT_EXCEPTION( ExcWrongCsvFormat, << EI_Specification::val << ",\n" << EI_TokenizerMsg::val << "\n" );
77  DECLARE_INPUT_EXCEPTION( ExcMultipleDefinitionCsvColumn, << "Multiple definition of column with index " << EI_ColumnIndex::qval
78  << " in included CSV file:\n" << EI_File::val << ",\n" );
79 
80  /// Constructor
82 
83 protected:
84 
85  /**
86  * @brief Create storage of given @p type.
87  *
88  * Check correctness of the input given by json_spirit or YAML-cpp node at head() of PathBase @p p
89  * against type specification @p type. Die on input error (and return NULL).
90  * For correct input, creates the storage tree and returns pointer to its root node.
91  */
92  StorageBase * make_storage(PathBase &p, const Type::TypeBase *type);
93 
94  /// Create storage of Type::Record type. Common method of all descendants.
95  StorageBase * make_sub_storage(PathBase &p, const Type::Record *record);
96 
97  /// Create storage of Type::Tuple type. Common method of all descendants.
98  StorageBase * make_sub_storage(PathBase &p, const Type::Tuple *tuple);
99 
100  /// Create storage of Type::Abstract type. Common method of all descendants.
101  StorageBase * make_sub_storage(PathBase &p, const Type::Abstract *abstr_rec);
102 
103  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Array *array)=0; ///< Create storage of Type::Array type
104  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Selection *selection)=0; ///< Create storage of Type::Selection type
105  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Bool *bool_type)=0; ///< Create storage of Type::Bool type
106  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Integer *int_type)=0; ///< Create storage of Type::Integer type
107  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Double *double_type)=0; ///< Create storage of Type::Double type
108  virtual StorageBase * make_sub_storage(PathBase &p, const Type::String *string_type)=0; ///< Create storage of Type::String type
109 
110  /// Apply automatic conversion of Type::Record type
111  StorageBase * record_automatic_conversion(PathBase &p, const Type::Record *record);
112 
113  /// Apply automatic conversion of Type::Abstract type
114  StorageBase * abstract_automatic_conversion(PathBase &p, const Type::Abstract *abstr_rec);
115 
116  /// Create storage of Type::Array with given size
117  StorageBase * make_array_storage(PathBase &p, const Type::Array *array, int arr_size);
118 
119  /// Dispatch according to @p type and create corresponding storage from the given string.
120  StorageBase * make_storage_from_default( const string &dflt_str, std::shared_ptr<Type::TypeBase> type);
121 
122  /// Create storage of included YAML or JSON input file
123  StorageBase * make_include_storage(PathBase &p, const Type::TypeBase *type);
124 
125  bool read_bool_value(PathBase &p, const Type::TypeBase *type); ///< Read boolean value from path
126  std::int64_t read_int_value(PathBase &p, const Type::TypeBase *type); ///< Read integer value from path
127  double read_double_value(PathBase &p, const Type::TypeBase *type); ///< Read double value from path
128  std::string read_string_value(PathBase &p, const Type::TypeBase *type); ///< Read string value from path
129 
130  /// Helper method. Get string value of included file or throw exception if reading failed.
131  std::string get_included_file(PathBase &p);
132 
133  /// Generate @p ExcInputError
134  void generate_input_error(PathBase &p, const Type::TypeBase *type, std::string spec, bool add_type);
135 
136  /// Complete specification, error address and JSON type error tags to @p ExcInputError
137  void complete_input_error(ExcInputError & e, PathBase &p, ValueTypes value_type);
138 
139 };
140 
141 
142 } // namespace Input
143 
144 #endif /* READER_INTERNAL_BASE_HH_ */
Base of classes for declaring structure of the input data.
Definition: type_base.hh:99
Base class for nodes of a data storage tree.
Definition: storage.hh:68
Class for declaration of the input of type Bool.
Definition: type_base.hh:459
Base abstract class used by ReaderToStorage class to iterate over the input tree. ...
Definition: path_base.hh:41
Abstract linear system class.
Definition: balance.hh:37
Class for declaration of the integral input data.
Definition: type_base.hh:490
Class for declaration of inputs sequences.
Definition: type_base.hh:346
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:541
Class for declaration of polymorphic Record.
Tuple type proxy class.
Definition: type_tuple.hh:45
TYPEDEF_ERR_INFO(EI_InputType, const string)
Record type proxy class.
Definition: type_record.hh:182
ValueTypes
Enum of possible input types.
DECLARE_INPUT_EXCEPTION(ExcInputMessage,<< EI_Message::val)
Simple input exception that accepts just string message.
Internal part of IST reader.
Class for declaration of the input data that are in string format.
Definition: type_base.hh:589
Template for classes storing finite set of named values.