Re: Help with policy based design
Finally I am starting with the following code given by soemone on this ng :
class IConvPtr {
void* m_ptr;
public:
IConvPtr() : m_ptr(0) {}
operator void *() { return m_ptr; }
void* alloc(size_t bytes, bool fZero = false) {
HANDLE hHeapProc = GetProcessHeap();
DWORD dwFlags = (fZero == true) ? HEAP_ZERO_MEMORY : 0;
if (hHeapProc) {
return HeapAlloc(hHeapProc,dwFlags,bytes);
}
}
void dealloc(void* ptr) {
...
}
IConvPtr& operator=(wchar_t* pStringW) {
#ifndef USE_NATIVE_UNICODE
int nBufLen = ::WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)pStringW, -1,
0, 0, 0, 0);
if (nBufLen) {
LPSTR buf = (LPSTR) alloc(nBufLen+1);
if (buf) {
::WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)pStringW, -1, buf,
nBufLen, 0, 0);
m_ptr = buf;
// NOW I am supposed to deallocate but
depends how original string was allocated or even if it was allocated
}
}
#else
// We are using utf16 so nothing to do - only assign
m_ptr = pStringW;
#endif
return *this;
}
template <typename T>
IConvPtr& operator=(T aptr) {
m_ptr = aptr;
return *this;
}
};
Now the question is, it is possible to add policy based designed to
choose memory allocation. I would need to have a alloc and dealloc method.