ParserItem.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Statoil ASA.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef PARSER_ITEM_H
20#define PARSER_ITEM_H
21
22#include <iosfwd>
23#include <string>
24#include <vector>
25
29
30namespace Json {
31 class JsonObject;
32}
33
34namespace Opm {
35
36 class UnitSystem;
37 class RawRecord;
38
39
40 /*
41 The ParserItem class describes one item handled by the parser. A parser
42 item is the schema for parsing values from the deck, when configuring the
43 ParserItem *two* types are in action:
44
45 InputType: These are the types specified when instantiating a
46 ParserItem, the available types are currently: INT, DOUBLE, STRING,
47 RAW_STRING and UDA.
48
49 DataType: This the C++ type of items generated when parsing the deck,
50 currently the available datatypes are int, double, std::string and
51 the user defined type UDAValue.
52
53 Splitting the type treatment in two layers in this way enables
54 properties/transformations to be added to the data before they are
55 internalized as data in a DataType instance; e.g. the difference between
56 STRING and RAW_STRING is that for the latter quotes and '*' tokens are
57 retained.
58 */
59
60
61 class ParserItem {
62 public:
63 enum class item_size { ALL, SINGLE };
66
67 enum class itype {UNKNOWN, DOUBLE, INT, STRING, RAW_STRING, UDA, CODE};
68 static itype from_string(const std::string& string_value);
69 static std::string to_string(itype input_type);
71
72
73 explicit ParserItem( const std::string& name, ParserItem::itype input_type );
74 explicit ParserItem( const Json::JsonObject& jsonConfig );
75
77 const std::vector<std::string>& dimensions() const;
78 const std::string& name() const;
81 void setSizeType(item_size size_type);
83 bool scalar() const;
84 void setDescription(const std::string& helpText);
85
86 template< typename T > void setDefault( T );
87 /* set type without a default value. will reset dimension etc. */
88 void setInputType( itype input_type );
89 bool parseRaw() const;
90 bool hasDefault() const;
91 template< typename T > const T& getDefault() const;
92
93 bool operator==( const ParserItem& ) const;
94 bool operator!=( const ParserItem& ) const;
95
96 DeckItem scan( RawRecord& rawRecord, UnitSystem& active_unitsystem, UnitSystem& default_unitsystem) const;
97
99 const std::string className() const;
100 std::string createCode(const std::string& indent) const;
101 std::ostream& inlineClass(std::ostream&, const std::string& indent) const;
103 const std::string* defaultValue = nullptr ) const;
104
105 private:
106 double dval;
107 int ival;
108 std::string sval;
109 RawString rsval;
110 UDAValue uval;
111 std::vector< std::string > m_dimensions;
112
113 std::string m_name;
114 item_size m_sizeType = item_size::SINGLE;
115 std::string m_description;
116
117 type_tag data_type = type_tag::unknown;
118 itype input_type = itype::UNKNOWN;
119 bool m_defaultSet;
120
121 template< typename T > T& value_ref();
122 template< typename T > const T& value_ref() const;
123 template< typename T > void setDataType( T );
124 friend std::ostream& operator<<( std::ostream&, const ParserItem& );
125 };
126
127std::ostream& operator<<( std::ostream&, const ParserItem::item_size& );
128
129}
130
131#endif
const char *const string
Definition: cJSON.h:170
Definition: JsonObject.hpp:31
Definition: DeckItem.hpp:37
Definition: ParserItem.hpp:61
static item_size size_from_string(const std::string &)
const std::string & name() const
std::ostream & inlineClass(std::ostream &, const std::string &indent) const
void setInputType(itype input_type)
bool operator!=(const ParserItem &) const
void push_backDimension(const std::string &)
friend std::ostream & operator<<(std::ostream &, const ParserItem &)
bool hasDefault() const
void setSizeType(item_size size_type)
itype
Definition: ParserItem.hpp:67
std::string getDescription() const
DeckItem scan(RawRecord &rawRecord, UnitSystem &active_unitsystem, UnitSystem &default_unitsystem) const
std::string size_literal() const
std::string type_literal() const
ParserItem(const Json::JsonObject &jsonConfig)
void setDefault(T)
const std::string className() const
const T & getDefault() const
static std::string to_string(itype input_type)
std::string inlineClassInit(const std::string &parentClass, const std::string *defaultValue=nullptr) const
ParserItem(const std::string &name, ParserItem::itype input_type)
const std::vector< std::string > & dimensions() const
std::string createCode(const std::string &indent) const
item_size sizeType() const
void setDescription(const std::string &helpText)
bool scalar() const
type_tag dataType() const
bool parseRaw() const
item_size
Definition: ParserItem.hpp:63
static std::string string_from_size(item_size)
bool operator==(const ParserItem &) const
static itype from_string(const std::string &string_value)
Definition: RawRecord.hpp:36
Definition: Typetools.hpp:37
Definition: UDAValue.hpp:32
Definition: UnitSystem.hpp:32
Definition: JsonObject.hpp:29
Definition: A.hpp:4
type_tag
Definition: Typetools.hpp:10
std::ostream & operator<<(std::ostream &os, const UniformTableLinear< T > &t)
Definition: UniformTableLinear.hpp:249
@ UNKNOWN
Definition: ParserEnums.hpp:31