objpkg.h

Go to the documentation of this file.
00001 /** @file tcl/objpkg.h tcl package helpers for defining basic
00002     class-level tcl commands (e.g. find all objects of a given type) */
00003 
00004 ///////////////////////////////////////////////////////////////////////
00005 //
00006 // Copyright (c) 2002-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: Fri Nov 22 17:05:11 2002
00011 // commit: $Id: objpkg.h 11876 2009-10-22 15:53:06Z icore $
00012 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/objpkg.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 //
00033 ///////////////////////////////////////////////////////////////////////
00034 
00035 #ifndef GROOVX_TCL_OBJPKG_H_UTC20050626084018_DEFINED
00036 #define GROOVX_TCL_OBJPKG_H_UTC20050626084018_DEFINED
00037 
00038 #include "nub/objfactory.h"
00039 
00040 #include "rutz/shared_ptr.h"
00041 
00042 namespace rutz
00043 {
00044   struct file_pos;
00045 }
00046 
00047 namespace tcl
00048 {
00049   class obj_caster;
00050   class pkg;
00051 }
00052 
00053 namespace nub
00054 {
00055   class object;
00056 }
00057 
00058 // ########################################################
00059 /// obj_caster class encapsulates casts to see if objects match a given type.
00060 
00061 class tcl::obj_caster
00062 {
00063 protected:
00064   obj_caster();
00065 
00066 public:
00067   virtual ~obj_caster();
00068 
00069   virtual bool is_my_type(const nub::object* obj) const = 0;
00070 
00071   virtual unsigned int get_sizeof() const = 0;
00072 
00073   bool is_not_my_type(const nub::object* obj) const { return !is_my_type(obj); }
00074 
00075   bool is_id_my_type(nub::uid uid) const;
00076 
00077   bool is_id_not_my_type(nub::uid uid) const { return !is_id_my_type(uid); }
00078 };
00079 
00080 namespace tcl
00081 {
00082   /// cobj_caster implements obj_caster with dynamic_cast.
00083   template <class C>
00084   class cobj_caster : public obj_caster
00085   {
00086   public:
00087     virtual unsigned int get_sizeof() const
00088     {
00089       return sizeof(C);
00090     }
00091 
00092     virtual bool is_my_type(const nub::object* obj) const
00093     {
00094       return (obj != 0 && dynamic_cast<const C*>(obj) != 0);
00095     }
00096   };
00097 
00098   void def_basic_type_cmds(pkg* pkg, rutz::shared_ptr<obj_caster> caster,
00099                            const rutz::file_pos& src_pos);
00100 
00101   template <class C>
00102   void def_basic_type_cmds(pkg* pkg, const rutz::file_pos& src_pos)
00103   {
00104     rutz::shared_ptr<obj_caster> caster(new cobj_caster<C>);
00105     def_basic_type_cmds(pkg, caster, src_pos);
00106   }
00107 
00108   template <class C>
00109   void def_creator(pkg*, const char* alias_name = 0)
00110   {
00111     const char* origName =
00112       nub::obj_factory::instance().register_creator(&C::make);
00113 
00114     if (alias_name != 0)
00115       nub::obj_factory::instance().register_alias(origName, alias_name);
00116   }
00117 }
00118 
00119 static const char __attribute__((used)) vcid_groovx_tcl_objpkg_h_utc20050626084018[] = "$Id: objpkg.h 11876 2009-10-22 15:53:06Z icore $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/objpkg.h $";
00120 #endif // !GROOVX_TCL_OBJPKG_H_UTC20050626084018_DEFINED
Generated on Sun May 8 08:42:22 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3