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 OPM_VALUE_HPP
21#define OPM_VALUE_HPP
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
36namespace Opm {
37
38template <typename T>
39class Value {
40
41private:
42 std::string m_name;
43 bool m_initialized = false;
44 T m_value;
45
46
47public:
48
49 Value() = default;
50 explicit Value(const std::string& name) :
51 m_name( name ),
52 m_initialized( false )
53 { }
54
55
57 m_name( name )
58 {
59 setValue( value );
60 }
61
62
63 bool hasValue() const {
64 return m_initialized;
65 }
66
67
68 T getValue() const {
69 if (m_initialized)
70 return m_value;
71 else
72 throw std::logic_error("The value has: " + m_name + " has not been initialized");
73 }
74
75
76 void setValue( T value ) {
77 m_initialized = true;
78 m_value = value;
79 }
80
86 bool equal( const Value<T>& other) const {
87 if (m_initialized == other.m_initialized) {
88 if (m_initialized) {
89 if (m_value == other.m_value)
90 return true; // Have been initialized to same value
91 else
92 return false;
93 } else
94 return true; // Both undefined
95 } else
96 return false;
97 }
98
99 bool operator==( const Value& rhs ) const {
100 return this->equal( rhs );
101 }
102
103 bool operator!=( const Value& rhs ) const {
104 return !(*this == rhs );
105 }
106
107
108};
109}
110
111#endif
const char *const name
Definition: cJSON.h:258
const char *const string
Definition: cJSON.h:170
Definition: Value.hpp:39
T getValue() const
Definition: Value.hpp:68
bool operator==(const Value &rhs) const
Definition: Value.hpp:99
void setValue(T value)
Definition: Value.hpp:76
Value(const std::string &name)
Definition: Value.hpp:50
Value()=default
bool operator!=(const Value &rhs) const
Definition: Value.hpp:103
bool equal(const Value< T > &other) const
Definition: Value.hpp:86
Value(const std::string &name, T value)
Definition: Value.hpp:56
bool hasValue() const
Definition: Value.hpp:63
#define false
Definition: msvc_stdbool.h:19
Definition: A.hpp:4
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12955