singleton - SingletonIntroductiondetail/singleton.hpp provides a way to access a Singleton of a class type. This is not a general Singleton solution! It is restricted in that the class type must have a default constructor. Synopsisnamespace details { namespace pool { template <typename T> class singleton_default { private: singleton_default(); public: typedef T object_type; static object_type & instance(); }; } // namespace pool } // namespace details Semantics
GuaranteesThe singleton instance is guaranteed to be constructed before main() begins, and destructed after main() ends. Furthermore, it is guaranteed to be constructed before the first call to singleton_default<T>::instance() is complete (even if called before main() begins). Thus, if there are not multiple threads running except within main(), and if all access to the singleton is restricted by mutexes, then this guarantee allows a thread-safe singleton. DetailsFor details on how we provide the guarantees above, see the comments in the header file. DependenciesNone. Future DirectionsThis header may be replaced by a Boost singleton library. Revised 05 December, 2006 Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com) Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |