Added SUM_TYPE macro
This commit is contained in:
parent
28a94a0499
commit
a52fccc1c6
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include "macros.h"
|
||||||
#include "observable.h"
|
#include "observable.h"
|
||||||
|
|
||||||
#define ESP_LOG1 ESP_LOGD
|
#define ESP_LOG1 ESP_LOGD
|
||||||
|
@ -25,86 +26,25 @@ struct ClearPairedDevices { PairedDevice kind; };
|
||||||
|
|
||||||
|
|
||||||
// a poor man's sum-type, because C++
|
// a poor man's sum-type, because C++
|
||||||
class Args {
|
SUM_TYPE(Args,
|
||||||
public:
|
(SetRollingCodeCounter, set_rolling_code_counter),
|
||||||
union {
|
(GetRollingCodeCounter, get_rolling_code_counter),
|
||||||
SetRollingCodeCounter set_rolling_code_counter;
|
(SetClientID, set_client_id),
|
||||||
GetRollingCodeCounter get_rolling_code_counter;
|
(QueryStatus, query_status),
|
||||||
SetClientID set_client_id;
|
(QueryOpenings, query_openings),
|
||||||
QueryStatus query_status;
|
(ActivateLearn, activate_learn),
|
||||||
QueryOpenings query_openings;
|
(InactivateLearn, inactivate_learn),
|
||||||
ActivateLearn activate_learn;
|
(QueryPairedDevices, query_paired_devices),
|
||||||
InactivateLearn inactivate_learn;
|
(QueryPairedDevicesAll, query_paired_devices_all),
|
||||||
QueryPairedDevices query_paired_devices;
|
(ClearPairedDevices, clear_paired_devices),
|
||||||
QueryPairedDevicesAll query_paired_devices_all;
|
)
|
||||||
ClearPairedDevices clear_paired_devices;
|
|
||||||
} value;
|
|
||||||
|
|
||||||
enum class Tag {
|
|
||||||
set_rolling_code_counter,
|
|
||||||
get_rolling_code_counter,
|
|
||||||
set_client_id,
|
|
||||||
query_status,
|
|
||||||
query_openings,
|
|
||||||
activate_learn,
|
|
||||||
inactivate_learn,
|
|
||||||
query_paired_devices,
|
|
||||||
query_paired_devices_all,
|
|
||||||
clear_paired_devices,
|
|
||||||
} tag;
|
|
||||||
|
|
||||||
Args(GetRollingCodeCounter&& arg): tag(Tag::get_rolling_code_counter) {
|
|
||||||
value.get_rolling_code_counter = std::move(arg);
|
|
||||||
}
|
|
||||||
Args(SetRollingCodeCounter&& arg): tag(Tag::set_rolling_code_counter) {
|
|
||||||
value.set_rolling_code_counter = std::move(arg);
|
|
||||||
}
|
|
||||||
Args(SetClientID&& arg): tag(Tag::set_client_id) {
|
|
||||||
value.set_client_id = std::move(arg);
|
|
||||||
}
|
|
||||||
Args(QueryStatus&& arg): tag(Tag::query_status) {
|
|
||||||
value.query_status = std::move(arg);
|
|
||||||
}
|
|
||||||
Args(QueryOpenings&& arg): tag(Tag::query_openings) {
|
|
||||||
value.query_openings = std::move(arg);
|
|
||||||
}
|
|
||||||
Args(ActivateLearn&& arg): tag(Tag::activate_learn) {
|
|
||||||
value.activate_learn = std::move(arg);
|
|
||||||
}
|
|
||||||
Args(InactivateLearn&& arg): tag(Tag::inactivate_learn) {
|
|
||||||
value.inactivate_learn = std::move(arg);
|
|
||||||
}
|
|
||||||
Args(QueryPairedDevices&& arg): tag(Tag::query_paired_devices) {
|
|
||||||
value.query_paired_devices = std::move(arg);
|
|
||||||
}
|
|
||||||
Args(QueryPairedDevicesAll&& arg): tag(Tag::query_paired_devices_all) {
|
|
||||||
value.query_paired_devices_all = std::move(arg);
|
|
||||||
}
|
|
||||||
Args(ClearPairedDevices&& arg): tag(Tag::clear_paired_devices) {
|
|
||||||
value.clear_paired_devices = std::move(arg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct RollingCodeCounter { observable<uint32_t>* value; };
|
struct RollingCodeCounter { observable<uint32_t>* value; };
|
||||||
|
|
||||||
class Result {
|
SUM_TYPE(Result,
|
||||||
public:
|
(RollingCodeCounter, rolling_code_counter),
|
||||||
union {
|
)
|
||||||
RollingCodeCounter rolling_code_counter;
|
|
||||||
} value;
|
|
||||||
|
|
||||||
enum class Tag {
|
|
||||||
rolling_code_counter,
|
|
||||||
void_,
|
|
||||||
} tag;
|
|
||||||
|
|
||||||
Result(): tag(Tag::void_) {
|
|
||||||
}
|
|
||||||
Result(RollingCodeCounter&& arg): tag(Tag::rolling_code_counter) {
|
|
||||||
value.rolling_code_counter = std::move(arg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace protocol
|
} // namespace protocol
|
||||||
} // namespace ratgdo
|
} // namespace ratgdo
|
||||||
|
|
|
@ -55,3 +55,35 @@
|
||||||
return _unknown; \
|
return _unknown; \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define SUM_TYPE_UNION_MEMBER0(type, var) type var;
|
||||||
|
#define SUM_TYPE_UNION_MEMBER(name, tuple) SUM_TYPE_UNION_MEMBER0 tuple
|
||||||
|
|
||||||
|
#define SUM_TYPE_ENUM_MEMBER0(type, var) var,
|
||||||
|
#define SUM_TYPE_ENUM_MEMBER(name, tuple) SUM_TYPE_ENUM_MEMBER0 tuple
|
||||||
|
|
||||||
|
#define SUM_TYPE_CONSTRUCTOR0(name, type, val) \
|
||||||
|
name(type&& arg) \
|
||||||
|
: tag(Tag::val) \
|
||||||
|
{ \
|
||||||
|
value.val = std::move(arg); \
|
||||||
|
}
|
||||||
|
#define SUM_TYPE_CONSTRUCTOR(name, tuple) SUM_TYPE_CONSTRUCTOR0 LPAREN name, TUPLE tuple)
|
||||||
|
|
||||||
|
#define SUM_TYPE(name, ...) \
|
||||||
|
class name { \
|
||||||
|
public: \
|
||||||
|
union { \
|
||||||
|
FOR_EACH(SUM_TYPE_UNION_MEMBER, name, __VA_ARGS__) \
|
||||||
|
} value; \
|
||||||
|
enum class Tag { \
|
||||||
|
void_, \
|
||||||
|
FOR_EACH(SUM_TYPE_ENUM_MEMBER, name, __VA_ARGS__) \
|
||||||
|
} tag; \
|
||||||
|
\
|
||||||
|
name() \
|
||||||
|
: tag(Tag::void_) \
|
||||||
|
{ \
|
||||||
|
} \
|
||||||
|
FOR_EACH(SUM_TYPE_CONSTRUCTOR, name, __VA_ARGS__) \
|
||||||
|
};
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
#include "observable.h"
|
#include "observable.h"
|
||||||
#include "callbacks.h"
|
#include "callbacks.h"
|
||||||
#include "enum.h"
|
#include "macros.h"
|
||||||
#include "ratgdo_state.h"
|
#include "ratgdo_state.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
************************************/
|
************************************/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "enum.h"
|
#include "macros.h"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
Loading…
Reference in New Issue