From 1896d36996ab48ed7271e855d7e32b4e61f64896 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 23 Apr 2012 16:48:00 +0200 Subject: Polymorphic inheritance support --- odb/pointer-traits.hxx | 89 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 6 deletions(-) (limited to 'odb/pointer-traits.hxx') diff --git a/odb/pointer-traits.hxx b/odb/pointer-traits.hxx index fbfa721..a24bf02 100644 --- a/odb/pointer-traits.hxx +++ b/odb/pointer-traits.hxx @@ -47,7 +47,7 @@ namespace odb release () {p_ = 0;} void - reset (P p) {delete p_; p_ = p;} + reset (P p = 0) {delete p_; p_ = p;} private: P p_; @@ -68,6 +68,9 @@ namespace odb release () {} void + reset () {} + + void reset (const P&) {} }; @@ -111,14 +114,28 @@ namespace odb return p == 0; } - // Cast away constness. + // Casts. // static unrestricted_pointer_type - cast (pointer_type p) + const_pointer_cast (pointer_type p) { return const_cast (p); } + template + static T1* + static_pointer_cast (pointer_type p) + { + return static_cast (p); + } + + template + static T1* + dynamic_pointer_cast (pointer_type p) + { + return dynamic_cast (p); + } + public: // Allocate memory for an element that will be managed by this // pointer. @@ -174,9 +191,32 @@ namespace odb return p.get () == 0; } - // cast() is not provided since it transfers the ownership. + // const_pointer_cast() is not provided. // + // Note: transfers ownership. + // + template + static std::auto_ptr + static_pointer_cast (pointer_type& p) + { + return std::auto_ptr (static_cast (p.release ())); + } + + // Note: transfers ownership if successful. + // + template + static std::auto_ptr + dynamic_pointer_cast (pointer_type& p) + { + T1* p1 (dynamic_cast (p.get ())); + + if (p1 != 0) + p.release (); + + return std::auto_ptr (p1); + } + public: static void* allocate (std::size_t n) @@ -225,8 +265,31 @@ namespace odb return !p; } - // cast() is not provided since it transfers the ownership. + // const_pointer_cast() is not provided. + // + + // Note: transfers ownership. // + template + static std::unique_ptr + static_pointer_cast (pointer_type& p) + { + return std::unique_ptr (static_cast (p.release ())); + } + + // Note: transfers ownership if successful. + // + template + static std::unique_ptr + dynamic_pointer_cast (pointer_type& p) + { + T1* p1 (dynamic_cast (p.get ())); + + if (p1 != 0) + p.release (); + + return std::unique_ptr (p1); + } public: static void* @@ -279,11 +342,25 @@ namespace odb } static unrestricted_pointer_type - cast (const pointer_type& p) + const_pointer_cast (const pointer_type& p) { return std::const_pointer_cast (p); } + template + static std::shared_ptr + static_pointer_cast (const pointer_type& p) + { + return std::static_pointer_cast (p); + } + + template + static std::shared_ptr + dynamic_pointer_cast (const pointer_type& p) + { + return std::dynamic_pointer_cast (p); + } + public: static void* allocate (std::size_t n) -- cgit v1.1