Coverage Report

Created: 2024-04-30 09:35

src/zserio/AllocatorHolder.h
Line
Count
Source
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
class AllocatorHolder
15
{
16
public:
17
    using allocator_type = ALLOC;
18
19
    /**
20
     * Empty constructor.
21
     */
22
    AllocatorHolder() :
23
            m_allocator(allocator_type())
24
1
    {}
25
26
    /**
27
     * Constructor from given allocator.
28
     *
29
     * \param allocator Allocator to be stored.
30
     */
31
    explicit AllocatorHolder(const allocator_type& allocator) :
32
            m_allocator(allocator)
33
27.5k
    {}
34
35
    /**
36
     * Constructor from given allocator.
37
     *
38
     * \param allocator Allocator to be stored.
39
     */
40
    explicit AllocatorHolder(allocator_type&& allocator) :
41
            m_allocator(std::move(allocator))
42
173
    {}
43
44
    /**
45
     * Method generated by default.
46
     * \{
47
     */
48
27.7k
    ~AllocatorHolder() = default;
49
50
    AllocatorHolder(const AllocatorHolder& other) = default;
51
    AllocatorHolder& operator=(const AllocatorHolder& other) = default;
52
53
220
    AllocatorHolder(AllocatorHolder&& other) = default;
54
17
    AllocatorHolder& operator=(AllocatorHolder&& other) = default;
55
    /**
56
     * \}
57
     */
58
59
    /**
60
     * Allocator getter.
61
     *
62
     * \return Copy of the stored allocator.
63
     */
64
    allocator_type get_allocator() const
65
42.9k
    {
66
42.9k
        return get_allocator_ref();
67
42.9k
    }
68
69
protected:
70
    /**
71
     * Allocator setter.
72
     *
73
     * \param allocator Allocator to be copy-assigned to the stored allocator.
74
     */
75
    void set_allocator(const allocator_type& allocator)
76
18
    {
77
18
        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
    void set_allocator(allocator_type&& allocator)
86
48.9k
    {
87
48.9k
        m_allocator = std::move(allocator);
88
48.9k
    }
89
90
    /**
91
     * Allocator getter.
92
     *
93
     * \return Reference to the stored allocator.
94
     */
95
    allocator_type& get_allocator_ref()
96
197k
    {
97
197k
        return m_allocator;
98
197k
    }
99
100
    /**
101
     * Allocator getter.
102
     *
103
     * \return Reference to the stored allocator.
104
     */
105
    const allocator_type& get_allocator_ref() const
106
42.9k
    {
107
42.9k
        return m_allocator;
108
42.9k
    }
109
110
private:
111
    ALLOC m_allocator;
112
};
113
114
} // namespace zserio
115
116
#endif // ifndef ZSERIO_ALLOCATOR_HOLDER_H_INC