diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-07-23 15:25:44 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-07-23 15:25:44 +0200 |
commit | 8286ac511144e4f17d34eac9affb97e50646344a (patch) | |
tree | f1af7320d7b6be6be059216d0ad08ac7b4f73fd0 /libcutl/cutl/static-ptr.hxx | |
parent | a15cf65c44d5c224169c32ef5495b68c758134b7 (diff) |
Imported Upstream version 4.0.0upstream/4.0.0
Diffstat (limited to 'libcutl/cutl/static-ptr.hxx')
-rw-r--r-- | libcutl/cutl/static-ptr.hxx | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/libcutl/cutl/static-ptr.hxx b/libcutl/cutl/static-ptr.hxx new file mode 100644 index 0000000..6c907c8 --- /dev/null +++ b/libcutl/cutl/static-ptr.hxx @@ -0,0 +1,74 @@ +// file : cutl/static-ptr.hxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CUTL_STATIC_PTR_HXX +#define CUTL_STATIC_PTR_HXX + +#include <cstddef> // std::size_t + +namespace cutl +{ + // This class template implements Jerry Schwarz's static + // initialization technique commonly found in iostream + // implementations. + // + // The second template argument is used to make sure the + // instantiation of static_ptr is unique. + // + template <typename X, typename ID> + class static_ptr + { + public: + static_ptr () + { + if (count_ == 0) + x_ = new X; + + ++count_; + } + + ~static_ptr () + { + if (--count_ == 0) + delete x_; + } + + private: + static_ptr (static_ptr const&); + + static_ptr& + operator= (static_ptr const&); + + public: + X* + operator-> () const + { + return x_; + } + + X& + operator* () const + { + return *x_; + } + + X* + get () const + { + return x_; + } + + private: + static X* x_; + static std::size_t count_; + }; + + template <typename X, typename ID> + X* static_ptr<X, ID>::x_ = 0; + + template <typename X, typename ID> + std::size_t static_ptr<X, ID>::count_ = 0; +} + +#endif // CUTL_STATIC_PTR_HXX |