Debian Codegraph @

src/core/lib/gprpp/no_destruct.h

raw
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef GRPC_CORE_LIB_GPRPP_NO_DESTRUCT_H
16 #define GRPC_CORE_LIB_GPRPP_NO_DESTRUCT_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <utility>
21
22 #include "src/core/lib/gprpp/construct_destruct.h"
23
24 namespace grpc_core {
25
26 // NoDestruct<T> is a wrapper around an object of type T that:
27 // - stores the value inline - no heap allocation
28 // - is non-copyable
29 // - is eagerly constructed (i.e. the constructor is called when NoDestruct is
30 // constructed)
31 // - *NEVER* calls ~T()
32 // It's useful in cases where no ordering can be assumed between destructors of
33 // objects that need to refer to each other - such as at program destruction
34 // time.
35 // Examples:
36 // // globally available object:
37 // static NoDestruct<Foo> g_foo(1, "foo", 2.0);
38 // // used as:
39 // g_foo->DoSomething();
40 // // singleton function:
41 // Bar* BarSingleton() {
42 // static NoDestruct<Bar> bar(1, "bar", 2.0);
43 // return &*bar;
44 // }
45 // The globally available version is constructed at program startup, and the
46 // singleton version is constructed at the first call to BarSingleton().
47 // Neither Foo nor Bar instance will be destructed.
48 template <typename T>
49 class NoDestruct {
50 public:
51 template <typename... Args>
52 explicit NoDestruct(Args&&... args) {
53 static_assert(std::is_trivially_destructible<NoDestruct<T>>::value,
54 "NoDestruct must be trivially destructible");
55 Construct(reinterpret_cast<T*>(&space_), std::forward<Args>(args)...);
56 }
57 NoDestruct(const NoDestruct&) = delete;
58 NoDestruct& operator=(const NoDestruct&) = delete;
59 ~NoDestruct() = default;
60
61 T* operator->() { return get(); }
62 const T* operator->() const { return get(); }
63 T& operator*() { return *get(); }
64 const T& operator*() const { return *get(); }
65
66 T* get() { return reinterpret_cast<T*>(&space_); }
67 const T* get() const { return reinterpret_cast<const T*>(&space_); }
68
69 private:
70 typename std::aligned_storage<sizeof(T), alignof(T)>::type space_;
71 };
72
73 // Helper for when a program desires a single *process wide* instance of a
74 // default constructed T to be always available.
75 // The instance is constructed eagerly at program startup, so it's essentially
76 // free to load the pointer to the instance.
77 template <typename T>
78 class NoDestructSingleton {
79 public:
80 static T* Get() { return &*value_; }
81
82 private:
83 NoDestructSingleton() = delete;
84 ~NoDestructSingleton() = delete;
85
86 static NoDestruct<T> value_;
87 };
88
89 template <typename T>
90 NoDestruct<T> NoDestructSingleton<T>::value_;
91
92 } // namespace grpc_core
93
94 #endif // GRPC_CORE_LIB_GPRPP_NO_DESTRUCT_H