X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fsingleton.hh;h=ec0d01e9997fbce426c4f41a460e00540f81c66f;hp=9cbb385b2b0340f66c57afd6ee94c284b76db65d;hb=7d15b919681bb9ec0088b4b27c6abf62d6dfb9b1;hpb=79b5f738f2e38acb60cda7e09f54802933a17105 diff --git a/src/singleton.hh b/src/singleton.hh index 9cbb385..ec0d01e 100644 --- a/src/singleton.hh +++ b/src/singleton.hh @@ -32,47 +32,60 @@ #include +namespace dc { + + template class singleton { - static T* ptr; + static T* ptr_; public: + struct exception : public std::runtime_error + { + explicit exception(const std::string& what_arg) : + std::runtime_error(what_arg) {} + }; + singleton() { - if (!ptr) + if (!ptr_) { // This hack is from Game Programming Gems. long long offset = (long long)(T*)1 - (long long)(singleton*)(T*)1; - ptr = (T*)((long long)this + offset); + ptr_ = (T*)((long long)this + offset); } } ~singleton() { long long offset = (long long)(T*)1 - (long long)(singleton*)(T*)1; - if (ptr == (T*)((long long)this + offset)) + if (ptr_ == (T*)((long long)this + offset)) { - ptr = 0; + ptr_ = 0; } } static T& instance() { - if (!ptr) + if (!ptr_) { - throw std::runtime_error("accessing uninstantiated singleton"); + throw exception("accessing uninstantiated singleton"); } - return *ptr; + return *ptr_; } - static T* instancePtr() + static T* instance_ptr() { - return ptr; + return ptr_; } }; -template T* singleton::ptr = 0; +template T* singleton::ptr_ = 0; + +} // namespace dc #endif // _SINGLETON_HH_ +/** vim: set ts=4 sw=4 tw=80: *************************************************/ +