objdb.h

Go to the documentation of this file.
00001 /** @file nub/objdb.h singleton repository that associates each
00002     nub::object with its nub::uid, so that objects can be looked up by
00003     id -- this provides the foundation for using uids as object
00004     handles in a scripting language */
00005 
00006 ///////////////////////////////////////////////////////////////////////
00007 //
00008 // Copyright (c) 1999-2004 California Institute of Technology
00009 // Copyright (c) 2004-2007 University of Southern California
00010 // Rob Peters <rjpeters at usc dot edu>
00011 //
00012 // created: Sun Nov 21 00:26:29 1999
00013 // commit: $Id: objdb.h 8249 2007-04-12 06:03:40Z rjpeters $
00014 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/nub/objdb.h $
00015 //
00016 // --------------------------------------------------------------------
00017 //
00018 // This file is part of GroovX.
00019 //   [http://ilab.usc.edu/rjpeters/groovx/]
00020 //
00021 // GroovX is free software; you can redistribute it and/or modify it
00022 // under the terms of the GNU General Public License as published by
00023 // the Free Software Foundation; either version 2 of the License, or
00024 // (at your option) any later version.
00025 //
00026 // GroovX is distributed in the hope that it will be useful, but
00027 // WITHOUT ANY WARRANTY; without even the implied warranty of
00028 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00029 // General Public License for more details.
00030 //
00031 // You should have received a copy of the GNU General Public License
00032 // along with GroovX; if not, write to the Free Software Foundation,
00033 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00034 //
00035 ///////////////////////////////////////////////////////////////////////
00036 
00037 #ifndef GROOVX_NUB_OBJDB_H_UTC20050626084019_DEFINED
00038 #define GROOVX_NUB_OBJDB_H_UTC20050626084019_DEFINED
00039 
00040 #include "nub/uid.h"
00041 
00042 #include "rutz/error.h"
00043 #include "rutz/iter.h"
00044 
00045 namespace nub
00046 {
00047   class object;
00048   class objectdb;
00049   class invalid_uid_error;
00050 }
00051 
00052 /**
00053  *
00054  * nub::invalid_uid_error is an exception class that will be thrown from
00055  * objectdb if an attempt to use an invalid id is made in a checked
00056  * function.
00057  *
00058  **/
00059 
00060 class nub::invalid_uid_error : public rutz::error
00061 {
00062 public:
00063   /// Constructor.
00064   invalid_uid_error(nub::uid id, const rutz::file_pos& pos);
00065 
00066   /// Virtual destructor.
00067   virtual ~invalid_uid_error() throw();
00068 };
00069 
00070 ///////////////////////////////////////////////////////////////////////
00071 /**
00072  *
00073  * objectdb is a database for storing nub::object objects, which can
00074  * be accessed by their nub::uid values. Most clients will not need to
00075  * use the objectdb directly, but can instead use the nub::ref and
00076  * nub::soft_ref smart pointers.
00077  *
00078  **/
00079 ///////////////////////////////////////////////////////////////////////
00080 
00081 class nub::objectdb
00082 {
00083 protected:
00084   /// Default constructor makes an empty list.
00085   objectdb();
00086 
00087 public:
00088   /// Virtual destructor.
00089   virtual ~objectdb();
00090 
00091   /// Returns the singleton instance of objectdb.
00092   static objectdb& instance();
00093 
00094   class impl;
00095 
00096   //
00097   // Iterators
00098   //
00099 
00100   typedef rutz::fwd_iter<object* const> iterator;
00101 
00102   iterator objects() const;
00103 
00104   /// A filtering iterator class; only exposes objects matching a given type.
00105   template <class T>
00106   class casting_iterator
00107   {
00108     iterator m_itr;
00109 
00110     void advance_to_valid()
00111     {
00112       while (!m_itr.at_end() && (dynamic_cast<T*>(*m_itr)==0))
00113         ++m_itr;
00114     }
00115 
00116   public:
00117     casting_iterator(const iterator& begin) : m_itr(begin)
00118     { advance_to_valid(); }
00119 
00120     casting_iterator& operator++() { ++m_itr; advance_to_valid(); return *this; }
00121 
00122     bool at_end() const { return m_itr.at_end(); }
00123     bool is_valid() const { return m_itr.is_valid(); }
00124 
00125     T* operator*() const { return &(dynamic_cast<T&>(**m_itr)); }
00126 
00127     T* operator->() const { return operator*(); }
00128   };
00129 
00130   //
00131   // Collection interface
00132   //
00133 
00134   /// Returns the number of valid objects in the database.
00135   int count() const throw();
00136 
00137   /// Returns true if 'id' is a valid uid.
00138   bool is_valid_uid(nub::uid id) const throw();
00139 
00140   /// Releases the object specified by \a id, but only if it is unshared.
00141   /** This causes the object to be destroyed since it was unshared. If
00142       the object is shared, this operation throws an exception. */
00143   void remove(nub::uid id);
00144 
00145   /// Removes reference to the object with uid \a id.
00146   void release(nub::uid id);
00147 
00148   /// Releases all unshared objects held in the database.
00149   /** Since the objects are unshared, they will be destroyed in the
00150       process. */
00151   void purge();
00152 
00153   /// Calls \c purge() repeatedly until no more items can be removed.
00154   /** This will get rid of items that were only referenced by other
00155       items in the list. */
00156   void clear();
00157 
00158   /// WARNING: should only be called during program exit.
00159   /** Does a full clear of all objects held by the objectdb. This breaks
00160       the usual semantics of objectdb, since it removes both shared and
00161       unshared objects. */
00162   void clear_on_exit();
00163 
00164   /// Return the \c nub::object* with the uid given by \a id.
00165   /** Checks first if \a id is a valid uid, and throws an \c
00166       nub::invalid_uid_error if it is not. */
00167   nub::object* get_checked_obj(nub::uid id) throw (nub::invalid_uid_error);
00168 
00169   /// Insert a strong reference to obj into the database.
00170   void insert_obj(nub::object* obj);
00171 
00172   /// Insert a weak reference to obj into the database.
00173   void insert_obj_weak(nub::object* obj);
00174 
00175 private:
00176   objectdb(const objectdb&);
00177   objectdb& operator=(const objectdb&);
00178 
00179   impl* const rep;
00180 };
00181 
00182 static const char __attribute__((used)) vcid_groovx_nub_objdb_h_utc20050626084019[] = "$Id: objdb.h 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/nub/objdb.h $";
00183 #endif // !GROOVX_NUB_OBJDB_H_UTC20050626084019_DEFINED
Generated on Sun May 8 08:41:07 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3