Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract the "generic" portions of DccAccConsumer into a "base" class #682

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions src/openlcb/DccAccyConsumer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@
namespace openlcb
{

class DccAccyConsumer : public SimpleEventHandler
/// Base (generic protocol) implementation of a DCC accessory consumer.
class DccAccyConsumerBase : public SimpleEventHandler
{
public:
protected:
/// Constructs a listener for DCC accessory control.
/// @param node is the virtual node that will be listening for events and
/// responding to Identify messages.
/// @param track is the interface through which we will be writing DCC
/// accessory packets.
DccAccyConsumer(Node *node, dcc::TrackIf *track)
DccAccyConsumerBase(Node *node)
: node_(node)
, track_(track)
{
EventRegistry::instance()->register_handler(
EventRegistryEntry(
Expand All @@ -67,7 +65,8 @@ public:
memset(isStateKnown_, 0, sizeof(isStateKnown_));
}

~DccAccyConsumer()
/// Destructor.
~DccAccyConsumerBase()
{
EventRegistry::instance()->unregister_handler(this);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably add a public: after this so that the virtual public interafce methods of the simpleeventhandler do not have their visibility changed. Then another protected: before the class variables.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree that this is required. As long as the virtual methods are declared public in the SimpleEventHandler class, they can always be called on that object type. We don't want anyone calling these methods on the DccAccyConsumerBase object type, so they should not be public.

Expand Down Expand Up @@ -111,11 +110,7 @@ public:
lastSetState_[eventOfs_] &= ~m;
}

dcc::TrackIf::message_type *pkt;
mainBufferPool->alloc(&pkt);
pkt->data()->add_dcc_basic_accessory(dccAddress_, onOff_);
pkt->data()->packet_header.rept_count = 3;
track_->send(pkt);
send_accy_command();
}

void handle_identify_consumer(const EventRegistryEntry &entry,
Expand Down Expand Up @@ -156,7 +151,9 @@ public:
done->new_child());
}

private:
/// Send the actual accessory command.
virtual void send_accy_command() = 0;

/// Parses an event into an openlcb accessory offset.
/// @return true if the event is in the accessory range, false if this
/// event can be ignored.
Expand Down Expand Up @@ -216,6 +213,39 @@ private:

/// OpenLCB node to export the consumer on.
Node *node_;
};

/// Specialized (DCC protocol) implementation of a DCC accessory consumer.
class DccAccyConsumer : public DccAccyConsumerBase
{
public:
/// Constructs a listener for DCC accessory control.
/// @param node is the virtual node that will be listening for events and
/// responding to Identify messages.
/// @param track is the interface through which we will be writing DCC
/// accessory packets.
DccAccyConsumer(Node *node, dcc::TrackIf *track)
: DccAccyConsumerBase(node)
, track_(track)
{
}

/// Destructor.
~DccAccyConsumer()
{
}

private:
/// Send the actual accessory command.
void send_accy_command() override
{
dcc::TrackIf::message_type *pkt;
mainBufferPool->alloc(&pkt);
pkt->data()->add_dcc_basic_accessory(dccAddress_, onOff_);
pkt->data()->packet_header.rept_count = 3;
track_->send(pkt);
}

/// Track to send DCC packets to.
dcc::TrackIf *track_;
};
Expand Down