Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:01

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
0018  */
0019 
0020 #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
0021 #define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1
0022 
0023 #include <thrift/protocol/TProtocol.h>
0024 #include <thrift/protocol/TVirtualProtocol.h>
0025 
0026 #include <memory>
0027 
0028 namespace apache {
0029 namespace thrift {
0030 namespace protocol {
0031 
0032 /**
0033  * The default binary protocol for thrift. Writes all data in a very basic
0034  * binary format, essentially just spitting out the raw bytes.
0035  *
0036  */
0037 template <class Transport_, class ByteOrder_ = TNetworkBigEndian>
0038 class TBinaryProtocolT : public TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> > {
0039 public:
0040   static const int32_t VERSION_MASK = ((int32_t)0xffff0000);
0041   static const int32_t VERSION_1 = ((int32_t)0x80010000);
0042   // VERSION_2 (0x80020000) was taken by TDenseProtocol (which has since been removed)
0043 
0044   TBinaryProtocolT(std::shared_ptr<Transport_> trans)
0045     : TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> >(trans),
0046       trans_(trans.get()),
0047       string_limit_(0),
0048       container_limit_(0),
0049       strict_read_(false),
0050       strict_write_(true) {}
0051 
0052   TBinaryProtocolT(std::shared_ptr<Transport_> trans,
0053                    int32_t string_limit,
0054                    int32_t container_limit,
0055                    bool strict_read,
0056                    bool strict_write)
0057     : TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> >(trans),
0058       trans_(trans.get()),
0059       string_limit_(string_limit),
0060       container_limit_(container_limit),
0061       strict_read_(strict_read),
0062       strict_write_(strict_write) {}
0063 
0064   void setStringSizeLimit(int32_t string_limit) { string_limit_ = string_limit; }
0065 
0066   void setContainerSizeLimit(int32_t container_limit) { container_limit_ = container_limit; }
0067 
0068   void setStrict(bool strict_read, bool strict_write) {
0069     strict_read_ = strict_read;
0070     strict_write_ = strict_write;
0071   }
0072 
0073   /**
0074    * Writing functions.
0075    */
0076 
0077   /*ol*/ uint32_t writeMessageBegin(const std::string& name,
0078                                     const TMessageType messageType,
0079                                     const int32_t seqid);
0080 
0081   /*ol*/ uint32_t writeMessageEnd();
0082 
0083   inline uint32_t writeStructBegin(const char* name);
0084 
0085   inline uint32_t writeStructEnd();
0086 
0087   inline uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId);
0088 
0089   inline uint32_t writeFieldEnd();
0090 
0091   inline uint32_t writeFieldStop();
0092 
0093   inline uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size);
0094 
0095   inline uint32_t writeMapEnd();
0096 
0097   inline uint32_t writeListBegin(const TType elemType, const uint32_t size);
0098 
0099   inline uint32_t writeListEnd();
0100 
0101   inline uint32_t writeSetBegin(const TType elemType, const uint32_t size);
0102 
0103   inline uint32_t writeSetEnd();
0104 
0105   inline uint32_t writeBool(const bool value);
0106 
0107   inline uint32_t writeByte(const int8_t byte);
0108 
0109   inline uint32_t writeI16(const int16_t i16);
0110 
0111   inline uint32_t writeI32(const int32_t i32);
0112 
0113   inline uint32_t writeI64(const int64_t i64);
0114 
0115   inline uint32_t writeDouble(const double dub);
0116 
0117   template <typename StrType>
0118   inline uint32_t writeString(const StrType& str);
0119 
0120   inline uint32_t writeBinary(const std::string& str);
0121 
0122   inline uint32_t writeUUID(const TUuid& uuid);
0123 
0124   /**
0125    * Reading functions
0126    */
0127 
0128   /*ol*/ uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid);
0129 
0130   /*ol*/ uint32_t readMessageEnd();
0131 
0132   inline uint32_t readStructBegin(std::string& name);
0133 
0134   inline uint32_t readStructEnd();
0135 
0136   inline uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId);
0137 
0138   inline uint32_t readFieldEnd();
0139 
0140   inline uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size);
0141 
0142   inline uint32_t readMapEnd();
0143 
0144   inline uint32_t readListBegin(TType& elemType, uint32_t& size);
0145 
0146   inline uint32_t readListEnd();
0147 
0148   inline uint32_t readSetBegin(TType& elemType, uint32_t& size);
0149 
0150   inline uint32_t readSetEnd();
0151 
0152   inline uint32_t readBool(bool& value);
0153   // Provide the default readBool() implementation for std::vector<bool>
0154   using TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> >::readBool;
0155 
0156   inline uint32_t readByte(int8_t& byte);
0157 
0158   inline uint32_t readI16(int16_t& i16);
0159 
0160   inline uint32_t readI32(int32_t& i32);
0161 
0162   inline uint32_t readI64(int64_t& i64);
0163 
0164   inline uint32_t readDouble(double& dub);
0165 
0166   template <typename StrType>
0167   inline uint32_t readString(StrType& str);
0168 
0169   inline uint32_t readBinary(std::string& str);
0170 
0171   inline uint32_t readUUID(TUuid& uuid);
0172 
0173   int getMinSerializedSize(TType type) override;
0174 
0175   void checkReadBytesAvailable(TSet& set) override
0176   {
0177       trans_->checkReadBytesAvailable(set.size_ * getMinSerializedSize(set.elemType_));
0178   }
0179 
0180   void checkReadBytesAvailable(TList& list) override
0181   {
0182       trans_->checkReadBytesAvailable(list.size_ * getMinSerializedSize(list.elemType_));
0183   }
0184 
0185   void checkReadBytesAvailable(TMap& map) override
0186   {
0187       int elmSize = getMinSerializedSize(map.keyType_) + getMinSerializedSize(map.valueType_);
0188       trans_->checkReadBytesAvailable(map.size_ * elmSize);
0189   }
0190 
0191 protected:
0192   template <typename StrType>
0193   uint32_t readStringBody(StrType& str, int32_t sz);
0194 
0195   Transport_* trans_;
0196 
0197   int32_t string_limit_;
0198   int32_t container_limit_;
0199 
0200   // Enforce presence of version identifier
0201   bool strict_read_;
0202   bool strict_write_;
0203 };
0204 
0205 typedef TBinaryProtocolT<TTransport> TBinaryProtocol;
0206 typedef TBinaryProtocolT<TTransport, TNetworkLittleEndian> TLEBinaryProtocol;
0207 
0208 /**
0209  * Constructs binary protocol handlers
0210  */
0211 template <class Transport_, class ByteOrder_ = TNetworkBigEndian>
0212 class TBinaryProtocolFactoryT : public TProtocolFactory {
0213 public:
0214   TBinaryProtocolFactoryT()
0215     : string_limit_(0), container_limit_(0), strict_read_(false), strict_write_(true) {}
0216 
0217   TBinaryProtocolFactoryT(int32_t string_limit,
0218                           int32_t container_limit,
0219                           bool strict_read,
0220                           bool strict_write)
0221     : string_limit_(string_limit),
0222       container_limit_(container_limit),
0223       strict_read_(strict_read),
0224       strict_write_(strict_write) {}
0225 
0226   ~TBinaryProtocolFactoryT() override = default;
0227 
0228   void setStringSizeLimit(int32_t string_limit) { string_limit_ = string_limit; }
0229 
0230   void setContainerSizeLimit(int32_t container_limit) { container_limit_ = container_limit; }
0231 
0232   void setStrict(bool strict_read, bool strict_write) {
0233     strict_read_ = strict_read;
0234     strict_write_ = strict_write;
0235   }
0236 
0237   std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<TTransport> trans) override {
0238     std::shared_ptr<Transport_> specific_trans = std::dynamic_pointer_cast<Transport_>(trans);
0239     TProtocol* prot;
0240     if (specific_trans) {
0241       prot = new TBinaryProtocolT<Transport_, ByteOrder_>(specific_trans,
0242                                                           string_limit_,
0243                                                           container_limit_,
0244                                                           strict_read_,
0245                                                           strict_write_);
0246     } else {
0247       prot = new TBinaryProtocolT<TTransport, ByteOrder_>(trans,
0248                                                           string_limit_,
0249                                                           container_limit_,
0250                                                           strict_read_,
0251                                                           strict_write_);
0252     }
0253 
0254     return std::shared_ptr<TProtocol>(prot);
0255   }
0256 
0257 private:
0258   int32_t string_limit_;
0259   int32_t container_limit_;
0260   bool strict_read_;
0261   bool strict_write_;
0262 };
0263 
0264 typedef TBinaryProtocolFactoryT<TTransport> TBinaryProtocolFactory;
0265 typedef TBinaryProtocolFactoryT<TTransport, TNetworkLittleEndian> TLEBinaryProtocolFactory;
0266 }
0267 }
0268 } // apache::thrift::protocol
0269 
0270 #include <thrift/protocol/TBinaryProtocol.tcc>
0271 
0272 #endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_