diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index 967329358..6ac044630 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -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) diff --git a/opm/parser/eclipse/Deck/DeckValue.hpp b/opm/parser/eclipse/Deck/DeckValue.hpp new file mode 100644 index 000000000..711df8b38 --- /dev/null +++ b/opm/parser/eclipse/Deck/DeckValue.hpp @@ -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 . +*/ + +#ifndef DECK_VALUE_HPP +#define DECK_VALUE_HPP + +#include + +class DeckValue { + + public: + DeckValue(); + DeckValue(int); + DeckValue(double); + DeckValue(const std::string&); + + template + T get() const; + + template + 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 diff --git a/src/opm/parser/eclipse/Deck/DeckValue.cpp b/src/opm/parser/eclipse/Deck/DeckValue.cpp new file mode 100644 index 000000000..2760458db --- /dev/null +++ b/src/opm/parser/eclipse/Deck/DeckValue.cpp @@ -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 . +*/ + +#include + +#include + + +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() const { + return is_int; +} + +template<> +bool DeckValue::is() const { + return is_double; +} + + +template<> +bool DeckValue::is() 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; +} + diff --git a/tests/parser/DeckValueTests.cpp b/tests/parser/DeckValueTests.cpp new file mode 100644 index 000000000..c3d89f99f --- /dev/null +++ b/tests/parser/DeckValueTests.cpp @@ -0,0 +1,52 @@ + + + +#define BOOST_TEST_MODULE DeckValueTests +#include + +#include + +BOOST_AUTO_TEST_CASE(DeckValueTest) { + + DeckValue value0; + + BOOST_CHECK(value0.is()); + BOOST_CHECK(!value0.is()); + BOOST_CHECK_EQUAL( value0.get(), 0); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + value0.reset( 10 ); + BOOST_CHECK_EQUAL( value0.get(), 10); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + + value0.reset( "STRING" ); + BOOST_CHECK_EQUAL( value0.get(), std::string("STRING")); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + + value0.reset( 2.5 ); + BOOST_CHECK_EQUAL( value0.get(), 2.5); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + + DeckValue value1(10); + BOOST_CHECK(value1.is()); + BOOST_CHECK(!value1.is()); + BOOST_CHECK_EQUAL( value1.get(), 10); + + DeckValue value2(10.0); + BOOST_CHECK(value2.is()); + BOOST_CHECK(!value2.is()); + BOOST_CHECK_EQUAL( value2.get(), 10); + BOOST_CHECK_THROW( value2.get(), std::invalid_argument); + BOOST_CHECK_THROW( value2.get(), std::invalid_argument); + + DeckValue value3("FUBHP"); + BOOST_CHECK(!value3.is()); + BOOST_CHECK(value3.is()); + BOOST_CHECK_EQUAL( value3.get(), std::string("FUBHP")); + BOOST_CHECK_THROW( value3.get(), std::invalid_argument); + BOOST_CHECK_THROW( value3.get(), std::invalid_argument); + + +}