00001 #ifndef IOKERNEL__STL_META_CLASS_H 00002 #define IOKERNEL__STL_META_CLASS_H 00003 00004 #include "iokernel_forwarders.h" 00005 #include "list_meta_class.h" 00006 00007 00008 #ifdef PGI_BUILD 00009 #include <new> 00010 #else 00011 #include <new.h> 00012 #endif 00013 00014 template<class T> class STLDescriptor; 00015 00016 00017 class TypeLessSTLDescriptor { 00018 public: 00019 virtual Iterator* get_iterator( ConstAddress address ) const=0; 00020 00021 virtual MetaClass* get_element_meta_class() const; 00022 virtual ConstructorFunction get_constructor_function() const; 00023 virtual DestructorFunction get_destructor_function() const; 00024 virtual size_t get_size_of_instance() const; 00025 00026 protected: 00027 // the following instance variables are initialized by the 00028 // STLDescriptor subclass 00029 MetaClass* _element_meta_class; 00030 size_t _size; 00031 ConstructorFunction _constructor_function; 00032 DestructorFunction _destructor_function; 00033 00034 TypeLessSTLDescriptor(MetaClass *element_meta_class, 00035 size_t size, 00036 ConstructorFunction constructor_function, 00037 DestructorFunction destructor_function) : 00038 _element_meta_class(element_meta_class), 00039 _size(size), 00040 _constructor_function(constructor_function), 00041 _destructor_function(destructor_function) {} 00042 private: 00043 // no definitions for these constructors 00044 TypeLessSTLDescriptor(const TypeLessSTLDescriptor &other); 00045 TypeLessSTLDescriptor &operator=(const TypeLessSTLDescriptor &other); 00046 }; 00047 00048 00049 template <class T> 00050 class STLIterator : public BaseListIterator { 00051 friend class STLDescriptor<T>; 00052 public: 00053 STLIterator( Address address, const MetaClass* elementMetaClass ) : 00054 BaseListIterator( elementMetaClass ), 00055 collectionObject( *(T*)address ) 00056 //,it (collectionObject.begin()) 00057 { 00058 it = collectionObject.begin(); 00059 00060 _is_valid = it != collectionObject.end(); 00061 } 00062 00063 virtual Address current() const { 00064 if ( _is_valid ) { 00065 return (Address)&(*it); 00066 } 00067 return 0; 00068 } 00069 00070 // iteration operations 00071 virtual void next() { 00072 if ( !_is_valid ) return; 00073 it++; _is_valid = ( it != collectionObject.end() ); 00074 } 00075 00076 virtual void previous() { 00077 if ( !_is_valid ) return; 00078 _is_valid = !( it == collectionObject.begin() ); 00079 if ( _is_valid ) it--; 00080 } 00081 00082 virtual void first() { 00083 it = collectionObject.begin(); 00084 _is_valid = it != collectionObject.end(); 00085 } 00086 00087 virtual size_t length() const { 00088 return collectionObject.size(); 00089 } 00090 00091 virtual void add( void* object ) { 00092 it = collectionObject.end(); 00093 collectionObject.insert( it, *((typename T::value_type*)object) ); 00094 it = collectionObject.end(); 00095 } 00096 00097 virtual Iterator *clone() const { 00098 // return new STLIterator<T>((T &)collectionObject, 00099 // (typename T::iterator)it); 00100 return new STLIterator<T>(_element_meta_class, 00101 collectionObject, 00102 (typename T::iterator &)it); 00103 } 00104 00105 00106 private: 00107 STLIterator(const MetaClass *element_meta_class, 00108 const T& co, 00109 typename T::iterator &i) : 00110 BaseListIterator(element_meta_class), 00111 collectionObject((T&)co),it(i) {} 00112 // We can't even consider making a const one of these 00113 // because our STL implementation can not deal with const. 00114 T& collectionObject; 00115 typename T::iterator it; 00116 private: 00117 // no definitions for these declarations 00118 STLIterator(const STLIterator<T> &other); 00119 STLIterator &operator=(const STLIterator<T> &other); 00120 00121 00122 }; 00123 00124 00125 template<class T> class STLDescriptor : public TypeLessSTLDescriptor { 00126 00127 static void STLMetaClassConstructorFunction( Address place ) { 00128 new(place) T; 00129 } 00130 00131 static void STLMetaClassDestructorFunction( const ObjectWrapper &obj ) { 00132 Address place = obj.get_address(); 00133 ((T*)place)->~T(); 00134 } 00135 00136 public: 00137 STLDescriptor( MetaClass* element_meta_class ) : 00138 TypeLessSTLDescriptor(element_meta_class, sizeof(T), 00139 STLMetaClassConstructorFunction, 00140 STLMetaClassDestructorFunction) 00141 {} 00142 00143 virtual Iterator* get_iterator( ConstAddress address ) const { 00144 return new STLIterator<T>( (Address)address, _element_meta_class ); 00145 } 00146 }; 00147 00148 00149 00150 00151 class STLMetaClass : public ListMetaClass { 00152 friend class ObjectFactory; 00153 public: 00154 STLMetaClass( LString metaClassName = LString() ); 00155 virtual ~STLMetaClass(); 00156 00157 virtual void set_descriptor( TypeLessSTLDescriptor* stl_descriptor ); 00158 00159 virtual Iterator* get_iterator( ConstAddress instance, 00160 Iterator::Contents contents = Iterator::Owned ) const; 00161 00162 virtual ConstructorFunction get_constructor_function() const; 00163 00164 virtual void set_constructor_function( ConstructorFunction constructorFunction ); 00165 00166 virtual void destruct( const ObjectWrapper &obj, 00167 bool called_from_destructor ) const; 00168 00169 static void constructor_function( void* place ); 00170 00171 static const LString &get_class_name(); 00172 00173 protected: 00174 virtual void copy_over( Address target, GenericList* source ) const; 00175 00176 private: 00177 TypeLessSTLDescriptor* _stl_descriptor; 00178 private: 00179 // no definitions for these declarations 00180 STLMetaClass(const STLMetaClass &); 00181 STLMetaClass &operator=(const STLMetaClass &); 00182 }; 00183 00184 00185 00186 00187 #endif 00188