summaryrefslogtreecommitdiff
path: root/libcult/cult/mm/bits/shptr.hxx
diff options
context:
space:
mode:
authorJörg Frings-Fürst <jff@merkur>2014-05-18 16:08:14 +0200
committerJörg Frings-Fürst <jff@merkur>2014-05-18 16:08:14 +0200
commita15cf65c44d5c224169c32ef5495b68c758134b7 (patch)
tree3419f58fc8e1b315ba8171910ee044c5d467c162 /libcult/cult/mm/bits/shptr.hxx
Imported Upstream version 3.3.0.2upstream/3.3.0.2
Diffstat (limited to 'libcult/cult/mm/bits/shptr.hxx')
-rw-r--r--libcult/cult/mm/bits/shptr.hxx85
1 files changed, 85 insertions, 0 deletions
diff --git a/libcult/cult/mm/bits/shptr.hxx b/libcult/cult/mm/bits/shptr.hxx
new file mode 100644
index 0000000..7089c53
--- /dev/null
+++ b/libcult/cult/mm/bits/shptr.hxx
@@ -0,0 +1,85 @@
+// file : cult/mm/bits/shptr.hxx
+// author : Boris Kolpackov <boris@kolpackov.net>
+// copyright : Copyright (c) 2005-2010 Boris Kolpackov
+// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+#ifndef CULT_MM_BITS_SHPTR_HXX
+#define CULT_MM_BITS_SHPTR_HXX
+
+#include <cult/types/fundamental.hxx>
+
+#include <cult/mm/counter.hxx>
+#include <cult/mm/exception.hxx>
+
+namespace Cult
+{
+ namespace MM
+ {
+ namespace Bits
+ {
+ template <typename X>
+ class Shptr
+ {
+ protected:
+ ~Shptr ()
+ {
+ if (c_ && c_->dec_ref ()) delete p_;
+ }
+
+ Shptr (X* p, Counter* c, Boolean inc = true)
+ : p_ (p), c_ (c)
+ {
+ if (c_ && inc) c_->inc_ref ();
+ }
+
+ protected:
+ X*
+ release_ () throw ()
+ {
+ X* tmp (p_);
+
+ c_ = 0;
+ p_ = 0;
+
+ return tmp;
+ }
+
+ protected:
+ template<typename y>
+ Void
+ assign (Shptr<y> const& bp, Boolean inc = true)
+ {
+ assign (bp.p_, bp.c_, inc);
+ }
+
+ Void
+ assign (X* p, Counter* c, Boolean inc)
+ {
+ if (c_ && c_ == c) throw SelfAssignment ();
+
+ if (c_)
+ {
+ Counter* t (c_);
+ c_ = 0;
+ p_ = 0;
+ t->dec_ref ();
+ }
+
+ if (c && inc) c->inc_ref ();
+
+ p_ = p;
+ c_ = c;
+ }
+
+ protected:
+ template <typename>
+ friend class Shptr;
+
+ X* p_;
+ Counter* c_;
+ };
+ }
+ }
+}
+
+#endif // CULT_MM_BITS_SHPTR_HXX