// file : odb/session.txx // author : Boris Kolpackov // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file #include namespace odb { template typename session::object_position session:: insert (database_type& db, const typename object_traits::id_type& id, const typename object_traits::pointer_type& obj) { // T can be const T while object_type will always be T. // typedef typename object_traits::object_type object_type; typedef odb::object_traits object_traits; typedef typename odb::object_traits::pointer_type pointer_type; typedef odb::pointer_traits pointer_traits; type_map& tm (db_map_[&db]); details::shared_ptr& pom (tm[&typeid (object_type)]); if (!pom) pom.reset (new (details::shared) object_map); object_map& om ( static_cast&> (*pom)); typename object_map::value_type vt ( id, object_pointers ()); vt.second.set (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 to 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.set (obj); return object_position (om, r.first); } template typename object_traits::pointer_type session:: find (database_type& db, const typename object_traits::id_type& id) const { // T can be const T while object_type will always be T. // typedef typename object_traits::object_type object_type; 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 (object_type))); 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 (); pointer_type r; oi->second.get (r); return r; } template void session:: erase (database_type& db, const typename object_traits::id_type& id) { // T can be const T while object_type will always be T. // typedef typename object_traits::object_type object_type; 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 (object_type))); 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); } }