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/messagebus/protobuf/ybusbuf.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/messagebus/protobuf/ybusbuf.h')
-rw-r--r-- | library/cpp/messagebus/protobuf/ybusbuf.h | 438 |
1 files changed, 219 insertions, 219 deletions
diff --git a/library/cpp/messagebus/protobuf/ybusbuf.h b/library/cpp/messagebus/protobuf/ybusbuf.h index b01338f986..57b4267ea5 100644 --- a/library/cpp/messagebus/protobuf/ybusbuf.h +++ b/library/cpp/messagebus/protobuf/ybusbuf.h @@ -12,222 +12,222 @@ #include <array> namespace NBus { - using TBusBufferRecord = ::google::protobuf::Message; - - template <class TBufferMessage> - class TBusBufferMessagePtr; - template <class TBufferMessage> - class TBusBufferMessageAutoPtr; - - class TBusBufferBase: public TBusMessage { - public: - TBusBufferBase(int type) - : TBusMessage((ui16)type) - { - } - TBusBufferBase(ECreateUninitialized) - : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) - { - } - - ui16 GetType() const { - return GetHeader()->Type; - } - - virtual TBusBufferRecord* GetRecord() const = 0; - virtual TBusBufferBase* New() = 0; - }; - - /////////////////////////////////////////////////////////////////// - /// \brief Template for all messages that have protobuf description - - /// @param TBufferRecord is record described in .proto file with namespace - /// @param MessageFile is offset for .proto file message ids - - /// \attention If you want one protocol NBus::TBusBufferProtocol to handle - /// messageges described in different .proto files, make sure that they have - /// unique values for MessageFile - - template <class TBufferRecord, int MType> - class TBusBufferMessage: public TBusBufferBase { - public: - static const int MessageType = MType; - - typedef TBusBufferMessagePtr<TBusBufferMessage<TBufferRecord, MType>> TPtr; - typedef TBusBufferMessageAutoPtr<TBusBufferMessage<TBufferRecord, MType>> TAutoPtr; - - public: - typedef TBufferRecord RecordType; - TBufferRecord Record; - - public: - TBusBufferMessage() - : TBusBufferBase(MessageType) - { - } - TBusBufferMessage(ECreateUninitialized) - : TBusBufferBase(MESSAGE_CREATE_UNINITIALIZED) - { - } - explicit TBusBufferMessage(const TBufferRecord& record) - : TBusBufferBase(MessageType) - , Record(record) - { - } - explicit TBusBufferMessage(TBufferRecord&& record) - : TBusBufferBase(MessageType) - , Record(std::move(record)) - { - } - - public: - TBusBufferRecord* GetRecord() const override { - return (TBusBufferRecord*)&Record; - } - TBusBufferBase* New() override { - return new TBusBufferMessage<TBufferRecord, MessageType>(); - } - }; - - template <class TSelf, class TBufferMessage> - class TBusBufferMessagePtrBase { - public: - typedef typename TBufferMessage::RecordType RecordType; - - private: - TSelf* GetSelf() { - return static_cast<TSelf*>(this); - } - const TSelf* GetSelf() const { - return static_cast<const TSelf*>(this); - } - - public: - RecordType* operator->() { - Y_ASSERT(GetSelf()->Get()); - return &(GetSelf()->Get()->Record); - } - const RecordType* operator->() const { - Y_ASSERT(GetSelf()->Get()); - return &(GetSelf()->Get()->Record); - } - RecordType& operator*() { - Y_ASSERT(GetSelf()->Get()); - return GetSelf()->Get()->Record; - } - const RecordType& operator*() const { - Y_ASSERT(GetSelf()->Get()); - return GetSelf()->Get()->Record; - } - - TBusHeader* GetHeader() { - return GetSelf()->Get()->GetHeader(); - } - const TBusHeader* GetHeader() const { - return GetSelf()->Get()->GetHeader(); - } - }; - - template <class TBufferMessage> - class TBusBufferMessagePtr: public TBusBufferMessagePtrBase<TBusBufferMessagePtr<TBufferMessage>, TBufferMessage> { - protected: - TBufferMessage* Holder; - - public: - TBusBufferMessagePtr(TBufferMessage* mess) - : Holder(mess) - { - } - static TBusBufferMessagePtr<TBufferMessage> DynamicCast(TBusMessage* message) { - return dynamic_cast<TBufferMessage*>(message); - } - TBufferMessage* Get() { - return Holder; - } - const TBufferMessage* Get() const { - return Holder; - } - - operator TBufferMessage*() { - return Holder; - } - operator const TBufferMessage*() const { - return Holder; - } - - operator TAutoPtr<TBusMessage>() { - TAutoPtr<TBusMessage> r(Holder); - Holder = 0; - return r; - } - operator TBusMessageAutoPtr() { - TBusMessageAutoPtr r(Holder); - Holder = nullptr; - return r; - } - }; - - template <class TBufferMessage> - class TBusBufferMessageAutoPtr: public TBusBufferMessagePtrBase<TBusBufferMessageAutoPtr<TBufferMessage>, TBufferMessage> { - public: - TAutoPtr<TBufferMessage> AutoPtr; - - public: - TBusBufferMessageAutoPtr() { - } - TBusBufferMessageAutoPtr(TBufferMessage* message) - : AutoPtr(message) - { - } - - TBufferMessage* Get() { - return AutoPtr.Get(); - } - const TBufferMessage* Get() const { - return AutoPtr.Get(); - } - - TBufferMessage* Release() const { - return AutoPtr.Release(); - } - - operator TAutoPtr<TBusMessage>() { - return AutoPtr.Release(); - } - operator TBusMessageAutoPtr() { - return AutoPtr.Release(); - } - }; - - ///////////////////////////////////////////// - /// \brief Generic protocol object for messages descibed with protobuf - - /// \attention If you mix messages in the same protocol from more than - /// .proto file make sure that they have different MessageFile parameter - /// in the NBus::TBusBufferMessage template - - class TBusBufferProtocol: public TBusProtocol { - private: - TVector<TBusBufferBase*> Types; - std::array<ui32, ((1 << 16) >> 5)> TypeMask; - - TBusBufferBase* FindType(int type); - bool IsRegisteredType(unsigned type); - - public: - TBusBufferProtocol(TBusService name, int port); - - ~TBusBufferProtocol() override; - - /// register all the message that this protocol should handle - void RegisterType(TAutoPtr<TBusBufferBase> mess); - - TArrayRef<TBusBufferBase* const> GetTypes() const; - - /// serialized protocol specific data into TBusData - void Serialize(const TBusMessage* mess, TBuffer& data) override; - - TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override; - }; - -} + using TBusBufferRecord = ::google::protobuf::Message; + + template <class TBufferMessage> + class TBusBufferMessagePtr; + template <class TBufferMessage> + class TBusBufferMessageAutoPtr; + + class TBusBufferBase: public TBusMessage { + public: + TBusBufferBase(int type) + : TBusMessage((ui16)type) + { + } + TBusBufferBase(ECreateUninitialized) + : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) + { + } + + ui16 GetType() const { + return GetHeader()->Type; + } + + virtual TBusBufferRecord* GetRecord() const = 0; + virtual TBusBufferBase* New() = 0; + }; + + /////////////////////////////////////////////////////////////////// + /// \brief Template for all messages that have protobuf description + + /// @param TBufferRecord is record described in .proto file with namespace + /// @param MessageFile is offset for .proto file message ids + + /// \attention If you want one protocol NBus::TBusBufferProtocol to handle + /// messageges described in different .proto files, make sure that they have + /// unique values for MessageFile + + template <class TBufferRecord, int MType> + class TBusBufferMessage: public TBusBufferBase { + public: + static const int MessageType = MType; + + typedef TBusBufferMessagePtr<TBusBufferMessage<TBufferRecord, MType>> TPtr; + typedef TBusBufferMessageAutoPtr<TBusBufferMessage<TBufferRecord, MType>> TAutoPtr; + + public: + typedef TBufferRecord RecordType; + TBufferRecord Record; + + public: + TBusBufferMessage() + : TBusBufferBase(MessageType) + { + } + TBusBufferMessage(ECreateUninitialized) + : TBusBufferBase(MESSAGE_CREATE_UNINITIALIZED) + { + } + explicit TBusBufferMessage(const TBufferRecord& record) + : TBusBufferBase(MessageType) + , Record(record) + { + } + explicit TBusBufferMessage(TBufferRecord&& record) + : TBusBufferBase(MessageType) + , Record(std::move(record)) + { + } + + public: + TBusBufferRecord* GetRecord() const override { + return (TBusBufferRecord*)&Record; + } + TBusBufferBase* New() override { + return new TBusBufferMessage<TBufferRecord, MessageType>(); + } + }; + + template <class TSelf, class TBufferMessage> + class TBusBufferMessagePtrBase { + public: + typedef typename TBufferMessage::RecordType RecordType; + + private: + TSelf* GetSelf() { + return static_cast<TSelf*>(this); + } + const TSelf* GetSelf() const { + return static_cast<const TSelf*>(this); + } + + public: + RecordType* operator->() { + Y_ASSERT(GetSelf()->Get()); + return &(GetSelf()->Get()->Record); + } + const RecordType* operator->() const { + Y_ASSERT(GetSelf()->Get()); + return &(GetSelf()->Get()->Record); + } + RecordType& operator*() { + Y_ASSERT(GetSelf()->Get()); + return GetSelf()->Get()->Record; + } + const RecordType& operator*() const { + Y_ASSERT(GetSelf()->Get()); + return GetSelf()->Get()->Record; + } + + TBusHeader* GetHeader() { + return GetSelf()->Get()->GetHeader(); + } + const TBusHeader* GetHeader() const { + return GetSelf()->Get()->GetHeader(); + } + }; + + template <class TBufferMessage> + class TBusBufferMessagePtr: public TBusBufferMessagePtrBase<TBusBufferMessagePtr<TBufferMessage>, TBufferMessage> { + protected: + TBufferMessage* Holder; + + public: + TBusBufferMessagePtr(TBufferMessage* mess) + : Holder(mess) + { + } + static TBusBufferMessagePtr<TBufferMessage> DynamicCast(TBusMessage* message) { + return dynamic_cast<TBufferMessage*>(message); + } + TBufferMessage* Get() { + return Holder; + } + const TBufferMessage* Get() const { + return Holder; + } + + operator TBufferMessage*() { + return Holder; + } + operator const TBufferMessage*() const { + return Holder; + } + + operator TAutoPtr<TBusMessage>() { + TAutoPtr<TBusMessage> r(Holder); + Holder = 0; + return r; + } + operator TBusMessageAutoPtr() { + TBusMessageAutoPtr r(Holder); + Holder = nullptr; + return r; + } + }; + + template <class TBufferMessage> + class TBusBufferMessageAutoPtr: public TBusBufferMessagePtrBase<TBusBufferMessageAutoPtr<TBufferMessage>, TBufferMessage> { + public: + TAutoPtr<TBufferMessage> AutoPtr; + + public: + TBusBufferMessageAutoPtr() { + } + TBusBufferMessageAutoPtr(TBufferMessage* message) + : AutoPtr(message) + { + } + + TBufferMessage* Get() { + return AutoPtr.Get(); + } + const TBufferMessage* Get() const { + return AutoPtr.Get(); + } + + TBufferMessage* Release() const { + return AutoPtr.Release(); + } + + operator TAutoPtr<TBusMessage>() { + return AutoPtr.Release(); + } + operator TBusMessageAutoPtr() { + return AutoPtr.Release(); + } + }; + + ///////////////////////////////////////////// + /// \brief Generic protocol object for messages descibed with protobuf + + /// \attention If you mix messages in the same protocol from more than + /// .proto file make sure that they have different MessageFile parameter + /// in the NBus::TBusBufferMessage template + + class TBusBufferProtocol: public TBusProtocol { + private: + TVector<TBusBufferBase*> Types; + std::array<ui32, ((1 << 16) >> 5)> TypeMask; + + TBusBufferBase* FindType(int type); + bool IsRegisteredType(unsigned type); + + public: + TBusBufferProtocol(TBusService name, int port); + + ~TBusBufferProtocol() override; + + /// register all the message that this protocol should handle + void RegisterType(TAutoPtr<TBusBufferBase> mess); + + TArrayRef<TBusBufferBase* const> GetTypes() const; + + /// serialized protocol specific data into TBusData + void Serialize(const TBusMessage* mess, TBuffer& data) override; + + TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override; + }; + +} |