// file : odb/session.txx // copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file namespace odb { template typename session::cache_position session:: cache_insert (database_type& db, const typename object_traits::id_type& id, const typename object_traits::pointer_type& obj) { type_map& tm (db_map_[&db]); details::shared_ptr& pom (tm[&typeid (T)]); if (!pom) pom.reset (new (details::shared) object_map); object_map& om (static_cast&> (*pom)); typename object_map::value_type vt (id, obj); std::pair::iterator, bool> r (om.insert (vt)); // In what situation may we possibly attempt to reinsert the object? // For example, when the user loads the same object in two different // instances (i.e., load into a pre-allocated object). In this case // we should probably update our entries accordingly. // if (!r.second) r.first->second = obj; return cache_position (om, r.first); } template typename object_traits::pointer_type session:: cache_find (database_type& db, const typename object_traits::id_type& id) const { typedef typename object_traits::pointer_type pointer_type; database_map::const_iterator di (db_map_.find (&db)); if (di == db_map_.end ()) return pointer_type (); const type_map& tm (di->second); type_map::const_iterator ti (tm.find (&typeid (T))); if (ti == tm.end ()) return pointer_type (); const object_map& om (static_cast&> (*ti->second)); typename object_map::const_iterator oi (om.find (id)); if (oi == om.end ()) return pointer_type (); return oi->second; } template void session:: cache_erase (database_type& db, const typename object_traits::id_type& id) { database_map::iterator di (db_map_.find (&db)); if (di == db_map_.end ()) return; type_map& tm (di->second); type_map::iterator ti (tm.find (&typeid (T))); if (ti == tm.end ()) return; object_map& om (static_cast&> (*ti->second)); typename object_map::iterator oi (om.find (id)); if (oi == om.end ()) return; om.erase (oi); if (om.empty ()) tm.erase (ti); if (tm.empty ()) db_map_.erase (di); } }