diff --git a/libraries/AP_Vehicle/AP_Vehicle.cpp b/libraries/AP_Vehicle/AP_Vehicle.cpp new file mode 100644 index 0000000000..eb5b474184 --- /dev/null +++ b/libraries/AP_Vehicle/AP_Vehicle.cpp @@ -0,0 +1,18 @@ +#include + +AP_Vehicle *AP_Vehicle::_singleton = nullptr; + +AP_Vehicle *AP_Vehicle::get_singleton() +{ + return _singleton; +} + +namespace AP { + +AP_Vehicle *vehicle() +{ + return AP_Vehicle::get_singleton(); +} + +}; + diff --git a/libraries/AP_Vehicle/AP_Vehicle.h b/libraries/AP_Vehicle/AP_Vehicle.h index 9c8fd4e0c3..308cde357f 100644 --- a/libraries/AP_Vehicle/AP_Vehicle.h +++ b/libraries/AP_Vehicle/AP_Vehicle.h @@ -36,7 +36,18 @@ class AP_Vehicle : public AP_HAL::HAL::Callbacks { public: - AP_Vehicle() {} + AP_Vehicle() { + if (_singleton) { + AP_HAL::panic("Too many Vehicles"); + } + _singleton = this; + } + + /* Do not allow copies */ + AP_Vehicle(const AP_Vehicle &other) = delete; + AP_Vehicle &operator=(const AP_Vehicle&) = delete; + + static AP_Vehicle *get_singleton(); /* common parameters for fixed wing aircraft @@ -121,6 +132,14 @@ protected: // false disables external leds) AP_Notify notify; +private: + + static AP_Vehicle *_singleton; + +}; + +namespace AP { + AP_Vehicle *vehicle(); }; extern const AP_HAL::HAL& hal;