1 #ifndef SEAFILE_CLIENT_UTILS_SINGLETON_H 2 #define SEAFILE_CLIENT_UTILS_SINGLETON_H 3 4 /** 5 * This macro helps conveniently define singleton classes. Usage: 6 * 7 * // foo.h 8 * #include "utils/singleton.h" 9 * class Foo { 10 * SINGLETON_DEFINE(Foo) 11 * private: 12 * Foo() 13 * ... 14 * } 15 16 * // foo.cpp 17 * #include "foo.h" 18 * SINGLETON_IMPL(Foo) 19 */ 20 21 #define SINGLETON_DEFINE(CLASS) \ 22 public: \ 23 static CLASS *instance(); \ 24 private: \ 25 static CLASS *singleton_; \ 26 27 #define SINGLETON_IMPL(CLASS) \ 28 CLASS* CLASS::singleton_; \ 29 CLASS* CLASS::instance() { \ 30 if (singleton_ == NULL) { \ 31 static CLASS instance; \ 32 singleton_ = &instance; \ 33 } \ 34 return singleton_; \ 35 } 36 37 #endif // SEAFILE_CLIENT_UTILS_SINGLETON_H 38