Back to home page

EIC code displayed by LXR

 
 

    


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

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_TRANSPORT_TVIRTUALTRANSPORT_H_
0021 #define _THRIFT_TRANSPORT_TVIRTUALTRANSPORT_H_ 1
0022 
0023 #include <thrift/transport/TTransport.h>
0024 
0025 namespace apache {
0026 namespace thrift {
0027 namespace transport {
0028 
0029 /**
0030  * Helper class that provides default implementations of TTransport methods.
0031  *
0032  * This class provides default implementations of read(), readAll(), write(),
0033  * borrow() and consume().
0034  *
0035  * In the TTransport base class, each of these methods simply invokes its
0036  * virtual counterpart.  This class overrides them to always perform the
0037  * default behavior, without a virtual function call.
0038  *
0039  * The primary purpose of this class is to serve as a base class for
0040  * TVirtualTransport, and prevent infinite recursion if one of its subclasses
0041  * does not override the TTransport implementation of these methods.  (Since
0042  * TVirtualTransport::read_virt() calls read(), and TTransport::read() calls
0043  * read_virt().)
0044  */
0045 class TTransportDefaults : public TTransport {
0046 public:
0047   /*
0048    * TTransport *_virt() methods provide reasonable default implementations.
0049    * Invoke them non-virtually.
0050    */
0051   uint32_t read(uint8_t* buf, uint32_t len) { return this->TTransport::read_virt(buf, len); }
0052   uint32_t readAll(uint8_t* buf, uint32_t len) { return this->TTransport::readAll_virt(buf, len); }
0053   void write(const uint8_t* buf, uint32_t len) { this->TTransport::write_virt(buf, len); }
0054   const uint8_t* borrow(uint8_t* buf, uint32_t* len) {
0055     return this->TTransport::borrow_virt(buf, len);
0056   }
0057   void consume(uint32_t len) { this->TTransport::consume_virt(len); }
0058 
0059 protected:
0060   TTransportDefaults(std::shared_ptr<TConfiguration> config = nullptr) : TTransport(config) {}
0061 };
0062 
0063 /**
0064  * Helper class to provide polymorphism for subclasses of TTransport.
0065  *
0066  * This class implements *_virt() methods of TTransport, to call the
0067  * non-virtual versions of these functions in the proper subclass.
0068  *
0069  * To define your own transport class using TVirtualTransport:
0070  * 1) Derive your subclass from TVirtualTransport<your class>
0071  *    e.g:  class MyTransport : public TVirtualTransport<MyTransport> {
0072  * 2) Provide your own implementations of read(), readAll(), etc.
0073  *    These methods should be non-virtual.
0074  *
0075  * Transport implementations that need to use virtual inheritance when
0076  * inheriting from TTransport cannot use TVirtualTransport.
0077  *
0078  * @author Chad Walters <chad@powerset.com>
0079  */
0080 template <class Transport_, class Super_ = TTransportDefaults>
0081 class TVirtualTransport : public Super_ {
0082 public:
0083   /*
0084    * Implementations of the *_virt() functions, to call the subclass's
0085    * non-virtual implementation function.
0086    */
0087   uint32_t read_virt(uint8_t* buf, uint32_t len) override {
0088     return static_cast<Transport_*>(this)->read(buf, len);
0089   }
0090 
0091   uint32_t readAll_virt(uint8_t* buf, uint32_t len) override {
0092     return static_cast<Transport_*>(this)->readAll(buf, len);
0093   }
0094 
0095   void write_virt(const uint8_t* buf, uint32_t len) override {
0096     static_cast<Transport_*>(this)->write(buf, len);
0097   }
0098 
0099   const uint8_t* borrow_virt(uint8_t* buf, uint32_t* len) override {
0100     return static_cast<Transport_*>(this)->borrow(buf, len);
0101   }
0102 
0103   void consume_virt(uint32_t len) override { static_cast<Transport_*>(this)->consume(len); }
0104 
0105   /*
0106    * Provide a default readAll() implementation that invokes
0107    * read() non-virtually.
0108    *
0109    * Note: subclasses that use TVirtualTransport to derive from another
0110    * transport implementation (i.e., not TTransportDefaults) should beware that
0111    * this may override any non-default readAll() implementation provided by
0112    * the parent transport class.  They may need to redefine readAll() to call
0113    * the correct parent implementation, if desired.
0114    */
0115   uint32_t readAll(uint8_t* buf, uint32_t len) {
0116     auto* trans = static_cast<Transport_*>(this);
0117     return ::apache::thrift::transport::readAll(*trans, buf, len);
0118   }
0119 
0120 protected:
0121   TVirtualTransport() : Super_() {}
0122 
0123   /*
0124    * Templatized constructors, to allow arguments to be passed to the Super_
0125    * constructor.  Currently we only support 0, 1, or 2 arguments, but
0126    * additional versions can be added as needed.
0127    */
0128   template <typename Arg_>
0129   TVirtualTransport(Arg_ const& arg)
0130     : Super_(arg) {}
0131 
0132   template <typename Arg1_, typename Arg2_>
0133   TVirtualTransport(Arg1_ const& a1, Arg2_ const& a2)
0134     : Super_(a1, a2) {}
0135 };
0136 }
0137 }
0138 } // apache::thrift::transport
0139 
0140 #endif // #ifndef _THRIFT_TRANSPORT_TVIRTUALTRANSPORT_H_