readobjectmap.h

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 2003-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Wed Jun 25 13:13:18 2003
00011 // commit: $Id: readobjectmap.h 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/io/readobjectmap.h $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00034 
00035 #ifndef GROOVX_IO_READOBJECTMAP_H_UTC20050626084021_DEFINED
00036 #define GROOVX_IO_READOBJECTMAP_H_UTC20050626084021_DEFINED
00037 
00038 #include "nub/uid.h"
00039 
00040 #include "rutz/sfmt.h"
00041 
00042 #include <map>
00043 
00044 namespace rutz
00045 {
00046   class fstring;
00047 }
00048 
00049 namespace nub
00050 {
00051   template <class T> class ref;
00052 }
00053 
00054 namespace io
00055 {
00056   class serializable;
00057 
00058   class object_map
00059   {
00060   private:
00061     typedef std::map<nub::uid, nub::ref<io::serializable> > map_type;
00062     map_type m_objects;
00063 
00064   public:
00065     inline
00066     object_map();
00067 
00068     // This returns the object for the given id; the object must
00069     // already have been created, otherwise an exception will be
00070     // thrown.
00071     inline
00072     nub::ref<io::serializable> get_existing_object(nub::uid id);
00073 
00074     // This will create an object for the id if one has not yet been
00075     // created, then return the object for that id.
00076     inline
00077     nub::ref<io::serializable> fetch_object(const rutz::fstring& type, nub::uid id);
00078 
00079     inline
00080     void add_object_for_id(nub::uid id, nub::ref<io::serializable> object);
00081 
00082     inline
00083     void clear();
00084   };
00085 }
00086 
00087 #include "io/io.h"
00088 
00089 #include "nub/objmgr.h"
00090 #include "nub/ref.h"
00091 
00092 #include "rutz/error.h"
00093 #include "rutz/fstring.h"
00094 
00095 inline
00096 io::object_map::object_map() : m_objects() {}
00097 
00098 inline
00099 nub::ref<io::serializable>
00100 io::object_map::get_existing_object(nub::uid id)
00101 {
00102   map_type::const_iterator itr = m_objects.find(id);
00103   if ( itr == m_objects.end() )
00104     {
00105       throw rutz::error(rutz::sfmt("no object was found "
00106                                    "for the given id: %lu", id),
00107                         SRC_POS);
00108     }
00109 
00110   return (*itr).second;
00111 }
00112 
00113 inline
00114 nub::ref<io::serializable>
00115 io::object_map::fetch_object(const rutz::fstring& type, nub::uid id)
00116 {
00117   map_type::const_iterator itr = m_objects.find(id);
00118 
00119   if ( itr == m_objects.end() )
00120     {
00121       nub::ref<io::serializable> obj
00122         (nub::obj_mgr::new_typed_obj<io::serializable>(type));
00123 
00124       m_objects.insert(map_type::value_type(id, obj));
00125 
00126       return obj;
00127     }
00128 
00129   return (*itr).second;
00130 }
00131 
00132 inline
00133 void io::object_map::add_object_for_id(nub::uid id,
00134                                        nub::ref<io::serializable> object)
00135 {
00136   map_type::const_iterator itr = m_objects.find(id);
00137 
00138   // See if an object has already been created for this id
00139   if ( itr != m_objects.end() )
00140     {
00141       const rutz::fstring msg =
00142         rutz::sfmt("object has already been created\n"
00143                    "\ttype: %s\n"
00144                    "\tid: %lu",
00145                    object->obj_typename().c_str(), id);
00146       throw rutz::error(msg, SRC_POS);
00147     }
00148 
00149   m_objects.insert(map_type::value_type(id, object));
00150 }
00151 
00152 inline
00153 void io::object_map::clear()
00154 {
00155   m_objects.clear();
00156 }
00157 
00158 static const char __attribute__((used)) vcid_groovx_io_readobjectmap_h_utc20050626084021[] = "$Id: readobjectmap.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00159 #endif // !GROOVX_IO_READOBJECTMAP_H_UTC20050626084021_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:39 2008 by Doxygen version 1.5.5.