Skip to content

Commit 84a0852

Browse files
authored
Merge pull request #4 from bw-hro/integer-input-for-double-properties
Integer input for double properties
2 parents 977bae9 + 0ac4fa7 commit 84a0852

File tree

6 files changed

+57
-18
lines changed

6 files changed

+57
-18
lines changed

include/bw/webthing/property.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,22 @@ json property_status_message(const Property<T>& property)
2323
});
2424
}
2525

26-
2726
template<class T>
2827
json property_value_object(const Property<T>& property)
2928
{
30-
3129
auto value_as_json = property.get_value() ? json(*property.get_value()) : json();
3230
return json({{property.get_name(), value_as_json}});
3331
}
3432

35-
3633
class PropertyBase
3734
{
3835
public:
3936

40-
PropertyBase(std::string name, json metadata)
37+
PropertyBase(std::string name, json metadata, bool wraps_double)
4138
: name(name)
4239
, metadata(metadata)
43-
{
40+
, wraps_double(wraps_double)
41+
{
4442
if(this->metadata.type() != json::value_t::object)
4543
throw PropertyError("Only json::object is allowed as meta data.");
4644

@@ -74,8 +72,8 @@ class PropertyBase
7472

7573
std::string get_name() const
7674
{
77-
return name;
78-
}
75+
return name;
76+
}
7977

8078
json get_metadata() const
8179
{
@@ -90,6 +88,9 @@ class PropertyBase
9088
template<class T> void set_value(T value)
9189
{
9290
try{
91+
if(wraps_double && !std::is_same_v<T, double>)
92+
return set_value(try_static_cast<double>(value));
93+
9394
auto property = dynamic_cast<Property<T>&>(*this);
9495
property.set_value(value);
9596
}
@@ -100,10 +101,11 @@ class PropertyBase
100101
}
101102

102103
protected:
103-
std::string name;
104+
std::string name;
104105
std::string href_prefix;
105106
std::string href;
106107
json metadata;
108+
const bool wraps_double;
107109
};
108110

109111
typedef std::function<void (json)> PropertyChangedCallback;
@@ -112,16 +114,15 @@ template<class T>
112114
class Property : public PropertyBase
113115
{
114116
public:
115-
116-
Property(PropertyChangedCallback changed_callback, std::string name, std::shared_ptr<Value<T>> value, json metadata = json::object())
117-
: PropertyBase(name, metadata)
117+
Property(PropertyChangedCallback changed_callback, std::string name, std::shared_ptr<Value<T>> value, json metadata = json::object())
118+
: PropertyBase(name, metadata, std::is_same_v<T, double>)
118119
, property_change_callback(changed_callback)
119120
, value(value)
120-
{
121+
{
121122
// Add value change observer to notify the Thing about a property change.
122123
if(property_change_callback)
123124
this->value->add_observer([&](auto v){property_change_callback(property_status_message(*this));});
124-
}
125+
}
125126

126127
// Validate new proptery value before setting it.
127128
void validate_value(const T& value) const
@@ -164,7 +165,7 @@ class Property : public PropertyBase
164165
}
165166

166167
private:
167-
std::shared_ptr<Value<T>> value;
168+
std::shared_ptr<Value<T>> value;
168169
PropertyChangedCallback property_change_callback;
169170
};
170171

include/bw/webthing/utils.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,5 +261,19 @@ struct fix_uuid_scoped
261261

262262
#define FIXED_UUID_SCOPED(uuid) auto fixed_uuid_scope_guard = fix_uuid_scoped(uuid);
263263

264+
// try to static_cast from a type to another, throws std::bad_cast on error
265+
template<class To, class From>
266+
inline To try_static_cast(const From& value)
267+
{
268+
try
269+
{
270+
if constexpr (std::is_convertible_v<From, To>)
271+
return static_cast<To>(value);
272+
}
273+
catch(...)
274+
{}
275+
276+
throw std::bad_cast();
277+
}
264278

265279
} // bw::webthing

test/unit-test/property_tests.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ TEST_CASE( "Properties can wrap json values", "[property]" )
116116

117117
TEST_CASE( "Properties can only be changed from external when provided values are valid", "[property]" )
118118
{
119-
SECTION( "Number" )
119+
SECTION( "Integer" )
120120
{
121121
auto val = create_value(666);
122122
json property_description = {{"type", "integer"}, {"minimum", 555}};
@@ -128,10 +128,34 @@ TEST_CASE( "Properties can only be changed from external when provided values ar
128128
);
129129
REQUIRE( *property->get_value<int>() == 666 );
130130

131+
REQUIRE_THROWS_MATCHES(property->set_value(777.0), PropertyError,
132+
MessageContains("value type not matching")
133+
);
134+
REQUIRE( *property->get_value<int>() == 666 );
135+
131136
property->set_value(1000);
132137
REQUIRE( *property->get_value<int>() == 1000 );
133138
}
134139

140+
SECTION( "Number" )
141+
{
142+
auto val = create_value(666.0);
143+
json property_description = {{"type", "number"}, {"minimum", 555.0}};
144+
auto property = create_proptery("writeable-number-prop", val, property_description);
145+
146+
REQUIRE_THROWS_MATCHES(property->set_value(123.0), PropertyError,
147+
MessageContains("Invalid property") &&
148+
MessageContains("below minimum of 555")
149+
);
150+
REQUIRE( *property->get_value<double>() == 666 );
151+
152+
property->set_value(1000.123);
153+
REQUIRE( *property->get_value<double>() == 1000.123 );
154+
155+
property->set_value(777);
156+
REQUIRE( *property->get_value<double>() == 777 );
157+
}
158+
135159
SECTION( "String" )
136160
{
137161
auto val = create_value(std::string("first-string-value"));

vcpkg-no-ssl.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webthing-cpp",
3-
"version-string": "1.0",
3+
"version-string": "1.0.2",
44
"dependencies": [
55
"catch2",
66
"json-schema-validator",

vcpkg-with-ssl.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webthing-cpp",
3-
"version-string": "1.0",
3+
"version-string": "1.0.2",
44
"dependencies": [
55
"catch2",
66
"json-schema-validator",

vcpkg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webthing-cpp",
3-
"version-string": "1.0",
3+
"version-string": "1.0.2",
44
"dependencies": [
55
"catch2",
66
"json-schema-validator",

0 commit comments

Comments
 (0)