manual_constructor.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_GPRPP_MANUAL_CONSTRUCTOR_H
20 #define GRPC_CORE_LIB_GPRPP_MANUAL_CONSTRUCTOR_H
21 
22 // manually construct a region of memory with some type
23 
25 
26 #include <stddef.h>
27 
28 #include <type_traits>
29 #include <utility>
30 
32 
33 namespace grpc_core {
34 
35 // this contains templated helpers needed to implement the ManualConstructors
36 // in this file.
37 namespace manual_ctor_impl {
38 
39 // is_one_of returns true it a class, Member, is present in a variadic list of
40 // classes, List.
41 template <class Member, class... List>
42 class is_one_of;
43 
44 template <class Member, class... List>
45 class is_one_of<Member, Member, List...> {
46  public:
47  static constexpr const bool value = true;
48 };
49 
50 template <class Member, class A, class... List>
51 class is_one_of<Member, A, List...> {
52  public:
53  static constexpr const bool value = is_one_of<Member, List...>::value;
54 };
55 
56 template <class Member>
57 class is_one_of<Member> {
58  public:
59  static constexpr const bool value = false;
60 };
61 
62 // max_size_of returns sizeof(Type) for the largest type in the variadic list
63 // of classes, Types.
64 template <class... Types>
66 
67 template <class A>
68 class max_size_of<A> {
69  public:
70  static constexpr const size_t value = sizeof(A);
71 };
72 
73 template <class A, class... B>
74 class max_size_of<A, B...> {
75  public:
76  static constexpr const size_t value = sizeof(A) > max_size_of<B...>::value
77  ? sizeof(A)
78  : max_size_of<B...>::value;
79 };
80 
81 // max_size_of returns alignof(Type) for the largest type in the variadic list
82 // of classes, Types.
83 template <class... Types>
85 
86 template <class A>
87 class max_align_of<A> {
88  public:
89  static constexpr const size_t value = alignof(A);
90 };
91 
92 template <class A, class... B>
93 class max_align_of<A, B...> {
94  public:
95  static constexpr const size_t value = alignof(A) > max_align_of<B...>::value
96  ? alignof(A)
97  : max_align_of<B...>::value;
98 };
99 
100 } // namespace manual_ctor_impl
101 
102 template <typename Type>
104  public:
105  // No constructor or destructor because one of the most useful uses of
106  // this class is as part of a union, and members of a union could not have
107  // constructors or destructors till C++11. And, anyway, the whole point of
108  // this class is to bypass constructor and destructor.
109 
110  Type* get() { return reinterpret_cast<Type*>(&space_); }
111  const Type* get() const { return reinterpret_cast<const Type*>(&space_); }
112 
113  Type* operator->() { return get(); }
114  const Type* operator->() const { return get(); }
115 
116  Type& operator*() { return *get(); }
117  const Type& operator*() const { return *get(); }
118 
119  void Init() { Construct(get()); }
120 
121  // Init() constructs the Type instance using the given arguments
122  // (which are forwarded to Type's constructor).
123  //
124  // Note that Init() with no arguments performs default-initialization,
125  // not zero-initialization (i.e it behaves the same as "new Type;", not
126  // "new Type();"), so it will leave non-class types uninitialized.
127  template <typename... Ts>
128  void Init(Ts&&... args) {
129  Construct(get(), std::forward<Ts>(args)...);
130  }
131 
132  // Init() that is equivalent to copy and move construction.
133  // Enables usage like this:
134  // ManualConstructor<std::vector<int>> v;
135  // v.Init({1, 2, 3});
136  void Init(const Type& x) { Construct(get(), x); }
137  void Init(Type&& x) { Construct(get(), std::forward<Type>(x)); }
138 
139  void Destroy() { Destruct(get()); }
140 
141  private:
142  typename std::aligned_storage<sizeof(Type), alignof(Type)>::type space_;
143 };
144 
145 } // namespace grpc_core
146 
147 #endif // GRPC_CORE_LIB_GPRPP_MANUAL_CONSTRUCTOR_H
Type
struct Type Type
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:673
grpc_core::ManualConstructor::get
Type * get()
Definition: manual_constructor.h:110
grpc_core::ManualConstructor::Init
void Init(Type &&x)
Definition: manual_constructor.h:137
grpc_core::ManualConstructor::operator->
const Type * operator->() const
Definition: manual_constructor.h:114
grpc_core::ManualConstructor::operator*
Type & operator*()
Definition: manual_constructor.h:116
grpc_core::ManualConstructor::space_
std::aligned_storage< sizeof(Type), alignof(Type)>::type space_
Definition: manual_constructor.h:142
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::ManualConstructor::Init
void Init(Ts &&... args)
Definition: manual_constructor.h:128
grpc_core::manual_ctor_impl::max_align_of
Definition: manual_constructor.h:84
grpc_core::ManualConstructor::operator*
const Type & operator*() const
Definition: manual_constructor.h:117
grpc_core::ManualConstructor::Destroy
void Destroy()
Definition: manual_constructor.h:139
testing::Types
internal::ProxyTypeList< Ts... > Types
Definition: boringssl-with-bazel/src/third_party/googletest/include/gtest/internal/gtest-type-util.h:183
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
grpc_core::ManualConstructor::Init
void Init()
Definition: manual_constructor.h:119
grpc_core::ManualConstructor::get
const Type * get() const
Definition: manual_constructor.h:111
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
grpc_core::ManualConstructor::Init
void Init(const Type &x)
Definition: manual_constructor.h:136
grpc_core::Construct
void Construct(T *p, Args &&... args)
Definition: construct_destruct.h:34
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::Destruct
void Destruct(T *p)
Definition: construct_destruct.h:27
grpc_core::manual_ctor_impl::max_size_of
Definition: manual_constructor.h:65
grpc_core::manual_ctor_impl::is_one_of
Definition: manual_constructor.h:42
A
Definition: miscompile_with_no_unique_address_test.cc:23
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
construct_destruct.h
grpc_core::ManualConstructor::operator->
Type * operator->()
Definition: manual_constructor.h:113
grpc_core::ManualConstructor
Definition: manual_constructor.h:103
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:30