From ed8db05fed10caeec880961c2bc6b9e27e3980fb Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 1 May 2019 12:31:08 +1000 Subject: [PATCH] singleton: add swap modifier --- singleton.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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.