proxygen
proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership > Class Template Reference

#include <FilterChain.h>

Inheritance diagram for proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >:

Public Types

using FilterChainType = GenericFilter< T1, T2, set_callback, TakeOwnership >
 

Public Member Functions

 FilterChain (std::unique_ptr< T1 > destination)
 
 FilterChain (T1 *destination)
 
void setCallback (T2 *cb) override
 
T1call ()
 
const T1call () const
 
T1getChainEndPtr ()
 
const T1getChainEnd () const
 
std::unique_ptr< T1setDestination (std::unique_ptr< T1 > destination)
 
template<typename C , typename C2 , typename... Types>
std::enable_if< std::is_constructible< C >::value >::type addFilters ()
 
template<typename C >
std::enable_if< std::is_constructible< C >::value >::type addFilters ()
 
template<typename C , typename... Types>
void addFilters (std::unique_ptr< C > cur, Types &&...remaining)
 
template<typename C , typename... Types>
void addFilters (C *cur, Types &&...remaining)
 
template<typename C , typename... Args>
FilterChain< T1, T2, FilterType, set_callback, TakeOwnership > & add (Args &&...args)
 
const T1operator-> () const
 
T1operator-> ()
 
void foreach (folly::FunctionRef< void(FilterChainType *)> fn)
 

Private Member Functions

void addFilters ()
 

Private Attributes

T1chainEnd_
 

Detailed Description

template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
class proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >

This class can be treated the same as a T1*, however internally it contains a chain of filters for T1 and T2. These filters are inserted between the original callback and destination.

If a filter does not care about one side of the calls, it will pass through the calls, saving a virtual function call. In the example below Filter1 only wants to intercept callbacks, and Filter2 only wants to intercept calls.

 FilterChain        Filter1          Filter2        destination
_____________    _____________    _____________    _____________
|           |    |           |    |           |    |           |
|   call_------------------------>|   call_------->|"call"     | T1
|           |    |           |    |           |    |           |

T2 | callback_ |<--—callback_ |<-----------------—|"callback" | |___________| |___________| |___________| |___________|

This class is templatized on the two interfaces, T1, T2, as well as on the pass through filter implementation, FilterType, the special set callback function, and a boolean indicating whether the chain owns the filters (if false, the filters must delete themselves at the correct time). FilterType must have GenericFilter as an ancestor.

Definition at line 207 of file FilterChain.h.

Member Typedef Documentation

template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
using proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::FilterChainType = GenericFilter<T1, T2, set_callback, TakeOwnership>

Definition at line 259 of file FilterChain.h.

Constructor & Destructor Documentation

template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::FilterChain ( std::unique_ptr< T1 destination)
inlineexplicit

Definition at line 209 of file FilterChain.h.

209  :
210  FilterType(false, false) {
211  static_assert(TakeOwnership, "unique_ptr constructor only available "
212  "if the chain owns the filters.");
213  this->call_ = CHECK_NOTNULL(destination.release());
214  this->callback_ = nullptr; //must call setCallback() explicitly
215  this->callSource_ = this;
216  this->callbackSource_ = this->call_;
217  this->chainEnd_ = this->call_;
218  }
folly::Function< void()> callback_
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::FilterChain ( T1 destination)
inlineexplicit

Definition at line 220 of file FilterChain.h.

220  :
221  FilterType(false, false) {
222  static_assert(!TakeOwnership, "raw pointer constructor only available "
223  "if the chain doesn't own the filters.");
224  this->call_ = CHECK_NOTNULL(destination);
225  this->callback_ = nullptr; //must call setCallback() explicitly
226  this->callSource_ = this;
227  this->callbackSource_ = this->call_;
228  this->chainEnd_ = this->call_;
229  }
folly::Function< void()> callback_

Member Function Documentation

template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
template<typename C , typename... Args>
FilterChain<T1, T2, FilterType, set_callback, TakeOwnership>& proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::add ( Args &&...  args)
inline

Another way to add filters. This way is similar to 'emplace_front' and returns a reference to itself so you can chain add() calls if you like.

Definition at line 341 of file FilterChain.h.

Referenced by proxygen::HTTPSession::WriteSegment::writeErr().

341  {
342  this->append(new C(std::forward<Args>(args)...));
343  return *this;
344  }
void append(std::unique_ptr< IOBuf > &buf, StringPiece str)
Definition: IOBufTest.cpp:37
#define C(name, bit)
Definition: CpuId.h:204
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
template<typename C , typename C2 , typename... Types>
std::enable_if<std::is_constructible<C>::value>::type proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::addFilters ( )
inline

Adds filters with the given types to the front of the chain.

Definition at line 301 of file FilterChain.h.

Referenced by proxygen::HTTPSession::setupCodec().

301  {
302  // Callback <-> F1 <-> F2 ... <-> F_new <-> Destination
303  this->append(new C());
304  addFilters<C2, Types...>();
305  }
std::enable_if< std::is_constructible< C >::value >::type addFilters()
Definition: FilterChain.h:301
void append(std::unique_ptr< IOBuf > &buf, StringPiece str)
Definition: IOBufTest.cpp:37
#define C(name, bit)
Definition: CpuId.h:204
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
template<typename C >
std::enable_if<std::is_constructible<C>::value>::type proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::addFilters ( )
inline

Base case of above function where we add a single filter.

Definition at line 311 of file FilterChain.h.

311  {
312  this->append(new C());
313  }
void append(std::unique_ptr< IOBuf > &buf, StringPiece str)
Definition: IOBufTest.cpp:37
#define C(name, bit)
Definition: CpuId.h:204
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
template<typename C , typename... Types>
void proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::addFilters ( std::unique_ptr< C cur,
Types &&...  remaining 
)
inline

