From 2636e266dc7a048e52b40b668c460c2793e897c4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 18 Aug 2010 20:02:11 +0200 Subject: Move shared_ptr to the details namespace --- odb/details/shared-ptr/base.txx | 206 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 odb/details/shared-ptr/base.txx (limited to 'odb/details/shared-ptr/base.txx') diff --git a/odb/details/shared-ptr/base.txx b/odb/details/shared-ptr/base.txx new file mode 100644 index 0000000..fc30fdf --- /dev/null +++ b/odb/details/shared-ptr/base.txx @@ -0,0 +1,206 @@ +// file : odb/details/shared-ptr/base.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include +#include + +namespace odb +{ + namespace details + { + namespace bits + { + // Support for locating the counter in the memory block. + // + template ::r> + struct locator; + + template + struct locator + { + static std::size_t* + counter (X* x) + { + std::size_t* p (reinterpret_cast (x)); + + if (*(--p) != 0xDEADBEEF) + throw not_shared (); + + return --p; + } + }; + + template + struct locator + { + static std::size_t* + counter (X* x) + { + std::size_t* p ( + static_cast ( + dynamic_cast (x))); + + if (*(--p) != 0xDEADBEEF) + throw not_shared (); + + return --p; + } + }; + + template + std::size_t* + counter (const X* p) + { + return bits::locator::counter (const_cast (p)); + } + + // Counter type and operations. + // + meta::no test (...); + meta::yes test (shared_base*); + + template (0)))> + struct counter_type; + + template + struct counter_type + { + typedef X r; + }; + + template + struct counter_type + { + typedef shared_base r; + }; + + template + struct counter_ops; + + template + struct counter_ops + { + counter_ops (const X* p) : counter_ (p ? bits::counter (p) : 0) {} + counter_ops (const counter_ops& x) : counter_ (x.counter_) {} + + template + counter_ops (const counter_ops& x) : counter_ (x.counter_) {} + + counter_ops& + operator= (const counter_ops& x) + { + counter_ = x.counter_; + return *this; + } + + template + counter_ops& + operator= (const counter_ops& x) + { + counter_ = x.counter_; + return *this; + } + + void + reset (const X* p) + { + counter_ = p ? bits::counter (p) : 0; + } + + void + inc (X*) + { + (*counter_)++; + } + + void + dec (X* p) + { + if (--(*counter_) == 0) + { + p->~X (); + + // Counter is the top of the memory block. + // + operator delete (counter_); + } + } + + std::size_t + count (const X*) const + { + return *counter_; + } + + std::size_t* counter_; + }; + + template + struct counter_ops + { + counter_ops (const Y*) {} + counter_ops (const counter_ops&) {} + + template + counter_ops (const counter_ops&) {} + + counter_ops& + operator= (const counter_ops&) + { + return *this; + } + + template + counter_ops& + operator= (const counter_ops&) + { + return *this; + } + + void + reset (const Y*) {} + + void + inc (shared_base* p) {p->_inc_ref ();} + + void + dec (Y* p) + { + if (static_cast (p)->_dec_ref ()) + delete p; + } + + std::size_t + count (const shared_base* p) const {return p->_ref_count ();} + }; + } + + template + inline X* + inc_ref (X* p) + { + bits::counter_ops::r, X> c (p); + c.inc (p); + return p; + } + + template + inline void + dec_ref (X* p) + { + bits::counter_ops::r, X> c (p); + c.dec (p); + } + + template + inline std::size_t + ref_count (const X* p) + { + bits::counter_ops::r, X> c (p); + return c.count (p); + } + } +} -- cgit v1.1