From 6b51de56026cf73f0f0ed8071e5e4918bc1999f9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 20 Nov 2014 15:46:21 +0200 Subject: Add test --- odb/database.hxx | 4 ++++ odb/database.ixx | 7 +++++++ odb/database.txx | 58 ++++++++++++++++++++++++++++++++------------------------ 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/odb/database.hxx b/odb/database.hxx index 0304d3c..3163c46 100644 --- a/odb/database.hxx +++ b/odb/database.hxx @@ -56,6 +56,10 @@ namespace odb template typename object_traits::id_type + persist (const T& object); + + template + typename object_traits::id_type persist (T* obj_ptr); template class P> diff --git a/odb/database.ixx b/odb/database.ixx index 57828a4..7c4b322 100644 --- a/odb/database.ixx +++ b/odb/database.ixx @@ -162,6 +162,13 @@ namespace odb template inline typename object_traits::id_type database:: + persist (const T& obj) + { + return persist_ (obj); + } + + template + inline typename object_traits::id_type database:: persist (T* p) { typedef typename object_traits::pointer_type object_pointer; diff --git a/odb/database.txx b/odb/database.txx index 81791db..ba10891 100644 --- a/odb/database.txx +++ b/odb/database.txx @@ -71,6 +71,10 @@ namespace odb return object_traits::id (obj); } + template ::auto_id> struct persist_type; + template struct persist_type {typedef T type;}; + template struct persist_type {typedef const T type;}; + template void database:: persist_ (I b, I e, details::meta::no /*ptr*/) @@ -86,12 +90,19 @@ namespace odb while (b != e) { std::size_t n (0); - object_type* a[object_traits::batch]; + T* a[object_traits::batch]; // T instead of persist_type for cache. for (; b != e && n < object_traits::batch; ++n) - a[n] = &(*b++); + { + typename persist_type::type* p (&(*b++)); + a[n] = const_cast (p); + } - object_traits::persist (*this, a, n, mex); + object_traits::persist ( + *this, + const_cast::type**> (a), + n, + mex); if (mex.fatal ()) break; @@ -125,28 +136,25 @@ namespace odb } } - namespace details + template + struct pointer_copy { - template - struct pointer_copy - { - const P* ref; - P copy; + const P* ref; + P copy; - void assign (const P& p) {ref = &p;} - template void assign (const P1& p1) - { - // The passed pointer should be the same or implicit-convertible - // to the object pointer. This way we make sure the object pointer - // does not assume ownership of the passed object. - // - const P& p (p1); - - copy = p; - ref = © - } - }; - } + void assign (const P& p) {ref = &p;} + template void assign (const P1& p1) + { + // The passed pointer should be the same or implicit-convertible + // to the object pointer. This way we make sure the object pointer + // does not assume ownership of the passed object. + // + const P& p (p1); + + copy = p; + ref = © + } + }; template void database:: @@ -165,8 +173,8 @@ namespace odb while (b != e) { std::size_t n (0); - object_type* a[object_traits::batch]; - details::pointer_copy p[object_traits::batch]; + typename persist_type::type* a[object_traits::batch]; + pointer_copy p[object_traits::batch]; for (; b != e && n < object_traits::batch; ++n) { -- cgit v1.1