Flow123d  3.9.0-a1b607a85
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( ExcDuplicitTag, << "Error in input file: " << EI_File::qval << " at address: " << EI_ErrorAddress::qval << "\n"
77  << "Duplicit Tag: " << EI_Tag::qval << "\n" );
78  DECLARE_INPUT_EXCEPTION( ExcWrongCsvFormat, << EI_Specification::val << ",\n" << EI_TokenizerMsg::val << "\n" );
79  DECLARE_INPUT_EXCEPTION( ExcMultipleDefinitionCsvColumn, << "Multiple definition of column with index " << EI_ColumnIndex::qval
80  << " in included CSV file:\n" << EI_File::val << ",\n" );
81 
82  /// Constructor
84 
85 protected:
86 
87  /**
88  * @brief Create storage of given @p type.
89  *
90  * Check correctness of the input given by json_spirit or YAML-cpp node at head() of PathBase @p p
91  * against type specification @p type. Die on input error (and return NULL).
92  * For correct input, creates the storage tree and returns pointer to its root node.
93  */
94  StorageBase * make_storage(PathBase &p, const Type::TypeBase *type);
95 
96  /// Create storage of Type::Record type. Common method of all descendants.
97  StorageBase * make_sub_storage(PathBase &p, const Type::Record *record);
98 
99  /// Create storage of Type::Tuple type. Common method of all descendants.
100  StorageBase * make_sub_storage(PathBase &p, const Type::Tuple *tuple);
101 
102  /// Create storage of Type::Abstract type. Common method of all descendants.
103  StorageBase * make_sub_storage(PathBase &p, const Type::Abstract *abstr_rec);
104 
105  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Array *array)=0; ///< Create storage of Type::Array type
106  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Selection *selection)=0; ///< Create storage of Type::Selection type
107  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Bool *bool_type)=0; ///< Create storage of Type::Bool type
108  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Integer *int_type)=0; ///< Create storage of Type::Integer type
109  virtual StorageBase * make_sub_storage(PathBase &p, const Type::Double *double_type)=0; ///< Create storage of Type::Double type
110  virtual StorageBase * make_sub_storage(PathBase &p, const Type::String *string_type)=0; ///< Create storage of Type::String type
111 
112  /// Apply automatic conversion of Type::Record type
113  StorageBase * record_automatic_conversion(PathBase &p, const Type::Record *record);
114 
115  /// Apply automatic conversion of Type::Abstract type
116  StorageBase * abstract_automatic_conversion(PathBase &p, const Type::Abstract *abstr_rec);
117 
118  /// Create storage of Type::Array with given size
119  StorageBase * make_array_storage(PathBase &p, const Type::Array *array, int arr_size);
120 
121  /// Dispatch according to @p type and create corresponding storage from the given string.
122  StorageBase * make_storage_from_default( const string &dflt_str, std::shared_ptr<Type::TypeBase> type);
123 
124  /// Create storage of included YAML or JSON input file
125  StorageBase * make_include_storage(PathBase &p, const Type::TypeBase *type);
126 
127  bool read_bool_value(PathBase &p, const Type::TypeBase *type); ///< Read boolean value from path
128  std::int64_t read_int_value(PathBase &p, const Type::TypeBase *type); ///< Read integer value from path
129  double read_double_value(PathBase &p, const Type::TypeBase *type); ///< Read double value from path
130  std::string read_string_value(PathBase &p, const Type::TypeBase *type); ///< Read string value from path
131 
132  /// Helper method. Get string value of included file or throw exception if reading failed.
133  std::string get_included_file(PathBase &p);
134 
135  /// Generate @p ExcInputError
136  void generate_input_error(PathBase &p, const Type::TypeBase *type, std::string spec, bool add_type);
137 
138  /// Complete specification, error address and JSON type error tags to @p ExcInputError
139  void complete_input_error(ExcInputError & e, PathBase &p, ValueTypes value_type);
140 
141 };
142 
143 
144 } // namespace Input
145 
146 #endif /* READER_INTERNAL_BASE_HH_ */
Input::StorageBase
Base class for nodes of a data storage tree.
Definition: storage.hh:68
Input::Type::Bool
Class for declaration of the input of type Bool.
Definition: type_base.hh:452
Input::int_type
@ int_type
Definition: reader_to_storage.hh:72
reader_to_storage.hh
Input
Abstract linear system class.
Definition: balance.hh:40
Input::Type::Integer
Class for declaration of the integral input data.
Definition: type_base.hh:483
Input::PathBase
Base abstract class used by ReaderToStorage class to iterate over the input tree.
Definition: path_base.hh:41
Input::Type::Double
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:534
Input::TYPEDEF_ERR_INFO
TYPEDEF_ERR_INFO(EI_InputType, const string)
Armor::array
Array< double > array
Definition: armor.hh:890
Input::Type::Tuple
Tuple type proxy class.
Definition: type_tuple.hh:45
Input::bool_type
@ bool_type
Definition: reader_to_storage.hh:72
input_type_forward.hh
storage.hh
Input::Type::Abstract
Class for declaration of polymorphic Record.
Definition: type_abstract.hh:62
input_exception.hh
Input::Type::Selection
Template for classes storing finite set of named values.
Definition: type_selection.hh:65
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
path_base.hh
Input::Type::Array
Class for declaration of inputs sequences.
Definition: type_base.hh:339
Input::Type::String
Class for declaration of the input data that are in string format.
Definition: type_base.hh:582
std
Definition: doxy_dummy_defs.hh:5
Input::Type::TypeBase
Base of classes for declaring structure of the input data.
Definition: type_base.hh:92
Input::DECLARE_INPUT_EXCEPTION
DECLARE_INPUT_EXCEPTION(ExcInputMessage,<< EI_Message::val)
Simple input exception that accepts just string message.
Input::ValueTypes
ValueTypes
Enum of possible input types.
Definition: reader_to_storage.hh:71
Input::ReaderInternalBase
Internal part of IST reader.
Definition: reader_internal_base.hh:42