// *************************************************************** // Function: IdGenerator.h // *************************************************************** #ifndef ID_GENERATOR_H_ #define ID_GENERATOR_H_ #include #include template class IdGenerator { public: IdGenerator(); ~IdGenerator(); void Init(); T GetFreeID(); T PeekFreeID(); void FreeID(T ID); void SetBounds(unsigned long lower, unsigned long upper); void FreeAll(); private: T counter; std::list inUse; std::list freed; unsigned long maxValue; T type; }; //************************************ // Name: SetBounds // // Sets the boundary for the id. I.e. the id's generates cannot be less than /lower/ or greater than /upper/. // // Parameters: // lower - lower boundary // upper - upper boundary // // Notes: // o Should be called after Init() // // Return: // //************************************ template void IdGenerator::SetBounds(unsigned long lower, unsigned long upper) { counter=(T)lower; maxValue=upper; } template IdGenerator::IdGenerator() { maxValue=(T)pow((float)2, (float)sizeof(type)*8); counter=5; } template IdGenerator::~IdGenerator() { this->freed.clear(); this->inUse.clear(); } //************************************ // Name: Init // // Sets the max value the id can be and sets the current value to 5. // // Parameters: // // Return: // //************************************ template void IdGenerator::Init() { maxValue=(unsigned long)pow((float)2, (float)sizeof(type)*8); counter=5; } //************************************ // Name: GetFreeID // // Gets an unused id // // Parameters: // // Return: // NOT 2 - Returns a free id and sets that id in-use // 2 - No more id's available //************************************ template T IdGenerator::GetFreeID() { if (freed.size()>0) { T temp; temp=freed.front(); freed.pop_front(); inUse.push_back(temp); return temp; } else { T temp; temp=counter; if ((unsigned long)(counter+1)>(maxValue-1)) { return 2; } counter++; inUse.push_back(temp); return temp; } } //************************************ // Name: PeekFreeID // // Peeks at the next available id. Does not set them to in-use. // // Parameters: // // Return: // NOT 2 - A free id // 2 - No more id's available //************************************ template T IdGenerator::PeekFreeID() { if (freed.size()>0) { T temp; temp=freed.front(); return temp; } else { T temp; temp=counter; if ((unsigned long)(counter+1)>(maxValue-1)) { return 2; } return temp; } } //************************************ // Name: FreeAll // // Releases all id's in-use by the id manager // // Parameters: // // Return: // //************************************ template void IdGenerator::FreeAll() { inUse.clear(); freed.clear(); } //************************************ // Name: FreeID // // Free /ID/ so it can be used again. // // Parameters: // ID - id number // // Return: // //************************************ template void IdGenerator::FreeID(T ID) { std::list::iterator itr; for (itr=inUse.begin(); itr!=inUse.end(); itr++) { if (*itr==ID) { inUse.erase(itr); freed.push_back(ID); return; } } } #endif