2009-10-08 23:23:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <limits>
|
2010-05-28 16:24:53 +00:00
|
|
|
#include <memory>
|
2009-10-08 23:23:35 +00:00
|
|
|
|
2010-05-28 16:24:53 +00:00
|
|
|
extern void _createHeap_wxString();
|
2009-10-08 23:23:35 +00:00
|
|
|
extern void _destroyHeap_wxString();
|
|
|
|
extern void* _allocHeap_wxString( size_t size );
|
2010-05-28 16:24:53 +00:00
|
|
|
extern void* _reallocHeap_wxString( void* original, size_t size );
|
2009-10-08 23:23:35 +00:00
|
|
|
extern void _freeHeap_wxString( void* ptr );
|
2010-05-28 16:24:53 +00:00
|
|
|
extern char* _mswHeap_Strdup( const char* src );
|
|
|
|
extern wchar_t* _mswHeap_Strdup( const wchar_t* src );
|
2009-10-08 23:23:35 +00:00
|
|
|
|
2010-05-28 16:24:53 +00:00
|
|
|
|
|
|
|
extern void _createHeap_wxObject();
|
2009-10-08 23:23:35 +00:00
|
|
|
extern void _destroyHeap_wxObject();
|
|
|
|
extern void* _allocHeap_wxObject( size_t size );
|
2010-05-28 16:24:53 +00:00
|
|
|
extern void* _reallocHeap_wxObject( void* original, size_t size );
|
2009-10-08 23:23:35 +00:00
|
|
|
extern void _freeHeap_wxObject( void* ptr );
|
|
|
|
|
2010-05-28 16:24:53 +00:00
|
|
|
// _pxDestroy_ uses microsoft's internal definition for _Destroy(), found in xmemory;
|
|
|
|
// which suppresses a warning that we otherwise get. The warning appears to be specific
|
|
|
|
// to MSVC, so a basic _MSC_VER define will hopefully do the trick to allow support to
|
|
|
|
// non-Micrsoft compilers.
|
|
|
|
template<class T> inline
|
|
|
|
void _pxDestroy_(T* _Ptr)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
2010-06-04 19:50:31 +00:00
|
|
|
std::_Destroy( _Ptr );
|
2010-05-28 16:24:53 +00:00
|
|
|
#else
|
|
|
|
(_Ptr)->~T();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// wxStringAllocator
|
|
|
|
// --------------------------------------------------------------------------------------
|
2009-10-08 23:23:35 +00:00
|
|
|
template<typename T>
|
|
|
|
class wxStringAllocator
|
|
|
|
{
|
2010-04-25 00:31:27 +00:00
|
|
|
public :
|
2009-10-08 23:23:35 +00:00
|
|
|
// typedefs
|
|
|
|
|
2010-05-28 16:24:53 +00:00
|
|
|
typedef T value_type;
|
|
|
|
typedef value_type* pointer;
|
|
|
|
typedef const value_type* const_pointer;
|
|
|
|
typedef value_type& reference;
|
|
|
|
typedef const value_type& const_reference;
|
|
|
|
typedef std::size_t size_type;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
2009-10-08 23:23:35 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
public :
|
2009-10-08 23:23:35 +00:00
|
|
|
// convert an allocator<T> to allocator<U>
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
struct rebind {
|
|
|
|
typedef wxStringAllocator<U> other;
|
|
|
|
};
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
public :
|
2009-10-08 23:23:35 +00:00
|
|
|
wxStringAllocator()
|
|
|
|
{
|
2010-05-28 16:24:53 +00:00
|
|
|
_createHeap_wxString();
|
2009-10-08 23:23:35 +00:00
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-10-08 23:23:35 +00:00
|
|
|
~wxStringAllocator()
|
|
|
|
{
|
|
|
|
_destroyHeap_wxString();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxStringAllocator(wxStringAllocator const&) {}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
explicit wxStringAllocator(wxStringAllocator<U> const&) {}
|
|
|
|
|
|
|
|
// address
|
|
|
|
|
|
|
|
pointer address(reference r) { return &r; }
|
|
|
|
const_pointer address(const_reference r) { return &r; }
|
|
|
|
|
|
|
|
// memory allocation
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
pointer allocate(size_type cnt,
|
2009-10-08 23:23:35 +00:00
|
|
|
typename std::allocator<void>::const_pointer = 0)
|
2010-04-25 00:31:27 +00:00
|
|
|
{
|
2009-10-08 23:23:35 +00:00
|
|
|
return reinterpret_cast<pointer>( _allocHeap_wxString(cnt * sizeof(T)) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(pointer p, size_type)
|
2010-04-25 00:31:27 +00:00
|
|
|
{
|
2009-10-08 23:23:35 +00:00
|
|
|
_freeHeap_wxString( p );
|
|
|
|
}
|
|
|
|
|
|
|
|
// size
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
size_type max_size() const {
|
2009-10-08 23:23:35 +00:00
|
|
|
return std::numeric_limits<size_type>::max() / sizeof(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
// construction/destruction
|
|
|
|
|
|
|
|
// standard placement-new syntax to initialize the object:
|
|
|
|
void construct(pointer p, const T& t) { new(p) T(t); }
|
|
|
|
// standard placement destructor:
|
2010-05-28 16:24:53 +00:00
|
|
|
void destroy(pointer p) { _pxDestroy_(p); }
|
2009-10-08 23:23:35 +00:00
|
|
|
|
|
|
|
bool operator==(wxStringAllocator const&) { return true; }
|
|
|
|
bool operator!=(wxStringAllocator const& a) { return !operator==(a); }
|
2010-04-25 00:31:27 +00:00
|
|
|
}; // end of class Allocator
|
2009-10-08 23:23:35 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
2010-05-28 16:24:53 +00:00
|
|
|
// wxObjectAllocator
|
2009-10-08 23:23:35 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
|
|
class wxObjectAllocator
|
|
|
|
{
|
2010-04-25 00:31:27 +00:00
|
|
|
public :
|
2009-10-08 23:23:35 +00:00
|
|
|
// typedefs
|
|
|
|
|
2010-05-28 16:24:53 +00:00
|
|
|
typedef T value_type;
|
|
|
|
typedef value_type* pointer;
|
|
|
|
typedef const value_type* const_pointer;
|
|
|
|
typedef value_type& reference;
|
|
|
|
typedef const value_type& const_reference;
|
|
|
|
typedef std::size_t size_type;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
2009-10-08 23:23:35 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
public :
|
2009-10-08 23:23:35 +00:00
|
|
|
// convert an allocator<T> to allocator<U>
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
struct rebind {
|
|
|
|
typedef wxObjectAllocator<U> other;
|
|
|
|
};
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
public :
|
2009-10-08 23:23:35 +00:00
|
|
|
wxObjectAllocator()
|
|
|
|
{
|
2010-05-28 16:24:53 +00:00
|
|
|
_createHeap_wxObject();
|
2009-10-08 23:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~wxObjectAllocator()
|
|
|
|
{
|
|
|
|
_destroyHeap_wxObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxObjectAllocator(wxObjectAllocator const&) {}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
explicit wxObjectAllocator(wxObjectAllocator<U> const&) {}
|
|
|
|
|
|
|
|
// address
|
|
|
|
|
|
|
|
pointer address(reference r) { return &r; }
|
|
|
|
const_pointer address(const_reference r) { return &r; }
|
|
|
|
|
|
|
|
// memory allocation
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
pointer allocate(size_type cnt,
|
2009-10-08 23:23:35 +00:00
|
|
|
typename std::allocator<void>::const_pointer = 0)
|
2010-04-25 00:31:27 +00:00
|
|
|
{
|
2009-10-08 23:23:35 +00:00
|
|
|
return reinterpret_cast<pointer>( _allocHeap_wxObject(cnt * sizeof(T)) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(pointer p, size_type)
|
2010-04-25 00:31:27 +00:00
|
|
|
{
|
2009-10-08 23:23:35 +00:00
|
|
|
_freeHeap_wxObject( p );
|
|
|
|
}
|
|
|
|
|
|
|
|
// size
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
size_type max_size() const {
|
2009-10-08 23:23:35 +00:00
|
|
|
return std::numeric_limits<size_type>::max() / sizeof(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
// construction/destruction
|
|
|
|
|
|
|
|
// standard placement-new syntax to initialize the object:
|
|
|
|
void construct(pointer p, const T& t) { new(p) T(t); }
|
2010-05-28 16:24:53 +00:00
|
|
|
|
2009-10-08 23:23:35 +00:00
|
|
|
// standard placement destructor:
|
2010-05-28 16:24:53 +00:00
|
|
|
void destroy(pointer p) { _pxDestroy_(p); }
|
2009-10-08 23:23:35 +00:00
|
|
|
|
|
|
|
bool operator==(wxObjectAllocator const&) { return true; }
|
|
|
|
bool operator!=(wxObjectAllocator const& a) { return !operator==(a); }
|
2010-04-25 00:31:27 +00:00
|
|
|
}; // end of class Allocator
|
2009-10-08 23:23:35 +00:00
|
|
|
|