Get rid of SUM_TYPE abstraction

I couldn't understand it -- maybe it was needed for something before our
refactoring too place, but now it's ridiculous.

400 SLoC + complaint about C++ => object.function()
This commit is contained in:
2026-05-27 12:40:44 +00:00
parent ff57cb8b2c
commit 0bdb599afe
6 changed files with 10 additions and 66 deletions
-30
View File
@@ -126,34 +126,4 @@ namespace detail {
} \
}
#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__) \
};
+2 -13
View File
@@ -16,18 +16,6 @@ class RATGDOComponent;
namespace protocol {
struct QueryStatus {
};
struct QueryOpenings {
};
// a poor man's sum-type, because C++
SUM_TYPE(Args,
(QueryStatus, query_status),
(QueryOpenings, query_openings), )
SUM_TYPE(Result, )
class Protocol {
public:
virtual void setup(RATGDOComponent* ratgdo, Scheduler* scheduler, InternalGPIOPin* rx_pin, InternalGPIOPin* tx_pin);
@@ -42,7 +30,8 @@ namespace protocol {
virtual void lock_action(LockAction action);
virtual void door_action(DoorAction action);
virtual protocol::Result call(protocol::Args args);
virtual void query_status() {}
virtual void query_openings() {}
};
}
+2 -7
View File
@@ -295,16 +295,11 @@ void RATGDOComponent::set_distance_measurement(int16_t distance)
this->last_distance_measurement = distance;
}
Result RATGDOComponent::call_protocol(Args args)
{
return this->protocol_->call(args);
}
void RATGDOComponent::query_status() { this->protocol_->call(QueryStatus { }); }
void RATGDOComponent::query_status() { this->protocol_->query_status(); }
void RATGDOComponent::query_openings()
{
this->protocol_->call(QueryOpenings { });
this->protocol_->query_openings();
}
void RATGDOComponent::sync()
+2 -3
View File
@@ -35,8 +35,7 @@ typedef Parented<RATGDOComponent> RATGDOClient;
const float DOOR_POSITION_UNKNOWN = -1.0;
const float DOOR_DELTA_UNKNOWN = -2.0;
using protocol::Args;
using protocol::Result;
class RATGDOComponent : public Component {
public:
@@ -83,7 +82,7 @@ public:
void set_output_gdo_pin(InternalGPIOPin* pin) { this->output_gdo_pin_ = pin; }
void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; }
Result call_protocol(Args args);
void received(const DoorState door_state);
void received(const LightState light_state);
+1 -10
View File
@@ -192,16 +192,7 @@ namespace secplus2 {
this->door_command(action);
}
Result Secplus2::call(Args args)
{
using Tag = Args::Tag;
if (args.tag == Tag::query_status) {
this->send_command(CommandType::GET_STATUS);
} else if (args.tag == Tag::query_openings) {
this->send_command(CommandType::GET_OPENINGS);
}
return { };
}
void Secplus2::door_command(DoorAction action)
{
+3 -3
View File
@@ -81,7 +81,8 @@ namespace secplus2 {
void lock_action(LockAction action);
void door_action(DoorAction action);
Result call(Args args);
void query_status() override;
void query_openings() override;
protected:
void increment_rolling_code_counter(int delta = 1);
@@ -114,8 +115,7 @@ namespace secplus2 {
void door_command(DoorAction action);
void query_status();
void query_openings();
void print_packet(const esphome::LogString* prefix, const WirePacket& packet) const;
optional<Command> decode_packet(const WirePacket& packet) const;