From 8650229ad4245e4d684c9c28653b7c31d5f1c19f Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 2 Oct 2019 20:54:10 +1000 Subject: [PATCH] AP_ADSB: added singleton interface --- libraries/AP_ADSB/AP_ADSB.cpp | 18 ++++++++++++++++++ libraries/AP_ADSB/AP_ADSB.h | 19 +++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/libraries/AP_ADSB/AP_ADSB.cpp b/libraries/AP_ADSB/AP_ADSB.cpp index b815aefb9a..28fcaed8f1 100644 --- a/libraries/AP_ADSB/AP_ADSB.cpp +++ b/libraries/AP_ADSB/AP_ADSB.cpp @@ -50,6 +50,8 @@ extern const AP_HAL::HAL& hal; +AP_ADSB *AP_ADSB::_singleton; + // table of user settable parameters const AP_Param::GroupInfo AP_ADSB::var_info[] = { // @Param: ENABLE @@ -159,6 +161,16 @@ const AP_Param::GroupInfo AP_ADSB::var_info[] = { AP_GROUPEND }; +// constructor +AP_ADSB::AP_ADSB() +{ + AP_Param::setup_object_defaults(this, var_info); + if (_singleton != nullptr) { + AP_HAL::panic("AP_ADSB must be singleton"); + } + _singleton = this; +} + /* * Initialize variables and allocate memory for array */ @@ -940,3 +952,9 @@ void AP_ADSB::write_log(const adsb_vehicle_t &vehicle) }; AP::logger().WriteBlock(&pkt, sizeof(pkt)); } + +AP_ADSB *AP::ADSB() +{ + return AP_ADSB::get_singleton(); +} + diff --git a/libraries/AP_ADSB/AP_ADSB.h b/libraries/AP_ADSB/AP_ADSB.h index 17fcc9d5e3..954970bedf 100644 --- a/libraries/AP_ADSB/AP_ADSB.h +++ b/libraries/AP_ADSB/AP_ADSB.h @@ -29,10 +29,8 @@ class AP_ADSB { public: - AP_ADSB() - { - AP_Param::setup_object_defaults(this, var_info); - } + // constructor + AP_ADSB(); /* Do not allow copies */ AP_ADSB(const AP_ADSB &other) = delete; @@ -88,7 +86,14 @@ public: // confirm a value is a valid callsign static bool is_valid_callsign(uint16_t octal) WARN_IF_UNUSED; + // get singleton instance + static AP_ADSB *get_singleton(void) { + return _singleton; + } + private: + static AP_ADSB *_singleton; + // initialize _vehicle_list void init(); @@ -202,4 +207,10 @@ private: SPECIAL_ONLY = 1, ALL = 2 }; + + +}; + +namespace AP { + AP_ADSB *ADSB(); };