Value.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2014 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 
20 #ifndef _VALUE_
21 #define _VALUE_
22 
23 #include <stdexcept>
24 #include <string>
25 
26 
27 /*
28  The simple class Value<T> keeps track of a named scalar variable;
29  the purpose of this class is to keep strick track of whether the
30  value has been assigned or not. Will throw an exception if trying to
31  use an unitialized value.
32 */
33 
34 
35 
36 namespace Opm {
37 
38 template <typename T>
39 class Value {
40 
41 private:
42  std::string m_name;
43  bool m_initialized;
44  T m_value;
45 
46 
47 public:
48 
49  Value(const std::string& name) :
50  m_name( name ),
51  m_initialized( false )
52  { }
53 
54 
55  Value(const std::string& name ,T value) :
56  m_name( name )
57  {
58  setValue( value );
59  }
60 
61 
62  bool hasValue() const {
63  return m_initialized;
64  }
65 
66 
67  T getValue() const {
68  if (m_initialized)
69  return m_value;
70  else
71  throw std::logic_error("The value has: " + m_name + " has not been initialized");
72  }
73 
74 
75  void setValue( T value ) {
76  m_initialized = true;
77  m_value = value;
78  }
79 
85  bool equal( const Value<T>& other) const {
86  if (m_initialized == other.m_initialized) {
87  if (m_initialized) {
88  if (m_value == other.m_value)
89  return true; // Have been initialized to same value
90  else
91  return false;
92  } else
93  return true; // Both undefined
94  } else
95  return false;
96  }
97 
98 
99 };
100 }
101 
102 #endif
Definition: Value.hpp:39
Definition: Deck.hpp:29
void setValue(T value)
Definition: Value.hpp:75
T getValue() const
Definition: Value.hpp:67
bool hasValue() const
Definition: Value.hpp:62
bool equal(const Value< T > &other) const
Definition: Value.hpp:85
Value(const std::string &name)
Definition: Value.hpp:49
Value(const std::string &name, T value)
Definition: Value.hpp:55