From 623c1aa3cd2da542befb836d7f3b45cba1d7e3c8 Mon Sep 17 00:00:00 2001 From: Peter Hall <33176108+IamPete1@users.noreply.github.com> Date: Mon, 25 Nov 2019 16:21:31 +0000 Subject: [PATCH] AP_Param: add singleton --- libraries/AP_Param/AP_Param.cpp | 11 +++++++++++ libraries/AP_Param/AP_Param.h | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/libraries/AP_Param/AP_Param.cpp b/libraries/AP_Param/AP_Param.cpp index 71e22a586b..f2abcc13ec 100644 --- a/libraries/AP_Param/AP_Param.cpp +++ b/libraries/AP_Param/AP_Param.cpp @@ -38,6 +38,9 @@ extern const AP_HAL::HAL &hal; uint16_t AP_Param::sentinal_offset; +// singleton instance +AP_Param *AP_Param::_singleton; + #define ENABLE_DEBUG 1 #if ENABLE_DEBUG @@ -379,6 +382,14 @@ bool AP_Param::get_base(const struct Info &info, ptrdiff_t &base) return true; } +namespace AP { + +AP_Param *param() +{ + return AP_Param::get_singleton(); +} + +} // find the info structure given a header and a group_info table // return the Info structure and a pointer to the variables storage diff --git a/libraries/AP_Param/AP_Param.h b/libraries/AP_Param/AP_Param.h index 86f84aac91..9c892cc912 100644 --- a/libraries/AP_Param/AP_Param.h +++ b/libraries/AP_Param/AP_Param.h @@ -197,6 +197,11 @@ public: uint16_t i; for (i=0; info[i].type != AP_PARAM_NONE; i++) ; _num_vars = i; + + if (_singleton != nullptr) { + AP_HAL::panic("AP_Param must be singleton"); + } + _singleton = this; } // empty constructor @@ -479,7 +484,11 @@ public: AP_HAL::BetterStream *port); #endif // AP_PARAM_KEY_DUMP + static AP_Param *get_singleton() { return _singleton; } + private: + static AP_Param *_singleton; + /// EEPROM header /// /// This structure is placed at the head of the EEPROM to indicate @@ -673,6 +682,10 @@ private: void save_io_handler(void); }; +namespace AP { + AP_Param *param(); +}; + /// Template class for scalar variables. /// /// Objects of this type have a value, and can be treated in many ways as though they