My Project
ParserItem.hpp
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 
26 #include <opm/input/eclipse/Deck/DeckItem.hpp>
27 #include <opm/input/eclipse/Utility/Typetools.hpp>
28 #include <opm/input/eclipse/Parser/ParserEnums.hpp>
29 
30 namespace Json {
31  class JsonObject;
32 }
33 
34 namespace 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 };
64  static item_size size_from_string( const std::string& );
65  static std::string string_from_size( item_size );
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);
70  std::string type_literal() const;
71 
72 
73  explicit ParserItem( const std::string& name, ParserItem::itype input_type );
74  explicit ParserItem( const Json::JsonObject& jsonConfig );
75 
76  void push_backDimension( const std::string& );
77  const std::vector<std::string>& dimensions() const;
78  const std::string& name() const;
79  item_size sizeType() const;
80  type_tag dataType() const;
81  void setSizeType(item_size size_type);
82  std::string getDescription() const;
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 
98  std::string size_literal() const;
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;
102  std::string inlineClassInit(const std::string& parentClass,
103  const std::string* defaultValue = nullptr ) const;
104 
105  private:
106  double dval{0};
107  int ival{0};
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 
127 std::ostream& operator<<( std::ostream&, const ParserItem::item_size& );
128 
129 }
130 
131 #endif
Definition: JsonObject.hpp:32
Definition: DeckItem.hpp:37
Definition: ParserItem.hpp:61
Definition: Typetools.hpp:37
Definition: UDAValue.hpp:32
Definition: UnitSystem.hpp:33
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29