#ifndef INCLUDED_PLACEMENTALLOC_H_ #define INCLUDED_PLACEMENTALLOC_H_ #include template class PlacementAlloc: public std::allocator { template friend std::ostream &operator<<(std::ostream &out, PlacementAlloc const &alloc); Data *d_data; static char s_commonPool[]; static char *s_free; public: PlacementAlloc(); PlacementAlloc(Data const &data); PlacementAlloc(PlacementAlloc const &other); ~PlacementAlloc(); operator Data &(); PlacementAlloc &operator=(Data const &data); private: char *request(); }; template char PlacementAlloc::s_commonPool[1000]; template char *PlacementAlloc::s_free = PlacementAlloc::s_commonPool; template PlacementAlloc::PlacementAlloc() : d_data(0) {} template PlacementAlloc::PlacementAlloc(Data const &data) : d_data(new(request()) Data(data)) {} template PlacementAlloc::PlacementAlloc(PlacementAlloc const &other) : d_data(new(request()) Data(*other.d_data)) {} template PlacementAlloc::~PlacementAlloc() { d_data->~Data(); } template PlacementAlloc::operator Data &() { return *d_data; } template PlacementAlloc &PlacementAlloc::operator=(Data const &data) { *d_data = data; } template char *PlacementAlloc::request() { char *cp = s_free; s_free += sizeof(Data); return cp; } template inline std::ostream &operator<<(std::ostream &out, PlacementAlloc const &alloc) { return out << *alloc.d_data; } #endif