objpkg.cc

Go to the documentation of this file.
00001 /** @file tcl/objpkg.cc tcl package helpers for defining basic
00002     class-level tcl commands (e.g. find all objects of a given type) */
00003 ///////////////////////////////////////////////////////////////////////
00004 //
00005 // Copyright (c) 2002-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Fri Nov 22 17:06:50 2002
00010 // commit: $Id: objpkg.cc 11876 2009-10-22 15:53:06Z icore $
00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/objpkg.cc $
00012 //
00013 // This file is part of GroovX
00014 //   [http://ilab.usc.edu/rjpeters/groovx/]
00015 //
00016 // GroovX is free software; you can redistribute it and/or modify it
00017 // under the terms of the GNU General Public License as published by
00018 // the Free Software Foundation; either version 2 of the License, or
00019 // (at your option) any later version.
00020 //
00021 // GroovX is distributed in the hope that it will be useful, but
00022 // WITHOUT ANY WARRANTY; without even the implied warranty of
00023 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024 // General Public License for more details.
00025 //
00026 // You should have received a copy of the GNU General Public License
00027 // along with GroovX; if not, write to the Free Software Foundation,
00028 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00029 //
00030 ///////////////////////////////////////////////////////////////////////
00031 
00032 #ifndef GROOVX_TCL_OBJPKG_CC_UTC20050626084017_DEFINED
00033 #define GROOVX_TCL_OBJPKG_CC_UTC20050626084017_DEFINED
00034 
00035 #include "objpkg.h"
00036 
00037 #include "nub/objdb.h"
00038 #include "nub/object.h"
00039 
00040 #include "tcl/list.h"
00041 #include "tcl/pkg.h"
00042 
00043 #include "rutz/iter.h"
00044 
00045 #include "rutz/trace.h"
00046 #include "rutz/debug.h"
00047 GVX_DBG_REGISTER
00048 
00049 using rutz::shared_ptr;
00050 
00051 //---------------------------------------------------------------------
00052 //
00053 // obj_caster
00054 //
00055 //---------------------------------------------------------------------
00056 
00057 tcl::obj_caster::obj_caster() {}
00058 
00059 tcl::obj_caster::~obj_caster() {}
00060 
00061 bool tcl::obj_caster::is_id_my_type(nub::uid uid) const
00062 {
00063   nub::soft_ref<nub::object> item(uid);
00064   return (item.is_valid() && is_my_type(item.get()));
00065 }
00066 
00067 namespace
00068 {
00069   int count_all(shared_ptr<tcl::obj_caster> caster)
00070   {
00071     nub::objectdb& instance = nub::objectdb::instance();
00072     int count = 0;
00073     for (nub::objectdb::iterator itr(instance.objects()); itr.is_valid(); ++itr)
00074       {
00075         if (caster->is_my_type(*itr))
00076           ++count;
00077       }
00078     return count;
00079   }
00080 
00081   tcl::list find_all(shared_ptr<tcl::obj_caster> caster)
00082   {
00083     nub::objectdb& instance = nub::objectdb::instance();
00084 
00085     tcl::list result;
00086 
00087     for (nub::objectdb::iterator itr(instance.objects()); itr.is_valid(); ++itr)
00088       {
00089         if (caster->is_my_type(*itr))
00090           result.append((*itr)->id());
00091       }
00092 
00093     return result;
00094   }
00095 
00096   void remove_all(shared_ptr<tcl::obj_caster> caster)
00097   {
00098     nub::objectdb& instance = nub::objectdb::instance();
00099     for (nub::objectdb::iterator itr(instance.objects());
00100          itr.is_valid();
00101          /* increment done in loop body */)
00102       {
00103         dbg_eval(3, (*itr)->id());
00104         dbg_dump(3, *(*itr)->get_counts());
00105 
00106         if (caster->is_my_type(*itr) && (*itr)->is_unshared())
00107           {
00108             nub::uid remove_me = (*itr)->id();
00109             ++itr;
00110             instance.remove(remove_me);
00111           }
00112         else
00113           {
00114             ++itr;
00115           }
00116       }
00117   }
00118 
00119   bool is_my_type(shared_ptr<tcl::obj_caster> caster, nub::uid id)
00120   {
00121     return caster->is_id_my_type(id);
00122   }
00123 
00124   unsigned int get_sizeof(shared_ptr<tcl::obj_caster> caster)
00125   {
00126     return caster->get_sizeof();
00127   }
00128 }
00129 
00130 void tcl::def_basic_type_cmds(tcl::pkg* pkg,
00131                               shared_ptr<tcl::obj_caster> caster,
00132                               const rutz::file_pos& src_pos)
00133 {
00134 GVX_TRACE("tcl::def_basic_type_cmds");
00135 
00136   const int flags = tcl::NO_EXPORT;
00137 
00138   pkg->def_vec( "is", "objref(s)",
00139                 rutz::bind_first(is_my_type, caster), 1, src_pos, flags );
00140   pkg->def( "count_all", "",
00141             rutz::bind_first(count_all, caster), src_pos, flags );
00142   pkg->def( "find_all", "",
00143             rutz::bind_first(find_all, caster), src_pos, flags );
00144   pkg->def( "remove_all", "",
00145             rutz::bind_first(remove_all, caster), src_pos, flags );
00146   pkg->def( "sizeof", "",
00147             rutz::bind_first(get_sizeof, caster), src_pos, flags );
00148 }
00149 
00150 static const char __attribute__((used)) vcid_groovx_tcl_objpkg_cc_utc20050626084017[] = "$Id: objpkg.cc 11876 2009-10-22 15:53:06Z icore $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/objpkg.cc $";
00151 #endif // !GROOVX_TCL_OBJPKG_CC_UTC20050626084017_DEFINED
Generated on Sun May 8 08:42:22 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3