| 1 |
#ifndef INCLUDED_NEWALLOC_H_ |
| 2 |
#define INCLUDED_NEWALLOC_H_ |
| 3 |
|
| 4 |
#include <ostream> |
| 5 |
|
| 6 |
template <typename Data> |
| 7 |
class NewAlloc: public std::allocator<Data> |
| 8 |
{ |
| 9 |
template<typename IData> |
| 10 |
friend std::ostream &operator<<(std::ostream &out, |
| 11 |
NewAlloc<IData> const &alloc); |
| 12 |
Data *d_data; |
| 13 |
|
| 14 |
public: |
| 15 |
NewAlloc(); |
| 16 |
NewAlloc(Data const &data); |
| 17 |
NewAlloc(NewAlloc<Data> const &other); |
| 18 |
~NewAlloc(); |
| 19 |
operator Data &(); |
| 20 |
NewAlloc &operator=(Data const &data); |
| 21 |
}; |
| 22 |
|
| 23 |
template<typename Data> |
| 24 |
NewAlloc<Data>::NewAlloc() |
| 25 |
: |
| 26 |
d_data(0) |
| 27 |
{} |
| 28 |
|
| 29 |
template<typename Data> |
| 30 |
NewAlloc<Data>::NewAlloc(Data const &data) |
| 31 |
: |
| 32 |
d_data(new Data(data)) |
| 33 |
{} |
| 34 |
|
| 35 |
template<typename Data> |
| 36 |
NewAlloc<Data>::NewAlloc(NewAlloc<Data> const &other) |
| 37 |
: |
| 38 |
d_data(new Data(*other.d_data)) |
| 39 |
{} |
| 40 |
|
| 41 |
template<typename Data> |
| 42 |
NewAlloc<Data>::~NewAlloc() |
| 43 |
{ |
| 44 |
delete d_data; |
| 45 |
} |
| 46 |
|
| 47 |
template<typename Data> |
| 48 |
NewAlloc<Data>::operator Data &() |
| 49 |
{ |
| 50 |
return *d_data; |
| 51 |
} |
| 52 |
|
| 53 |
template<typename Data> |
| 54 |
NewAlloc<Data> &NewAlloc<Data>::operator=(Data const &data) |
| 55 |
{ |
| 56 |
*d_data = data; |
| 57 |
} |
| 58 |
|
| 59 |
template<typename IData> |
| 60 |
inline std::ostream &operator<<(std::ostream &out, |
| 61 |
NewAlloc<IData> const &alloc) |
| 62 |
{ |
| 63 |
return out << *alloc.d_data; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
#endif |