diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index 72aaf2b2e..0c327100e 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -45,6 +45,7 @@ if(ENABLE_ECL_INPUT) src/opm/parser/eclipse/Deck/DeckRecord.cpp 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/EclipseState/AquiferCT.cpp src/opm/parser/eclipse/EclipseState/Aquifetp.cpp src/opm/parser/eclipse/EclipseState/Aquancon.cpp @@ -552,6 +553,7 @@ if(ENABLE_ECL_INPUT) opm/parser/eclipse/Deck/DeckOutput.hpp opm/parser/eclipse/Deck/DeckKeyword.hpp opm/parser/eclipse/Deck/DeckRecord.hpp + opm/parser/eclipse/Deck/UDAValue.hpp opm/parser/eclipse/RawDeck/StarToken.hpp opm/parser/eclipse/RawDeck/RawEnums.hpp opm/parser/eclipse/RawDeck/RawRecord.hpp diff --git a/opm/parser/eclipse/Deck/UDAValue.hpp b/opm/parser/eclipse/Deck/UDAValue.hpp new file mode 100644 index 000000000..8e0c5c4f7 --- /dev/null +++ b/opm/parser/eclipse/Deck/UDAValue.hpp @@ -0,0 +1,61 @@ +/* + 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 UDA_VALUE_HPP +#define UDA_VALUE_HPP + +#include +#include +#include + +#include + +namespace Opm { + +class UDAValue { +public: + UDAValue(); + UDAValue(double); + UDAValue(const std::string&); + + template + T get() const; + + template + bool is() const; + + void set_dim(const Dimension& dim) const; + const Dimension& get_dim() const; + + bool operator==(const UDAValue& other) const; + bool operator!=(const UDAValue& other) const; +private: + bool numeric_value; + double double_value; + std::string string_value; + + /* This 'mutable' modifier is a hack to avoid tampering with the overall + const-ness of the data in a deck item. */ + mutable Dimension dim; +}; +} + + + +#endif diff --git a/src/opm/parser/eclipse/Deck/UDAValue.cpp b/src/opm/parser/eclipse/Deck/UDAValue.cpp new file mode 100644 index 000000000..5efd294fe --- /dev/null +++ b/src/opm/parser/eclipse/Deck/UDAValue.cpp @@ -0,0 +1,96 @@ +/* + 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 + + +namespace Opm { + +UDAValue::UDAValue(double value): + numeric_value(true), + double_value(value) +{ +} + + +UDAValue::UDAValue() : + UDAValue(0) +{} + +UDAValue::UDAValue(const std::string& value): + numeric_value(false), + string_value(value) +{ +} + + +template<> +bool UDAValue::is() const { + return this->numeric_value; +} + + +template<> +bool UDAValue::is() const { + return !this->numeric_value; +} + + +template<> +double UDAValue::get() const { + if (this->numeric_value) + return this->double_value; + + throw std::invalid_argument("UDAValue does not hold a numerical value"); +} + +template<> +std::string UDAValue::get() const { + if (!this->numeric_value) + return this->string_value; + + throw std::invalid_argument("UDAValue does not hold a string value"); +} + + +void UDAValue::set_dim(const Dimension& dim) const { + this->dim = dim; +} + +const Dimension& UDAValue::get_dim() const { + return this->dim; +} + + +bool UDAValue::operator==(const UDAValue& other) const { + if (this->numeric_value != other.numeric_value) + return false; + + if (this->numeric_value) + return (this->double_value == other.double_value); + + return this->string_value == other.string_value; +} + + +bool UDAValue::operator!=(const UDAValue& other) const { + return !(*this == other); +} + +} diff --git a/tests/parser/UDQTests.cpp b/tests/parser/UDQTests.cpp index 292c132a3..16a47f2a2 100644 --- a/tests/parser/UDQTests.cpp +++ b/tests/parser/UDQTests.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -954,3 +955,31 @@ BOOST_AUTO_TEST_CASE(UDQPARSE_PARSECONTEXT) { parseContext.update(ParseContext::UDQ_PARSE_ERROR, InputError::THROW_EXCEPTION); BOOST_CHECK_THROW( UDQDefine(udqp, "WUBHP", tokens, parseContext, errors), std::invalid_argument); } + + + +BOOST_AUTO_TEST_CASE(UDA_VALUE) { + UDAValue value0; + BOOST_CHECK(value0.is()); + BOOST_CHECK(!value0.is()); + BOOST_CHECK_EQUAL( value0.get(), 0); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + + + UDAValue value1(10); + BOOST_CHECK(value1.is()); + BOOST_CHECK(!value1.is()); + BOOST_CHECK_EQUAL( value1.get(), 10); + BOOST_CHECK_THROW( value1.get(), std::invalid_argument); + + + UDAValue value2("FUBHP"); + BOOST_CHECK(!value2.is()); + BOOST_CHECK(value2.is()); + BOOST_CHECK_EQUAL( value2.get(), std::string("FUBHP")); + BOOST_CHECK_THROW( value2.get(), std::invalid_argument); + + + std::vector values; + values.resize(100, UDAValue(1000)); +}