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

/magnus/back_end/general/include/Type.h

Go to the documentation of this file.
00001 /*
00002  *   $Id: Type.h,v 1.3 1999/11/23 20:37:37 bormotov Exp $
00003  */
00004  
00005 // Copyright (C) 1994 The New York Group Theory Cooperative
00006 // See magnus/doc/COPYRIGHT for the full notice.
00007  
00008 // Contents: Definition of Type class
00009 //
00010 // * Type objects represent the type of a class, and thus permit
00011 //   objects to explicitely identify themselves by returning such
00012 //   an object. This is useful for run-time type enquiries and
00013 //   explicit polymorphism. Type objects can only be created, copied,
00014 //   and compared.
00015 //
00016 // Principal Author: Stephane Collart
00017 //
00018 // Status: in use
00019 //
00020 // Usage:
00021 //
00022 // * A class MyClass must store a
00023 //      static const Type MyClass::theMyClassType
00024 //   and initialise this in MyClass.C with
00025 //      const Type MyClass::theMyClassType = Type( unique() );
00026 //   at a suitable place. Access to the type is conveniently done for
00027 //   instance from a function
00028 //      virtual Type MyClass::actualType( ) const { return theMyClassType; }
00029 //   It is guaranteed that the Types of different classes are different
00030 //   amongst each other and different from the Type constructed by
00031 //   Type().
00032 //
00033 // * In the case of an object-level class or hierarchy with multiple
00034 //   representations, the type ur-object will be in the representation,
00035 //   e.g.
00036 //      static const Type MyClassRep::theMyClassType;
00037 //   the object level class will have e.g.
00038 //      static Type MyClass::type( ) { return MyClassRep::type(); }
00039 //      Type MyClass::actualType( ) const { return look()->actualType(); }
00040 //   and in its represenation one will find e.g.
00041 //      static Type MyClassRep::type( ) { return theMyClassType; }
00042 //      virtual Type MyClassRep::actualType const { return theMyClassType; }
00043 //   the latter to be overridden in derived representations.
00044 //   
00045 // Extensions:
00046 //
00047 // * If useful, concrete information can be added to the type objects,
00048 //   from instance by deriving from Type.
00049 //
00050 // Revision History:
00051 //
00052 // Implementation Notes :
00053 //
00054 // * Alternative Implementation:
00055 //
00056 //   class TypeDatum {
00057 //   private :
00058 //      // disable all operations with TypeDatum
00059 //      TypeDatum( const TypeDatum& ) { }
00060 //      TypeDatum operator=( const TypeDatum& );
00061 //   };
00062 //   // TypeDatums are dummy objects which just exist; they can only be
00063 //   // created or deleted -- their only purpose is to be pointed to
00064 //   // by Type objects representing the same type; for every type, there
00065 //   // is exactly one TypeDatum object, in that class as a static
00066 //   // const member.
00067 //   
00068 //   class Type {
00069 //   private :
00070 //      TypeDatum* typeflag;
00071 //   public :
00072 //      bool operator == ( const Type& t ) { return typeflag == t.typeflag; }
00073 //      boll operator!= ( const Type& t) { return !(*this == t); }
00074 //   };
00075 //   // Type objects identify themselves by pointing to the TypeDatum
00076 //   // of the class whose type they represent.
00077 //
00078 
00079 #ifndef _TYPE_H_
00080 #define _TYPE_H_
00081 
00082 
00083 #include <iostream.h>
00084  
00085 class Type {
00086 public :
00087 
00088 // constructors:
00089 
00090         class notype { }; // pure `marker' helper class
00091         class unique { }; // pure `marker' helper class
00092 
00093     Type( const notype& ) : typeNumber(0) { }
00094         // default constructor yields Type object guaranteed not to
00095     // represent type of any class
00096 
00097     Type( const unique& ) : typeNumber(++uniqueTypeNumber) { }
00098         // special constructor for initialisation of the client class static
00099         // Type member
00100 
00101         // copy constructor provided by compiler
00102 
00103         // destructor provided by compiler
00104 
00105 // operators:
00106 
00107         // operator = provided by compiler
00108 
00109 // comparators:
00110 
00111     int operator== ( const Type& t) { return typeNumber == t.typeNumber; }
00112     int operator!= ( const Type& t) { return !(*this == t); }
00113 
00114 // I/O:
00115 
00116         void writeTo( ostream& o ) const { o << typeNumber; }
00117 
00118         void readMeFrom( istream& i ) { i >> typeNumber; }
00119  
00120 private :
00121 
00122 // data members:
00123 
00124     int typeNumber;
00125         // the class Types have a unique identification number each
00126 
00127 // shared data:
00128 
00129         static int uniqueTypeNumber;
00130         // central register of used unique Type numbers; always set to the
00131         // last used number. Numbering of types starts at 1; 0 reserved for
00132         // `no type'.
00133 
00134 // debuggging stuff:
00135 
00136         #ifdef DEBUG
00137         //friend int main( );
00138         #endif
00139 
00140 };
00141 
00142 
00143 // Operator Synonyms for I/O members:
00144 
00145 inline ostream& operator << ( ostream& o, const Type& t )
00146         { t.writeTo(o); return o; }
00147 
00148 inline istream& operator >> ( istream& i, Type& t )
00149         { t.readMeFrom(i); return i; }
00150 
00151 
00152 #endif
00153  
00154 

Generated at Tue Jun 19 09:49:35 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001