introduced new class DeckValue.

added DeckValue.cpp.

completed DeckValue.
This commit is contained in:
Steinar Foss
2019-09-26 10:13:33 +02:00
parent 0edd5c2207
commit af878ba229
4 changed files with 222 additions and 0 deletions

View File

@@ -46,6 +46,7 @@ if(ENABLE_ECL_INPUT)
src/opm/parser/eclipse/Deck/DeckOutput.cpp
src/opm/parser/eclipse/Deck/Section.cpp
src/opm/parser/eclipse/Deck/UDAValue.cpp
src/opm/parser/eclipse/Deck/DeckValue.cpp
src/opm/parser/eclipse/Python/Python.cpp
src/opm/parser/eclipse/EclipseState/AquiferCT.cpp
src/opm/parser/eclipse/EclipseState/Aquifetp.cpp
@@ -260,6 +261,7 @@ if(ENABLE_ECL_INPUT)
tests/parser/ConnectionTests.cpp
tests/parser/COMPSEGUnits.cpp
tests/parser/CopyRegTests.cpp
tests/parser/DeckValueTests.cpp
tests/parser/DeckTests.cpp
tests/parser/DynamicStateTests.cpp
tests/parser/DynamicVectorTests.cpp
@@ -626,6 +628,7 @@ if(ENABLE_ECL_INPUT)
opm/parser/eclipse/Deck/DeckKeyword.hpp
opm/parser/eclipse/Deck/DeckRecord.hpp
opm/parser/eclipse/Deck/UDAValue.hpp
opm/parser/eclipse/Deck/DeckValue.hpp
opm/parser/eclipse/Python/Python.hpp)
endif()
if(ENABLE_ECL_OUTPUT)

View File

@@ -0,0 +1,59 @@
/*
Copyright 2019 Equinor 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 DECK_VALUE_HPP
#define DECK_VALUE_HPP
#include <string>
class DeckValue {
public:
DeckValue();
DeckValue(int);
DeckValue(double);
DeckValue(const std::string&);
template<typename T>
T get() const;
template<typename T>
bool is() const;
void reset(int value);
void reset(double value);
void reset(const std::string& value);
private:
bool is_numeric() const;
bool is_int;
bool is_double;
int int_value;
double double_value;
std::string string_value;
};
#endif

View File

@@ -0,0 +1,108 @@
/*
Copyright 2019 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 <opm/parser/eclipse/Deck/DeckValue.hpp>
DeckValue::DeckValue():
DeckValue(0)
{}
DeckValue::DeckValue(int value):
is_int(true),
is_double(false),
int_value(value)
{}
DeckValue::DeckValue(double value):
is_int(false),
is_double(true),
double_value(value)
{}
DeckValue::DeckValue(const std::string& value):
is_int(false),
is_double(false),
string_value(value)
{}
template<>
int DeckValue::get() const {
if (!is_int)
throw std::invalid_argument("DeckValue does not hold an integer value");
return this->int_value;
}
template<>
double DeckValue::get() const {
if (!is_double)
throw std::invalid_argument("DeckValue does not hold a double value");
return this->double_value;
}
template<>
std::string DeckValue::get() const {
if (!is_numeric())
return this->string_value;
throw std::invalid_argument("DeckValue does not hold a string value");
}
template<>
bool DeckValue::is<int>() const {
return is_int;
}
template<>
bool DeckValue::is<double>() const {
return is_double;
}
template<>
bool DeckValue::is<std::string>() const {
return !is_numeric();
}
bool DeckValue::is_numeric() const {
return (is_int or is_double);
}
void DeckValue::reset(int value) {
this->is_int = true;
this->is_double = false;
this->int_value = value;
}
void DeckValue::reset(double value) {
this->is_int = false;
this->is_double = true;
this->double_value = value;
}
void DeckValue::reset(const std::string& value) {
this->is_int = false;
this->is_double = false;
this->string_value = value;
}

View File

@@ -0,0 +1,52 @@
#define BOOST_TEST_MODULE DeckValueTests
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/Deck/DeckValue.hpp>
BOOST_AUTO_TEST_CASE(DeckValueTest) {
DeckValue value0;
BOOST_CHECK(value0.is<int>());
BOOST_CHECK(!value0.is<std::string>());
BOOST_CHECK_EQUAL( value0.get<int>(), 0);
BOOST_CHECK_THROW( value0.get<std::string>(), std::invalid_argument);
value0.reset( 10 );
BOOST_CHECK_EQUAL( value0.get<int>(), 10);
BOOST_CHECK_THROW( value0.get<std::string>(), std::invalid_argument);
BOOST_CHECK_THROW( value0.get<double>(), std::invalid_argument);
value0.reset( "STRING" );
BOOST_CHECK_EQUAL( value0.get<std::string>(), std::string("STRING"));
BOOST_CHECK_THROW( value0.get<int>(), std::invalid_argument);
BOOST_CHECK_THROW( value0.get<double>(), std::invalid_argument);
value0.reset( 2.5 );
BOOST_CHECK_EQUAL( value0.get<double>(), 2.5);
BOOST_CHECK_THROW( value0.get<std::string>(), std::invalid_argument);
BOOST_CHECK_THROW( value0.get<int>(), std::invalid_argument);
DeckValue value1(10);
BOOST_CHECK(value1.is<int>());
BOOST_CHECK(!value1.is<std::string>());
BOOST_CHECK_EQUAL( value1.get<int>(), 10);
DeckValue value2(10.0);
BOOST_CHECK(value2.is<double>());
BOOST_CHECK(!value2.is<std::string>());
BOOST_CHECK_EQUAL( value2.get<double>(), 10);
BOOST_CHECK_THROW( value2.get<std::string>(), std::invalid_argument);
BOOST_CHECK_THROW( value2.get<int>(), std::invalid_argument);
DeckValue value3("FUBHP");
BOOST_CHECK(!value3.is<double>());
BOOST_CHECK(value3.is<std::string>());
BOOST_CHECK_EQUAL( value3.get<std::string>(), std::string("FUBHP"));
BOOST_CHECK_THROW( value3.get<double>(), std::invalid_argument);
BOOST_CHECK_THROW( value3.get<int>(), std::invalid_argument);
}