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

/magnus/back_end/SessionManager/include/Property.h

Go to the documentation of this file.
00001 /*
00002  *   $Id: Property.h,v 1.2 2000/03/22 21:55:55 bormotov Exp $
00003  */
00004  
00005 //   Copyright (C) 1998 The New York Group Theory Cooperative
00006 //   See magnus/doc/COPYRIGHT for the full notice.
00007  
00008 // Contents: Definition of an object property template 
00009 //           and collection of properties.
00010 //
00011 // Principal Author: Dmitry Pechkin
00012 //
00013 // Status: In progress.
00014 //
00015 // Revision History:
00016 //
00017 //
00018 // Special Notes:
00019 //
00020 //  See examples of use at the bottom of the file.
00021 //
00022 // Further implementation steps:
00023 //
00024 //
00025 // Bugs:
00026 //
00027 //
00028 
00029 #ifndef _PROPERTY_H_
00030 #define _PROPERTY_H_
00031  
00032 #include "QuickAssociations.h"
00033 #include "Chars.h"
00034 #include "APofFreeGroups.h"
00035 #include "Automorphism.h"
00036 #include "HNNExtOfFreeGroup.h"
00037 #include "NilpotentGroup.h"
00038 #include "CosetEnumerator.h"
00039 #include "DFSA.h"
00040 #include "DiffMachine.h"
00041 #include "KBMachine.h"
00042 #include "DecomposeInSubgroup.h"
00043 #include "AbelianSGPresentation.h"
00044 #include "SGOfNilpotentGroup.h"
00045 #include "PolyWord.h"
00046 
00047 class PropertyType {
00048 public:
00049   PropertyType( int t ) : type(t) {}
00050   int unwrap() const { return type; }
00051   int operator++() { return ++type; }
00052 
00053   //bool operator ==( const PropertyType& p ) { return type == p.type; }
00054   //bool operator !=( const PropertyType& p ) { return type != p.type; }
00055   
00056   friend ostream& operator<<(ostream& ostr, const PropertyType& p) { 
00057     ostr << p.type; 
00058   }
00059   friend istream& operator>>(istream& istr, PropertyType& p) { 
00060     istr >> p.type; 
00061   }
00062 
00063 private:
00064   int type;
00065 };
00066 
00067 inline bool operator==( const PropertyType a, const PropertyType b ) {
00068   return a.unwrap() == b.unwrap();
00069 }
00070 
00071 inline bool operator!=( const PropertyType a, const PropertyType b ) {
00072   return a.unwrap() != b.unwrap();
00073 }
00074 
00075 class GenericProperty;
00076 
00077 //============================================================
00078 //
00079 //          Properties Manager
00080 //
00081 //============================================================
00082 
00083 class PropertiesManager {
00084   // The class implements a `Singleton' design pattern (see the G4's book)
00085 public:
00086   static PropertiesManager* instance();
00087   
00088   PropertyType typeOf( const Chars& propertyName ) const;
00089   Chars nameOf( const PropertyType& ptype ) const;
00090 
00091   const GenericProperty* property( const PropertyType& ptype ) const;
00092 
00093   bool isRegisteredProperty( const PropertyType ptype ) const;
00094   void registerProperty( const GenericProperty& property );
00095   void unregisterProperty( const GenericProperty& property );
00096   void unregisterProperty( const PropertyType ptype );
00097 
00098   GenericProperty* readProperty(istream& istr, bool extraDetails);
00099   GenericProperty* readProperty(istream& istr, const PropertyType ptype,
00100                                 bool extraDetails);
00101 
00102 private:
00103   PropertiesManager() {}
00104 
00105   // Disable the copying of the instance -- 
00106   // these two functions are not to be implemented.
00107   PropertiesManager( const PropertiesManager& );
00108   PropertiesManager& operator=( const PropertiesManager& );
00109 
00110   class SingletonKiller {
00111   public:
00112     ~SingletonKiller() { delete PropertiesManager::theInstance; }
00113   };
00114   friend class SingletonKiller;
00115 
00116   static PropertiesManager *theInstance;
00117   static SingletonKiller    theKiller;
00118 
00119   // Instance's data members:
00120   QuickAssociationsOf<PropertyType, GenericProperty*> theRegisteredProperties;
00121   QuickAssociationsOf<Chars, PropertyType>            theRegisteredTypes;
00122 };
00123 
00124 void registerProperties();
00125 
00126 extern const PropertyType unregisteredProperty;
00127 
00128 //============================================================
00129 //
00130 //          Properties Collection
00131 //
00132 //============================================================
00133 
00134 class PropertiesCollection
00135 {
00136 public:
00137   PropertiesCollection( ) { }
00138 
00139   Trichotomy haveProperty  ( const PropertyType ptype  ) const;
00140   void  putProperty       ( const GenericProperty& property, Trichotomy have );
00141   Chars getDescription    ( const PropertyType ptype  ) const; 
00142   void  setUnknownProperty( const PropertyType ptype  );
00143   void  setHaveNotProperty( const PropertyType ptype, const Chars& expl );
00144   void  setKnownProperty  ( const GenericProperty& property );
00145   void  getKnownProperty  ( GenericProperty& property ) const;
00146   const GenericProperty* getKnownProperty( const PropertyType ptype ) const;
00147         GenericProperty* getKnownProperty( const PropertyType ptype );
00148   void read ( istream& istr, bool extraDetails );
00149   void write( ostream& ostr, bool extraDetails );
00150 
00151 private:
00152 
00153   QuickAssociationsOf<PropertyType, GenericProperty*> knownProperties;
00154   QuickAssociationsOf<PropertyType, Chars>            haveNotProperties;
00155 };
00156 
00157 //============================================================
00158 //
00159 //          Properties Parser
00160 //
00161 //============================================================
00162 
00163 class PropertiesParser //: protected PresentationParser
00164 {
00165 public:
00166 
00167   /////////////////////////////////////////////////////////////////////////
00168   //                                                                     //
00169   // Constructors:                                                       //
00170   //                                                                     //
00171   /////////////////////////////////////////////////////////////////////////
00172 
00173   PropertiesParser(istream &i) : istr(i) {} 
00174   // Initialize the parser with the istream from which to read.
00175 
00176   // Destructor supplied by compiler.
00177 
00178   /////////////////////////////////////////////////////////////////////////
00179   //                                                                     //
00180   // Parsing Methods:                                                    //
00181   //                                                                     //
00182   /////////////////////////////////////////////////////////////////////////
00183 
00184   Chars parsePropertiesCollection(
00185     QuickAssociationsOf<PropertyType, GenericProperty*>& known,
00186     QuickAssociationsOf<PropertyType, Chars>&            unknown,
00187     bool extraDetails );
00188 
00189   Chars parseRawWord(Word& w);
00190 
00191   bool skipWhiteSpaces();
00192   bool skipWhiteSpacesAndGivenChar(char c);
00193 
00194 private:
00195   Chars parseKnownProperties(
00196     QuickAssociationsOf<PropertyType,GenericProperty*>& known,
00197     bool extraDetails );
00198 
00199   Chars parseUnknownProperties( 
00200     QuickAssociationsOf<PropertyType,Chars>& unknown );
00201 
00202   char peekCh() { return istr.peek(); }
00203   char getCh()  { return istr.get();  }
00204 
00205   istream& istr;
00206 };
00207 
00208 //============================================================
00209 //
00210 //          Generic Properties
00211 //
00212 //============================================================
00213 
00214 class GenericProperty {
00215 public:
00216   GenericProperty( const Chars& descr ) : theDescription( descr ) {}
00217 
00218   virtual ~GenericProperty() {}
00219   virtual PropertyType actualType  () const = 0;
00220   virtual Chars        propertyName() const = 0;
00221   virtual void         putProperty( GenericProperty* property ) = 0;
00222 
00223   Chars getDescription() const { return theDescription; }
00224 
00225   virtual bool isStorable() const { return true; }
00226 
00227   // I/O routines
00228   friend  istream& operator>>( istream& istr, GenericProperty& p ) {
00229     p.read(istr);
00230     return istr;
00231   }
00232 
00233   friend ostream& operator<<( ostream& ostr, const GenericProperty& p ) {
00234     Chars noExtraFile;
00235     p.write(ostr, false, noExtraFile);
00236     return ostr;
00237   }
00238 
00239   virtual void read (istream& istr, bool extraDetails = false)  = 0;
00240   virtual void write(ostream& ostr, bool extraDetails, 
00241                      const Chars& extrasDetailsFileName ) const = 0;
00242 
00243 protected:
00244   friend class PropertiesCollection;
00245   friend class PropertiesManager;
00246   virtual GenericProperty* clone() const = 0;
00247 
00248 protected:
00249   virtual void readFrom(istream& istr);
00250   virtual void writeTo (ostream& ostr) const;
00251 
00252   static PropertyType unique();
00253 private:
00254   static PropertyType theTypeCounter;
00255 
00256   Chars theDescription;
00257 };
00258 
00259 class GenericUnstorableProperty : public GenericProperty {
00260 public:
00261   GenericUnstorableProperty( const Chars& descr ) : GenericProperty(descr) {}
00262 
00263   bool isStorable() const { return false; }
00264 protected:
00265   void read (istream& istr, bool extraDetails = false) {
00266     error( "Cannot load an unstorable propery.");
00267   }
00268   void write(ostream& ostr, bool extraDetails, 
00269              const Chars& extrasDetailsFileName) const {
00270     // Do not save anything.
00271   }
00272 };
00273 
00274 class GenericSimpleProperty : public GenericProperty {
00275 public:
00276   GenericSimpleProperty( const Chars& descr ) : GenericProperty(descr) {}
00277 protected:
00278   void read (istream& istr, bool extraDetails = false);
00279   void write(ostream& ostr, bool extraDetails, 
00280              const Chars& extrasDetailsFileName) const;
00281 };
00282 
00283 class GenericComplexProperty : public GenericProperty {
00284 public:
00285   GenericComplexProperty( const Chars& descr ) : GenericProperty(descr) {}
00286 
00287 protected:
00288   void read (istream& istr, bool extraDetails = false);
00289   void write(ostream& ostr, bool extraDetails, 
00290              const Chars& extrasDetailsFileName) const;
00291 
00292 private:
00293   static void completeHeader( char *header, const char *title, 
00294                               const char *name, int start, int length );
00295   static const int header_size;
00296 };
00297 
00298 template <class T>
00299 class UnstorableProperty : public GenericUnstorableProperty {
00300 public:
00301 
00302   UnstorableProperty(const T& data, const Chars descr) 
00303     : GenericUnstorableProperty(descr), theData(data) { }
00304 
00305   const T& value() const { return theData; }
00306         T& ref  ()       { return theData; }
00307 
00308   void putProperty( GenericProperty* property ) {
00309     if( property->actualType() != actualType() ) {
00310       error( "UnstorableProperty<T>::putProperty(): "
00311              "wrong an actual type of the argument" );
00312       return;
00313     }
00314     theData = ((UnstorableProperty<T> *)property)->theData;
00315   }
00316 
00317   void readFrom(istream& istr)       {  }
00318   void writeTo (ostream& ostr) const {  }
00319 
00320 protected:
00321   T theData;
00322 };
00323 
00324 template <class T>
00325 class SimpleProperty : public GenericSimpleProperty {
00326 public:
00327 
00328   SimpleProperty(const T& data, const Chars descr) 
00329     : GenericSimpleProperty(descr), theData(data) { }
00330 
00331   const T& value() const { return theData; }
00332         T& ref  ()       { return theData; }
00333 
00334   void putProperty( GenericProperty* property ) {
00335     if( property->actualType() != actualType() ) {
00336       error( "SimpleProperty<T>::putProperty(): "
00337              "wrong an actual type of the argument" );
00338       return;
00339     }
00340     theData = ((SimpleProperty<T> *)property)->theData;
00341   }
00342 
00343   void readFrom(istream& istr)       { istr >> theData; }
00344   void writeTo (ostream& ostr) const { ostr << theData; }
00345 
00346 protected:
00347   T theData;
00348 };
00349 
00350 template <class T>
00351 class ComplexProperty : public GenericComplexProperty {
00352 public:
00353 
00354   ComplexProperty(const T& data, const Chars descr) 
00355     : GenericComplexProperty(descr), theData(data) { }
00356 
00357   const T& value() const { return theData; }
00358         T& ref  ()       { return theData; }
00359 
00360   void putProperty( GenericProperty* property ) {
00361     if( property->actualType() != actualType() ) {
00362       error( "ComplexProperty<T>::putProperty(): "
00363              "wrong an actual type of the argument" );
00364       return;
00365     }
00366     theData = ((ComplexProperty<T> *)property)->theData;
00367   }
00368 
00369   //PropertyType actualType()   const { return unregisteredProperty; } 
00370   //Chars        propertyName() const { return "unregisteredProperty"; }
00371 
00372 protected:
00373   //GenericProperty* clone() const { return new ComplexProperty(*this); }
00374 
00375 protected:
00376   T theData;
00377 };
00378 
00379 //
00380 //  EXAMPLES OF USING
00381 //
00382 // 1) Define a property class.
00383 //
00384 // class GroupHasOrder : public Property<int> 
00385 // {
00386 // public:
00387 //   GroupHasOrder( ) : Property<int>() { }
00388 //   GroupHasOrder( int order ) : Property<int>( new int(order) ) { } 
00389 //   static PropertyType type( ) { return theGroupHasOrder; }
00390 //   PropertyType actualType( ) const { return type(); }
00391 //   static const PropertyType theGroupHasOrder;
00392 // };
00393 //
00394 // // Static data members of GroupHasOrder class.
00395 // const PropertyType GroupHasOrder::theGroupHasOrder = 
00396 //   GenericProperty::unique();
00397 //
00398 //
00399 // 2) Work with the property like this:
00400 //
00401 //   PropertyCollection C; // Declare a properties collection
00402 //
00403 //   C.addProperty( new GroupHasOrder( 5 ) );
00404 //
00405 //   // Search and retreive the first property from the collection.
00406 //
00407 //   if( C.hasProperty( GroupHasOrder::type() ) != dontknow ) {
00408 //     GroupHasOrder P;
00409 //     C.getProperty( &P );
00410 //     int order = P.value();
00411 //     cout << "property = " << GroupHasOrder::type() 
00412 //          << ", value = " << order << endl;
00413 //   }
00414 // 
00415 //   // Store and restore a collection from a file
00416 //
00417 //      To appear.
00418 //
00419 //
00420 //  That's all for now.
00421 
00422 
00423 #endif

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