Add construct/destruct helper functions (#26906)

* Add construct/destruct helper functions

Will be used in upcoming promise implementation

* sanitized
pull/26928/head
Craig Tiller 3 years ago committed by GitHub
parent 7ccb55ea73
commit cee1bf8532
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      BUILD
  2. 1
      build_autogenerated.yaml
  3. 2
      gRPC-C++.podspec
  4. 2
      gRPC-Core.podspec
  5. 1
      grpc.gemspec
  6. 1
      package.xml
  7. 39
      src/core/lib/gprpp/construct_destruct.h
  8. 12
      src/core/lib/gprpp/manual_constructor.h
  9. 1
      tools/doxygen/Doxyfile.c++.internal
  10. 1
      tools/doxygen/Doxyfile.core.internal

@ -709,6 +709,7 @@ grpc_cc_library(
public_hdrs = GPR_PUBLIC_HDRS,
visibility = ["@grpc:alt_gpr_base_legacy"],
deps = [
"construct_destruct",
"debug_location",
"google_api_upb",
"gpr_codegen",
@ -716,6 +717,12 @@ grpc_cc_library(
],
)
grpc_cc_library(
name = "construct_destruct",
language = "c++",
public_hdrs = ["src/core/lib/gprpp/construct_destruct.h"],
)
grpc_cc_library(
name = "gpr_codegen",
language = "c++",

@ -316,6 +316,7 @@ libs:
- src/core/lib/gpr/useful.h
- src/core/lib/gprpp/arena.h
- src/core/lib/gprpp/atomic.h
- src/core/lib/gprpp/construct_destruct.h
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/examine_stack.h
- src/core/lib/gprpp/fork.h

@ -545,6 +545,7 @@ Pod::Spec.new do |s|
'src/core/lib/gpr/useful.h',
'src/core/lib/gprpp/arena.h',
'src/core/lib/gprpp/atomic.h',
'src/core/lib/gprpp/construct_destruct.h',
'src/core/lib/gprpp/debug_location.h',
'src/core/lib/gprpp/dual_ref_counted.h',
'src/core/lib/gprpp/examine_stack.h',
@ -1207,6 +1208,7 @@ Pod::Spec.new do |s|
'src/core/lib/gpr/useful.h',
'src/core/lib/gprpp/arena.h',
'src/core/lib/gprpp/atomic.h',
'src/core/lib/gprpp/construct_destruct.h',
'src/core/lib/gprpp/debug_location.h',
'src/core/lib/gprpp/dual_ref_counted.h',
'src/core/lib/gprpp/examine_stack.h',

@ -917,6 +917,7 @@ Pod::Spec.new do |s|
'src/core/lib/gprpp/arena.cc',
'src/core/lib/gprpp/arena.h',
'src/core/lib/gprpp/atomic.h',
'src/core/lib/gprpp/construct_destruct.h',
'src/core/lib/gprpp/debug_location.h',
'src/core/lib/gprpp/dual_ref_counted.h',
'src/core/lib/gprpp/examine_stack.cc',
@ -1795,6 +1796,7 @@ Pod::Spec.new do |s|
'src/core/lib/gpr/useful.h',
'src/core/lib/gprpp/arena.h',
'src/core/lib/gprpp/atomic.h',
'src/core/lib/gprpp/construct_destruct.h',
'src/core/lib/gprpp/debug_location.h',
'src/core/lib/gprpp/dual_ref_counted.h',
'src/core/lib/gprpp/examine_stack.h',

@ -831,6 +831,7 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/gprpp/arena.cc )
s.files += %w( src/core/lib/gprpp/arena.h )
s.files += %w( src/core/lib/gprpp/atomic.h )
s.files += %w( src/core/lib/gprpp/construct_destruct.h )
s.files += %w( src/core/lib/gprpp/debug_location.h )
s.files += %w( src/core/lib/gprpp/dual_ref_counted.h )
s.files += %w( src/core/lib/gprpp/examine_stack.cc )

@ -811,6 +811,7 @@
<file baseinstalldir="/" name="src/core/lib/gprpp/arena.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/gprpp/arena.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/gprpp/atomic.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/gprpp/construct_destruct.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/gprpp/debug_location.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/gprpp/dual_ref_counted.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/gprpp/examine_stack.cc" role="src" />

@ -0,0 +1,39 @@
// Copyright 2021 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GRPC_CORE_LIB_GPRPP_CONSTRUCT_DESTRUCT_H
#define GRPC_CORE_LIB_GPRPP_CONSTRUCT_DESTRUCT_H
#include <grpc/impl/codegen/port_platform.h>
#include <utility>
namespace grpc_core {
// Call the destructor of p without having to name the type of p.
template <typename T>
void Destruct(T* p) {
p->~T();
}
// Call the constructor of p without having to name the type of p and forward
// any arguments
template <typename T, typename... Args>
void Construct(T* p, Args&&... args) {
new (p) T(std::forward<Args>(args)...);
}
} // namespace grpc_core
#endif // GRPC_CORE_LIB_GPRPP_CONSTRUCT_DESTRUCT_H

@ -31,6 +31,8 @@
#include <grpc/support/log.h>
#include "src/core/lib/gprpp/construct_destruct.h"
namespace grpc_core {
// this contains templated helpers needed to implement the ManualConstructors
@ -182,7 +184,7 @@ class ManualConstructor {
Type& operator*() { return *get(); }
const Type& operator*() const { return *get(); }
void Init() { new (&space_) Type; }
void Init() { Construct(get()); }
// Init() constructs the Type instance using the given arguments
// (which are forwarded to Type's constructor).
@ -192,17 +194,17 @@ class ManualConstructor {
// "new Type();"), so it will leave non-class types uninitialized.
template <typename... Ts>
void Init(Ts&&... args) {
new (&space_) Type(std::forward<Ts>(args)...);
Construct(get(), std::forward<Ts>(args)...);
}
// Init() that is equivalent to copy and move construction.
// Enables usage like this:
// ManualConstructor<std::vector<int>> v;
// v.Init({1, 2, 3});
void Init(const Type& x) { new (&space_) Type(x); }
void Init(Type&& x) { new (&space_) Type(std::move(x)); }
void Init(const Type& x) { Construct(get(), x); }
void Init(Type&& x) { Construct(get(), std::forward<Type>(x)); }
void Destroy() { get()->~Type(); }
void Destroy() { Destruct(get()); }
private:
typename std::aligned_storage<sizeof(Type), alignof(Type)>::type space_;

@ -1764,6 +1764,7 @@ src/core/lib/gpr/wrap_memcpy.cc \
src/core/lib/gprpp/arena.cc \
src/core/lib/gprpp/arena.h \
src/core/lib/gprpp/atomic.h \
src/core/lib/gprpp/construct_destruct.h \
src/core/lib/gprpp/debug_location.h \
src/core/lib/gprpp/dual_ref_counted.h \
src/core/lib/gprpp/examine_stack.cc \

@ -1603,6 +1603,7 @@ src/core/lib/gprpp/README.md \
src/core/lib/gprpp/arena.cc \
src/core/lib/gprpp/arena.h \
src/core/lib/gprpp/atomic.h \
src/core/lib/gprpp/construct_destruct.h \
src/core/lib/gprpp/debug_location.h \
src/core/lib/gprpp/dual_ref_counted.h \
src/core/lib/gprpp/examine_stack.cc \

Loading…
Cancel
Save