diff --git a/singleton.hpp b/singleton.hpp index bbd546c9..6b8daf56 100644 --- a/singleton.hpp +++ b/singleton.hpp @@ -49,6 +49,22 @@ namespace cruft { } + /// Swap the stored object pointer for the provided one and return + /// the old value. + /// + /// Useful for bypassing `instantiate` by initialising the pointer + /// to a pre-existing object. + /// + /// The caller to swap must free any resources associated with the + /// prior value and arrange for the new object to be safely destructed + /// in the future. + static SelfT* + swap (SelfT *next) + { + return std::exchange (instance, next); + } + + /// returns a reference to sole instantiated value /// /// `instantiate` must have already been called before `get` is called.