Skip to content

Commit a5a189e

Browse files
authored
Merge pull request #311 from sivar2311/master
Implemented std::function
2 parents c03fd1b + 1016211 commit a5a189e

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## Version 2.10.4
3+
- Implemented std::function for RangeController to allow lambda functions
4+
25
## Version 2.10.3
36
- Fixed version number for PlatformIO library registry
47

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"maintainer": true
1414
}
1515
],
16-
"version": "2.10.3",
16+
"version": "2.10.4",
1717
"frameworks": "arduino",
1818
"platforms": [
1919
"espressif8266",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SinricPro
2-
version=2.10.3
2+
version=2.10.4
33
author=Boris Jaeger <sivar2311@gmail.com>
44
maintainer=Boris Jaeger <sivar2311@gmail.com>
55
sentence=Library for https://sinric.pro - simple way to connect your device to alexa

src/Capabilities/RangeController.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,33 @@ FSTR(RANGE, rangeValueDelta); // "rangeValueDelta"
2727
* @snippet callbacks.cpp onRangeValue
2828
**/
2929

30-
using GenericRangeValueCallback_int = bool (*)(const String &, const String &, int &);
31-
using GenericRangeValueCallback_float = bool (*)(const String &, const String &, float &);
30+
using GenericRangeValueCallback_int = std::function<bool(const String &, const String &, int &)>;
31+
using GenericRangeValueCallback_float = std::function<bool(const String &, const String &, float &)>;
3232

3333
struct GenericRangeValueCallback {
3434
GenericRangeValueCallback() : type(type_unknown) {}
35-
GenericRangeValueCallback(GenericRangeValueCallback_int cb) : type(type_int), cb_int(cb) {}
36-
GenericRangeValueCallback(GenericRangeValueCallback_float cb) : type(type_float), cb_float(cb) {}
35+
GenericRangeValueCallback(GenericRangeValueCallback_int cb) : type(type_int), callback(cb) {}
36+
GenericRangeValueCallback(GenericRangeValueCallback_float cb) : type(type_float), callback(cb) {}
37+
~GenericRangeValueCallback() {};
38+
GenericRangeValueCallback& operator=(const GenericRangeValueCallback& other) { this->callback = other.callback; this->type = other.type; return *this; };
3739
enum {
3840
type_unknown,
3941
type_int,
4042
type_float
4143
} type;
42-
union {
44+
union Callback {
45+
Callback() {};
46+
Callback(const GenericRangeValueCallback_int& cb) : cb_int(cb) {};
47+
Callback(const GenericRangeValueCallback_float& cb) : cb_float(cb) {};
48+
Callback& operator=(const Callback& other) { cb_int = other.cb_int; return *this; }
49+
~Callback(){};
50+
4351
GenericRangeValueCallback_int cb_int;
4452
GenericRangeValueCallback_float cb_float;
45-
};
53+
} callback;
4654
};
4755

48-
using SetRangeValueCallback = bool (*)(const String &, int &);
56+
using SetRangeValueCallback = std::function<bool(const String &, int &)>;
4957

5058
/**
5159
* @brief Callback definition for onRangeValue function on a specific instance
@@ -272,14 +280,14 @@ bool RangeController<T>::handleRangeController(SinricProRequest &request) {
272280

273281
if (cb.type == GenericRangeValueCallback::type_float) {
274282
float value = request.request_value[FSTR_RANGE_rangeValue];
275-
success = cb.cb_float(device->deviceId, request.instance, value);
283+
success = cb.callback.cb_float(device->deviceId, request.instance, value);
276284
request.response_value[FSTR_RANGE_rangeValue] = value;
277285
return success;
278286
}
279287

280288
if (cb.type == GenericRangeValueCallback::type_int) {
281289
int value = request.request_value[FSTR_RANGE_rangeValue];
282-
success = cb.cb_int(device->deviceId, request.instance, value);
290+
success = cb.callback.cb_int(device->deviceId, request.instance, value);
283291
request.response_value[FSTR_RANGE_rangeValue] = value;
284292
return success;
285293
}
@@ -303,14 +311,14 @@ bool RangeController<T>::handleRangeController(SinricProRequest &request) {
303311

304312
if (cb.type == GenericRangeValueCallback::type_float) {
305313
float value = request.request_value[FSTR_RANGE_rangeValueDelta];
306-
success = cb.cb_float(device->deviceId, request.instance, value);
314+
success = cb.callback.cb_float(device->deviceId, request.instance, value);
307315
request.response_value[FSTR_RANGE_rangeValue] = value;
308316
return success;
309317
}
310318

311319
if (cb.type == GenericRangeValueCallback::type_int) {
312320
int value = request.request_value[FSTR_RANGE_rangeValueDelta];
313-
success = cb.cb_int(device->deviceId, request.instance, value);
321+
success = cb.callback.cb_int(device->deviceId, request.instance, value);
314322
request.response_value[FSTR_RANGE_rangeValue] = value;
315323
return success;
316324
}

src/SinricProVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Version Configuration
77
#define SINRICPRO_VERSION_MAJOR 2
88
#define SINRICPRO_VERSION_MINOR 10
9-
#define SINRICPRO_VERSION_REVISION 3
9+
#define SINRICPRO_VERSION_REVISION 4
1010
#define SINRICPRO_VERSION STR(SINRICPRO_VERSION_MAJOR) "." STR(SINRICPRO_VERSION_MINOR) "." STR(SINRICPRO_VERSION_REVISION)
1111
#define SINRICPRO_VERSION_STR "SinricPro (v" SINRICPRO_VERSION ")"
1212
#define SINRICPRO_VERISON_INT SINRICPRO_VERSION_MAJOR * 1000000 + SINRICPRO_VERSION_MINOR * 1000 + SINRICPRO_VERSION_REVISION

0 commit comments

Comments
 (0)