// file : common/session/custom/session.txx // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file #include template typename session::position session:: insert (odb::database&, const typename odb::object_traits::id_type& id, const typename odb::object_traits::pointer_type& obj) { typedef odb::object_traits object_traits; std::shared_ptr& pm (map_[&typeid (T)]); if (!pm) pm.reset (new object_map); object_map& m (static_cast&> (*pm)); typename object_map::value_type vt (id, object_state (obj)); std::pair::iterator, bool> r (m.insert (vt)); // We shall never try to re-insert the same object into the cache. // assert (r.second); return position (m, r.first); } template void session:: initialize (const position& p) { typedef typename odb::object_traits::pointer_type pointer_type; // Make a copy for change tracking. If our object model had a // polymorphic hierarchy, then we would have had to use a // virtual function-based mechanism (e.g., clone()) instead of // the copy constructor since for a polymorphic hierarchy all // the derived objects are stored as pointers to the root object. // p.pos_->second.orig = pointer_type (new T (*p.pos_->second.obj)); } template typename odb::object_traits::pointer_type session:: find (odb::database&, const typename odb::object_traits::id_type& id) const { typedef typename odb::object_traits::pointer_type pointer_type; type_map::const_iterator ti (map_.find (&typeid (T))); if (ti == map_.end ()) return pointer_type (); const object_map& m (static_cast&> (*ti->second)); typename object_map::const_iterator oi (m.find (id)); if (oi == m.end ()) return pointer_type (); return oi->second.obj; } template void session:: erase (odb::database&, const typename odb::object_traits::id_type& id) { type_map::iterator ti (map_.find (&typeid (T))); if (ti == map_.end ()) return; object_map& m (static_cast&> (*ti->second)); typename object_map::iterator oi (m.find (id)); if (oi == m.end ()) return; m.erase (oi); if (m.empty ()) map_.erase (ti); } template void session::object_map:: flush (odb::database& db) { for (typename object_map::iterator i (this->begin ()), e (this->end ()); i != e; ++i) { const T& obj (*i->second.obj); if (obj.changed (*i->second.orig)) { db.update (obj); i->second.flushed_ = true; } } } template void session::object_map:: mark () { for (typename object_map::iterator i (this->begin ()), e (this->end ()); i != e; ++i) { if (i->second.flushed_) { i->second.orig.reset (new T (*i->second.obj)); i->second.flushed_ = false; } } }