#ifndef _IV_SPACE_H_ #define _IV_SPACE_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include "uchar.h" #include "conversions.h" namespace iv { namespace core { class SpaceObject { public: template void* operator new(std::size_t size, Factory* factory) { return factory->New(size); } void operator delete(void*, std::size_t) { UNREACHABLE(); } }; template class SpaceAllocator { public: typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; typedef SpaceAllocator this_type; template struct rebind { typedef SpaceAllocator other; }; SpaceAllocator() : space_(NULL) { } explicit SpaceAllocator(Factory* factory) throw() : space_(factory) { } template SpaceAllocator(const SpaceAllocator& alloc) throw() // NOLINT : space_(alloc.space()) { } inline pointer address(reference x) const { return &x; } inline const_pointer address(const_reference x) const { return &x; } inline pointer allocate(size_type n, const void* = 0) { assert(space_); return reinterpret_cast(space_->New(n * sizeof(T))); } inline void deallocate(pointer, size_type) { } inline size_type max_size() const { return std::numeric_limits::max() / sizeof(T); } inline void construct(pointer p, const T& val) { new(reinterpret_cast(p)) T(val); } inline void destroy(pointer p) { (p)->~T(); } inline char* _Charalloc(size_type n) { return allocate(n); } template inline this_type& operator=(const SpaceAllocator& rhs) { if (this != &rhs) { this_type(rhs).Swap(*this); } return *this; } inline Factory* space() const { return space_; } private: void Swap(this_type& rhs) { using std::swap; swap(space_, rhs.space_); } void operator=(const SpaceAllocator&); Factory* space_; }; template bool operator==(const SpaceAllocator& lhs, const SpaceAllocator& rhs) { return true; } template bool operator!=(const SpaceAllocator& lhs, const SpaceAllocator& rhs) { return false; } template struct SpaceVector { typedef std::vector > type; }; template struct SpaceMap { typedef std::map, SpaceAllocator > > type; }; template struct SpaceHashMap { typedef std::tr1::unordered_map, std::equal_to, SpaceAllocator< Factory, std::pair > > type; }; template struct SpaceList { typedef std::list > type; }; template struct SpaceUString { typedef std::basic_string, SpaceAllocator > type; }; } } // namespace iv::core namespace std { namespace tr1 { // template specialization for SpaceUString in std::tr1::unordered_map // allowed in section 17.4.3.1 template struct hash, iv::core::SpaceAllocator > > { typedef std::basic_string, iv::core::SpaceAllocator > argument_type; std::size_t operator()(const argument_type& x) const { return iv::core::StringToHash(x); } }; } } // namespace std::tr1 #endif // _IV_SPACE_H_