summaryrefslogtreecommitdiff
path: root/src/mongo/util/decorable.h
blob: 721604bbe6df0335dd394f1d2ec6874611d393a8 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

/**
 * This header describes a mechanism for making "decorable" types.
 *
 * A decorable type is one to which various subsystems may attach subsystem-private data, so long as
 * they declare what that data will be before any instances of the decorable type are created.
 *
 * For example, suppose you had a class Client, representing on a server a network connection to a
 * client process.  Suppose that your server has an authentication module, that attaches data to the
 * client about authentication.  If class Client looks something like this:
 *
 * class Client : public Decorable<Client>{
 * ...
 * };
 *
 * Then the authentication module, before the first client object is created, calls
 *
 *     const auto authDataDescriptor = Client::declareDecoration<AuthenticationPrivateData>();
 *
 * And stores authDataDescriptor in a module-global variable,
 *
 * And later, when it has a Client object, client, and wants to get at the per-client
 * AuthenticationPrivateData object, it calls
 *
 *    authDataDescriptor(client)
 *
 * to get a reference to the AuthenticationPrivateData for that client object.
 *
 * With this approach, individual subsystems get to privately augment the client object via
 * declarations local to the subsystem, rather than in the global client header.
 */

#pragma once

#include "mongo/util/decoration_container.h"
#include "mongo/util/decoration_registry.h"
#include <type_traits>

namespace mongo {

template <typename D>
class Decorable {
private:
    struct DecorationConstructionTag {};

public:
    template <typename T>
    class Decoration {
    public:
        Decoration() = delete;
        Decoration(DecorationConstructionTag ignored,
                   typename DecorationContainer<D>::template DecorationDescriptorWithType<T> raw)
            : _raw(std::move(raw)) {}

        T& operator()(D& d) const {
            return static_cast<Decorable&>(d)._decorations.getDecoration(this->_raw);
        }

        T& operator()(D* const d) const {
            return (*this)(*d);
        }

        const T& operator()(const D& d) const {
            return static_cast<const Decorable&>(d)._decorations.getDecoration(this->_raw);
        }

        const T& operator()(const D* const d) const {
            return (*this)(*d);
        }

        const D* owner(const T* const t) const {
            return static_cast<const D*>(getOwnerImpl(t));
        }

        D* owner(T* const t) const {
            return static_cast<D*>(getOwnerImpl(t));
        }

        const D& owner(const T& t) const {
            return *owner(&t);
        }

        D& owner(T& t) const {
            return *owner(&t);
        }

    private:
        const Decorable* getOwnerImpl(const T* const t) const {
            return *reinterpret_cast<const Decorable* const*>(
                reinterpret_cast<const unsigned char* const>(t) - _raw._raw._index);
        }

        Decorable* getOwnerImpl(T* const t) const {
            return const_cast<Decorable*>(getOwnerImpl(const_cast<const T*>(t)));
        }

        typename DecorationContainer<D>::template DecorationDescriptorWithType<T> _raw;
    };

    template <typename T>
    static Decoration<T> declareDecoration() {
        if constexpr (std::is_copy_constructible_v<D> || std::is_copy_assignable_v<D>) {
            return {DecorationConstructionTag{},
                    getRegistry()->template declareDecorationCopyable<T>()};
        } else {
            return {DecorationConstructionTag{}, getRegistry()->template declareDecoration<T>()};
        }
    }

protected:
    Decorable() : _decorations(this, getRegistry()) {}
    ~Decorable() = default;

    Decorable(const Decorable& other) : _decorations(this, getRegistry(), other._decorations) {}
    Decorable& operator=(const Decorable& rhs) {
        getRegistry()->copyAssign(&_decorations, &rhs._decorations);
        return *this;
    }

private:
    static DecorationRegistry<D>* getRegistry() {
        static DecorationRegistry<D>* theRegistry = new DecorationRegistry<D>();
        return theRegistry;
    }

    DecorationContainer<D> _decorations;
};

}  // namespace mongo