| 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 |
|
| 24 |
#include <grpc/support/port_platform.h> |
| 25 |
|
| 26 |
#include <stddef.h> |
| 27 |
|
| 28 |
#include <utility> |
| 29 |
|
| 30 |
#include "src/core/lib/gprpp/construct_destruct.h" |
| 31 |
|
| 32 |
namespace grpc_core { |
| 33 |
|
| 34 |
// this contains templated helpers needed to implement the ManualConstructors |
| 35 |
// in this file. |
| 36 |
namespace manual_ctor_impl { |
| 37 |
|
| 38 |
// is_one_of returns true it a class, Member, is present in a variadic list of |
| 39 |
// classes, List. |
| 40 |
template <class Member, class... List> |
| 41 |
class is_one_of; |
| 42 |
|
| 43 |
template <class Member, class... List> |
| 44 |
class is_one_of<Member, Member, List...> { |
| 45 |
public: |
| 46 |
static constexpr const bool value = true; |
| 47 |
}; |
| 48 |
|
| 49 |
template <class Member, class A, class... List> |
| 50 |
class is_one_of<Member, A, List...> { |
| 51 |
public: |
| 52 |
static constexpr const bool value = is_one_of<Member, List...>::value; |
| 53 |
}; |
| 54 |
|
| 55 |
template <class Member> |
| 56 |
class is_one_of<Member> { |
| 57 |
public: |
| 58 |
static constexpr const bool value = false; |
| 59 |
}; |
| 60 |
|
| 61 |
// max_size_of returns sizeof(Type) for the largest type in the variadic list |
| 62 |
// of classes, Types. |
| 63 |
template <class... Types> |
| 64 |
class max_size_of; |
| 65 |
|
| 66 |
template <class A> |
| 67 |
class max_size_of<A> { |
| 68 |
public: |
| 69 |
static constexpr const size_t value = sizeof(A); |
| 70 |
}; |
| 71 |
|
| 72 |
template <class A, class... B> |
| 73 |
class max_size_of<A, B...> { |
| 74 |
public: |
| 75 |
static constexpr const size_t value = sizeof(A) > max_size_of<B...>::value |
| 76 |
? sizeof(A) |
| 77 |
: max_size_of<B...>::value; |
| 78 |
}; |
| 79 |
|
| 80 |
// max_size_of returns alignof(Type) for the largest type in the variadic list |
| 81 |
// of classes, Types. |
| 82 |
template <class... Types> |
| 83 |
class max_align_of; |
| 84 |
|
| 85 |
template <class A> |
| 86 |
class max_align_of<A> { |
| 87 |
public: |
| 88 |
static constexpr const size_t value = alignof(A); |
| 89 |
}; |
| 90 |
|
| 91 |
template <class A, class... B> |
| 92 |
class max_align_of<A, B...> { |
| 93 |
public: |
| 94 |
static constexpr const size_t value = alignof(A) > max_align_of<B...>::value |
| 95 |
? alignof(A) |
| 96 |
: max_align_of<B...>::value; |
| 97 |
}; |
| 98 |
|
| 99 |
} // namespace manual_ctor_impl |
| 100 |
|
| 101 |
template <typename Type> |
| 102 |
class ManualConstructor { |
| 103 |
public: |
| 104 |
// No constructor or destructor because one of the most useful uses of |
| 105 |
// this class is as part of a union, and members of a union could not have |
| 106 |
// constructors or destructors till C++11. And, anyway, the whole point of |
| 107 |
// this class is to bypass constructor and destructor. |
| 108 |
|
| 109 |
Type* get() { return reinterpret_cast<Type*>(&space_); } |
| 110 |
const Type* get() const { return reinterpret_cast<const Type*>(&space_); } |
| 111 |
|
| 112 |
Type* operator->() { return get(); } |
| 113 |
const Type* operator->() const { return get(); } |
| 114 |
|
| 115 |
Type& operator*() { return *get(); } |
| 116 |
const Type& operator*() const { return *get(); } |
| 117 |
|
| 118 |
void Init() { Construct(get()); } |
| 119 |
|
| 120 |
// Init() constructs the Type instance using the given arguments |
| 121 |
// (which are forwarded to Type's constructor). |
| 122 |
// |
| 123 |
// Note that Init() with no arguments performs default-initialization, |
| 124 |
// not zero-initialization (i.e it behaves the same as "new Type;", not |
| 125 |
// "new Type();"), so it will leave non-class types uninitialized. |
| 126 |
template <typename... Ts> |
| 127 |
void Init(Ts&&... args) { |
| 128 |
Construct(get(), std::forward<Ts>(args)...); |
| 129 |
} |
| 130 |
|
| 131 |
// Init() that is equivalent to copy and move construction. |
| 132 |
// Enables usage like this: |
| 133 |
// ManualConstructor<std::vector<int>> v; |
| 134 |
// v.Init({1, 2, 3}); |
| 135 |
void Init(const Type& x) { Construct(get(), x); } |
| 136 |
void Init(Type&& x) { Construct(get(), std::forward<Type>(x)); } |
| 137 |
|
| 138 |
void Destroy() { Destruct(get()); } |
| 139 |
|
| 140 |
private: |
| 141 |
typename std::aligned_storage<sizeof(Type), alignof(Type)>::type space_; |
| 142 |
}; |
| 143 |
|
| 144 |
} // namespace grpc_core |
| 145 |
|
| 146 |
#endif // GRPC_CORE_LIB_GPRPP_MANUAL_CONSTRUCTOR_H |