Polaris: BinRep.h Source File

BinRep.h

Go to the documentation of this file.
00001 ///
00002 ///
00003 /// file BinRep.h
00004 ///
00005 #ifndef _BIN_REP_H
00006 #define _BIN_REP_H
00007 ///
00008 /// \class BinRep 
00009 /// \brief a class for representing the interchange format.
00010 /// \defgroup Polaris
00011 /// \ingroup Polaris
00012 ///  Base
00013 /// \see BinRep.h
00014 /// \see BinRep.h
00015 /// \see BinRep.cc
00016 ///
00017 /// \endcode
00018 /// \section Overview Overview
00019 /// This classes is intended for representing the interchange format in
00020 /// a manner that allows easy manipulation.
00021 /// 
00022 /// \endcode
00023 /// \section Description Description
00024 /// The interchange format is based upon Lists and Sets of interchange
00025 /// objects.  Each object represents one element in the interchange
00026 /// format.  This class is intented as a easy way to manipulate and
00027 /// examine the interchange format.  The BinRep class is a recursive
00028 /// datastructure that allows one to arbitrarity nest the BinRep objects.
00029 /// These BinRep objects can be changed into standard Polaris Collection
00030 /// objects to be examined and mutated in standard ways.
00031 ///
00032 #ifdef POLARIS_GNU_PRAGMAS
00033 #pragma interface
00034 #endif
00035 ///
00036 #include "ClassNames.h"
00037 #include "Listable.h"
00038 #include "IntElem.h"
00039 #include "Collection/List.h"
00040 #include "Collection/Set.h"
00041 #include "Collection/Database.h"
00042 #include "String.h"
00043 #include "Boolean.h"
00044 ///
00045 class MemBuf;
00046 class BinPtr;
00047 
00048 #include "bin_str_def.h"
00049 
00050 
00051 class BinRep : public Listable {
00052  private:
00053     BINSTR_TYPE     _fc;
00054     ///< The type of the object we are representing.
00055     int             _len;
00056     ///< The size of the object.
00057 
00058     union BinRep_union {
00059         List<BinRep>            *L;
00060         Set<BinRep>             *S;
00061         Database<String,BinRep> *D;
00062         void                    *C;
00063     } _u;
00064 
00065     ///< A BinRep object can be any of the above types.   Each node in the
00066     ///< obejct can be either a List, a Set, a Database or a memory buffer.
00067 
00068  protected:
00069     BinRep & _omega() const;
00070     ///< return a reference to an FT_OMEGA object.
00071 
00072     void _init();
00073     ///< Set to the empty node.
00074 
00075     void _destroy();
00076     ///< Erase the current node.
00077 
00078     void _parse(const void *binstr);
00079     ///< Parse a memory buffer, determine if it is the IEF or SETL format.
00080     void _parse(istream &ifs);
00081     ///< Parse an istream, determine if it is the IEF or SETL format.
00082 
00083     void _ief_parse(const void *bin_header, const void *bin_body);
00084     ///< Parse the memory buffers in IEF format.
00085     void _ief_parse(istream &ifs);
00086     ///< Parse an istream in IEF format.
00087 
00088     void _setl_parse(const void *binstr);
00089     ///< Parse the memory buffer in SETL format.
00090     void _setl_parse(istream &ifs);
00091     ///< Parse an istream in SETL format.
00092  
00093     void _ascii_format(String & str) const;
00094     ///< This method appends to str.
00095     ///< Note: This method adds double quotes to strings so that it can
00096     ///< be easily seen when substructures of a binstr are strings.
00097     ///< It also adds "(double)" on the front of double (float) values
00098     ///< for the same reason.  Sets are formatted with "{...}", tuples
00099     ///< with "[...]".
00100 
00101     int  _ief_length(Database<String,IntElem> *map) const;
00102     ///< Return the length in bytes of the current node in IEF format.  
00103     ///< If map is non-null then use it to lookup dictionary objects to 
00104     ///< compact the length.
00105 
00106     void _ief_format(BinPtr &p, Database<String,IntElem> *map) const;
00107     ///< This method appends to str.
00108     ///< Note: This method changes the BinRep into a flat representation
00109     ///< suitable for interchange with other compilers like KAP.
00110 
00111     int  _setl_length() const;
00112     ///< Return the length in bytes of the current node in SETL.  If map 
00113     ///< is non-null then use it to lookup dictionary objects to compact
00114     ///< the length.
00115 
00116     void _setl_format(BinPtr &p) const;
00117     ///< This method appends to str.
00118     ///< Note: This method changes the BinRep into a flat representation
00119     ///< suitable for interchange with the SETL environment in Delta.
00120 
00121  public:
00122     BinRep();
00123     ///< Create a NULL node.
00124 
00125     BinRep(const void *binstr);
00126     ///< Create a BinRep object from the memory buffer.
00127 
00128     inline BinRep( List<BinRep> *L );
00129     ///< Create a BinRep object from a List.
00130 
00131     inline BinRep( Set<BinRep> *S );
00132     ///< Create a BinRep object from a List.
00133 
00134     inline BinRep( String &s );
00135     ///< Create a BinRep object from a string.
00136 
00137     inline BinRep( const char *s );
00138     ///< Create a BinRep object from a string.
00139 
00140     inline BinRep( int x );
00141     ///< Create a BinRep object from an integer.
00142 
00143     BinRep(const BinRep &br);
00144     ///< Copy constructor.
00145 
00146     virtual ~BinRep();
00147     ///< Destroy the BinRep object.
00148 
00149     BinRep & operator = (const void *binstr);
00150     ///< Create a BinRep object from the memory buffer.
00151 
00152     BinRep & operator = (const BinRep &br);
00153     ///< Copy the BinRep br into the current object.
00154 
00155     void del(int i);
00156     ///< This operator overloading is for tuple object only.
00157     ///< Delete the ith element of the tuple.
00158 
00159     BinRep & operator[] (int i) const;
00160     ///< This operator overloading is for tuple object only.
00161     ///< It returns a BinRep which corresponds to the ith element
00162     ///< of the tuple (where i == 0 is the first element)
00163 
00164     BinRep * find_ref(const char *field) const;
00165     ///< Check to see if field is a member of this BinRep object, return 0 if not.
00166 
00167     void del(const char *field);
00168     ///< This operator overloading is for set and map objects only.
00169     ///< Delete the specifed field of the map.
00170 
00171     BinRep & operator[] (const char *field) const;
00172     ///< This operator overloading of the square brackets is for set and map
00173     ///< objects only, and is really only meant for maps (sets of 2-tuples, 
00174     ///< where the first element of each tuple is used as a tag and the second 
00175     ///< element is used as the value for that tag, and furthermore the same 
00176     ///< element does not appear as a tag twice).
00177     ///< It searches through the map for a tag having a value equal to
00178     ///< 'field' and returns a BinRep corresponding to the value for 
00179     ///< that tag.  If no such tag is found, a BinRep corresponding 
00180     ///< to OM (undefined type) is returned.
00181 
00182     void ins( BinRep *b );
00183     ///< Insert the BinRep into the top-level structure.
00184 
00185     void ins( const BinRep &b );
00186     ///< Insert a copy of the BinRep into the top-level structure.
00187  
00188     void ascii_format(String & str) const;
00189     ///< Note: This method adds double quotes to strings so that it can
00190     ///< be easily seen when substructures of a binstr are strings.
00191     ///< It also adds "(double)" on the front of double (float) values
00192     ///< for the same reason.  Sets are formatted with "{...}", tuples
00193     ///< with "[...]".
00194 
00195     void ief_format(MemBuf &buf) const;
00196     ///< Note: This method changes the BinRep into a flat representation
00197     ///< suitable for interchange with other compilers like KAP.
00198 
00199     void setl_format(MemBuf &buf) const;
00200     ///< Note: This method changes the BinRep into a flat representation
00201     ///< suitable for interchange with the SETL environment in Delta.
00202 
00203     void            read(istream &ifs);
00204     ///< Read the BinRep from the istream ifs.
00205 
00206     BinRep         *clone() const;
00207     ///< Create a copy of the BinRep.
00208 
00209     virtual Listable *listable_clone() const;
00210     ///< Copy and return a pointer to any Listable item
00211     ///< (MUST be implemented correctly by subclasses
00212     ///< for List copy functions to work)
00213 
00214     virtual void    print(ostream & o) const;
00215     ///< Print the current BinRep node and it's children.
00216 
00217     int             structures_OK() const;
00218     ///< Check the structure of the data for errors or inconsistency
00219     ///< Return 0 and print error message if problems found, otherwise
00220     ///< return 1 without message.
00221 
00222     inline BINSTR_TYPE type() const;
00223     ///< Return the type of this node.
00224 
00225     Boolean is_omega() const;   
00226     ///< Check to see if this node is an OM.
00227 
00228     Boolean is_boolean() const;
00229     ///< Check to see if this node is a Boolean.
00230 
00231     Boolean is_double() const;
00232     ///< Check to see if this node is a Real.
00233 
00234     Boolean is_integer() const;
00235     ///< Check to see if this node is an Integer.
00236 
00237     Boolean is_string() const;
00238     ///< Check to see if this node is a String.
00239 
00240     Boolean is_tuple() const;
00241     ///< Check to see if this node is a Tuple.
00242 
00243     Boolean is_set() const;
00244     ///< Check to see if this node is a Set.
00245 
00246     inline Boolean is_map() const;
00247     ///< Check to see if this node is a Map.
00248     ///< \note currently Map's are not fully supported so this function
00249     ///< defaults to False.
00250 
00251     Boolean to_boolean() const;
00252     ///< Returns the Boolean value of a BinRep.  If is_boolean() is
00253     ///< not true, aborts with an error message.
00254 
00255     double          to_double() const;
00256     ///< Returns the double value of a BinRep.  If is_double() is
00257     ///< not true, aborts with an error message.
00258 
00259     int             to_integer() const;
00260     ///< Returns the int value of a BinRep.  If is_integer() is
00261     ///< not true, aborts with an error message.
00262 
00263     void            to_string(String & str) const;
00264     ///< Note:  Due to technical concerns and to avoid an unobvious pitfall
00265     ///< of the String class the to_string method does not return a String, 
00266     ///< but rather sets 'str' to the string value of the BinRep.  
00267     ///< If is_string() is not true, aborts with an error message.
00268 
00269     List<BinRep> & to_tuple() const;
00270     ///< Returns the List value of a BinRep.  If is_tuple() is
00271     ///< not true, aborts with an error message.
00272 
00273     Set<BinRep> & to_set() const;
00274     ///< Returns the Set value of a BinRep.  If is_set() is
00275     ///< not true, aborts with an error message.
00276 
00277     Database<String,BinRep> & to_map() const;
00278     ///< Returns the Map value of a BinRep.  If is_map() is
00279     ///< not true, aborts with an error message.
00280 
00281     void put_tuple( List<BinRep> *L );
00282     ///< Store a tuple into the current node.
00283 
00284     void put_set( Set<BinRep> *S );
00285     ///< Store a set into the current node.
00286 
00287     void put_map( Database<String,BinRep> *D );
00288     ///< Store a map into the current node.
00289 
00290     void put_omega();
00291     ///< Store an OM into the current node.
00292 
00293     void put_integer( const int x );
00294     ///< Store an Integer into the current node.
00295 
00296     void put_boolean( const Boolean b );
00297     ///< Store a Boolean into the current node.
00298 
00299     void put_string( const String &s );
00300     ///< Store an String into the current node.
00301     void put_string( const char *s );
00302     ///< Store a C string (null terminated) into the current node.
00303     void put_string( const char *s, int length );
00304     ///< Store an C string of length into the current node.
00305 
00306     void put_double( const double d );
00307     ///< Store an Real into the current node.
00308 };
00309 
00310 ///< Description:
00311 ///< Return the ASCII type name for a formcode.
00312 
00313 char *binstr_type_name( BINSTR_TYPE expected_type );
00314 
00315 inline BINSTR_TYPE 
00316 BinRep::type() const 
00317 { 
00318     return _fc; 
00319 }
00320 
00321 inline Boolean 
00322 BinRep::is_map() const 
00323 { 
00324     return False; 
00325 }
00326 
00327 inline 
00328 BinRep::BinRep( List<BinRep> *L )
00329 {
00330     _init();
00331     put_tuple( L );
00332 }
00333 
00334 inline
00335 BinRep::BinRep( Set<BinRep> *S )
00336 {
00337     _init();
00338     put_set( S );
00339 }
00340 
00341 inline
00342 BinRep::BinRep( String &s )
00343 {
00344     _init();
00345     put_string( s );
00346 }
00347 
00348 inline
00349 BinRep::BinRep( const char *s )
00350 {
00351     _init();
00352     put_string( s );
00353 }
00354 
00355 inline 
00356 BinRep::BinRep( int x )
00357 {
00358     _init();
00359     put_integer( x );
00360 }
00361 
00362 #endif
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:41 2005