#ifndef _THREAD_THREADSTORAGE_HPP_ #define _THREAD_THREADSTORAGE_HPP_ #ifndef _COMMON_WINDOWS_HPP_ #include #endif #ifndef _COMMON_SMARTPOINTER_HPP_ #include #endif #ifndef _COMMON_PUREDWORD_HPP_ #include #endif #ifndef _BSPTREE_BINARYTREE_HPP_ #include #endif template class ThreadStorage { public: ThreadStorage(void); ThreadStorage(const ThreadStorage &threadStorage); virtual ~ThreadStorage(); bool operator==(const ThreadStorage &threadStorage)const; bool operator<(const ThreadStorage &threadStorage)const; bool operator>(const ThreadStorage &threadStorage)const; T &getData(void); void setData(const T &data); DWORD getThreadId(void)const; void setThreadId(DWORD threadId); private: T mData; DWORD mThreadId; }; template inline ThreadStorage::ThreadStorage(void) : mThreadId(0) { } template inline ThreadStorage::ThreadStorage(const ThreadStorage &threadStorage) { *this=threadStorage; } template inline ThreadStorage::~ThreadStorage() { } template inline bool ThreadStorage::operator==(const ThreadStorage &threadStorage)const { return mThreadId==threadStorage.mThreadId; } template inline bool ThreadStorage::operator<(const ThreadStorage &threadStorage)const { return mThreadId inline bool ThreadStorage::operator>(const ThreadStorage &threadStorage)const { return mThreadId inline T &ThreadStorage::getData(void) { return mData; } template inline void ThreadStorage::setData(const T &data) { mData=data; } template inline DWORD ThreadStorage::getThreadId(void)const { return mThreadId; } template inline void ThreadStorage::setThreadId(DWORD threadId) { mThreadId=threadId; } template class ThreadLocalStorage : private BinaryTree > { public: ThreadLocalStorage(); bool getThreadData(SmartPointer &threadData); bool haveThreadData(void); void removeThreadData(void); bool addThreadData(const T &data); private: ThreadLocalStorage(const ThreadLocalStorage &threadLocalStorage); ThreadLocalStorage &operator=(const ThreadLocalStorage &threadLocalaStorage); }; template inline ThreadLocalStorage::ThreadLocalStorage() { } template inline ThreadLocalStorage::ThreadLocalStorage(const ThreadLocalStorage &threadLocalStorage) { // copy constructor is private } template inline ThreadLocalStorage &ThreadLocalStorage::operator=(const ThreadLocalStorage &threadLocalaStorage) { // assignment operator is private return *this; } template inline bool ThreadLocalStorage::getThreadData(SmartPointer &threadData) { ThreadStorage threadStorage; SmartPointer > pThreadStorage; threadStorage.setThreadId(::GetCurrentThreadId()); if(!searchItem(threadStorage,pThreadStorage))return false; threadData=&pThreadStorage->getData(); return true; } template inline bool ThreadLocalStorage::haveThreadData(void) { ThreadStorage threadStorage; threadStorage.setThreadId(::GetCurrentThreadId()); return searchItem(threadStorage); } template inline void ThreadLocalStorage::removeThreadData(void) { if(!haveThreadData())return; ThreadStorage threadStorage; threadStorage.setThreadId(::GetCurrentThreadId()); remove(threadStorage); } template inline bool ThreadLocalStorage::addThreadData(const T &data) { if(haveThreadData())return false; ThreadStorage threadStorage; threadStorage.setThreadId(::GetCurrentThreadId()); threadStorage.setData(data); return insert(threadStorage); } #endif