14 #include <glog/logging.h> 38 template <
typename T1,
typename T2, void (
T1::*set_callback)(T2*),
39 bool TakeOwnership,
typename Dp = std::default_delete<T1> >
51 kWantsCallbacks_(callbacks) {}
55 callbackSource_ =
nullptr;
59 auto next = next_ ? next_ : call_;
61 if (TakeOwnership &&
next) {
70 nextFilter->
next_ = next_;
71 nextFilter->
prev_ =
this;
72 nextFilter->
call_ = call_;
74 nextFilter->
callSource_ = kWantsCalls_ ?
this : callSource_;
77 next_->prev_ = nextFilter;
84 callSource_->
call_ = nextFilter;
93 while (cur->prev_ && !cur->kWantsCallbacks_) {
96 cur->callbackSource_ = nextFilter;
112 setCallbackInternalImpl(cb,
this);
121 prev_->next_ = next_;
124 next_->prev_ = prev_;
127 if (kWantsCalls_ && callSource_) {
128 callSource_->call_ = call_;
130 auto callFilter =
dynamic_cast<Filter*
>(call_);
137 if (kWantsCallbacks_ && callbackSource_) {
138 ((callbackSource_)->*(set_callback))(
callback_);
141 if (callbackFilter) {
146 call_ = callbackSource_ =
nullptr;
148 callSource_ = next_ = prev_ =
nullptr;
161 ((callbackSource_)->*(set_callback))(cb ? sourceSet :
nullptr);
174 T1* callbackSource_{
nullptr};
176 template <
class A,
class B,
class F,
void (A::*fn)(B*),
bool Own>
205 template <
typename T1,
typename T2,
typename FilterType,
206 void (T1::*set_callback)(T2*),
bool TakeOwnership>
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());
215 this->callSource_ =
this;
216 this->callbackSource_ = this->call_;
217 this->chainEnd_ = this->call_;
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);
226 this->callSource_ =
this;
227 this->callbackSource_ = this->call_;
228 this->chainEnd_ = this->call_;
235 void setCallback(T2* cb)
override { this->setCallbackInternalImpl(cb, cb); }
261 static_assert(TakeOwnership,
"unique_ptr setDestination only available " 262 "if the chain owns the filters.");
268 while (lastFilter->
next_) {
270 lastCall = lastFilter;
273 lastCallback = lastFilter;
275 if (lastFilter->
call_ == this->chainEnd_) {
277 lastFilter->
call_ = destination.get();
279 lastFilter = lastFilter->
next_;
282 lastCall = lastFilter;
285 lastCallback = lastFilter;
287 lastFilter->
call_ = CHECK_NOTNULL(destination.release());
290 auto oldChainEnd = this->chainEnd_;
291 this->chainEnd_ = lastFilter->
call_;
293 this->chainEnd_->setCallback(lastCallback);
294 return std::unique_ptr<T1>(oldChainEnd);
300 template<
typename C,
typename C2,
typename... Types>
304 addFilters<C2, Types...>();
319 template<
typename C,
typename... Types>
320 void addFilters(std::unique_ptr<C> cur, Types&&... remaining) {
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)...);
327 template<
typename C,
typename... Types>
329 static_assert(!TakeOwnership,
"addFilters() can only take " 330 "pointers if the chain doesn't own the filters");
332 addFilters(std::forward<Types>(remaining)...);
339 template<
typename C,
typename...
Args>
342 this->
append(
new C(std::forward<Args>(args)...));
350 auto cur = this->next_;
void setCallbackInternalImpl(T2 *cb, T2 *sourceSet)
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter
void append(Filter *nextFilter)
const T1 & getChainEnd() const
const T1 * operator->() const
std::enable_if< std::is_constructible< C >::value >::type addFilters()
A reference wrapper for callable objects.
void append(std::unique_ptr< IOBuf > &buf, StringPiece str)
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
void addFilters(C *cur, Types &&...remaining)
FilterChain< T1, T2, FilterType, set_callback, TakeOwnership > & add(Args &&...args)
void setCallbackInternal(T2 *cb)
const bool kWantsCallbacks_
GenericFilter(bool calls, bool callbacks)
void setCallback(T2 *cb) override
static const char *const value
std::enable_if< std::is_constructible< C >::value >::type addFilters()
std::unique_ptr< T1 > setDestination(std::unique_ptr< T1 > destination)
FilterChain(std::unique_ptr< T1 > destination)
void addFilters(std::unique_ptr< C > cur, Types &&...remaining)
FilterChain(T1 *destination)
folly::Function< void()> callback_
Future< bool > call(int depth, Executor *executor)
~GenericFilter() override