OpenSDN source code
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  * Modifications Copyright (c) Juniper Networks, Inc.
20  * Copyright Contributors to the OpenSDN project
21  */
22 
23 #ifndef _SANDESH_TRANSPORT_TVIRTUALTRANSPORT_H_
24 #define _SANDESH_TRANSPORT_TVIRTUALTRANSPORT_H_ 1
25 
26 #include <sandesh/transport/TTransport.h>
27 
28 namespace contrail { namespace sandesh { namespace transport {
29 
30 
48  public:
49  /*
50  * TTransport *_virt() methods provide reasonable default implementations.
51  * Invoke them non-virtually.
52  */
53  uint32_t read(uint8_t* buf, uint32_t len) {
54  return this->TTransport::read_virt(buf, len);
55  }
56  uint32_t readAll(uint8_t* buf, uint32_t len) {
57  return this->TTransport::readAll_virt(buf, len);
58  }
59  int write(const uint8_t* buf, uint32_t len) {
60  return this->TTransport::write_virt(buf, len);
61  }
62  const uint8_t* borrow(uint8_t* buf, uint32_t* len) {
63  return this->TTransport::borrow_virt(buf, len);
64  }
65  void consume(uint32_t len) {
66  this->TTransport::consume_virt(len);
67  }
68 
69  protected:
71 };
72 
90 template <class Transport_, class Super_=TTransportDefaults>
91 class TVirtualTransport : public Super_ {
92  public:
93  /*
94  * Implementations of the *_virt() functions, to call the subclass's
95  * non-virtual implementation function.
96  */
97  virtual int32_t read_virt(uint8_t* buf, uint32_t len) {
98  return static_cast<Transport_*>(this)->read(buf, len);
99  }
100 
101  virtual int32_t readAll_virt(uint8_t* buf, uint32_t len) {
102  return static_cast<Transport_*>(this)->readAll(buf, len);
103  }
104 
105  virtual int write_virt(const uint8_t* buf, uint32_t len) {
106  return static_cast<Transport_*>(this)->write(buf, len);
107  }
108 
109  virtual const uint8_t* borrow_virt(uint8_t* buf, uint32_t* len) {
110  return static_cast<Transport_*>(this)->borrow(buf, len);
111  }
112 
113  virtual void consume_virt(uint32_t len) {
114  static_cast<Transport_*>(this)->consume(len);
115  }
116 
117  /*
118  * Provide a default readAll() implementation that invokes
119  * read() non-virtually.
120  *
121  * Note: subclasses that use TVirtualTransport to derive from another
122  * transport implementation (i.e., not TTransportDefaults) should beware that
123  * this may override any non-default readAll() implementation provided by
124  * the parent transport class. They may need to redefine readAll() to call
125  * the correct parent implementation, if desired.
126  */
127  uint32_t readAll(uint8_t* buf, uint32_t len) {
128  Transport_* trans = static_cast<Transport_*>(this);
130  }
131 
132  protected:
134 
135  /*
136  * Templatized constructors, to allow arguments to be passed to the Super_
137  * constructor. Currently we only support 0, 1, or 2 arguments, but
138  * additional versions can be added as needed.
139  */
140  template <typename Arg_>
141  TVirtualTransport(Arg_ const& arg) : Super_(arg) { }
142 
143  template <typename Arg1_, typename Arg2_>
144  TVirtualTransport(Arg1_ const& a1, Arg2_ const& a2) : Super_(a1, a2) { }
145 };
146 
147 }}} // contrail::sandesh::transport
148 
149 #endif // #ifndef _SANDESH_TRANSPORT_TVIRTUALTRANSPORT_H_
uint32_t readAll(uint8_t *buf, uint32_t len)
int write(const uint8_t *buf, uint32_t len)
const uint8_t * borrow(uint8_t *buf, uint32_t *len)
uint32_t read(uint8_t *buf, uint32_t len)
virtual int write_virt(const uint8_t *, uint32_t)
Definition: TTransport.h:164
virtual const uint8_t * borrow_virt(uint8_t *, uint32_t *)
Definition: TTransport.h:223
virtual int32_t read_virt(uint8_t *, uint32_t)
Definition: TTransport.h:115
virtual void consume_virt(uint32_t)
Definition: TTransport.h:240
virtual int32_t readAll_virt(uint8_t *buf, uint32_t len)
Definition: TTransport.h:132
virtual int32_t read_virt(uint8_t *buf, uint32_t len)
virtual const uint8_t * borrow_virt(uint8_t *buf, uint32_t *len)
virtual int write_virt(const uint8_t *buf, uint32_t len)
TVirtualTransport(Arg1_ const &a1, Arg2_ const &a2)
virtual int32_t readAll_virt(uint8_t *buf, uint32_t len)
uint32_t readAll(uint8_t *buf, uint32_t len)
int32_t readAll(Transport_ &trans, uint8_t *buf, uint32_t len)
Definition: TTransport.h:38
Definition: regex.h:11