1 | #include <boost/multi_array.hpp>
|
---|
2 | #include <iostream>
|
---|
3 | #include <string>
|
---|
4 | #include <boost/array.hpp>
|
---|
5 |
|
---|
6 | template<typename T>
|
---|
7 | class my_allocator : public std::allocator<T>
|
---|
8 | {
|
---|
9 | private:
|
---|
10 | std::string name_;
|
---|
11 | template<typename B> friend class my_allocator;
|
---|
12 | public:
|
---|
13 | typedef std::allocator<T> base_type;
|
---|
14 |
|
---|
15 | template<typename B>
|
---|
16 | my_allocator(const my_allocator<B> &b)
|
---|
17 | : name_(b.name_) {}
|
---|
18 |
|
---|
19 | explicit my_allocator(const std::string &name = "default", const base_type &base = base_type())
|
---|
20 | : base_type(base), name_(name) {}
|
---|
21 |
|
---|
22 | template<typename U> struct rebind
|
---|
23 | {
|
---|
24 | typedef my_allocator<U> other;
|
---|
25 | };
|
---|
26 |
|
---|
27 |
|
---|
28 | typename base_type::pointer allocate(typename base_type::size_type n)
|
---|
29 | {
|
---|
30 | typename base_type::pointer ans = base_type::allocate(n);
|
---|
31 | if (ans)
|
---|
32 | std::cout << "Allocated " << n << " bytes [name = " << name_ << ']' << std::endl;
|
---|
33 | return ans;
|
---|
34 | }
|
---|
35 |
|
---|
36 | typename base_type::pointer allocate(typename base_type::size_type n,
|
---|
37 | typename base_type::const_pointer hint)
|
---|
38 | {
|
---|
39 | typename base_type::pointer ans = base_type::allocate(n, hint);
|
---|
40 | if (ans)
|
---|
41 | std::cout << "Allocated " << n << " bytes [name = " << name_ << ']' << std::endl;
|
---|
42 | return ans;
|
---|
43 | }
|
---|
44 | };
|
---|
45 |
|
---|
46 | int main()
|
---|
47 | {
|
---|
48 | int dummy = 0;
|
---|
49 | std::vector<int, my_allocator<int> > v(&dummy, &dummy, my_allocator<int>("vector"));
|
---|
50 | v.resize(17);
|
---|
51 |
|
---|
52 | boost::array<int, 2> zero = {{0, 0}};
|
---|
53 | boost::multi_array<int, 2, my_allocator<int> > m(zero, boost::c_storage_order(), my_allocator<int>("multi_array"));
|
---|
54 |
|
---|
55 | boost::array<int, 2> dims = {{5, 3}};
|
---|
56 | m.resize(dims);
|
---|
57 | return 0;
|
---|
58 | }
|
---|