00001 00010 #ifndef INSTANCEPOOL_H 00011 #define INSTANCEPOOL_H 00012 00013 #include <map> 00014 #include <list> 00015 #include <stack> 00016 #include <boost/shared_ptr.hpp> 00017 #include "TemplateTable.h" 00018 #include "VariableTableTemplate.h" 00019 #include "VariableTableInstance.h" 00020 #include "Variable.h" 00021 #include "util/Error.h" 00022 00023 namespace green 00024 { 00031 class InstancePool 00032 { 00033 public: 00034 virtual ~InstancePool() {} 00035 00040 ID AddInstance(VariableTableInstancePtr inst) 00041 { 00042 CHECK_SHARED(inst); 00043 inst->SetID(this->_getUnusedID()); 00044 mIDMap[inst->GetID()] = inst; 00045 return inst->GetID(); 00046 } 00047 00052 bool HasInstance(ID id) const 00053 { 00054 _idMap::const_iterator i = mIDMap.find(id); 00055 return(i != mIDMap.end()); 00056 } 00057 00062 VariableTableInstancePtr GetInstance(ID id) const 00063 { 00064 _idMap::const_iterator i = mIDMap.find(id); 00065 if(i == mIDMap.end()) 00066 THROW_ERROR("that id does not exist in this pool"); 00067 return i->second; 00068 } 00069 00073 void RemoveInstance(ID id) 00074 { 00075 _idMap::iterator i = mIDMap.find(id); 00076 ERROR_ASSERT(i != mIDMap.end(), "that id does not exist in this pool"); 00077 mReusedIDs.push(i->second->GetID()); 00078 mIDMap.erase(id); 00079 } 00080 00081 protected: 00083 ID _getUnusedID() 00084 { 00085 if(mReusedIDs.empty()) 00086 return mIDMap.size(); 00087 else { 00088 ID ret = mReusedIDs.top(); 00089 mReusedIDs.pop(); 00090 return ret; 00091 } 00092 } 00093 00095 typedef std::map<ID, VariableTableInstancePtr> 00096 _idMap; 00097 00099 _idMap mIDMap; 00100 00102 std::stack<ID> mReusedIDs; 00103 }; 00104 typedef boost::shared_ptr<InstancePool> InstancePoolPtr; 00105 #define INSTANCE_POOL_CAST(x) boost::dynamic_pointer_cast<InstancePool>(x) 00106 } 00107 00108 #endif