The whole idea was to make a thread-safe class that could store memory kind of like a linked list. Problem was that the memory was stored in raw form and no type information was available, so the operator ++ had to know the type of the data (hence the template) in order to cast the memory to the correct type and increase it.
I'm thinking this is a bit of a problem and there's probably easier ways to do it. For one, storing type information might be done, but it's just that it's so prone to errors since there's so many different types of data that can be stored.
Code:
class CSyncClass
{
public:
enum eDataType
{
TYPE_CHAR,
TYPE_BYTE,
TYPE_INT16,
TYPE_UINT16,
TYPE_INT32,
TYPE_UINT32,
TYPE_INT64,
TYPE_UINT64,
TYPE_CSTRING,
TYPE_CHARP,
TYPE_BYTEP,
TYPE_INT16P,
TYPE_UINT16P,
TYPE_INT32P,
TYPE_UINT32P,
TYPE_INT64P,
TYPE_UINT64P,
TYPE_CSTRINGP
};
class CBufferValueInfo
{
CString strValueName; // Name of the stored data block
eDataType DataType; // Type of data stored
UINT32 nBlockOffset; // Offset in memory where data is stored
UINT32 nBlockSize; // Total size of the data block where the data is stored
UINT32 nDataSize; // Size of data (elements of an array) (eg if data = char & this = 4, then there's 4 chars)
bool bAllocated; // If true, then data is stored here; if false, then the block is free
};
class CValueHandler
{
private:
friend class CSyncClass;
CBufferValueInfo* pValue;
public:
//template<typename T> CValueHandler operator + (T tData);
//template<typename T> CValueHandler operator - (T tData);
//template<typename T> CValueHandler operator * (T tData);
//template<typename T> CValueHandler operator / (T tData);
template<typename T> CValueHandler& operator += (T tData);
template<typename T> CValueHandler& operator -= (T tData);
template<typename T> CValueHandler& operator *= (T tData);
template<typename T> CValueHandler& operator /= (T tData);
CValueHandler& operator ++ (); // Prefix
CValueHandler& operator -- (); // Prefix
CValueHandler operator ++ (int); // Postfix
CValueHandler operator -- (int); // Postfix
template<typename T> CValueHandler& operator = (T tData);
//template<typename T> operator T;
};
CSyncClass();
~CSyncClass();
template<typename T> void SetValue(CString strName, T tData, UINT32 nSize = 0);
template<typename T> T GetValue(CString strName, UINT32 nSize = 0);
//template<typename T> void IncValue(CString strName);
//template<typename T> void DecValue(CString strName);
const CValueHandler& operator [] (CString strName);
private:
CValueHandler m_Value;
pp<BYTE> m_pBuffer; // Array to hold memory
CMapStringToPtr m_LookupMap; // Array containing info of stored values, looked up by name
CMap<DWORD, DWORD, CBufferValueInfo*, CBufferValueInfo*> m_BlockLookupMap; // Array containing info of stored values, looked up by offset
UINT32 m_nBufferSize;
CCriticalSection cSync;
template<typename T> eDataType GetDataType(T tData);
template<typename T> DWORD GetSize(T& tData);
template<typename T> bool IsNumType(T& tData)
{
if ( typeid(tData) == typeid(UINT8) || typeid(tData) == typeid(INT8) || typeid(tData) == typeid(UINT16) || typeid(tData) == typeid(INT16) || typeid(tData) == typeid(UINT32) ||
typeid(tData) == typeid(INT32) || typeid(tData) == typeid(UINT64) || typeid(tData) == typeid(INT64) || typeid(tData) == typeid(float) || typeid(tData) == typeid(double) )
{
return true;
}
else
{
return false;
}
}
};