From 6dc84b67df3aead4a33b1aef6b165ccfaada6495 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Thu, 1 Oct 2020 08:07:18 +0200 Subject: [PATCH] Remove homemade optional - use std::optional --- CMakeLists_files.cmake | 2 - .../eclipse/EclipseState/Grid/EclipseGrid.hpp | 4 +- .../EclipseState/Grid/MULTREGTScanner.hpp | 1 - .../EclipseState/Schedule/Well/Connection.hpp | 1 - .../eclipse/EclipseState/Util/Value.hpp | 111 ------------------ .../eclipse/EclipseState/Grid/EclipseGrid.cpp | 12 +- .../EclipseState/Schedule/Well/Connection.cpp | 2 - tests/parser/EclipseGridTests.cpp | 3 +- tests/parser/GroupTests.cpp | 1 - tests/parser/ValueTests.cpp | 80 ------------- 10 files changed, 8 insertions(+), 209 deletions(-) delete mode 100644 opm/parser/eclipse/EclipseState/Util/Value.hpp delete mode 100644 tests/parser/ValueTests.cpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index f358ce95e..4aef04bc8 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -376,7 +376,6 @@ if(ENABLE_ECL_INPUT) tests/parser/TuningTests.cpp tests/parser/UDQTests.cpp tests/parser/UnitTests.cpp - tests/parser/ValueTests.cpp tests/parser/WellSolventTests.cpp tests/parser/WellTracerTests.cpp tests/parser/WellTests.cpp @@ -567,7 +566,6 @@ if(ENABLE_ECL_INPUT) opm/parser/eclipse/EclipseState/InitConfig/InitConfig.hpp opm/parser/eclipse/EclipseState/InitConfig/Equil.hpp opm/parser/eclipse/EclipseState/InitConfig/FoamConfig.hpp - opm/parser/eclipse/EclipseState/Util/Value.hpp opm/parser/eclipse/EclipseState/Util/IOrderSet.hpp opm/parser/eclipse/EclipseState/Util/OrderedMap.hpp opm/parser/eclipse/EclipseState/SummaryConfig/SummaryConfig.hpp diff --git a/opm/parser/eclipse/EclipseState/Grid/EclipseGrid.hpp b/opm/parser/eclipse/EclipseState/Grid/EclipseGrid.hpp index bd16388c1..42c06aa31 100644 --- a/opm/parser/eclipse/EclipseState/Grid/EclipseGrid.hpp +++ b/opm/parser/eclipse/EclipseState/Grid/EclipseGrid.hpp @@ -22,7 +22,6 @@ #define OPM_PARSER_ECLIPSE_GRID_HPP -#include #include #include #include @@ -32,6 +31,7 @@ #include #include +#include #include namespace Opm { @@ -194,7 +194,7 @@ namespace Opm { private: std::vector m_minpvVector; MinpvMode::ModeEnum m_minpvMode; - Value m_pinch; + std::optional m_pinch; PinchMode::ModeEnum m_pinchoutMode; PinchMode::ModeEnum m_multzMode; PinchMode::ModeEnum m_pinchGapMode; diff --git a/opm/parser/eclipse/EclipseState/Grid/MULTREGTScanner.hpp b/opm/parser/eclipse/EclipseState/Grid/MULTREGTScanner.hpp index 85e5136b7..13e8bf7e9 100644 --- a/opm/parser/eclipse/EclipseState/Grid/MULTREGTScanner.hpp +++ b/opm/parser/eclipse/EclipseState/Grid/MULTREGTScanner.hpp @@ -23,7 +23,6 @@ #include #include -#include namespace Opm { diff --git a/opm/parser/eclipse/EclipseState/Schedule/Well/Connection.hpp b/opm/parser/eclipse/EclipseState/Schedule/Well/Connection.hpp index 4fd991fbd..65da4cde2 100644 --- a/opm/parser/eclipse/EclipseState/Schedule/Well/Connection.hpp +++ b/opm/parser/eclipse/EclipseState/Schedule/Well/Connection.hpp @@ -30,7 +30,6 @@ #include #include -#include namespace Opm { diff --git a/opm/parser/eclipse/EclipseState/Util/Value.hpp b/opm/parser/eclipse/EclipseState/Util/Value.hpp deleted file mode 100644 index f829ddddf..000000000 --- a/opm/parser/eclipse/EclipseState/Util/Value.hpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - 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 . - */ - -#ifndef OPM_VALUE_HPP -#define OPM_VALUE_HPP - -#include -#include - - -/* - The simple class Value 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 -class Value { - -private: - std::string m_name; - bool m_initialized = false; - T m_value; - - -public: - - Value() = default; - explicit 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& 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; - } - - bool operator==( const Value& rhs ) const { - return this->equal( rhs ); - } - - bool operator!=( const Value& rhs ) const { - return !(*this == rhs ); - } - - -}; -} - -#endif diff --git a/src/opm/parser/eclipse/EclipseState/Grid/EclipseGrid.cpp b/src/opm/parser/eclipse/EclipseState/Grid/EclipseGrid.cpp index ab48f6c97..bdc45fa11 100644 --- a/src/opm/parser/eclipse/EclipseState/Grid/EclipseGrid.cpp +++ b/src/opm/parser/eclipse/EclipseState/Grid/EclipseGrid.cpp @@ -66,7 +66,6 @@ EclipseGrid::EclipseGrid(std::array& dims , const double * mapaxes) : GridDims(dims), m_minpvMode(MinpvMode::ModeEnum::Inactive), - m_pinch("PINCH"), m_pinchoutMode(PinchMode::ModeEnum::TOPBOT), m_multzMode(PinchMode::ModeEnum::TOP), m_pinchGapMode(PinchMode::ModeEnum::GAP) @@ -83,7 +82,6 @@ EclipseGrid::EclipseGrid(std::array& dims , EclipseGrid::EclipseGrid(const std::string& fileName ) : GridDims(), m_minpvMode(MinpvMode::ModeEnum::Inactive), - m_pinch("PINCH"), m_pinchoutMode(PinchMode::ModeEnum::TOPBOT), m_multzMode(PinchMode::ModeEnum::TOP), m_pinchGapMode(PinchMode::ModeEnum::GAP) @@ -99,7 +97,6 @@ EclipseGrid::EclipseGrid(size_t nx, size_t ny , size_t nz, double dx, double dy, double dz) : GridDims(nx, ny, nz), m_minpvMode(MinpvMode::ModeEnum::Inactive), - m_pinch("PINCH"), m_pinchoutMode(PinchMode::ModeEnum::TOPBOT), m_multzMode(PinchMode::ModeEnum::TOP), m_pinchGapMode(PinchMode::ModeEnum::GAP) @@ -208,7 +205,6 @@ EclipseGrid::EclipseGrid(const EclipseGrid& src, const std::vector& actnum) EclipseGrid::EclipseGrid(const Deck& deck, const int * actnum) : GridDims(deck), m_minpvMode(MinpvMode::ModeEnum::Inactive), - m_pinch("PINCH"), m_pinchoutMode(PinchMode::ModeEnum::TOPBOT), m_multzMode(PinchMode::ModeEnum::TOP), m_pinchGapMode(PinchMode::ModeEnum::GAP) @@ -306,7 +302,7 @@ EclipseGrid::EclipseGrid(const Deck& deck, const int * actnum) if (deck.hasKeyword()) { const auto& record = deck.getKeyword( ).getRecord(0); const auto& item = record.getItem( ); - m_pinch.setValue( item.getSIDouble(0) ); + m_pinch = item.getSIDouble(0); auto pinchoutString = record.getItem().get< std::string >(0); m_pinchoutMode = PinchMode::PinchModeFromString(pinchoutString); @@ -471,11 +467,11 @@ EclipseGrid::EclipseGrid(const Deck& deck, const int * actnum) bool EclipseGrid::isPinchActive( ) const { - return m_pinch.hasValue(); + return m_pinch.has_value(); } double EclipseGrid::getPinchThresholdThickness( ) const { - return m_pinch.getValue(); + return m_pinch.value(); } PinchMode::ModeEnum EclipseGrid::getPinchOption( ) const { @@ -1326,7 +1322,7 @@ std::vector EclipseGrid::createDVector(const std::array& dims, st if (m_mapaxes != other.m_mapaxes) return false; - bool status = (m_pinch.equal( other.m_pinch ) && (m_minpvMode == other.getMinpvMode())); + bool status = ((m_pinch == other.m_pinch) && (m_minpvMode == other.getMinpvMode())); if(m_minpvMode!=MinpvMode::ModeEnum::Inactive) { status = status && (m_minpvVector == other.getMinpvVector()); diff --git a/src/opm/parser/eclipse/EclipseState/Schedule/Well/Connection.cpp b/src/opm/parser/eclipse/EclipseState/Schedule/Well/Connection.cpp index 737d53af0..9ccab6f61 100644 --- a/src/opm/parser/eclipse/EclipseState/Schedule/Well/Connection.cpp +++ b/src/opm/parser/eclipse/EclipseState/Schedule/Well/Connection.cpp @@ -34,8 +34,6 @@ #include #include #include -#include - namespace Opm { diff --git a/tests/parser/EclipseGridTests.cpp b/tests/parser/EclipseGridTests.cpp index ee409c6fb..31c444670 100644 --- a/tests/parser/EclipseGridTests.cpp +++ b/tests/parser/EclipseGridTests.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -825,7 +826,7 @@ BOOST_AUTO_TEST_CASE(ConstructorNORUNSPEC_PINCH) { BOOST_CHECK(!grid1.equal( grid2 )); BOOST_CHECK(!grid1.isPinchActive()); - BOOST_CHECK_THROW(grid1.getPinchThresholdThickness(), std::logic_error); + BOOST_CHECK_THROW(grid1.getPinchThresholdThickness(), std::bad_optional_access); BOOST_CHECK(grid2.isPinchActive()); BOOST_CHECK_EQUAL(grid2.getPinchThresholdThickness(), 0.2); BOOST_CHECK_EQUAL(grid2.getPinchGapMode(), Opm::PinchMode::ModeEnum::GAP); diff --git a/tests/parser/GroupTests.cpp b/tests/parser/GroupTests.cpp index 1db4e9417..9e891c350 100644 --- a/tests/parser/GroupTests.cpp +++ b/tests/parser/GroupTests.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include diff --git a/tests/parser/ValueTests.cpp b/tests/parser/ValueTests.cpp deleted file mode 100644 index 93d5eda68..000000000 --- a/tests/parser/ValueTests.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - 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 . - */ - -#include -#include -#include - -#define BOOST_TEST_MODULE VALUETESTS -#include -#include - -#include - - -BOOST_AUTO_TEST_CASE( check_default_constructor ) { - Opm::Value 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 v("Value" , 100); - - BOOST_CHECK_EQUAL( true , v.hasValue() ); - BOOST_CHECK_EQUAL( 100 , v.getValue()); -} - - - -BOOST_AUTO_TEST_CASE( check_equal1 ) { - Opm::Value v1("v1" , 100); - Opm::Value 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 v1("v1"); - Opm::Value 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)); -} - - -BOOST_AUTO_TEST_CASE( check_assign) { - Opm::Value v1("v1",100); - Opm::Value v2(v1); - - BOOST_CHECK(v1.equal(v2)); -}