📄 sqltemp.h
字号:
//////////////////////////////////////////////////////////////////////
//
// DB2 Access Object Version 1.0
//
// Developer: Jeff Lee
// Jan 10, 2003
//
//////////////////////////////////////////////////////////////////////
#ifndef _SQL_TEMPLATE_H
#define _SQL_TEMPLATE_H
#if defined(_MSC_VER)
// use STL provided by VC
#include <xmemory>
using namespace std;
#else // defined(_MSC_VER)
// use STL provided by STLport
#include <memory> #include <stl/_alloc.h>
#endif // defined(_MSC_VER)
/////////////////////////////////////////////////////////////////////////////
// class CVaryArray - Represents a variable length array
template<class TYPE>
class CVaryArray
{
public:
typedef allocator<TYPE> _A;
typedef typename _A::size_type size_type;
typedef typename _A::reference reference;
typedef typename _A::const_reference const_reference;
typedef typename _A::pointer pointer;
protected:
_A m_allocator;
pointer m_pData;
size_type m_nMaxSize;
size_type m_nSize;
size_type m_nGrowby;
public:
// Attributes:
size_type GetSize() const
{ return m_nSize; }
size_type GetMaxSize() const
{ return m_nMaxSize; }
bool IsEmpty() const
{ return 0 == m_nSize; }
// Operations:
void Add(const_reference newElement)
{
if (m_nSize >= m_nMaxSize)
{
size_type nSize = m_nSize + max(m_nGrowby, 16);
pointer ptr = m_allocator.allocate(nSize, (void*)0);
for (UINT n=0; n<m_nSize; n++)
m_allocator.construct(&ptr[n], m_pData[n]);
swap(m_pData, ptr);
// Added on 2002-11-25, to fix the memory leak bug
for (n=(UINT)m_nMaxSize-1; n>=0; n--)
m_allocator.destroy(&ptr[n]);
m_allocator.deallocate(ptr, m_nMaxSize);
m_nMaxSize = nSize;
}
m_allocator.construct(&m_pData[m_nSize], newElement);
m_nSize++;
}
void Remove()
{
if (m_nSize > 0)
{
m_nSize--;
m_allocator.destroy(&m_pData[m_nSize]);
if (!m_nSize)
{
m_allocator.deallocate(m_pData, m_nMaxSize);
m_pData = NULL;
m_nMaxSize = 0;
}
}
}
void RemoveAll()
{
while (!IsEmpty())
Remove();
}
// Iteration:
const_reference GetAt(size_type nIndex) const
{
if (nIndex < 0) nIndex = 0;
if (nIndex >= m_nSize)
_THROW(out_of_range, "invalid CVaryArray<T> subscript");
return m_pData[nIndex];
}
reference GetAt(size_type nIndex)
{
if (nIndex < 0) nIndex = 0;
if (nIndex >= m_nSize)
_THROW(out_of_range, "invalid CVaryArray<T> subscript");
return m_pData[nIndex];
}
const_reference operator[](size_type nIndex) const
{ return GetAt(nIndex); }
reference operator[](size_type nIndex)
{ return GetAt(nIndex); }
public:
CVaryArray(size_type nGrowby=0) :
m_allocator(_A()),
m_pData(NULL),
m_nMaxSize(0),
m_nSize(0),
m_nGrowby(nGrowby)
{
}
~CVaryArray()
{
RemoveAll();
}
};
/////////////////////////////////////////////////////////////////////////////
// class CFixedString - Represents a fixed-length string
template<class TYPE>
class CFixedString
{
public:
typedef CFixedString<TYPE>& reference;
typedef const CFixedString<TYPE>& const_reference;
protected:
struct CDataBuffer
{
int nRefs;
TYPE pVal[1];
};
CDataBuffer* m_pData;
int m_nMaxSize;
protected:
void Init()
{
m_pData = NULL;
m_nMaxSize = 0;
}
int GetLength(const TYPE* pData) const
{
#if !defined(_MSC_VER)
//return char_traits<TYPE>::length(pData);
return pData != NULL ? ::strlen((const char*)pData) : 0;
#else
int nSize = 0;
if (pData != NULL)
{
TYPE null(0);
while (pData[nSize] != null)
nSize++;
}
return nSize;
#endif
}
void CopyString(const TYPE* pData, int nSize)
{
#if !defined(_MSC_VER)
//char_traits<TYPE>::copy(m_pData->pVal, pData, nSize);
::memcpy(m_pData->pVal, pData, sizeof(TYPE)*nSize);
#else
for (int n=0; n<nSize; n++)
m_pData->pVal[n] = pData[n];
#endif
}
public:
// Initialization:
void Assign(const_reference rData)
{
if (this != &rData)
{
Free();
m_pData = rData.m_pData;
m_nMaxSize = rData.m_nMaxSize;
if (m_pData != NULL)
m_pData->nRefs++;
}
}
void Assign(const TYPE* pData)
{
if (!m_pData)
Alloc(GetLength(pData));
if (m_pData != NULL)
{
int nSize = min(GetLength(pData), m_nMaxSize);
CopyString(pData, nSize);
m_pData->pVal[nSize] = TYPE(0);
}
}
reference operator=(const_reference rData)
{
Assign(rData);
return *this;
}
reference operator=(const TYPE* pData)
{
Assign(pData);
return *this;
}
// Attributes:
int GetMaxSize() const
{ return m_nMaxSize; }
TYPE* GetData()
{ return m_pData != NULL ? m_pData->pVal : NULL; }
TYPE* GetData() const
{ return m_pData != NULL ? m_pData->pVal : NULL; }
operator TYPE*()
{ return GetData(); }
operator const TYPE*() const
{ return GetData(); }
TYPE& operator[](int nIndex)
{
ASSERT(nIndex < m_nMaxSize);
return m_pData->pVal[nIndex];
}
const TYPE& operator[](int nIndex) const
{
ASSERT(nIndex < m_nMaxSize);
return m_pData->pVal[nIndex];
}
// Allocation
bool Alloc(int nSize)
{
Free();
int nTotalSize = nSize * sizeof(TYPE);
nTotalSize += sizeof(CDataBuffer);
m_pData = (CDataBuffer*) new char[nTotalSize];
if (!m_pData)
return FALSE;
m_pData->nRefs = 1;
m_nMaxSize = nSize;
return TRUE;
}
void Free()
{
if (m_pData != NULL)
{
m_pData->nRefs--;
if (!m_pData->nRefs)
delete[] (char*)m_pData;
m_pData = NULL;
m_nMaxSize = 0;
}
}
public:
CFixedString() { Init(); }
CFixedString(const_reference rData)
{
Init();
Assign(rData);
}
CFixedString(const TYPE* pData)
{
Init();
Assign(pData);
}
~CFixedString() { Free(); }
};
#if defined(_MSC_VER)
// CFixedString<char>
inline int CFixedString<char>::GetLength(const char* pData) const
{
return pData ? (int)::strlen(pData) : 0;
}
inline void CFixedString<char>::CopyString(const char* pData, int nSize)
{
::memcpy(m_pData->pVal, pData, nSize);
}
// CFixedString<unsigned char>
inline int CFixedString<unsigned char>::GetLength(const unsigned char* pData) const
{
return pData ? (int)::strlen((const char*)pData) : 0;
}
inline void CFixedString<unsigned char>::CopyString(const unsigned char* pData, int nSize)
{
::memcpy(m_pData->pVal, pData, nSize);
}
#endif // defined(_MSC_VER)
#endif // _SQL_TEMPLATE_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -