Changeset 4933 in orxonox.OLD for orxonox/trunk/src/util
- Timestamp:
- Jul 22, 2005, 6:20:42 PM (19 years ago)
- Location:
- orxonox/trunk/src/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/util/object_manager.cc
r4932 r4933 29 29 * constructor, sets everything to zero and define factoryName 30 30 */ 31 FastFactory::FastFactory ( const char* fastFactoryName, ClassID classID)31 FastFactory::FastFactory (ClassID classID, const char* fastFactoryName) 32 32 { 33 33 this->setClassID(CL_FAST_FACTORY, "FastFactory"); … … 36 36 this->storedClassID = classID; 37 37 this->next = NULL; 38 39 this->storedDeadObjects = 0; 38 40 39 41 FastFactory::registerFastFactory(this); … … 56 58 } 57 59 58 /** 59 * add a FastFactory to the FastFactory Queue 60 * @param factory a FastFactory to be registered 61 */ 62 void FastFactory::registerFastFactory( FastFactory* factory) 60 void FastFactory::registerFastFactory(FastFactory* fastFactory) 63 61 { 64 PRINTF( 3)("Registered FastFactory for '%s'\n", factory->getName());62 PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName()); 65 63 66 64 if( FastFactory::first == NULL) 67 FastFactory::first = fa ctory;65 FastFactory::first = fastFactory; 68 66 else 69 67 { 70 68 FastFactory* tmpFac = FastFactory::first; 71 69 while( tmpFac->next != NULL) 72 {73 70 tmpFac = tmpFac->next; 74 } 75 tmpFac->setNext(factory); 71 tmpFac->setNext(fastFactory); 76 72 } 77 73 } 78 74 79 75 76 /** 77 * searches for a FastFactory 78 * @param factoryName the Name of the Factory to search for (not used) 79 * @param classID the ClassID of the FastFactory to search for 80 * @returns true if found, false otherwise. 81 */ 82 FastFactory* FastFactory::searchFastFactory(ClassID classID, const char* fastFactoryName) 83 { 84 if (FastFactory::first == NULL) 85 return NULL; 86 else 87 { 88 FastFactory* tmpFac = FastFactory::first; 89 while (tmpFac != NULL) 90 { 91 if (tmpFac->storedClassID == classID) 92 return tmpFac; 93 tmpFac = tmpFac->next; 94 } 95 } 96 return NULL; 97 } 80 98 81 99 -
orxonox/trunk/src/util/object_manager.h
r4932 r4933 30 30 * Creates a FastFactory to a Loadable FastFactory. 31 31 */ 32 #define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \33 tFastFactory<CLASS_NAME>* global_##CLASS_NAME##_FastFactory = new tFastFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID)32 //#define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \ 33 //tFastFactory<CLASS_NAME>* global_##CLASS_NAME##_FastFactory = new tFastFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID) 34 34 35 35 //! A struct, that holds Lists of Objects of a certain type. 36 t ypedefstruct FastObjectMember37 { 38 BaseObject*objectPointer;39 40 FastObjectMember * next;36 template <class T> struct FastObjectMember 37 { 38 T* objectPointer; 39 40 FastObjectMember<T>* next; 41 41 }; 42 42 43 43 //! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler. 44 /** 45 * FastFactory is needed to glue all the tFastFactory<T>'s together. 46 * Furthermore one can retrieve the corresponding tFastFactory over 47 * tFastFactory<T>* = FastFacroty::getFastFactory(ID); 48 */ 44 49 class FastFactory : public BaseObject { 45 50 46 51 public: 47 FastFactory (const char* fastFactoryName, ClassID classID);48 52 virtual ~FastFactory (); 49 53 50 54 // functions to push and pop elements of this class 51 55 BaseObject* resurect(ClassID classID); 52 56 void kill(ClassID classID, BaseObject* object); 53 57 54 virtual BaseObject* fabricate(ClassID classID) = NULL; 55 56 static void registerFastFactory(FastFactory* fastFactory); 57 static void registerFastFactory(const char* fastFactoryName, ClassID classID); 58 // retrival functions for fast Ineraction 59 //FastFactory* getFastFactory(ClassID classID); 58 60 59 61 /** @returns the first FastFactory */ … … 66 68 FastFactory* getNext() const { return this->next; }; 67 69 70 virtual BaseObject* fabricate(ClassID classID) = NULL; 71 72 private: 73 FastFactory (ClassID classID, const char* fastFactoryName = NULL); 74 static void registerFastFactory(FastFactory* fastFactory); 75 static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = NULL); 76 68 77 protected: 69 78 ClassID storedClassID; //!< The classID of the specified class. 79 unsigned int storedDeadObjects; //!< How many dead objects are stored in this class 70 80 71 81 private: … … 82 92 { 83 93 public: 84 tFastFactory(const char* fastFactoryName, ClassID fastFactory); 85 86 private: 87 virtual BaseObject* fabricate(ClassID classID); 94 static FastFactory* getFastFactory(ClassID classID, const char* fastFactoryName = NULL); 95 96 void prepare(unsigned int count); 97 98 T* resurect(); 99 void kill(T* object); 100 101 private: 102 tFastFactory(ClassID classID, const char* fastFactoryName); 103 104 T* fabricate(); 105 106 private: 107 FastObjectMember<T>* deadList; //!< A List of all stored dead Objects of this class. 108 FastObjectMember<T>* unusedContainers; //!< This is a List of unused containers, that will be reused by kill. 109 88 110 }; 89 111 … … 94 116 */ 95 117 template<class T> 96 tFastFactory<T>::tFastFactory(const char* fastFactoryName, ClassID fastFactory) : FastFactory(fastFactoryName, fastFactory) 118 tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName) 119 : FastFactory(classID, fastFactoryName) 97 120 { 98 121 PRINTF(5)("Class: %s loadable as a FastFactory\n", this->getName()); 99 } 100 101 template<class T> 102 BaseObject* tFastFactory<T>::fabricate(ClassID classID) 103 { 104 if (this->storedClassID == classID) 105 return new T (); 106 else if( getNext() != NULL) 107 return getNext()->fabricate(classID); 122 123 this->deadList = NULL; 124 this->unusedContainers = NULL; 125 } 126 127 template<class T> 128 FastFactory* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName) 129 { 130 tFastFactory<T>* tmpFac = NULL; 131 if (FastFactory::getFirst() != NULL) 132 tmpFac = FastFactory::getFirst()->searchFastFactory(classID, fastFactoryName); 133 134 if (tmpFac != NULL) 135 return tmpFac; 108 136 else 109 return NULL; 110 } 111 112 113 137 return new tFastFactory<T>; 138 } 139 140 141 template<class T> 142 T* tFastFactory<T>::fabricate() 143 { 144 FastObjectMember<T>* tmpFirstDead = this->deadList; 145 tmpFirstDead->objectPointer = new T(); 146 tmpFirstDead->next = this->deadList; 147 ++this->storedDeadObjects; 148 149 this->deadList = tmpFirstDead; 150 return this->deadList; 151 } 152 153 template<class T> 154 void tFastFactory<T>::prepare(unsigned int count) 155 { 156 if (this->storedDeadObjects + this->storedLivingObjects >= count) 157 { 158 PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName()); 159 } 160 for (int i = this->storedDeadObjects + this->storedLivingObjects; i < count; i++) 161 { 162 this->fabricate(); 163 } 164 } 165 166 template<class T> 167 T* tFastFactory<T>::resurect() 168 { 169 if (unlikely(this->deadList == NULL)) 170 { 171 PRINTF(2)("The deadList of Class %s is empty, this may be either because it has not been filled yet, or the cache is to small.\n" \ 172 "Fabricating a new %s", this->getName(), this->getName()); 173 return this->fabricate(); 174 } 175 else 176 { 177 FastObjectMember<T>* tmpC = deadList; 178 this->deadList = this->deadList->next; 179 180 tmpC->next = this->unusedContainers; 181 this->unusedContainers->tmpC; 182 183 return tmpC; 184 } 185 } 186 187 template<class T> 188 void tFastFactory<T>::kill(T* object) 189 { 190 FastObjectMember<T>* tmpC; 191 if (unlikely(this->unusedContainers == NULL)) 192 { 193 tmpC = new FastObjectMember<T>; 194 } 195 else 196 { 197 tmpC = this->unusedContainers; 198 this->unusedContainers = this->unusedContainers->next; 199 } 200 201 tmpC->next = this->deadList; 202 tmpC->objectPointer = object; 203 } 114 204 115 205 ////////////////////
Note: See TracChangeset
for help on using the changeset viewer.