From c56f32eb5e0b88ef8297a691f5286ba4aa4aa8b4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 16 Aug 2010 11:45:23 +0200 Subject: Add pointer guard for naked pointer --- odb/pointer-traits.hxx | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'odb/pointer-traits.hxx') diff --git a/odb/pointer-traits.hxx b/odb/pointer-traits.hxx index a715a9d..26f8b90 100644 --- a/odb/pointer-traits.hxx +++ b/odb/pointer-traits.hxx @@ -16,6 +16,24 @@ namespace odb template class pointer_traits; + // No-op pointer guard for smart pointers. + // + template + class nop_guard + { + public: + nop_guard () {} + + explicit + nop_guard (P) {} + + void + release () {} + + void + reset (P) {} + }; + // Default implementation that should work for any sensible smart // pointer with one template argument (object type). The only // assumptions that we make are the availability of operator-> and @@ -28,6 +46,7 @@ namespace odb public: typedef T type; typedef P pointer; + typedef nop_guard guard; // Return underlying pointer, including NULL. // @@ -77,12 +96,33 @@ namespace odb // Specialization for naked pointer. // + template + class nptr_guard + { + public: + ~nptr_guard () {delete p_;} + nptr_guard (): p_ (0) {} + + explicit + nptr_guard (P p): p_ (p) {} + + void + release () {p_ = 0;} + + void + reset (P p) {delete p_; p_ = p;} + + private: + P p_; + }; + template class pointer_traits { public: typedef T type; typedef T* pointer; + typedef nptr_guard guard; static type* get_ptr (pointer p) @@ -126,6 +166,7 @@ namespace odb public: typedef T type; typedef odb::shared_ptr pointer; + typedef nop_guard guard; static type* get_ptr (const pointer& p) -- cgit v1.1