Simplify ParserItem::dimensions() api
This commit is contained in:
@@ -73,9 +73,7 @@ namespace Opm {
|
||||
explicit ParserItem( const Json::JsonObject& jsonConfig );
|
||||
|
||||
void push_backDimension( const std::string& );
|
||||
const std::string& getDimension(size_t index) const;
|
||||
bool hasDimension() const;
|
||||
size_t numDimensions() const;
|
||||
const std::vector<std::string>& dimensions() const;
|
||||
const std::string& name() const;
|
||||
item_size sizeType() const;
|
||||
type_tag dataType() const;
|
||||
@@ -108,7 +106,7 @@ namespace Opm {
|
||||
int ival;
|
||||
std::string sval;
|
||||
UDAValue uval;
|
||||
std::vector< std::string > dimensions;
|
||||
std::vector< std::string > m_dimensions;
|
||||
|
||||
std::string m_name;
|
||||
item_size m_sizeType = item_size::SINGLE;
|
||||
|
||||
@@ -199,10 +199,8 @@ namespace Opm {
|
||||
stream << " const auto& parserRecord = jsonKeyword.getRecord(0);" << std::endl;
|
||||
stream << " for (size_t i=0; i < parserRecord.size(); i++){" << std::endl;
|
||||
stream << " const auto& item = parserRecord.get( i );" << std::endl;
|
||||
stream << " for (size_t j=0; j < item.numDimensions(); j++) {" << std::endl;
|
||||
stream << " const std::string& dimString = item.getDimension(j);" << std::endl;
|
||||
stream << " BOOST_CHECK_NO_THROW( unitSystem.getNewDimension( dimString ));" << std::endl;
|
||||
stream << " }" << std::endl;
|
||||
stream << " for (const auto& dim : item.dimensions())" << std::endl;
|
||||
stream << " BOOST_CHECK_NO_THROW( unitSystem.getNewDimension( dim ));" << std::endl;
|
||||
stream << " }" << std::endl;
|
||||
stream << " }" << std::endl;
|
||||
stream << endTest( );
|
||||
|
||||
@@ -252,31 +252,22 @@ const T& ParserItem::getDefault() const {
|
||||
return this->value_ref< T >();
|
||||
}
|
||||
|
||||
bool ParserItem::hasDimension() const {
|
||||
return !this->dimensions.empty();
|
||||
}
|
||||
|
||||
size_t ParserItem::numDimensions() const {
|
||||
return this->dimensions.size();
|
||||
}
|
||||
|
||||
const std::string& ParserItem::getDimension( size_t index ) const {
|
||||
if( this->data_type == type_tag::fdouble || this->data_type == type_tag::uda)
|
||||
return this->dimensions.at( index );
|
||||
throw std::invalid_argument("Item is not double / UDA .");
|
||||
const std::vector<std::string>& ParserItem::dimensions() const {
|
||||
return this->m_dimensions;
|
||||
}
|
||||
|
||||
void ParserItem::push_backDimension( const std::string& dim ) {
|
||||
if (!(this->input_type == ParserItem::itype::DOUBLE || this->input_type == ParserItem::itype::UDA))
|
||||
throw std::invalid_argument( "Invalid type, does not have dimension." );
|
||||
|
||||
if( this->sizeType() == item_size::SINGLE && this->dimensions.size() > 0 ) {
|
||||
if( this->sizeType() == item_size::SINGLE && this->m_dimensions.size() > 0 ) {
|
||||
throw std::invalid_argument(
|
||||
"Internal error: "
|
||||
"cannot add more than one dimension to an item of size 1" );
|
||||
}
|
||||
|
||||
this->dimensions.push_back( dim );
|
||||
this->m_dimensions.push_back( dim );
|
||||
}
|
||||
|
||||
const std::string& ParserItem::name() const {
|
||||
@@ -364,10 +355,10 @@ bool ParserItem::operator==( const ParserItem& rhs ) const {
|
||||
}
|
||||
}
|
||||
if( this->data_type != type_tag::fdouble ) return true;
|
||||
return this->dimensions.size() == rhs.dimensions.size()
|
||||
&& std::equal( this->dimensions.begin(),
|
||||
this->dimensions.end(),
|
||||
rhs.dimensions.begin() );
|
||||
return this->m_dimensions.size() == rhs.m_dimensions.size()
|
||||
&& std::equal( this->m_dimensions.begin(),
|
||||
this->m_dimensions.end(),
|
||||
rhs.m_dimensions.begin() );
|
||||
}
|
||||
|
||||
bool ParserItem::operator!=( const ParserItem& rhs ) const {
|
||||
@@ -462,8 +453,8 @@ std::string ParserItem::createCode(const std::string& indent) const {
|
||||
stream << " );" << '\n';
|
||||
}
|
||||
|
||||
for (size_t idim=0; idim < this->numDimensions(); idim++)
|
||||
stream << indent <<"item.push_backDimension(\"" << this->getDimension( idim ) << "\");" << '\n';
|
||||
for (const auto& dim : this->m_dimensions)
|
||||
stream << indent <<"item.push_backDimension(\"" << dim << "\");" << '\n';
|
||||
|
||||
if (this->m_description.size() > 0)
|
||||
stream << indent << "item.setDescription(\"" << this->m_description << "\");" << '\n';
|
||||
@@ -677,12 +668,12 @@ std::ostream& operator<<( std::ostream& stream, const ParserItem& item ) {
|
||||
stream << " ";
|
||||
}
|
||||
|
||||
if( !item.hasDimension() )
|
||||
if( item.dimensions().empty() )
|
||||
stream << "dimensions: none";
|
||||
else {
|
||||
stream << "dimensions: [ ";
|
||||
for( size_t i = 0; i < item.numDimensions(); ++i )
|
||||
stream << "'" << item.getDimension( i ) << "' ";
|
||||
for (const auto& dim : item.dimensions())
|
||||
stream << "'" << dim << "' ";
|
||||
stream << "]";
|
||||
}
|
||||
|
||||
|
||||
@@ -90,19 +90,19 @@ namespace {
|
||||
|
||||
bool ParserRecord::hasDimension() const {
|
||||
return std::any_of( this->begin(), this->end(),
|
||||
[]( const ParserItem& x ) { return x.hasDimension(); } );
|
||||
[]( const ParserItem& x ) { return x.dimensions().size() > 0; } );
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ParserRecord::applyUnitsToDeck( Deck& deck, DeckRecord& deckRecord ) const {
|
||||
for( const auto& parser_item : *this ) {
|
||||
if( !parser_item.hasDimension() ) continue;
|
||||
if( parser_item.dimensions().empty() ) continue;
|
||||
|
||||
auto& deckItem = deckRecord.getItem( parser_item.name() );
|
||||
for (size_t idim = 0; idim < parser_item.numDimensions(); idim++) {
|
||||
auto activeDimension = deck.getActiveUnitSystem().getNewDimension( parser_item.getDimension(idim) );
|
||||
auto defaultDimension = deck.getDefaultUnitSystem().getNewDimension( parser_item.getDimension(idim) );
|
||||
for (const auto& dim : parser_item.dimensions()) {
|
||||
auto activeDimension = deck.getActiveUnitSystem().getNewDimension(dim);
|
||||
auto defaultDimension = deck.getDefaultUnitSystem().getNewDimension(dim);
|
||||
deckItem.push_backDimension( activeDimension , defaultDimension );
|
||||
}
|
||||
|
||||
@@ -116,9 +116,9 @@ namespace {
|
||||
if (parser_item.dataType() == type_tag::uda && deckItem.size() > 0) {
|
||||
auto uda = deckItem.get<UDAValue>(0);
|
||||
if (deckItem.defaultApplied(0))
|
||||
uda.set_dim( deck.getDefaultUnitSystem().getNewDimension( parser_item.getDimension(0)));
|
||||
uda.set_dim( deck.getDefaultUnitSystem().getNewDimension( parser_item.dimensions().front()));
|
||||
else
|
||||
uda.set_dim( deck.getActiveUnitSystem().getNewDimension( parser_item.getDimension(0)));
|
||||
uda.set_dim( deck.getActiveUnitSystem().getNewDimension( parser_item.dimensions().front()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -911,15 +911,14 @@ BOOST_AUTO_TEST_CASE(ParserDefaultHasDimensionReturnsFalse) {
|
||||
ParserItem stringItem(std::string("SOMESTRING"), STRING);
|
||||
ParserItem doubleItem(std::string("SOMEDOUBLE"), DOUBLE);
|
||||
|
||||
BOOST_CHECK( !intItem.hasDimension() );
|
||||
BOOST_CHECK( !stringItem.hasDimension() );
|
||||
BOOST_CHECK( !doubleItem.hasDimension() );
|
||||
BOOST_CHECK( intItem.dimensions().empty() );
|
||||
BOOST_CHECK( stringItem.dimensions().empty() );
|
||||
BOOST_CHECK( doubleItem.dimensions().empty() );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParserIntItemGetDimensionThrows) {
|
||||
ParserItem intItem(std::string("SOMEINT"), INT);
|
||||
|
||||
BOOST_CHECK_THROW( intItem.getDimension(0) , std::invalid_argument );
|
||||
BOOST_CHECK_THROW( intItem.push_backDimension("Length") , std::invalid_argument );
|
||||
}
|
||||
|
||||
@@ -936,9 +935,9 @@ BOOST_AUTO_TEST_CASE(ParserDoubleItemAddMultipleDimensionToSIngleSizeThrows) {
|
||||
BOOST_AUTO_TEST_CASE(ParserDoubleItemWithDimensionHasReturnsCorrect) {
|
||||
ParserItem doubleItem("SOMEDOUBLE", DOUBLE);
|
||||
|
||||
BOOST_CHECK( !doubleItem.hasDimension() );
|
||||
BOOST_CHECK( doubleItem.dimensions().empty() );
|
||||
doubleItem.push_backDimension("Length*Length");
|
||||
BOOST_CHECK( doubleItem.hasDimension() );
|
||||
BOOST_CHECK( !doubleItem.dimensions().empty() );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParserDoubleItemGetDimension) {
|
||||
@@ -946,19 +945,18 @@ BOOST_AUTO_TEST_CASE(ParserDoubleItemGetDimension) {
|
||||
doubleItem.setSizeType( ParserItem::item_size::ALL );
|
||||
doubleItem.setDefault(0.0);
|
||||
|
||||
BOOST_CHECK_THROW( doubleItem.getDimension( 10 ) , std::out_of_range );
|
||||
BOOST_CHECK_THROW( doubleItem.getDimension( 0 ) , std::out_of_range );
|
||||
|
||||
doubleItem.push_backDimension("Length");
|
||||
doubleItem.push_backDimension("Length*Length");
|
||||
doubleItem.push_backDimension("Length*Length*Length");
|
||||
|
||||
BOOST_CHECK_EQUAL( "Length" , doubleItem.getDimension(0));
|
||||
BOOST_CHECK_EQUAL( "Length*Length" , doubleItem.getDimension(1));
|
||||
BOOST_CHECK_EQUAL( "Length*Length*Length" , doubleItem.getDimension(2));
|
||||
BOOST_CHECK_THROW( doubleItem.getDimension( 3 ) , std::out_of_range );
|
||||
const auto& dimensions = doubleItem.dimensions();
|
||||
BOOST_CHECK_EQUAL( "Length" , dimensions[0]);
|
||||
BOOST_CHECK_EQUAL( "Length*Length" ,dimensions[1]);
|
||||
BOOST_CHECK_EQUAL( "Length*Length*Length" , dimensions[2]);
|
||||
BOOST_CHECK_EQUAL(3, dimensions.size());
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(DefaultConstructor_NoParams_NoThrow) {
|
||||
BOOST_CHECK_NO_THROW(ParserRecord record);
|
||||
}
|
||||
@@ -1645,15 +1643,15 @@ BOOST_AUTO_TEST_CASE(ParseKeywordHasDimensionCorrect) {
|
||||
record.addItem( item1 );
|
||||
parserKeyword.addRecord( record );
|
||||
BOOST_CHECK( !parserKeyword.hasDimension() );
|
||||
BOOST_CHECK_EQUAL( 0U , item1.numDimensions() );
|
||||
BOOST_CHECK_EQUAL( 0U , item1.dimensions().size() );
|
||||
|
||||
item2.push_backDimension("Length*Length/Time");
|
||||
item2.push_backDimension("Length");
|
||||
record.addItem( item2 );
|
||||
|
||||
auto parserKeyword2 = createDynamicSized("JA");
|
||||
parserKeyword2.addRecord( record );
|
||||
BOOST_CHECK( parserKeyword2.hasDimension() );
|
||||
BOOST_CHECK_EQUAL( 1U , item2.numDimensions() );
|
||||
BOOST_CHECK_EQUAL( 1U , item2.dimensions().size() );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_withDimension) {
|
||||
@@ -1668,8 +1666,7 @@ BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_withDimension) {
|
||||
BOOST_CHECK_EQUAL( 100U , parserKeyword.getFixedSize() );
|
||||
|
||||
BOOST_CHECK( parserKeyword.hasDimension() );
|
||||
BOOST_CHECK( item.hasDimension() );
|
||||
BOOST_CHECK_EQUAL( 1U , item.numDimensions() );
|
||||
BOOST_CHECK_EQUAL( 1U , item.dimensions().size() );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_withDimensionList) {
|
||||
@@ -1683,8 +1680,7 @@ BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_withDimensionList) {
|
||||
BOOST_CHECK_EQUAL( 100U , parserKeyword.getFixedSize() );
|
||||
|
||||
BOOST_CHECK( parserKeyword.hasDimension() );
|
||||
BOOST_CHECK( item.hasDimension() );
|
||||
BOOST_CHECK_EQUAL( 3U , item.numDimensions() );
|
||||
BOOST_CHECK_EQUAL( 3U , item.dimensions().size() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user