aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2014-11-20 15:46:21 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2014-11-20 15:46:21 +0200
commit6b51de56026cf73f0f0ed8071e5e4918bc1999f9 (patch)
treef2ea080ebebbe14714710378e5347a995f13272d
parent558de908345af7680444cab46ffb6080b1000a06 (diff)
Add test
-rw-r--r--odb/database.hxx4
-rw-r--r--odb/database.ixx7
-rw-r--r--odb/database.txx58
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 T>
typename object_traits<T>::id_type
+ persist (const T& object);
+
+ template <typename T>
+ typename object_traits<T>::id_type
persist (T* obj_ptr);
template <typename T, template <typename> 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 <typename T>
inline typename object_traits<T>::id_type database::
+ persist (const T& obj)
+ {
+ return persist_<const T, id_common> (obj);
+ }
+
+ template <typename T>
+ inline typename object_traits<T>::id_type database::
persist (T* p)
{
typedef typename object_traits<T>::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 <typename T, bool = object_traits<T>::auto_id> struct persist_type;
+ template <typename T> struct persist_type<T, true> {typedef T type;};
+ template <typename T> struct persist_type<T, false> {typedef const T type;};
+
template <typename I, typename T, database_id DB>
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<object_type>::type* p (&(*b++));
+ a[n] = const_cast<T*> (p);
+ }
- object_traits::persist (*this, a, n, mex);
+ object_traits::persist (
+ *this,
+ const_cast<typename persist_type<object_type>::type**> (a),
+ n,
+ mex);
if (mex.fatal ())
break;
@@ -125,28 +136,25 @@ namespace odb
}
}
- namespace details
+ template <typename P>
+ struct pointer_copy
{
- template <typename P>
- struct pointer_copy
- {
- const P* ref;
- P copy;
+ const P* ref;
+ P copy;
- void assign (const P& p) {ref = &p;}
- template <typename P1> 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 = &copy;
- }
- };
- }
+ void assign (const P& p) {ref = &p;}
+ template <typename P1> 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 = &copy;
+ }
+ };
template <typename I, typename T, database_id DB>
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<pointer_type> p[object_traits::batch];
+ typename persist_type<object_type>::type* a[object_traits::batch];
+ pointer_copy<pointer_type> p[object_traits::batch];
for (; b != e && n < object_traits::batch; ++n)
{