/* $Id: singleton.hh 1726 2008-10-14 22:00:36Z awgn $ */ /* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Nicola Bonelli * ---------------------------------------------------------------------------- */ #ifndef MEYERS_SINGLETON_HH #define MEYERS_SINGLETON_HH #include // // My singleton implementation inspired to that of Meyers. It is thread safe // when compiled with g++ 4.0 or higher // #define SINGLETON_CTOR(x, ...) \ using generic::singleton::instance; \ x(const tag &abc, ## __VA_ARGS__) : generic::singleton(abc) #define VOLATILE_SINGLETON_CTOR(x, ...) \ using generic::singleton::instance; \ x(const tag &abc, ## __VA_ARGS__) : generic::singleton(abc) namespace generic { template struct singleton { template struct enable_volatile_if; template struct enable_volatile_if { typedef typename std::tr1::add_volatile::type type; }; template struct enable_volatile_if { typedef U type; }; private: singleton(); singleton(const singleton&); // noncopyable singleton &operator=(const singleton &); // noncopyable protected: ~singleton() {} struct tag { friend class singleton; private: tag() {} ~tag() {} }; public: singleton(const singleton::tag &) {} // singleton instance... // static typename enable_volatile_if::type & instance() { static typename enable_volatile_if::type _one_((tag())); return _one_; } // multitons... // template static typename enable_volatile_if::type & instance(const U &u = U()) { static typename enable_volatile_if::type _n_((tag()), u); return _n_; } template static typename enable_volatile_if::type & instance(const U &u = U(), const V &v = V()) { static typename enable_volatile_if::type _n_((tag()), u, v); return _n_; } template static typename enable_volatile_if::type & instance(const U &u = U(), const V &v = V(), const W &w = W()) { static typename enable_volatile_if::type _n_((tag()), u, v, w); return _n_; } template static typename enable_volatile_if::type & instance(const U &u = U(), const V &v = V(), const W &w = W(), const X &x = X()) { static typename enable_volatile_if::type _n_((tag()), u, v, w, x); return _n_; } template static typename enable_volatile_if::type & instance(const U &u = U(), const V &v = V(), const W &w = W(), const X &x = X(), const Y &y = Y()) { static typename enable_volatile_if::type _n_((tag()), u, v, w, x, y); return _n_; } template static typename enable_volatile_if::type & instance(const U &u = U(), const V &v = V(), const W &w = W(), const X &x = X(), const Y &y = Y(), const Z &z = Z()) { static typename enable_volatile_if::type _n_((tag()), u, v, w, x, y, z); return _n_; } }; } #endif /* MEYERS_SINGLETON_HH */