/* * namepool.h * * Created on: Jul 14, 2012 * Author: gregor */ #ifndef GLTB_NAMEPOOL_H_ #define GLTB_NAMEPOOL_H_ #include #include #include #include "GLTB/exception.h" namespace gltb { template class NamePoolBase { public: bool isNameUnique(stringtype name) { if(names.find(name)!=names.end()) { return false; } return true; } stringtype makeNameUnique(stringtype name) { if(isNameUnique(name)) { return name; } stringtype newName; int counter=1; do { std::basic_ostringstream newNameBuffer; newNameBuffer << name << counter; newName=newNameBuffer.str(); } while(!isNameUnique(newName)); return newName; } stringtype insertName(stringtype name, bool makeUnique=false) { if(!isNameUnique(name)) { if(!makeUnique) { throw Exception("name is not unique and cannot be inserted into pool","gltb::NamePool::insertName()"); } else { name=makeNameUnique(name); } } names.insert(name); return name; } private: std::set names; }; typedef NamePoolBase NamePool; typedef NamePoolBase WNamePool; } #endif /* GLTB_NAMEPOOL_H_ */