[json] Try to not generate AutoLoader destructors (#30501)

pull/30503/head
Craig Tiller 2 years ago committed by GitHub
parent 3c87019897
commit 326a1fd8fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      BUILD
  2. 87
      src/core/lib/json/json_object_loader.h
  3. 7
      src/core/lib/json/json_util.cc

@ -7271,6 +7271,7 @@ grpc_cc_library(
"json",
"json_args",
"json_object_loader",
"no_destruct",
"time",
],
)

@ -119,7 +119,7 @@ class LoaderInterface {
ErrorList* errors) const = 0;
protected:
virtual ~LoaderInterface() = default;
~LoaderInterface() = default;
};
// Loads a scalar (string or number).
@ -129,7 +129,7 @@ class LoadScalar : public LoaderInterface {
ErrorList* errors) const override;
protected:
~LoadScalar() override = default;
~LoadScalar() = default;
private:
// true if we're loading a number, false if we're loading a string.
@ -144,7 +144,7 @@ class LoadScalar : public LoaderInterface {
// Load a string.
class LoadString : public LoadScalar {
protected:
~LoadString() override = default;
~LoadString() = default;
private:
bool IsNumber() const override;
@ -155,7 +155,7 @@ class LoadString : public LoadScalar {
// Load a Duration.
class LoadDuration : public LoadScalar {
protected:
~LoadDuration() override = default;
~LoadDuration() = default;
private:
bool IsNumber() const override;
@ -166,7 +166,7 @@ class LoadDuration : public LoadScalar {
// Load a number.
class LoadNumber : public LoadScalar {
protected:
~LoadNumber() override = default;
~LoadNumber() = default;
private:
bool IsNumber() const override;
@ -176,7 +176,7 @@ class LoadNumber : public LoadScalar {
template <typename T>
class TypedLoadSignedNumber : public LoadNumber {
protected:
~TypedLoadSignedNumber() override = default;
~TypedLoadSignedNumber() = default;
private:
void LoadInto(const std::string& value, void* dst,
@ -191,7 +191,7 @@ class TypedLoadSignedNumber : public LoadNumber {
template <typename T>
class TypedLoadUnsignedNumber : public LoadNumber {
protected:
~TypedLoadUnsignedNumber() override = default;
~TypedLoadUnsignedNumber() = default;
private:
void LoadInto(const std::string& value, void* dst,
@ -205,7 +205,7 @@ class TypedLoadUnsignedNumber : public LoadNumber {
// Load a float.
class LoadFloat : public LoadNumber {
protected:
~LoadFloat() override = default;
~LoadFloat() = default;
private:
void LoadInto(const std::string& value, void* dst,
@ -219,7 +219,7 @@ class LoadFloat : public LoadNumber {
// Load a double.
class LoadDouble : public LoadNumber {
protected:
~LoadDouble() override = default;
~LoadDouble() = default;
private:
void LoadInto(const std::string& value, void* dst,
@ -251,7 +251,7 @@ class LoadVector : public LoaderInterface {
ErrorList* errors) const override;
protected:
~LoadVector() override = default;
~LoadVector() = default;
private:
virtual void* EmplaceBack(void* dst) const = 0;
@ -265,7 +265,7 @@ class LoadMap : public LoaderInterface {
ErrorList* errors) const override;
protected:
~LoadMap() override = default;
~LoadMap() = default;
private:
virtual void* Insert(const std::string& name, void* dst) const = 0;
@ -279,7 +279,7 @@ class LoadOptional : public LoaderInterface {
ErrorList* errors) const override;
protected:
~LoadOptional() override = default;
~LoadOptional() = default;
private:
virtual void* Emplace(void* dst) const = 0;
@ -301,34 +301,68 @@ class AutoLoader final : public LoaderInterface {
ErrorList* errors) const override {
T::JsonLoader(args)->LoadInto(json, args, dst, errors);
}
private:
~AutoLoader() = default;
};
// Specializations of AutoLoader for basic types.
template <>
class AutoLoader<std::string> final : public LoadString {};
class AutoLoader<std::string> final : public LoadString {
private:
~AutoLoader() = default;
};
template <>
class AutoLoader<Duration> final : public LoadDuration {};
class AutoLoader<Duration> final : public LoadDuration {
private:
~AutoLoader() = default;
};
template <>
class AutoLoader<int32_t> final : public TypedLoadSignedNumber<int32_t> {};
class AutoLoader<int32_t> final : public TypedLoadSignedNumber<int32_t> {
private:
~AutoLoader() = default;
};
template <>
class AutoLoader<int64_t> final : public TypedLoadSignedNumber<int64_t> {};
class AutoLoader<int64_t> final : public TypedLoadSignedNumber<int64_t> {
private:
~AutoLoader() = default;
};
template <>
class AutoLoader<uint32_t> final : public TypedLoadUnsignedNumber<uint32_t> {};
class AutoLoader<uint32_t> final : public TypedLoadUnsignedNumber<uint32_t> {
private:
~AutoLoader() = default;
};
template <>
class AutoLoader<uint64_t> final : public TypedLoadUnsignedNumber<uint64_t> {};
class AutoLoader<uint64_t> final : public TypedLoadUnsignedNumber<uint64_t> {
private:
~AutoLoader() = default;
};
template <>
class AutoLoader<float> final : public LoadFloat {};
class AutoLoader<float> final : public LoadFloat {
private:
~AutoLoader() = default;
};
template <>
class AutoLoader<double> final : public LoadDouble {};
class AutoLoader<double> final : public LoadDouble {
private:
~AutoLoader() = default;
};
template <>
class AutoLoader<bool> final : public LoadBool {};
class AutoLoader<bool> final : public LoadBool {
private:
~AutoLoader() = default;
};
template <>
class AutoLoader<Json::Object> final : public LoadUnprocessedJsonObject {};
class AutoLoader<Json::Object> final : public LoadUnprocessedJsonObject {
private:
~AutoLoader() = default;
};
// Specializations of AutoLoader for vectors.
template <typename T>
class AutoLoader<std::vector<T>> final : public LoadVector {
private:
~AutoLoader() = default;
void* EmplaceBack(void* dst) const final {
auto* vec = static_cast<std::vector<T>*>(dst);
vec->emplace_back();
@ -348,6 +382,9 @@ class AutoLoader<std::vector<bool>> final : public LoaderInterface {
public:
void LoadInto(const Json& json, const JsonArgs& args, void* dst,
ErrorList* errors) const override;
private:
~AutoLoader() = default;
};
// Specializations of AutoLoader for maps.
@ -362,6 +399,9 @@ class AutoLoader<std::map<std::string, T>> final : public LoadMap {
const LoaderInterface* ElementLoader() const final {
return LoaderForType<T>();
}
private:
~AutoLoader() = default;
};
// Specializations of AutoLoader for absl::optional<>.
@ -374,6 +414,9 @@ class AutoLoader<absl::optional<T>> final : public LoadOptional {
const LoaderInterface* ElementLoader() const final {
return LoaderForType<T>();
}
private:
~AutoLoader() = default;
};
// Implementation of aforementioned LoaderForType.

@ -20,16 +20,17 @@
#include "src/core/lib/json/json_util.h"
#include "src/core/lib/gprpp/no_destruct.h"
#include "src/core/lib/json/json_args.h"
#include "src/core/lib/json/json_object_loader.h"
namespace grpc_core {
bool ParseDurationFromJson(const Json& field, Duration* duration) {
json_detail::AutoLoader<Duration> loader;
ErrorList errors;
static_cast<json_detail::LoaderInterface&>(loader).LoadInto(
field, JsonArgs(), duration, &errors);
static_cast<json_detail::LoaderInterface*>(
NoDestructSingleton<json_detail::AutoLoader<Duration>>::Get())
->LoadInto(field, JsonArgs(), duration, &errors);
return errors.ok();
}

Loading…
Cancel
Save