Field.h

00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    H3D API 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 H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
00022 //
00023 //
00024 //
00025 //
00027 #ifndef __FIELD_H__
00028 #define __FIELD_H__
00029 
00030 #include <iostream>
00031 #include <string>
00032 #include <set>
00033 #include <vector>
00034 #include "H3DApi.h"
00035 #include "H3DTypes.h"
00036 #include "TimeStamp.h"
00037 #include "X3DTypes.h"
00038 
00039 using namespace std;
00040 
00041 namespace H3D {
00042   // forward declaration
00043   class Node;
00044 
00046   class H3DAPI_API Field {
00047   public:
00050     H3D_API_EXCEPTION( FieldAccessError );
00051 
00053     typedef enum {
00057       INITIALIZE_ONLY,
00062       OUTPUT_ONLY,
00067       INPUT_ONLY,
00069       INPUT_OUTPUT 
00070     } AccessType;
00071 
00075     struct H3DAPI_API Event {
00076       Event( Field *_ptr, const TimeStamp &_time_stamp ):
00077         ptr( _ptr ),
00078         time_stamp( _time_stamp ) {}
00080       Field *ptr;
00082       TimeStamp time_stamp;       
00083     };    
00084 
00086     Field();
00087     
00089     virtual ~Field();
00090         
00095     virtual void checkFieldType( Field *f, int index ) {}
00096 
00098     void setName( string _name ) { name = _name; }
00099 
00101     string getName();
00102 
00105     string getFullName();
00106 
00108     virtual string getTypeName() { return classTypeName(); }
00109 
00111     virtual X3DTypes::X3DType getX3DType() { return X3DTypes::UNKNOWN_X3D_TYPE; }
00112 
00115     static string classTypeName() { return "Field"; }
00116 
00119     virtual void route( Field*, int id = 0 );
00120 
00123     virtual void routeNoEvent( Field*, int id = 0 );
00124     
00126     virtual void unroute( Field* );
00127 
00130     virtual Field *replaceRoute( Field* f, unsigned int i, int id = 0 );
00131 
00134     virtual Field *replaceRouteNoEvent( Field* f, unsigned int i, int id = 0 );
00135 
00138     template< class F >
00139     inline void route( auto_ptr< F > &f, int id = 0 ) { 
00140       route( f.get(), id ); }
00141 
00144     template< class F >
00145     inline void routeNoEvent( auto_ptr< F > &f, int id = 0 ) { 
00146       routeNoEvent( f.get(), id ); 
00147     }
00148 
00151     template< class F >
00152     void unroute( auto_ptr< F > &f ) { unroute( f.get() ); }
00153 
00155     inline void unrouteAll() {
00156       while( routes_out.begin() != routes_out.end() ) {
00157         unroute( *routes_out.begin() );
00158       }
00159     }
00160 
00163     virtual void upToDate();
00164     
00167     inline bool isUpToDate() {
00168       return event.ptr == NULL;
00169     }
00170 
00172     virtual void touch();
00173 
00175     inline Node *getOwner() {
00176       return owner;
00177     }
00178 
00180     void setOwner( Node *n );
00181 
00183     inline void setAccessType( AccessType _access_type ) {
00184       access_type = _access_type;
00185     }
00186 
00188     inline AccessType getAccessType() {
00189       return access_type;
00190     }
00191 
00192     typedef vector< Field * > FieldSet;
00193     typedef vector< Field * > FieldVector;
00194 
00197     inline bool routesTo( Field *f ) {
00198       for( FieldSet::const_iterator i = routes_out.begin();
00199            i != routes_out.end(); i++ ) {
00200         if( (*i) == f ) return true;
00201       }
00202       return false;
00203     }
00204 
00207     inline bool hasRouteFrom( Field *f ) {
00208       for( FieldVector::const_iterator i = routes_in.begin();
00209            i != routes_in.end(); i++ ) {
00210         if( (*i) == f ) return true;
00211       }
00212       return false;
00213     }
00214 
00216     inline const FieldVector &getRoutesIn() {
00217       return routes_in;
00218     }
00219     
00221     inline const FieldSet &getRoutesOut() {
00222       return routes_out;
00223     }
00224 
00226     inline const Event &getLatestEvent() {
00227       return event;
00228     }
00229 
00230   protected:
00234     virtual void startEvent();
00235 
00237     virtual void propagateEvent( Event e );
00238     
00240     virtual void routeFrom( Field*, int id );
00241 
00243     virtual void unrouteFrom( Field* );
00244 
00246     virtual Field *replaceRouteFrom( Field* f, unsigned int i, int id );
00247 
00250     virtual void update() {}
00251 
00255     void checkAccessTypeRoute( Field *f, int id );
00256 
00260     void checkAccessTypeRouteFrom( Field *f, int id );
00261 
00264     void checkAccessTypeGet( int id );
00265 
00268     void checkAccessTypeSet( int id );
00269 
00271     int ownerId();
00272 
00274     string name;
00275 
00278     bool event_lock;
00279 
00282     bool update_lock;
00283 
00285     FieldSet routes_out;
00287     FieldVector routes_in;
00289     Event event;
00292     Node *owner;
00294     AccessType access_type;
00295   };
00296 
00299   class H3DAPI_API ParsableField: public Field {
00300   public:
00304     virtual void setValueFromString( const string &s ) = 0;
00307     inline virtual string getValueAsString( const string& separator = " " ) {
00308       return "";
00309     }
00310   };
00311 
00314   class H3DAPI_API ParsableMField: public ParsableField {
00315   public:
00319     virtual size_t getSize( ) = 0;
00322     inline virtual string getElementAsString( size_t element ) {
00323       return "";
00324     }
00326     inline virtual void addElementFromString( const string &s ) = 0;
00327   };
00328 
00329 
00330 }
00331 
00332 #endif
00333 

Generated on Thu Aug 24 12:38:32 2006 for H3D API by  doxygen 1.4.5