1 |
|
|
#ifndef ZSERIO_ALLOCATOR_HOLDER_H_INC |
2 |
|
|
#define ZSERIO_ALLOCATOR_HOLDER_H_INC |
3 |
|
|
|
4 |
|
|
#include <utility> |
5 |
|
|
|
6 |
|
|
namespace zserio |
7 |
|
|
{ |
8 |
|
|
|
9 |
|
|
/** |
10 |
|
|
* Base class for allocator-holding classes, so that the empty base class |
11 |
|
|
* optimization may happen. |
12 |
|
|
*/ |
13 |
|
|
template <typename ALLOC> |
14 |
|
17 |
class AllocatorHolder |
15 |
|
|
{ |
16 |
|
|
public: |
17 |
|
|
using allocator_type = ALLOC; |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* Empty constructor. |
21 |
|
|
*/ |
22 |
|
1 |
AllocatorHolder() : |
23 |
|
1 |
m_allocator(allocator_type()) |
24 |
|
1 |
{} |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* Constructor from given allocator. |
28 |
|
|
* |
29 |
|
|
* \param allocator Allocator to be stored. |
30 |
|
|
*/ |
31 |
|
27432 |
explicit AllocatorHolder(const allocator_type &allocator) : |
32 |
|
27432 |
m_allocator(allocator) |
33 |
|
27432 |
{} |
34 |
|
|
|
35 |
|
|
/** |
36 |
|
|
* Constructor from given allocator. |
37 |
|
|
* |
38 |
|
|
* \param allocator Allocator to be stored. |
39 |
|
|
*/ |
40 |
|
186 |
explicit AllocatorHolder(allocator_type&& allocator) : |
41 |
|
186 |
m_allocator(std::move(allocator)) |
42 |
|
186 |
{} |
43 |
|
|
|
44 |
|
|
/** |
45 |
|
|
* Method generated by default. |
46 |
|
|
* \{ |
47 |
|
|
*/ |
48 |
|
27660 |
~AllocatorHolder() = default; |
49 |
|
|
|
50 |
|
|
AllocatorHolder(const AllocatorHolder& other) = default; |
51 |
|
|
AllocatorHolder& operator=(const AllocatorHolder& other) = default; |
52 |
|
|
|
53 |
|
214 |
AllocatorHolder(AllocatorHolder&& other) = default; |
54 |
|
|
AllocatorHolder& operator=(AllocatorHolder&& other) = default; |
55 |
|
|
/** |
56 |
|
|
* \} |
57 |
|
|
*/ |
58 |
|
|
|
59 |
|
|
/** |
60 |
|
|
* Allocator getter. |
61 |
|
|
* |
62 |
|
|
* \return Copy of the stored allocator. |
63 |
|
|
*/ |
64 |
|
42773 |
allocator_type get_allocator() const |
65 |
|
|
{ |
66 |
|
42773 |
return get_allocator_ref(); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
protected: |
70 |
|
|
/** |
71 |
|
|
* Allocator setter. |
72 |
|
|
* |
73 |
|
|
* \param allocator Allocator to be copy-assigned to the stored allocator. |
74 |
|
|
*/ |
75 |
|
18 |
void set_allocator(const allocator_type& allocator) |
76 |
|
|
{ |
77 |
|
16 |
m_allocator = allocator; |
78 |
|
18 |
} |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* Allocator setter. |
82 |
|
|
* |
83 |
|
|
* \param allocator Allocator to be move-assigned to the stored allocator. |
84 |
|
|
*/ |
85 |
|
48883 |
void set_allocator(allocator_type&& allocator) |
86 |
|
|
{ |
87 |
|
48883 |
m_allocator = std::move(allocator); |
88 |
|
48883 |
} |
89 |
|
|
|
90 |
|
|
/** |
91 |
|
|
* Allocator getter. |
92 |
|
|
* |
93 |
|
|
* \return Reference to the stored allocator. |
94 |
|
|
*/ |
95 |
|
197183 |
allocator_type& get_allocator_ref() |
96 |
|
|
{ |
97 |
|
197183 |
return m_allocator; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* Allocator getter. |
102 |
|
|
* |
103 |
|
|
* \return Reference to the stored allocator. |
104 |
|
|
*/ |
105 |
|
42843 |
const allocator_type& get_allocator_ref() const |
106 |
|
|
{ |
107 |
|
42843 |
return m_allocator; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
private: |
111 |
|
|
ALLOC m_allocator; |
112 |
|
|
}; |
113 |
|
|
|
114 |
|
|
} // namespace zserio |
115 |
|
|
|
116 |
|
|
#endif // ifndef ZSERIO_ALLOCATOR_HOLDER_H_INC |