firmware/src/Status.h

57 lines
1.5 KiB
C
Raw Normal View History

2020-06-29 01:17:52 +00:00
#pragma once
#include "Observer.h"
// Constants for the various status types, so we can tell subclass instances apart
#define STATUS_TYPE_BASE 0
#define STATUS_TYPE_POWER 1
#define STATUS_TYPE_GPS 2
#define STATUS_TYPE_NODE 3
namespace meshtastic
{
2023-01-21 13:34:29 +00:00
// A base class for observable status
class Status
{
protected:
// Allows us to observe an Observable
CallbackObserver<Status, const Status *> statusObserver =
CallbackObserver<Status, const Status *>(this, &Status::updateStatus);
bool initialized = false;
// Workaround for no typeid support
int statusType = 0;
public:
// Allows us to generate observable events
Observable<const Status *> onNewStatus;
// Enable polymorphism ?
virtual ~Status() = default;
Status()
2020-06-29 01:17:52 +00:00
{
2023-01-21 13:34:29 +00:00
if (!statusType) {
statusType = STATUS_TYPE_BASE;
2020-06-29 01:17:52 +00:00
}
2023-01-21 13:34:29 +00:00
}
2020-06-29 01:17:52 +00:00
2023-01-21 13:34:29 +00:00
// Prevent object copy/move
Status(const Status &) = delete;
Status &operator=(const Status &) = delete;
2020-06-29 01:17:52 +00:00
2023-01-21 13:34:29 +00:00
// Start observing a source of data
void observe(Observable<const Status *> *source) { statusObserver.observe(source); }
2020-06-29 01:17:52 +00:00
2023-01-21 13:34:29 +00:00
// Determines whether or not existing data matches the data in another Status instance
bool matches(const Status *otherStatus) const { return true; }
2020-06-29 01:17:52 +00:00
2023-01-21 13:34:29 +00:00
bool isInitialized() const { return initialized; }
2020-06-29 01:17:52 +00:00
2023-01-21 13:34:29 +00:00
int getStatusType() const { return statusType; }
2020-06-29 01:17:52 +00:00
2023-01-21 13:34:29 +00:00
// Called when the Observable we're observing generates a new notification
int updateStatus(const Status *newStatus) { return 0; }
2020-06-29 01:17:52 +00:00
};
2023-01-21 13:34:29 +00:00
}; // namespace meshtastic