From b366c3176462d470e445bc8aeb93a13df87e0518 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 7 Jan 2013 12:51:22 +0200 Subject: Cleanup object cache position type --- odb/cache-traits.hxx | 22 ++++++++++++++++------ odb/session.hxx | 18 +++++++++++------- odb/session.ixx | 2 +- odb/session.txx | 4 ++-- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/odb/cache-traits.hxx b/odb/cache-traits.hxx index c0c1272..6a6741e 100644 --- a/odb/cache-traits.hxx +++ b/odb/cache-traits.hxx @@ -27,7 +27,17 @@ namespace odb typedef odb::pointer_traits pointer_traits; typedef typename pointer_traits::element_type object_type; typedef typename object_traits::id_type id_type; - typedef session::object_position position_type; + + struct position_type + { + typedef session::position base_type; + + position_type (): empty_ (true) {} + position_type (const base_type& pos): empty_ (false), pos_ (pos) {} + + bool empty_; + base_type pos_; + }; struct insert_guard { @@ -35,13 +45,13 @@ namespace odb insert_guard (const position_type& pos): pos_ (pos) {} ~insert_guard () {erase (pos_);} - position_type + const position_type& position () const {return pos_;} void - release () {pos_.map_ = 0;} + release () {pos_.empty_ = true;} - // Note: doesn't call erase() on the old position (assumes not set). + // Note: doesn't call erase() on the old position (assumes empty). // void reset (const position_type& pos) {pos_ = pos;} @@ -94,8 +104,8 @@ namespace odb static void erase (const position_type& p) { - if (p.map_ != 0) - session::current ().erase (p); + if (!p.empty_) + session::current ().erase (p.pos_); } }; diff --git a/odb/session.hxx b/odb/session.hxx index cfeebce..f9c8739 100644 --- a/odb/session.hxx +++ b/odb/session.hxx @@ -83,22 +83,26 @@ namespace odb // Object cache. // public: + // Position in the cache of an inserted element. The requirements + // for this class template are: default and copy-constructible and + // copy-assignable. In particular, a standard iterator can be used + // as a position. + // template - struct object_position + struct position { - typedef T object_type; - typedef object_map map; + typedef object_map map; typedef typename map::iterator iterator; - object_position (): map_ (0) {} - object_position (map& m, const iterator& p): map_ (&m), pos_ (p) {} + position () {} + position (map& m, const iterator& p): map_ (&m), pos_ (p) {} map* map_; iterator pos_; }; template - object_position + position insert (database_type&, const typename object_traits::id_type&, const typename object_traits::pointer_type&); @@ -113,7 +117,7 @@ namespace odb template void - erase (const object_position&); + erase (const position&); protected: typedef std::map inline void session:: - erase (const object_position& p) + erase (const position& p) { // @@ Empty maps are not cleaned up by this version of erase. // diff --git a/odb/session.txx b/odb/session.txx index ce13970..42aa965 100644 --- a/odb/session.txx +++ b/odb/session.txx @@ -7,7 +7,7 @@ namespace odb { template - typename session::object_position session:: + typename session::position session:: insert (database_type& db, const typename object_traits::id_type& id, const typename object_traits::pointer_type& obj) @@ -33,7 +33,7 @@ namespace odb if (!r.second) r.first->second = obj; - return object_position (om, r.first); + return position (om, r.first); } template -- cgit v1.1