diff options
author | babenko <babenko@yandex-team.com> | 2024-08-12 15:01:39 +0300 |
---|---|---|
committer | babenko <babenko@yandex-team.com> | 2024-08-12 15:11:46 +0300 |
commit | 63e00b040bda0f49cd303babce3a27e0bcb3a473 (patch) | |
tree | 2a25649c3d6ccff51f01a8519ce659a6b766cc03 | |
parent | e3206dbabc87736656ea7f93a10733b19df972ef (diff) | |
download | ydb-63e00b040bda0f49cd303babce3a27e0bcb3a473.tar.gz |
Move persistence logic to operator() of field registrars
5219b3fd482d3d7a10a7c7560ed5e8307a9021fd
-rw-r--r-- | yt/yt/core/phoenix/type_def-inl.h | 82 | ||||
-rw-r--r-- | yt/yt/core/phoenix/type_def.cpp | 2 | ||||
-rw-r--r-- | yt/yt/core/phoenix/unittests/phoenix_ut.cpp | 78 |
3 files changed, 76 insertions, 86 deletions
diff --git a/yt/yt/core/phoenix/type_def-inl.h b/yt/yt/core/phoenix/type_def-inl.h index a57d812b83..d0c50b17db 100644 --- a/yt/yt/core/phoenix/type_def-inl.h +++ b/yt/yt/core/phoenix/type_def-inl.h @@ -73,7 +73,9 @@ namespace NYT::NPhoenix2::NDetail { //////////////////////////////////////////////////////////////////////////////// -class TDummyFieldRegistrar +#define PHOENIX_REGISTRAR_NODISCARD [[nodiscard("Did you forget to call operator()?")]] + +class PHOENIX_REGISTRAR_NODISCARD TDummyFieldRegistrar { public: auto SinceVersion(auto /*version*/) && @@ -96,6 +98,9 @@ public: { return std::move(*this); } + + void operator()() && + { } }; class TTypeRegistrarBase @@ -120,7 +125,7 @@ public: void AfterLoad(auto&& /*handler*/) { } - void Finish() && + void operator()() && { } }; @@ -128,7 +133,7 @@ template <class TThis> decltype(auto) RunRegistrar(auto&& registrar) { TThis::RegisterMetadata(registrar); - return std::move(registrar).Finish(); + return std::move(registrar)(); } //////////////////////////////////////////////////////////////////////////////// @@ -162,7 +167,7 @@ public: TypeDescriptor_->BaseTypes_.push_back(&TBase::GetTypeDescriptor()); } - const TTypeDescriptor& Finish() &&; + const TTypeDescriptor& operator()() &&; private: std::unique_ptr<TTypeDescriptor> TypeDescriptor_ = std::make_unique<TTypeDescriptor>(); @@ -212,7 +217,7 @@ const TTypeDescriptor& RegisterTypeDescriptorImpl() template <class TThis> const TTypeDescriptor& RegisterOpaqueTypeDescriptorImpl() { - return MakeTypeSchemaBuilderRegistrar<TThis, /*Template*/ false>().Finish(); + return MakeTypeSchemaBuilderRegistrar<TThis, /*Template*/ false>()(); } //////////////////////////////////////////////////////////////////////////////// @@ -239,7 +244,7 @@ private: }; template <auto Member, class TThis, class TContext, class TFieldSerializer> -class TFieldSaveRegistrar +class PHOENIX_REGISTRAR_NODISCARD TFieldSaveRegistrar { public: TFieldSaveRegistrar(const TThis* this_, TContext& context) @@ -251,9 +256,7 @@ public: TFieldSaveRegistrar(TFieldSaveRegistrar<Member, TThis, TContext, TFieldSerializer_>&& other) : This_(other.This_) , Context_(other.Context_) - { - other.Armed_ = false; - } + { } auto SinceVersion(auto /*version*/) && { @@ -276,11 +279,9 @@ public: return TFieldSaveRegistrar<Member, TThis, TContext, TFieldSerializer_>(std::move(*this)); } - ~TFieldSaveRegistrar() + void operator()() && { - if (Armed_) { - TFieldSerializer::Save(Context_, This_->*Member); - } + TFieldSerializer::Save(Context_, This_->*Member); } private: @@ -289,8 +290,6 @@ private: const TThis* const This_; TContext& Context_; - - bool Armed_ = true; }; template <class TThis, class TContext> @@ -347,7 +346,7 @@ private: }; template <auto Member, class TThis, class TContext, class TFieldSerializer> -class TFieldLoadRegistrar +class PHOENIX_REGISTRAR_NODISCARD TFieldLoadRegistrar { public: TFieldLoadRegistrar(TThis* this_, TContext& context, TStringBuf name) @@ -364,9 +363,7 @@ public: , MinVersion_(other.MinVersion_) , VersionFilter_(other.VersionFilter_) , MissingHandler_(other.MissingHandler_) - { - other.Armed_ = false; - } + { } using TVersion = decltype(std::declval<typename TThis::TLoadContextImpl>().GetVersion()); @@ -399,17 +396,15 @@ public: return TFieldLoadRegistrar<Member, TThis, TContext, TFieldSerializer_>(std::move(*this)); } - ~TFieldLoadRegistrar() + void operator()() && { - if (Armed_) { - if (auto version = Context_.GetVersion(); version >= MinVersion_ && (!VersionFilter_ || VersionFilter_(version))) { - Context_.Dumper().SetFieldName(Name_); - TFieldSerializer::Load(Context_, This_->*Member); - } else if (MissingHandler_) { - MissingHandler_(This_, Context_); - } else { - This_->*Member = {}; - } + if (auto version = Context_.GetVersion(); version >= MinVersion_ && (!VersionFilter_ || VersionFilter_(version))) { + Context_.Dumper().SetFieldName(Name_); + TFieldSerializer::Load(Context_, This_->*Member); + } else if (MissingHandler_) { + MissingHandler_(This_, Context_); + } else { + This_->*Member = {}; } } @@ -424,8 +419,6 @@ private: TVersion MinVersion_ = static_cast<TVersion>(std::numeric_limits<int>::min()); TVersionFilter VersionFilter_ = nullptr; TMissingHandler MissingHandler_ = nullptr; - - bool Armed_ = true; }; template <class TThis, class TContext> @@ -510,7 +503,7 @@ template <class TThis, class TContext> using TRuntimeFieldDescriptorMap = THashMap<TFieldTag, TRuntimeFieldDescriptor<TThis, TContext>>; template <auto Member, class TThis, class TContext, class TFieldSerializer> -class TRuntimeFieldDescriptorBuilderRegistar +class PHOENIX_REGISTRAR_NODISCARD TRuntimeFieldDescriptorBuilderRegistar { public: using TRuntimeFieldDescriptor = NPhoenix2::NDetail::TRuntimeFieldDescriptor<TThis, TContext>; @@ -522,9 +515,7 @@ public: template <class TFieldSerializer_> TRuntimeFieldDescriptorBuilderRegistar(TRuntimeFieldDescriptorBuilderRegistar<Member, TThis, TContext, TFieldSerializer_>&& other) : Descriptor_(other.Descriptor_) - { - other.Armed_ = false; - } + { } auto SinceVersion(auto /*version*/) && { @@ -548,13 +539,11 @@ public: return TRuntimeFieldDescriptorBuilderRegistar<Member, TThis, TContext, TFieldSerializer_>(std::move(*this)); } - ~TRuntimeFieldDescriptorBuilderRegistar() + void operator()() && { - if (Armed_) { - Descriptor_->LoadHandler = [] (TThis* this_, TContext& context) { - TFieldSerializer::Load(context, this_->*Member); - }; - } + Descriptor_->LoadHandler = [] (TThis* this_, TContext& context) { + TFieldSerializer::Load(context, this_->*Member); + }; } private: @@ -562,8 +551,6 @@ private: friend class TRuntimeFieldDescriptorBuilderRegistar; TRuntimeFieldDescriptor* const Descriptor_; - - bool Armed_ = true; }; template <class TThis, class TContext> @@ -598,10 +585,11 @@ public: return std::move(*this); } + void operator()() && + { } + private: TRuntimeFieldDescriptor* const Descriptor_; - - bool Armed_ = true; }; template <class TThis, class TContext> @@ -629,7 +617,7 @@ public: return TRuntimeDeprecatedFieldDescriptorBuilderRegistar<TThis, TContext>(descriptor); } - auto Finish() && + auto operator()() && { return std::move(Map_); } @@ -908,6 +896,8 @@ struct TSerializer } }; +#undef PHOENIX_REGISTRAR_NODISCARD + //////////////////////////////////////////////////////////////////////////////// } // namespace NYT::NPhoenix2::NDetail diff --git a/yt/yt/core/phoenix/type_def.cpp b/yt/yt/core/phoenix/type_def.cpp index a9ce100317..c551ceb201 100644 --- a/yt/yt/core/phoenix/type_def.cpp +++ b/yt/yt/core/phoenix/type_def.cpp @@ -19,7 +19,7 @@ TTypeSchemaBuilderRegistar::TTypeSchemaBuilderRegistar( TypeDescriptor_->ConcreteConstructor_ = concreteConstructor; } -const TTypeDescriptor& TTypeSchemaBuilderRegistar::Finish() && +const TTypeDescriptor& TTypeSchemaBuilderRegistar::operator()() && { const auto& result = *TypeDescriptor_; ::NYT::NPhoenix2::ITypeRegistry::Get()->RegisterTypeDescriptor(std::move(TypeDescriptor_)); diff --git a/yt/yt/core/phoenix/unittests/phoenix_ut.cpp b/yt/yt/core/phoenix/unittests/phoenix_ut.cpp index 4159f9042f..d0f3e1058d 100644 --- a/yt/yt/core/phoenix/unittests/phoenix_ut.cpp +++ b/yt/yt/core/phoenix/unittests/phoenix_ut.cpp @@ -91,8 +91,8 @@ private: void TPoint::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X_>("x"); - registrar.template Field<2, &TThis::Y_>("y"); + registrar.template Field<1, &TThis::X_>("x")(); + registrar.template Field<2, &TThis::Y_>("y")(); } PHOENIX_DEFINE_TYPE(TPoint); @@ -112,7 +112,7 @@ struct TBaseStruct void TBaseStruct::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::A>("a"); + registrar.template Field<1, &TThis::A>("a")(); } PHOENIX_DEFINE_TYPE(TBaseStruct); @@ -134,7 +134,7 @@ struct TDerivedStruct void TDerivedStruct::RegisterMetadata(auto&& registrar) { registrar.template BaseType<TBaseStruct>(); - registrar.template Field<1, &TThis::B>("b"); + registrar.template Field<1, &TThis::B>("b")(); } PHOENIX_DEFINE_TYPE(TDerivedStruct); @@ -157,8 +157,8 @@ struct TPair template <class T1, class T2> void TPair<T1, T2>::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::First>("first"); - registrar.template Field<2, &TThis::Second>("second"); + registrar.template Field<1, &TThis::First>("first")(); + registrar.template Field<2, &TThis::Second>("second")(); } PHOENIX_DEFINE_TEMPLATE_TYPE(TPair, (<int, int>)); @@ -247,14 +247,14 @@ struct S void S::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::A>("a"); + registrar.template Field<1, &TThis::A>("a")(); registrar.template Field<2, &TThis::B>("b") - .SinceVersion(100); + .SinceVersion(100)(); registrar.template Field<3, &TThis::C>("c") .SinceVersion(200) .WhenMissing([] (TThis* this_, auto& /*context*/) { this_->C = 777; - }); + })(); } PHOENIX_DEFINE_TYPE(S); @@ -310,18 +310,18 @@ struct S void S::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::A>("a"); + registrar.template Field<1, &TThis::A>("a")(); registrar.template Field<2, &TThis::B>("b") .InVersions([] (int version) { return version >= 150 && version <= 250; - }); + })(); registrar.template Field<3, &TThis::C>("c") .InVersions([] (int version) { return version >= 100 && version <= 200; }) .WhenMissing([] (TThis* this_, auto& /*context*/) { this_->C = 777; - }); + })(); } PHOENIX_DEFINE_TYPE(S); @@ -483,8 +483,8 @@ struct S void S::RegisterMetadata(auto&& registrar) { registrar.template Field<1, &TThis::A>("a") - .template Serializer<TSerializer>(); - registrar.template Field<2, &TThis::B>("b"); + .template Serializer<TSerializer>()(); + registrar.template Field<2, &TThis::B>("b")(); } PHOENIX_DEFINE_TYPE(S); @@ -685,7 +685,7 @@ void S::RegisterMetadata(auto&& registrar) { registrar.template DeprecatedField<1>("a", [] (TThis* this_, auto& context) { this_->A = Load<int>(context); - }); + })(); } PHOENIX_DEFINE_TYPE(S); @@ -810,7 +810,7 @@ struct TBase void TBase::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::A>("a"); + registrar.template Field<1, &TThis::A>("a")(); } PHOENIX_DEFINE_TYPE(TBase); @@ -860,8 +860,8 @@ struct A void A::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X>("a"); - registrar.template Field<2, &TThis::Y>("b"); + registrar.template Field<1, &TThis::X>("a")(); + registrar.template Field<2, &TThis::Y>("b")(); } PHOENIX_DEFINE_TYPE(A); @@ -876,7 +876,7 @@ struct B void B::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::V>("v"); + registrar.template Field<1, &TThis::V>("v")(); } PHOENIX_DEFINE_TYPE(B); @@ -915,7 +915,7 @@ struct A void A::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X>("x"); + registrar.template Field<1, &TThis::X>("x")(); } PHOENIX_DEFINE_TYPE(A); @@ -963,7 +963,7 @@ struct A void A::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X>("x"); + registrar.template Field<1, &TThis::X>("x")(); } PHOENIX_DEFINE_TYPE(A); @@ -1012,8 +1012,8 @@ struct A void A::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X>("x"); - registrar.template Field<2, &TThis::Y>("y"); + registrar.template Field<1, &TThis::X>("x")(); + registrar.template Field<2, &TThis::Y>("y")(); } PHOENIX_DEFINE_TYPE(A); @@ -1028,7 +1028,7 @@ struct B void B::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &B::V>("v"); + registrar.template Field<1, &B::V>("v")(); } PHOENIX_DEFINE_TYPE(B); @@ -1078,7 +1078,7 @@ struct TDerived1 void TDerived1::RegisterMetadata(auto&& registrar) { registrar.template BaseType<TBase>(); - registrar.template Field<1, &TThis::V>("v"); + registrar.template Field<1, &TThis::V>("v")(); } PHOENIX_DEFINE_TYPE(TDerived1); @@ -1094,7 +1094,7 @@ struct TDerived2 void TDerived2::RegisterMetadata(auto&& registrar) { registrar.template BaseType<TBase>(); - registrar.template Field<1, &TThis::V>("v"); + registrar.template Field<1, &TThis::V>("v")(); } PHOENIX_DEFINE_TYPE(TDerived2); @@ -1134,7 +1134,7 @@ struct S void S::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X>("x"); + registrar.template Field<1, &TThis::X>("x")(); } PHOENIX_DEFINE_TYPE(S); @@ -1170,8 +1170,8 @@ struct A void A::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X>("x"); - registrar.template Field<2, &TThis::T>("t"); + registrar.template Field<1, &TThis::X>("x")(); + registrar.template Field<2, &TThis::T>("t")(); } PHOENIX_DEFINE_TYPE(A); @@ -1186,8 +1186,8 @@ struct B void B::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::Y>("y"); - registrar.template Field<2, &TThis::Z>("z"); + registrar.template Field<1, &TThis::Y>("y")(); + registrar.template Field<2, &TThis::Z>("z")(); } PHOENIX_DEFINE_TYPE(B); @@ -1233,7 +1233,7 @@ struct TBase void TBase::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X>("x"); + registrar.template Field<1, &TThis::X>("x")(); } PHOENIX_DEFINE_TYPE(TBase); @@ -1257,8 +1257,8 @@ struct TDervied void TDervied::RegisterMetadata(auto&& registrar) { registrar.template BaseType<TBase>(); - registrar.template Field<1, &TThis::Y>("y"); - registrar.template Field<2, &TThis::Z>("z"); + registrar.template Field<1, &TThis::Y>("y")(); + registrar.template Field<2, &TThis::Z>("z")(); } PHOENIX_DEFINE_TYPE(TDervied); @@ -1298,7 +1298,7 @@ struct TBase1 void TBase1::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X1>("x1"); + registrar.template Field<1, &TThis::X1>("x1")(); } PHOENIX_DEFINE_TYPE(TBase1); @@ -1314,7 +1314,7 @@ struct TBase2 void TBase2::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::X2>("x2"); + registrar.template Field<1, &TThis::X2>("x2")(); } PHOENIX_DEFINE_TYPE(TBase2); @@ -1333,8 +1333,8 @@ void TDerived::RegisterMetadata(auto&& registrar) { registrar.template BaseType<TBase1>(); registrar.template BaseType<TBase2>(); - registrar.template Field<1, &TThis::Y>("y"); - registrar.template Field<2, &TThis::Z>("z"); + registrar.template Field<1, &TThis::Y>("y")(); + registrar.template Field<2, &TThis::Z>("z")(); } PHOENIX_DEFINE_TYPE(TDerived); @@ -1430,7 +1430,7 @@ struct S void S::RegisterMetadata(auto&& registrar) { - registrar.template Field<1, &TThis::A>("a"); + registrar.template Field<1, &TThis::A>("a")(); } PHOENIX_DEFINE_TYPE(S); |