Added class Value<T>
The class Value<T> will keep track of a named variable, and whether that particular value has been explicitly initialized. Will throw std::logic_error if trying to use an unitialized value.
This commit is contained in:
@@ -137,7 +137,9 @@ EclipseState/Schedule/CompletionSet.hpp
|
||||
EclipseState/Schedule/ScheduleEnums.hpp
|
||||
EclipseState/Schedule/GroupTreeNode.hpp
|
||||
EclipseState/Schedule/GroupTree.hpp
|
||||
#
|
||||
EclipseState/Util/OrderedMap.hpp
|
||||
EclipseState/Util/Value.hpp
|
||||
#
|
||||
EclipseState/Grid/EclipseGrid.hpp
|
||||
EclipseState/Grid/GridProperty.hpp
|
||||
|
||||
102
opm/parser/eclipse/EclipseState/Util/Value.hpp
Normal file
102
opm/parser/eclipse/EclipseState/Util/Value.hpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
Copyright 2014 Statoil ASA.
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _VALUE_
|
||||
#define _VALUE_
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
|
||||
/*
|
||||
The simple class Value<T> keeps track of a named scalar variable;
|
||||
the purpose of this class is to keep strick track of whether the
|
||||
value has been assigned or not. Will throw an exception if trying to
|
||||
use an unitialized value.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
namespace Opm {
|
||||
|
||||
template <typename T>
|
||||
class Value {
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
bool m_initialized;
|
||||
T m_value;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
Value(const std::string& name) :
|
||||
m_name( name ),
|
||||
m_initialized( false )
|
||||
{ }
|
||||
|
||||
|
||||
Value(const std::string& name ,T value) :
|
||||
m_name( name )
|
||||
{
|
||||
setValue( value );
|
||||
}
|
||||
|
||||
|
||||
bool hasValue() const {
|
||||
return m_initialized;
|
||||
}
|
||||
|
||||
|
||||
T getValue() const {
|
||||
if (m_initialized)
|
||||
return m_value;
|
||||
else
|
||||
throw std::logic_error("The value has: " + m_name + " has not been initialized");
|
||||
}
|
||||
|
||||
|
||||
void setValue( T value ) {
|
||||
m_initialized = true;
|
||||
m_value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
Will return true if both value instances have been initialized
|
||||
to the same value; or none of the values have been
|
||||
initialized. Does not compare names.
|
||||
*/
|
||||
bool equal( const Value<T>& other) const {
|
||||
if (m_initialized == other.m_initialized) {
|
||||
if (m_initialized) {
|
||||
if (m_value == other.m_value)
|
||||
return true; // Have been initialized to same value
|
||||
else
|
||||
return false;
|
||||
} else
|
||||
return true; // Both undefined
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -3,3 +3,8 @@ target_link_libraries(runOrderedMapTests Parser ${Boost_LIBRARIES})
|
||||
add_test(NAME runOrderedMapTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${TEST_MEMCHECK_TOOL} ${EXECUTABLE_OUTPUT_PATH}/runOrderedMapTests )
|
||||
|
||||
|
||||
add_executable(runValueTests ValueTests.cpp)
|
||||
target_link_libraries(runValueTests Parser ${Boost_LIBRARIES})
|
||||
add_test(NAME runValueTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${TEST_MEMCHECK_TOOL} ${EXECUTABLE_OUTPUT_PATH}/runValueTests )
|
||||
|
||||
|
||||
|
||||
72
opm/parser/eclipse/EclipseState/Util/tests/ValueTests.cpp
Normal file
72
opm/parser/eclipse/EclipseState/Util/tests/ValueTests.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
Copyright 2014 Statoil ASA.
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#define BOOST_TEST_MODULE VALUETESTS
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
#include <opm/parser/eclipse/EclipseState/Util/Value.hpp>
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( check_default_constructor ) {
|
||||
Opm::Value<int> v("Value");
|
||||
|
||||
BOOST_CHECK_EQUAL( false , v.hasValue() );
|
||||
BOOST_CHECK_THROW( v.getValue() , std::logic_error );
|
||||
|
||||
v.setValue( 70 );
|
||||
BOOST_CHECK_EQUAL( 70 , v.getValue());
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( check_value_constructor ) {
|
||||
Opm::Value<int> v("Value" , 100);
|
||||
|
||||
BOOST_CHECK_EQUAL( true , v.hasValue() );
|
||||
BOOST_CHECK_EQUAL( 100 , v.getValue());
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( check_equal1 ) {
|
||||
Opm::Value<int> v1("v1" , 100);
|
||||
Opm::Value<int> v2("v2" , 100);
|
||||
|
||||
BOOST_CHECK(v1.equal( v2 ));
|
||||
|
||||
v1.setValue(110);
|
||||
BOOST_CHECK_EQUAL( false , v1.equal(v2));
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( check_equal2 ) {
|
||||
Opm::Value<int> v1("v1");
|
||||
Opm::Value<int> v2("v2");
|
||||
|
||||
BOOST_CHECK_EQUAL(true , v1.equal( v2 ));
|
||||
|
||||
v1.setValue(110);
|
||||
BOOST_CHECK_EQUAL( false , v1.equal(v2));
|
||||
v2.setValue(110);
|
||||
BOOST_CHECK_EQUAL( true , v1.equal(v2));
|
||||
}
|
||||
Reference in New Issue
Block a user