OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TVirtualTransport.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef _SANDESH_TRANSPORT_TVIRTUALTRANSPORT_H_
21 #define _SANDESH_TRANSPORT_TVIRTUALTRANSPORT_H_ 1
22 
23 #include <sandesh/transport/TTransport.h>
24 
25 namespace contrail { namespace sandesh { namespace transport {
26 
27 
45  public:
46  /*
47  * TTransport *_virt() methods provide reasonable default implementations.
48  * Invoke them non-virtually.
49  */
50  uint32_t read(uint8_t* buf, uint32_t len) {
51  return this->TTransport::read_virt(buf, len);
52  }
53  uint32_t readAll(uint8_t* buf, uint32_t len) {
54  return this->TTransport::readAll_virt(buf, len);
55  }
56  int write(const uint8_t* buf, uint32_t len) {
57  return this->TTransport::write_virt(buf, len);
58  }
59  const uint8_t* borrow(uint8_t* buf, uint32_t* len) {
60  return this->TTransport::borrow_virt(buf, len);
61  }
62  void consume(uint32_t len) {
63  this->TTransport::consume_virt(len);
64  }
65 
66  protected:
68 };
69 
87 template <class Transport_, class Super_=TTransportDefaults>
88 class TVirtualTransport : public Super_ {
89  public:
90  /*
91  * Implementations of the *_virt() functions, to call the subclass's
92  * non-virtual implementation function.
93  */
94  virtual int32_t read_virt(uint8_t* buf, uint32_t len) {
95  return static_cast<Transport_*>(this)->read(buf, len);
96  }
97 
98  virtual int32_t readAll_virt(uint8_t* buf, uint32_t len) {
99  return static_cast<Transport_*>(this)->readAll(buf, len);
100  }
101 
102  virtual int write_virt(const uint8_t* buf, uint32_t len) {
103  return static_cast<Transport_*>(this)->write(buf, len);
104  }
105 
106  virtual const uint8_t* borrow_virt(uint8_t* buf, uint32_t* len) {
107  return static_cast<Transport_*>(this)->borrow(buf, len);
108  }
109 
110  virtual void consume_virt(uint32_t len) {
111  static_cast<Transport_*>(this)->consume(len);
112  }
113 
114  /*
115  * Provide a default readAll() implementation that invokes
116  * read() non-virtually.
117  *
118  * Note: subclasses that use TVirtualTransport to derive from another
119  * transport implementation (i.e., not TTransportDefaults) should beware that
120  * this may override any non-default readAll() implementation provided by
121  * the parent transport class. They may need to redefine readAll() to call
122  * the correct parent implementation, if desired.
123  */
124  uint32_t readAll(uint8_t* buf, uint32_t len) {
125  Transport_* trans = static_cast<Transport_*>(this);
127  }
128 
129  protected:
131 
132  /*
133  * Templatized constructors, to allow arguments to be passed to the Super_
134  * constructor. Currently we only support 0, 1, or 2 arguments, but
135  * additional versions can be added as needed.
136  */
137  template <typename Arg_>
138  TVirtualTransport(Arg_ const& arg) : Super_(arg) { }
139 
140  template <typename Arg1_, typename Arg2_>
141  TVirtualTransport(Arg1_ const& a1, Arg2_ const& a2) : Super_(a1, a2) { }
142 };
143 
144 }}} // contrail::sandesh::transport
145 
146 #endif // #ifndef _SANDESH_TRANSPORT_TVIRTUALTRANSPORT_H_
virtual void consume_virt(uint32_t)
Definition: TTransport.h:237
virtual int write_virt(const uint8_t *buf, uint32_t len)
virtual int32_t read_virt(uint8_t *, uint32_t)
Definition: TTransport.h:112
virtual const uint8_t * borrow_virt(uint8_t *, uint32_t *)
Definition: TTransport.h:220
uint32_t readAll(uint8_t *buf, uint32_t len)
const uint8_t * borrow(uint8_t *buf, uint32_t *len)
virtual int32_t readAll_virt(uint8_t *buf, uint32_t len)
Definition: TTransport.h:129
virtual int write_virt(const uint8_t *, uint32_t)
Definition: TTransport.h:161
TVirtualTransport(Arg1_ const &a1, Arg2_ const &a2)
uint32_t readAll(uint8_t *buf, uint32_t len)
virtual int32_t readAll_virt(uint8_t *buf, uint32_t len)
uint32_t read(uint8_t *buf, uint32_t len)
virtual int32_t read_virt(uint8_t *buf, uint32_t len)
int32_t readAll(Transport_ &trans, uint8_t *buf, uint32_t len)
Definition: TTransport.h:35
virtual const uint8_t * borrow_virt(uint8_t *buf, uint32_t *len)
int write(const uint8_t *buf, uint32_t len)