Skip to content

Commit

Permalink
Adding contains() method
Browse files Browse the repository at this point in the history
  • Loading branch information
whart222 committed Sep 3, 2024
1 parent 65fe25e commit c034fee
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lib/coek/coek/util/DataPortal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,4 +732,14 @@ std::string DataPortalRepn::to_string(unsigned int indent)
#endif
}

bool DataPortalRepn::contains(const std::string& name)
{
#if __cplusplus >= 202002L
return parameter_data.contains(name) || indexed_parameter_data.contains(name) || set_data.contains(name) || indexed_set_data.contains(name);
#else
return parameter_data.count(name) + indexed_parameter_data.count(name) + set_data.count(name) + indexed_set_data.count(name) > 0;
#endif
return false;
}

} // namespace coek
12 changes: 9 additions & 3 deletions lib/coek/coek/util/DataPortal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ class DataPortalRepn {

using ParameterTypes = std::variant<StringType, IntegerType, DoubleType, TupleValueType>;

std::map<std::string, ParameterTypes> parameter_data;
std::map<std::string, std::map<KeyTypes, ParameterTypes>> indexed_parameter_data;

using StringSetType = std::set<std::string>;
using IntegerSetType = std::set<int>;
using DoubleSetType = std::set<double>;
using TupleSetType = std::set<TupleValueType>;
using SetTypes = std::variant<StringSetType, IntegerSetType, DoubleSetType, TupleSetType>;

public:
std::map<std::string, ParameterTypes> parameter_data;
std::map<std::string, std::map<KeyTypes, ParameterTypes>> indexed_parameter_data;

std::map<std::string, SetTypes> set_data;
std::map<std::string, std::map<KeyTypes, SetTypes>> indexed_set_data;

Expand All @@ -50,6 +51,8 @@ class DataPortalRepn {
void save_to_file(const std::string& filename, unsigned int indent = 0);
std::string to_string(unsigned int indent = 0);

bool contains(const std::string& name);

//
// GET methods
//
Expand Down Expand Up @@ -760,6 +763,9 @@ class DataPortal {
readable JSON format. */
std::string to_string(unsigned int indent = 0) { return repn->to_string(indent); }

/** Returns true if the dataportal contains the specified data. */
bool contains(const std::string& name) { return repn->contains(name); }

//
// GET methods
//
Expand Down
18 changes: 18 additions & 0 deletions lib/coek/test/smoke/test_util_DataPortal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
#include "catch2/catch_test_macros.hpp"
#include "coek/util/DataPortal.hpp"

TEST_CASE("DP_misc", "[smoke]")
{
SECTION("contains")
{
auto dp = coek::DataPortal();
dp.load_from_json_string(R"(
{
"A": {
"set_type": "i",
"data": [1, 2, 3]
}
}
)");
REQUIRE(dp.contains("A") == true);
REQUIRE(dp.contains("unknown") == false);
}
}

TEST_CASE("get_DP_set", "[smoke]")
{
SECTION("error1")
Expand Down

0 comments on commit c034fee

Please sign in to comment.