Adds already constructed filters (inside unique_ptr) to the front of the chain.

Definition at line 320 of file FilterChain.h.

320  {
321  static_assert(TakeOwnership, "addFilters() can only take "
322  "unique_ptr if the chain owns the filters");
323  this->append(cur.release());
324  addFilters(std::forward<Types>(remaining)...);
325  }
std::enable_if< std::is_constructible< C >::value >::type addFilters()
Definition: FilterChain.h:301
void append(std::unique_ptr< IOBuf > &buf, StringPiece str)
Definition: IOBufTest.cpp:37
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
template<typename C , typename... Types>
void proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::addFilters ( C cur,
Types &&...  remaining 
)
inline

Definition at line 328 of file FilterChain.h.

328  {
329  static_assert(!TakeOwnership, "addFilters() can only take "
330  "pointers if the chain doesn't own the filters");
331  this->append(cur);
332  addFilters(std::forward<Types>(remaining)...);
333  }
std::enable_if< std::is_constructible< C >::value >::type addFilters()
Definition: FilterChain.h:301
void append(std::unique_ptr< IOBuf > &buf, StringPiece str)
Definition: IOBufTest.cpp:37
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
void proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::addFilters ( )
inlineprivate

Base case for addFilters() called with no arguments. It doesn't need to be public since trying to add zero filters doesn't make sense from a public API pov.

Definition at line 364 of file FilterChain.h.

364 {}
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
T1* proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::call ( )
inline

Returns the head of the call chain. Do* not* call T1::set_callback() on this member. To change the callback of this entire filter chain, use the separate setCallback() method.

Definition at line 242 of file FilterChain.h.

Referenced by proxygen::HTTPSession::setupCodec().

242  {
243  return this->call_;
244  }
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
const T1* proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::call ( ) const
inline

Definition at line 245 of file FilterChain.h.

245  {
246  return this->call_;
247  }
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
void proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::foreach ( folly::FunctionRef< void(FilterChainType *)>  fn)
inline

Definition at line 349 of file FilterChain.h.

Referenced by proxygen::HTTPUpstreamSession::attachThreadLocals().

349  {
350  auto cur = this->next_;
351  while (cur) {
352  auto filter = cur;
353  cur = cur->next_;
354  fn(filter);
355  }
356  }
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter
Definition: filter.h:75
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
const T1& proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::getChainEnd ( ) const
inline

Definition at line 255 of file FilterChain.h.

Referenced by proxygen::HTTPSession::getCodec().

255  {
256  return *chainEnd_;
257  }
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
T1* proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::getChainEndPtr ( )
inline

Returns the concrete implementation at the end of the filter chain.

Definition at line 252 of file FilterChain.h.

Referenced by proxygen::HTTPSessionBase::initCodecHeaderIndexingStrategy().

252  {
253  return chainEnd_;
254  }
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
const T1* proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::operator-> ( ) const
inline

Definition at line 346 of file FilterChain.h.

346 { return call(); }
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
T1* proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::operator-> ( )
inline

Definition at line 347 of file FilterChain.h.

347 { return call(); }
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
void proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::setCallback ( T2 cb)
inlineoverride

Set the callback for this entire filter chain. Setting this to null will uninstall the callback from the concrete object at the end of the chain.

Definition at line 235 of file FilterChain.h.

Referenced by proxygen::HTTPSession::setupCodec().

235 { this->setCallbackInternalImpl(cb, cb); }
template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
std::unique_ptr<T1> proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::setDestination ( std::unique_ptr< T1 destination)
inline

Definition at line 260 of file FilterChain.h.

Referenced by proxygen::HTTPSession::onNativeProtocolUpgradeImpl().

260  {
261  static_assert(TakeOwnership, "unique_ptr setDestination only available "
262  "if the chain owns the filters.");
263  // find the last filter in the chain, and the last filter that wants calls,
264  // callbacks
265  FilterChainType* lastFilter = this;
266  FilterChainType* lastCall = this;
267  FilterChainType* lastCallback = this;
268  while (lastFilter->next_) {
269  if (lastFilter->kWantsCalls_) {
270  lastCall = lastFilter;
271  }
272  if (lastFilter->kWantsCallbacks_) {
273  lastCallback = lastFilter;
274  }
275  if (lastFilter->call_ == this->chainEnd_) {
276  // Search and replace, the last N non-call filters all point to dest
277  lastFilter->call_ = destination.get();
278  }
279  lastFilter = lastFilter->next_;
280  }
281  if (lastFilter->kWantsCalls_) {
282  lastCall = lastFilter;
283  }
284  if (lastFilter->kWantsCallbacks_) {
285  lastCallback = lastFilter;
286  }
287  lastFilter->call_ = CHECK_NOTNULL(destination.release());
288  lastCall->call_ = lastFilter->call_;
289  lastCallback->callbackSource_ = lastFilter->call_;
290  auto oldChainEnd = this->chainEnd_;
291  this->chainEnd_ = lastFilter->call_;
292 
293  this->chainEnd_->setCallback(lastCallback);
294  return std::unique_ptr<T1>(oldChainEnd);
295  }
GenericFilter< T1, T2, set_callback, TakeOwnership > FilterChainType
Definition: FilterChain.h:259

Member Data Documentation

template<typename T1, typename T2, typename FilterType, void(T1::*)(T2 *) set_callback, bool TakeOwnership>
T1* proxygen::FilterChain< T1, T2, FilterType, set_callback, TakeOwnership >::chainEnd_
private

Definition at line 366 of file FilterChain.h.


The documentation for this class was generated from the following file: