UGDK
|
00001 #ifndef UGDK_MODULE_PROXY_CACHE_H_ 00002 #define UGDK_MODULE_PROXY_CACHE_H_ 00003 00004 #include <map> 00005 #include <ugdk/script/virtualobj.h> 00006 # 00007 00008 namespace ugdk { 00009 namespace script { 00010 00011 00012 template <class T> 00013 class BaseProxy { 00014 public: 00015 BaseProxy(const ugdk::script::VirtualObj& proxy) : proxy_(proxy) {} 00016 ~BaseProxy() { 00017 BaseProxy::table_.erase(proxy_.unsafe_data()); 00018 } 00019 00020 ugdk::script::VirtualObj get_proxy_vobj() const { return proxy_; } 00021 00022 00023 static void Set(void* key, T* object) { BaseProxy::table_[key] = object; } 00024 00025 static bool Check(const ugdk::script::VirtualObj& proxy) { 00026 return BaseProxy::table_.count(proxy.unsafe_data()) > 0; 00027 } 00028 00029 static T* Get(const ugdk::script::VirtualObj& proxy) { 00030 void* key = proxy.unsafe_data(); 00031 T* obj; 00032 if (BaseProxy::table_.count(key) == 0) { 00033 obj = new T(proxy); 00034 Set(key, obj); 00035 } 00036 else { 00037 obj = BaseProxy::table_[key]; 00038 } 00039 return obj; 00040 } 00041 00042 static T* Get(ugdk::script::VirtualData* proxy) { 00043 void* key = proxy->unsafe_data(); 00044 T* obj; 00045 if (BaseProxy::table_.count(key) == 0) { 00046 obj = new T(VirtualObj(VirtualData::Ptr(proxy))); 00047 Set(key, obj); 00048 } 00049 else { 00050 obj = BaseProxy::table_[key]; 00051 } 00052 return obj; 00053 } 00054 00055 protected: 00056 ugdk::script::VirtualObj proxy_; 00057 00058 private: 00059 static std::map<void*, T*> table_; 00060 }; 00061 00062 template <class T> 00063 std::map<void*, T*> BaseProxy<T>::table_; 00064 00065 } 00066 } 00067 #endif