Skip to content

Commit c034fee

Browse files
committed
Adding contains() method
1 parent 65fe25e commit c034fee

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

Diff for: lib/coek/coek/util/DataPortal.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -732,4 +732,14 @@ std::string DataPortalRepn::to_string(unsigned int indent)
732732
#endif
733733
}
734734

735+
bool DataPortalRepn::contains(const std::string& name)
736+
{
737+
#if __cplusplus >= 202002L
738+
return parameter_data.contains(name) || indexed_parameter_data.contains(name) || set_data.contains(name) || indexed_set_data.contains(name);
739+
#else
740+
return parameter_data.count(name) + indexed_parameter_data.count(name) + set_data.count(name) + indexed_set_data.count(name) > 0;
741+
#endif
742+
return false;
743+
}
744+
735745
} // namespace coek

Diff for: lib/coek/coek/util/DataPortal.hpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ class DataPortalRepn {
2323

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

26-
std::map<std::string, ParameterTypes> parameter_data;
27-
std::map<std::string, std::map<KeyTypes, ParameterTypes>> indexed_parameter_data;
28-
2926
using StringSetType = std::set<std::string>;
3027
using IntegerSetType = std::set<int>;
3128
using DoubleSetType = std::set<double>;
3229
using TupleSetType = std::set<TupleValueType>;
3330
using SetTypes = std::variant<StringSetType, IntegerSetType, DoubleSetType, TupleSetType>;
3431

32+
public:
33+
std::map<std::string, ParameterTypes> parameter_data;
34+
std::map<std::string, std::map<KeyTypes, ParameterTypes>> indexed_parameter_data;
35+
3536
std::map<std::string, SetTypes> set_data;
3637
std::map<std::string, std::map<KeyTypes, SetTypes>> indexed_set_data;
3738

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

54+
bool contains(const std::string& name);
55+
5356
//
5457
// GET methods
5558
//
@@ -760,6 +763,9 @@ class DataPortal {
760763
readable JSON format. */
761764
std::string to_string(unsigned int indent = 0) { return repn->to_string(indent); }
762765

766+
/** Returns true if the dataportal contains the specified data. */
767+
bool contains(const std::string& name) { return repn->contains(name); }
768+
763769
//
764770
// GET methods
765771
//

Diff for: lib/coek/test/smoke/test_util_DataPortal.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
#include "catch2/catch_test_macros.hpp"
55
#include "coek/util/DataPortal.hpp"
66

7+
TEST_CASE("DP_misc", "[smoke]")
8+
{
9+
SECTION("contains")
10+
{
11+
auto dp = coek::DataPortal();
12+
dp.load_from_json_string(R"(
13+
{
14+
"A": {
15+
"set_type": "i",
16+
"data": [1, 2, 3]
17+
}
18+
}
19+
)");
20+
REQUIRE(dp.contains("A") == true);
21+
REQUIRE(dp.contains("unknown") == false);
22+
}
23+
}
24+
725
TEST_CASE("get_DP_set", "[smoke]")
826
{
927
SECTION("error1")

0 commit comments

Comments
 (0)