From bd174bc11315ebde7edf46a4bf03eb38c178a547 Mon Sep 17 00:00:00 2001 From: Phil Miller Date: Tue, 19 Sep 2023 17:39:56 -0700 Subject: [PATCH] Refactor get_available_variable_names to return a span rather than a concrete vector --- include/forcing/CsvPerFeatureForcingProvider.hpp | 2 +- include/forcing/DataProvider.hpp | 3 ++- include/forcing/DeferredWrappedProvider.hpp | 4 ++-- include/forcing/NetCDFPerFeatureDataProvider.hpp | 2 +- include/forcing/OptionalWrappedDataProvider.hpp | 2 +- include/forcing/WrappedDataProvider.hpp | 2 +- .../catchment/Bmi_Module_Formulation.hpp | 13 +++---------- .../catchment/Bmi_Multi_Formulation.hpp | 4 +--- src/forcing/NetCDFPerFeatureDataProvider.cpp | 2 +- .../catchment/Bmi_Multi_Formulation.cpp | 3 +-- test/forcing/CsvPerFeatureForcingProvider_Test.cpp | 4 ++-- test/forcing/TrivialForcingProvider.hpp | 2 +- 12 files changed, 17 insertions(+), 26 deletions(-) diff --git a/include/forcing/CsvPerFeatureForcingProvider.hpp b/include/forcing/CsvPerFeatureForcingProvider.hpp index 94c1c3c5e7..f131da01ed 100644 --- a/include/forcing/CsvPerFeatureForcingProvider.hpp +++ b/include/forcing/CsvPerFeatureForcingProvider.hpp @@ -222,7 +222,7 @@ class CsvPerFeatureForcingProvider : public data_access::GenericDataProvider return is_param_sum_over_time_step(name); } - const std::vector &get_available_variable_names() override { + boost::span get_available_variable_names() override { return available_forcings; } diff --git a/include/forcing/DataProvider.hpp b/include/forcing/DataProvider.hpp index 120b0b0249..59fd7b9562 100644 --- a/include/forcing/DataProvider.hpp +++ b/include/forcing/DataProvider.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace data_access { @@ -32,7 +33,7 @@ namespace data_access /** Return the variables that are accessable by this data provider */ - virtual const std::vector& get_available_variable_names() = 0; + virtual boost::span get_available_variable_names() = 0; /** Return the first valid time for which data from the request variable can be requested */ diff --git a/include/forcing/DeferredWrappedProvider.hpp b/include/forcing/DeferredWrappedProvider.hpp index 5ffd86674a..98f7b3a90d 100644 --- a/include/forcing/DeferredWrappedProvider.hpp +++ b/include/forcing/DeferredWrappedProvider.hpp @@ -70,7 +70,7 @@ namespace data_access { * @return The names of the outputs for which this instance is (or will be) able to provide values. */ - const std::vector &get_available_variable_names() { + boost::span get_available_variable_names() { return providedOutputs; } @@ -137,7 +137,7 @@ namespace data_access { } // Confirm this will provide everything needed - const std::vector &available = provider->get_available_variable_names(); + const auto available = provider->get_available_variable_names(); for (const std::string &requiredName : providedOutputs) { if (std::find(available.begin(), available.end(), requiredName) == available.end()) { setMessage = "Given provider does not provide the required " + requiredName; diff --git a/include/forcing/NetCDFPerFeatureDataProvider.hpp b/include/forcing/NetCDFPerFeatureDataProvider.hpp index 1627d3aef0..47751536cc 100644 --- a/include/forcing/NetCDFPerFeatureDataProvider.hpp +++ b/include/forcing/NetCDFPerFeatureDataProvider.hpp @@ -67,7 +67,7 @@ namespace data_access ~NetCDFPerFeatureDataProvider(); /** Return the variables that are accessable by this data provider */ - const std::vector& get_available_variable_names() override; + boost::span get_available_variable_names() override; /** return a list of ids in the current file */ const std::vector& get_ids() const; diff --git a/include/forcing/OptionalWrappedDataProvider.hpp b/include/forcing/OptionalWrappedDataProvider.hpp index 1ca37330fd..c4e16eef09 100644 --- a/include/forcing/OptionalWrappedDataProvider.hpp +++ b/include/forcing/OptionalWrappedDataProvider.hpp @@ -397,7 +397,7 @@ namespace data_access { std::map defaultUsageWaits; static bool isSuppliedByProvider(const std::string &outputName, GenericDataProvider *provider) { - const std::vector &available = provider->get_available_variable_names(); + auto available = provider->get_available_variable_names(); return find(available.begin(), available.end(), outputName) != available.end(); } diff --git a/include/forcing/WrappedDataProvider.hpp b/include/forcing/WrappedDataProvider.hpp index 53d81f19b5..84e76ebef1 100644 --- a/include/forcing/WrappedDataProvider.hpp +++ b/include/forcing/WrappedDataProvider.hpp @@ -51,7 +51,7 @@ namespace data_access { * @return const std::vector& the names of available data variables */ - const std::vector &get_available_variable_names() override { + boost::span get_available_variable_names() override { return wrapped_provider->get_available_variable_names(); } diff --git a/include/realizations/catchment/Bmi_Module_Formulation.hpp b/include/realizations/catchment/Bmi_Module_Formulation.hpp index 4e4108bfa0..0c275232e0 100644 --- a/include/realizations/catchment/Bmi_Module_Formulation.hpp +++ b/include/realizations/catchment/Bmi_Module_Formulation.hpp @@ -70,8 +70,7 @@ namespace realization { * @return The collection of forcing output property names this instance can provide. * @see ForcingProvider */ - //const vector &get_available_forcing_outputs() { - const std::vector &get_available_variable_names() override { + boost::span get_available_variable_names() override { if (is_model_initialized() && available_forcings.empty()) { for (const std::string &output_var_name : get_bmi_model()->GetOutputVarNames()) { available_forcings.push_back(output_var_name); @@ -82,10 +81,6 @@ namespace realization { return available_forcings; } - //inline const vector &get_available_variable_names() override { - // return get_available_forcing_outputs(); - //} - /** * Get the inclusive beginning of the period of time over which this instance can provide data for this forcing. * @@ -219,8 +214,7 @@ namespace realization { std::string output_units = selector.get_output_units(); // First make sure this is an available output - //const std::vector forcing_outputs = get_available_forcing_outputs(); - const std::vector forcing_outputs = get_available_variable_names(); + auto forcing_outputs = get_available_variable_names(); if (std::find(forcing_outputs.begin(), forcing_outputs.end(), output_name) == forcing_outputs.end()) { throw std::runtime_error(get_formulation_type() + " received invalid output forcing name " + output_name); } @@ -288,8 +282,7 @@ namespace realization { std::string output_units = selector.get_output_units(); // First make sure this is an available output - //const std::vector forcing_outputs = get_available_forcing_outputs(); - const std::vector forcing_outputs = get_available_variable_names(); + auto forcing_outputs = get_available_variable_names(); if (std::find(forcing_outputs.begin(), forcing_outputs.end(), output_name) == forcing_outputs.end()) { throw std::runtime_error(get_formulation_type() + " received invalid output forcing name " + output_name); } diff --git a/include/realizations/catchment/Bmi_Multi_Formulation.hpp b/include/realizations/catchment/Bmi_Multi_Formulation.hpp index e7d056f874..0241c3e639 100644 --- a/include/realizations/catchment/Bmi_Multi_Formulation.hpp +++ b/include/realizations/catchment/Bmi_Multi_Formulation.hpp @@ -116,9 +116,7 @@ namespace realization { * @return The collection of forcing output property names this instance can provide. * @see ForcingProvider */ - //const vector &get_available_forcing_outputs(); - //const vector &get_available_variable_names() override { return get_available_forcing_outputs(); } - const std::vector &get_available_variable_names() override; + boost::span get_available_variable_names() override; /** * Get the input variables of diff --git a/src/forcing/NetCDFPerFeatureDataProvider.cpp b/src/forcing/NetCDFPerFeatureDataProvider.cpp index 1e1a35fce4..535e7ca450 100644 --- a/src/forcing/NetCDFPerFeatureDataProvider.cpp +++ b/src/forcing/NetCDFPerFeatureDataProvider.cpp @@ -262,7 +262,7 @@ NetCDFPerFeatureDataProvider::NetCDFPerFeatureDataProvider(std::string input_pat NetCDFPerFeatureDataProvider::~NetCDFPerFeatureDataProvider() = default; -const std::vector& NetCDFPerFeatureDataProvider::get_available_variable_names() +boost::span NetCDFPerFeatureDataProvider::get_available_variable_names() { return variable_names; } diff --git a/src/realizations/catchment/Bmi_Multi_Formulation.cpp b/src/realizations/catchment/Bmi_Multi_Formulation.cpp index df35389a16..04f8889f33 100644 --- a/src/realizations/catchment/Bmi_Multi_Formulation.cpp +++ b/src/realizations/catchment/Bmi_Multi_Formulation.cpp @@ -179,8 +179,7 @@ const bool &Bmi_Multi_Formulation::get_allow_model_exceed_end_time() const { * @return The collection of forcing output property names this instance can provide. * @see ForcingProvider */ -//const vector &Bmi_Multi_Formulation::get_available_forcing_outputs() { -const std::vector &Bmi_Multi_Formulation::get_available_variable_names() { +boost::span Bmi_Multi_Formulation::get_available_variable_names() { if (is_model_initialized() && available_forcings.empty()) { for (const nested_module_ptr &module: modules) { for (const std::string &out_var_name: module->get_bmi_output_variables()) { diff --git a/test/forcing/CsvPerFeatureForcingProvider_Test.cpp b/test/forcing/CsvPerFeatureForcingProvider_Test.cpp index c3684a4e6c..9e338236d1 100644 --- a/test/forcing/CsvPerFeatureForcingProvider_Test.cpp +++ b/test/forcing/CsvPerFeatureForcingProvider_Test.cpp @@ -169,7 +169,7 @@ TEST_F(CsvPerFeatureForcingProviderTest, TestForcingDataUnitConversion) ///Test AORC Forcing Object TEST_F(CsvPerFeatureForcingProviderTest, TestGetAvailableForcingOutputs) { - const std::vector& afos = Forcing_Object->get_available_variable_names(); + auto afos = Forcing_Object->get_available_variable_names(); EXPECT_EQ(afos.size(), 18); EXPECT_TRUE(std::find(afos.begin(), afos.end(), "DLWRF_surface") != afos.end()); EXPECT_TRUE(std::find(afos.begin(), afos.end(), CSDMS_STD_NAME_SOLAR_LONGWAVE) != afos.end()); @@ -221,4 +221,4 @@ TEST_F(CsvPerFeatureForcingProviderTest, TestForcingUnitHeaderParsing) } } -} \ No newline at end of file +} diff --git a/test/forcing/TrivialForcingProvider.hpp b/test/forcing/TrivialForcingProvider.hpp index ea78fc6954..0f170c73fe 100644 --- a/test/forcing/TrivialForcingProvider.hpp +++ b/test/forcing/TrivialForcingProvider.hpp @@ -23,7 +23,7 @@ namespace data_access { outputs.push_back(OUTPUT_NAME_1); } - const std::vector& get_available_variable_names() override { + boost::span get_available_variable_names() override { return outputs; }