diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/protobuf/util/cast.h | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/protobuf/util/cast.h')
-rw-r--r-- | library/cpp/protobuf/util/cast.h | 272 |
1 files changed, 136 insertions, 136 deletions
diff --git a/library/cpp/protobuf/util/cast.h b/library/cpp/protobuf/util/cast.h index e99d4f200a..83749dfcee 100644 --- a/library/cpp/protobuf/util/cast.h +++ b/library/cpp/protobuf/util/cast.h @@ -1,5 +1,5 @@ #pragma once - + #include "traits.h" #include <google/protobuf/descriptor.h> @@ -8,149 +8,149 @@ #include <util/generic/cast.h> namespace NProtoBuf { - // C++ compatible conversions of FieldDescriptor::CppType's + // C++ compatible conversions of FieldDescriptor::CppType's + + using ECppType = FieldDescriptor::CppType; - using ECppType = FieldDescriptor::CppType; + namespace NCast { + template <ECppType src, ECppType dst> + struct TIsCompatibleCppType { + enum { + Result = src == dst || + (TIsNumericCppType<src>::Result && TIsNumericCppType<dst>::Result) + }; + }; - namespace NCast { - template <ECppType src, ECppType dst> - struct TIsCompatibleCppType { - enum { - Result = src == dst || - (TIsNumericCppType<src>::Result && TIsNumericCppType<dst>::Result) - }; + template <ECppType src, ECppType dst> + struct TIsEnumToNumericCppType { + enum { + Result = (src == FieldDescriptor::CPPTYPE_ENUM && TIsNumericCppType<dst>::Result) + }; }; - template <ECppType src, ECppType dst> - struct TIsEnumToNumericCppType { - enum { - Result = (src == FieldDescriptor::CPPTYPE_ENUM && TIsNumericCppType<dst>::Result) - }; + template <ECppType src, ECppType dst, bool compatible> // compatible == true + struct TCompatCastBase { + static const bool IsCompatible = true; + + typedef typename TCppTypeTraits<src>::T TSrc; + typedef typename TCppTypeTraits<dst>::T TDst; + + static inline TDst Cast(TSrc value) { + return value; + } }; - template <ECppType src, ECppType dst, bool compatible> // compatible == true - struct TCompatCastBase { - static const bool IsCompatible = true; - - typedef typename TCppTypeTraits<src>::T TSrc; - typedef typename TCppTypeTraits<dst>::T TDst; - - static inline TDst Cast(TSrc value) { - return value; - } - }; - - template <ECppType src, ECppType dst> // compatible == false - struct TCompatCastBase<src, dst, false> { - static const bool IsCompatible = false; - - typedef typename TCppTypeTraits<src>::T TSrc; - typedef typename TCppTypeTraits<dst>::T TDst; - - static inline TDst Cast(TSrc) { - ythrow TBadCastException() << "Incompatible FieldDescriptor::CppType conversion: #" - << (size_t)src << " to #" << (size_t)dst; - } - }; - - template <ECppType src, ECppType dst, bool isEnumToNum> // enum -> numeric - struct TCompatCastImpl { - static const bool IsCompatible = true; - - typedef typename TCppTypeTraits<dst>::T TDst; - - static inline TDst Cast(const EnumValueDescriptor* value) { - Y_ASSERT(value != nullptr); - return value->number(); - } - }; - - template <ECppType src, ECppType dst> - struct TCompatCastImpl<src, dst, false>: public TCompatCastBase<src, dst, TIsCompatibleCppType<src, dst>::Result> { - using TCompatCastBase<src, dst, TIsCompatibleCppType<src, dst>::Result>::IsCompatible; - }; - - template <ECppType src, ECppType dst> - struct TCompatCast: public TCompatCastImpl<src, dst, TIsEnumToNumericCppType<src, dst>::Result> { - typedef TCompatCastImpl<src, dst, TIsEnumToNumericCppType<src, dst>::Result> TBase; - - typedef typename TCppTypeTraits<src>::T TSrc; - typedef typename TCppTypeTraits<dst>::T TDst; - - using TBase::Cast; - using TBase::IsCompatible; - - inline bool Try(TSrc value, TDst& res) { - if (IsCompatible) { - res = Cast(value); - return true; - } - return false; - } - }; - - } - + template <ECppType src, ECppType dst> // compatible == false + struct TCompatCastBase<src, dst, false> { + static const bool IsCompatible = false; + + typedef typename TCppTypeTraits<src>::T TSrc; + typedef typename TCppTypeTraits<dst>::T TDst; + + static inline TDst Cast(TSrc) { + ythrow TBadCastException() << "Incompatible FieldDescriptor::CppType conversion: #" + << (size_t)src << " to #" << (size_t)dst; + } + }; + + template <ECppType src, ECppType dst, bool isEnumToNum> // enum -> numeric + struct TCompatCastImpl { + static const bool IsCompatible = true; + + typedef typename TCppTypeTraits<dst>::T TDst; + + static inline TDst Cast(const EnumValueDescriptor* value) { + Y_ASSERT(value != nullptr); + return value->number(); + } + }; + + template <ECppType src, ECppType dst> + struct TCompatCastImpl<src, dst, false>: public TCompatCastBase<src, dst, TIsCompatibleCppType<src, dst>::Result> { + using TCompatCastBase<src, dst, TIsCompatibleCppType<src, dst>::Result>::IsCompatible; + }; + + template <ECppType src, ECppType dst> + struct TCompatCast: public TCompatCastImpl<src, dst, TIsEnumToNumericCppType<src, dst>::Result> { + typedef TCompatCastImpl<src, dst, TIsEnumToNumericCppType<src, dst>::Result> TBase; + + typedef typename TCppTypeTraits<src>::T TSrc; + typedef typename TCppTypeTraits<dst>::T TDst; + + using TBase::Cast; + using TBase::IsCompatible; + + inline bool Try(TSrc value, TDst& res) { + if (IsCompatible) { + res = Cast(value); + return true; + } + return false; + } + }; + + } + template <ECppType src, ECppType dst> - inline typename TCppTypeTraits<dst>::T CompatCast(typename TCppTypeTraits<src>::T value) { - return NCast::TCompatCast<src, dst>::Cast(value); - } + inline typename TCppTypeTraits<dst>::T CompatCast(typename TCppTypeTraits<src>::T value) { + return NCast::TCompatCast<src, dst>::Cast(value); + } template <ECppType src, ECppType dst> - inline bool TryCompatCast(typename TCppTypeTraits<src>::T value, typename TCppTypeTraits<dst>::T& res) { - return NCast::TCompatCast<src, dst>::Try(value, res); - } - - // Message static/dynamic checked casts - - template <typename TpMessage> - inline const TpMessage* TryCast(const Message* msg) { - if (!msg || TpMessage::descriptor() != msg->GetDescriptor()) - return NULL; - return CheckedCast<const TpMessage*>(msg); - } - - template <typename TpMessage> - inline const TpMessage* TryCast(const Message* msg, const TpMessage*& ret) { - ret = TryCast<TpMessage>(msg); - return ret; - } - - template <typename TpMessage> - inline TpMessage* TryCast(Message* msg) { - if (!msg || TpMessage::descriptor() != msg->GetDescriptor()) - return nullptr; - return CheckedCast<TpMessage*>(msg); - } - - template <typename TpMessage> - inline TpMessage* TryCast(Message* msg, TpMessage*& ret) { - ret = TryCast<TpMessage>(msg); - return ret; - } - - // specialize for Message itself - - template <> - inline const Message* TryCast<Message>(const Message* msg) { - return msg; - } - - template <> - inline Message* TryCast<Message>(Message* msg) { - return msg; - } - - // Binary serialization compatible conversion - inline bool TryBinaryCast(const Message* from, Message* to, TString* buffer = nullptr) { - TString tmpbuf; - if (!buffer) - buffer = &tmpbuf; - - if (!from->SerializeToString(buffer)) - return false; - - return to->ParseFromString(*buffer); - } + inline bool TryCompatCast(typename TCppTypeTraits<src>::T value, typename TCppTypeTraits<dst>::T& res) { + return NCast::TCompatCast<src, dst>::Try(value, res); + } + + // Message static/dynamic checked casts + + template <typename TpMessage> + inline const TpMessage* TryCast(const Message* msg) { + if (!msg || TpMessage::descriptor() != msg->GetDescriptor()) + return NULL; + return CheckedCast<const TpMessage*>(msg); + } + + template <typename TpMessage> + inline const TpMessage* TryCast(const Message* msg, const TpMessage*& ret) { + ret = TryCast<TpMessage>(msg); + return ret; + } + + template <typename TpMessage> + inline TpMessage* TryCast(Message* msg) { + if (!msg || TpMessage::descriptor() != msg->GetDescriptor()) + return nullptr; + return CheckedCast<TpMessage*>(msg); + } + + template <typename TpMessage> + inline TpMessage* TryCast(Message* msg, TpMessage*& ret) { + ret = TryCast<TpMessage>(msg); + return ret; + } + + // specialize for Message itself + + template <> + inline const Message* TryCast<Message>(const Message* msg) { + return msg; + } + + template <> + inline Message* TryCast<Message>(Message* msg) { + return msg; + } + + // Binary serialization compatible conversion + inline bool TryBinaryCast(const Message* from, Message* to, TString* buffer = nullptr) { + TString tmpbuf; + if (!buffer) + buffer = &tmpbuf; + + if (!from->SerializeToString(buffer)) + return false; + + return to->ParseFromString(*buffer); + } } |