Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Object.H

Go to the documentation of this file.
00001 /* $Id: Object.H,v 1.3 2001/01/02 20:14:15 rpav Exp $
00002  *
00003  * SFW - Studio FrameWork
00004  * Copyright (C) 2000  Ryan Pavlik
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 /*! \file Object.H
00023  *  \brief Declarations of SFW::Object and a few related things.
00024  */
00025 
00026 #ifndef OBJECT_H
00027 #define OBJECT_H
00028 
00029 #include <vector>
00030 #include <deque>
00031 #include <list>
00032 #include <set>
00033 #include <map>
00034 #include <string>
00035 
00036 //! Provide a convenient way to pull STL containers into the hierarchy
00037 #define STL_TO_SFW(sfwcls, stlcls, arg)                   \
00038 class sfwcls : public Object, public stlcls<arg> {        \
00039 public:                                                   \
00040     TYPE_INFO(SFW::sfwcls, SFW::Object);                  \
00041 };
00042 
00043 //! Provide a convenient way to pull STL containers into the hierarchy
00044 /*! This version is for containers that take two parameters */
00045 #define STL2_TO_SFW(sfwcls, stlcls, arg0, arg1)           \
00046 class sfwcls : public Object, public stlcls<arg0, arg1> { \
00047 public:                                                   \
00048     TYPE_INFO(SFW::sfwcls, SFW::Object);                  \
00049 };
00050 
00051 //! This is the outer namespace for all Studio Framework constructs.
00052 namespace SFW {
00053 
00054     //! The root of the class tree.
00055     
00056     /*!
00057      *
00058      * The \p Object class is the root of all other framework classes.
00059      * Internally, it provides the structure for runtime type
00060      * information.  It's important that all classes be subclasses of
00061      * \p Object, because most SFW constructs handle \p Object*.
00062      *
00063      * Every \p Object subclass must also have corresponding \p
00064      * TYPE_INFO and \p TYPE_INIT declarations, or \p V_TYPE_INFO for
00065      * abstract classes.
00066      */
00067     
00068     class Object {
00069         static string SFWob_type;
00070         
00071     public:
00072         //! A blank virtual destructor.
00073         /*! This ensures proper handling for subclasses. */
00074         virtual ~Object() { }
00075 
00076         //! Return the string representation of the type name
00077         /*! This returns the full type name, including any classes or
00078          *  namespaces it's contained in.
00079          */
00080         virtual string getType() const;
00081 
00082         //! Check for type equality
00083         /*! This function will return \a true if this object is of
00084          *  class \p type, or if this object inherits from class
00085          *  \p type.
00086          */          
00087         virtual bool   isType(const string &type) const;
00088 
00089         //! Return a duplicate of this object
00090         /*! Note that there is no magic here, a proper copy
00091          *  constructor still needs to be implemented in any case it
00092          *  would regularly be needed.  This function merely provides
00093          *  a reference from a particular object to that copy
00094          *  constructor, without having to explicitly know what class
00095          *  the object is.
00096          */
00097         virtual SFW::Object * clone() const = 0;
00098     };
00099 }
00100 
00101 /*
00102  * A cheap little runtime type system
00103  *
00104  */
00105 
00106 /* Note this should go in a PUBLIC section of the class declaration */
00107 #define V_TYPE_INFO(class,super) \
00108 private:\
00109 static string SFWob_type;\
00110 \
00111 public:\
00112 virtual string getType() const { return class ## ::SFWob_type; }\
00113 \
00114 virtual bool isType(const string &_SFWob_type) const {\
00115     return (_SFWob_type == class ## ::SFWob_type || super ## ::isType(_SFWob_type));\
00116 }
00117 
00118 #define TYPE_INFO(class,super) \
00119 private:\
00120 static string SFWob_type;\
00121 \
00122 public:\
00123 virtual string getType() const { return class ## ::SFWob_type; }\
00124 \
00125 virtual bool isType(const string &_SFWob_type) const {\
00126     return (_SFWob_type == class ## ::SFWob_type || super ## ::isType(_SFWob_type));\
00127 }\
00128 \
00129 virtual class* clone() const {\
00130     return new class(*((class *) this));\
00131 }
00132 
00133 #define TYPE_INIT(class) string class ## ::SFWob_type = #class
00134 
00135 #define METHOD_PTR(cls)\
00136 class Method : public SFW::Method {\
00137     typedef Object* (cls::*fn)(Object*);\
00138     fn mptr;\
00139     \
00140 public:\
00141     TYPE_INFO(cls::Method, SFW::Method);\
00142 \
00143     Method()                   { mptr = 0; }\
00144     Method(fn           _mptr) { mptr = _mptr; }\
00145     Method(cls::Method &_m)    { mptr = _m.mptr; }\
00146 \
00147     fn      operator=  (fn     _mptr) { return mptr = _mptr; }\
00148     Method& operator=  (Method _m)    { mptr = _m.mptr; return *this; }\
00149 \
00150     virtual Object* operator() (Object *c, Object *o) {\
00151         if(!c->isType(#cls)) {\
00152             cerr << "Called " << #cls << " method on a " << c->getType() << endl;\
00153             abort();\
00154         }\
00155 \
00156         return (((cls*)c)->*mptr)(o);\
00157     }\
00158 };
00159 
00160 namespace SFW {
00161     class Method : public Object {
00162     public:
00163         V_TYPE_INFO(SFW::Method, SFW::Object);
00164         
00165         virtual Object* operator() (Object *c, Object *o) = 0;
00166     };
00167 
00168     STL_TO_SFW (Vector,      vector,      Object*);
00169     STL_TO_SFW (Deque,       deque,       Object*);
00170     STL_TO_SFW (List,        list,        Object*);
00171     STL_TO_SFW (Set,         set,         Object*);
00172     STL_TO_SFW (MultiSet,    multiset,    Object*);
00173 
00174     STL2_TO_SFW(PMap,        map,         Object*, Object*);
00175     STL2_TO_SFW(PMultiMap,   multimap,    Object*, Object*);
00176 }
00177 
00178 #endif /* OBJECT_H */

Generated at Tue Jan 2 15:38:34 2001 for SFW by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000