aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/store_policy.h
blob: 04aa3ca108dd90b88d4b11fcdd87114a092e13ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#pragma once 
 
#include <utility>
#include "ptr.h" 
 
template <class TBase, class TCounter> 
struct TWithRefCount: public TBase, public TRefCounted<TWithRefCount<TBase, TCounter>, TCounter> { 
    template <typename... Args> 
    inline TWithRefCount(Args&&... args) 
        : TBase(std::forward<Args>(args)...)
    { 
    } 
}; 
 
template <class T> 
struct TPtrPolicy { 
    inline TPtrPolicy(T* t)
        : T_(t) 
    { 
    } 
 
    inline T* Ptr() noexcept {
        return T_;
    }

    inline const T* Ptr() const noexcept { 
        return T_; 
    } 
 
    T* T_;
}; 
 
template <class T> 
struct TEmbedPolicy { 
    template <typename... Args> 
    inline TEmbedPolicy(Args&&... args) 
        : T_(std::forward<Args>(args)...)
    { 
    } 
 
    inline T* Ptr() noexcept { 
        return &T_; 
    } 
 
    inline const T* Ptr() const noexcept { 
        return &T_; 
    } 
 
    T T_; 
}; 
 
template <class T, class TCounter> 
struct TRefPolicy { 
    using THelper = TWithRefCount<T, TCounter>; 
 
    template <typename... Args> 
    inline TRefPolicy(Args&&... args) 
        : T_(new THelper(std::forward<Args>(args)...))
    { 
    } 
 
    inline T* Ptr() noexcept { 
        return T_.Get(); 
    } 
 
    inline const T* Ptr() const noexcept { 
        return T_.Get(); 
    } 
 
    TIntrusivePtr<THelper> T_; 
}; 
 
/**
 * Storage class that can be handy for implementing proxies / adaptors that can
 * accept both lvalues and rvalues. In the latter case it's often required to
 * extend the lifetime of the passed rvalue, and the only option is to store it
 * in your proxy / adaptor.
 *
 * Example usage:
 * \code
 * template<class T>
 * struct TProxy {
 *    TAutoEmbedOrPtrPolicy<T> Value_;
 *    // Your proxy code...
 * };
 *
 * template<class T>
 * TProxy<T> MakeProxy(T&& value) {
 *     // Rvalues are automagically moved-from, and stored inside the proxy.
 *     return {std::forward<T>(value)};
 * }
 * \endcode
 *
 * Look at `Reversed` in `adaptor.h` for real example.
 */
template <class T, bool IsReference = std::is_reference<T>::value>
struct TAutoEmbedOrPtrPolicy: TPtrPolicy<std::remove_reference_t<T>> { 
    using TBase = TPtrPolicy<std::remove_reference_t<T>>;

    TAutoEmbedOrPtrPolicy(T& reference)
        : TBase(&reference)
    {
    }
};

template <class T>
struct TAutoEmbedOrPtrPolicy<T, false>: TEmbedPolicy<T> { 
    using TBase = TEmbedPolicy<T>;

    TAutoEmbedOrPtrPolicy(T&& object)
        : TBase(std::move(object))
    {
    }
};

template <class T> 
using TAtomicRefPolicy = TRefPolicy<T, TAtomicCounter>; 
 
template <class T> 
using TSimpleRefPolicy = TRefPolicy<T, TSimpleCounter>;