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