proxygen
folly::Function< FunctionType > Class Template Reference

#include <Function.h>

Inheritance diagram for folly::Function< FunctionType >:
folly::detail::function::FunctionTraits< FunctionType >

Public Member Functions

 Function ()=default
 
 Function (const Function &)=delete
 
 Function (Function &&that) noexcept
 
 Function (std::nullptr_t) noexcept
 
template<typename Fun , typename = detail::function::EnableIfNotFunction<Fun>, typename = typename Traits::template ResultOf<Fun>>
 Function (Fun fun) noexcept(IsSmall< Fun >::value &&noexcept(Fun(std::declval< Fun >())))
 
template<typename Signature , typename = typename Traits::template ResultOf<Function<Signature>>>
 Function (Function< Signature > &&that) noexcept(noexcept(Function(std::move(that), CoerceTag{})))
 
template<typename Member , typename Class , typename = decltype(Function(std::mem_fn((Member Class::*)0)))>
 Function (Member Class::*ptr) noexcept
 
 ~Function ()
 
Functionoperator= (const Function &)=delete
 
Functionoperator= (Function &&that) noexcept
 
template<typename Fun , typename = decltype(Function(std::declval<Fun>()))>
Functionoperator= (Fun fun) noexcept(noexcept(/*implicit */Function(std::declval< Fun >())))
 
template<typename Signature , typename = typename Traits::template ResultOf<Function<Signature>>>
Functionoperator= (Function< Signature > &&that) noexcept(noexcept(Function(std::move(that))))
 
Functionoperator= (std::nullptr_t) noexcept
 
template<typename Member , typename Class >
auto operator= (Member Class::*ptr) noexcept-> decltype(operator=(std::mem_fn(ptr)))
 
void swap (Function &that) noexcept
 
 operator bool () const noexcept
 
bool hasAllocatedMemory () const noexcept
 
SharedProxy asSharedProxy ()&&
 
std::function< typename Traits::NonConstSignature > asStdFunction ()&&
 

Private Types

using Data = detail::function::Data
 
using Op = detail::function::Op
 
using SmallTag = detail::function::SmallTag
 
using HeapTag = detail::function::HeapTag
 
using CoerceTag = detail::function::CoerceTag
 
using Traits = detail::function::FunctionTraits< FunctionType >
 
using Call = typename Traits::Call
 
using Exec = bool(*)(Op, Data *, Data *)
 
template<typename Fun >
using IsSmall = detail::function::IsSmall< Fun >
 

Private Member Functions

bool exec (Op o, Data *src, Data *dst) const
 
template<typename Fun >
 Function (Fun &&fun, SmallTag) noexcept
 
template<typename Fun >
 Function (Fun &&fun, HeapTag)
 
template<typename Signature >
 Function (Function< Signature > &&that, CoerceTag)
 
 Function (Function< typename Traits::OtherSignature > &&that, CoerceTag) noexcept
 

Private Attributes

Data data_ {}
 
Call call_ {&Traits::uninitCall}
 
Exec exec_ {nullptr}
 
friend Traits
 

Friends

class Function< typename Traits::OtherSignature >
 
Function< typename Traits::ConstSignature > folly::constCastFunction (Function< typename Traits::NonConstSignature > &&) noexcept
 

Detailed Description

template<typename FunctionType>
class folly::Function< FunctionType >

Definition at line 235 of file Function.h.

Member Typedef Documentation

template<typename FunctionType>
using folly::Function< FunctionType >::Call = typename Traits::Call
private

Definition at line 529 of file Function.h.

template<typename FunctionType>
using folly::Function< FunctionType >::CoerceTag = detail::function::CoerceTag
private

Definition at line 526 of file Function.h.

template<typename FunctionType>
using folly::Function< FunctionType >::Data = detail::function::Data
private

Definition at line 522 of file Function.h.

template<typename FunctionType>
using folly::Function< FunctionType >::Exec = bool (*)(Op, Data*, Data*)
private

Definition at line 530 of file Function.h.

template<typename FunctionType>
using folly::Function< FunctionType >::HeapTag = detail::function::HeapTag
private

Definition at line 525 of file Function.h.

template<typename FunctionType>
template<typename Fun >
using folly::Function< FunctionType >::IsSmall = detail::function::IsSmall<Fun>
private

Definition at line 533 of file Function.h.

template<typename FunctionType>
using folly::Function< FunctionType >::Op = detail::function::Op
private

Definition at line 523 of file Function.h.

template<typename FunctionType>
using folly::Function< FunctionType >::SmallTag = detail::function::SmallTag
private

Definition at line 524 of file Function.h.

template<typename FunctionType>
using folly::Function< FunctionType >::Traits = detail::function::FunctionTraits<FunctionType>
private

Definition at line 528 of file Function.h.

Constructor & Destructor Documentation

template<typename FunctionType>
template<typename Fun >
folly::Function< FunctionType >::Function ( Fun &&  fun,
SmallTag   
)
inlineprivatenoexcept

Definition at line 553 of file Function.h.

553  {
554  using FunT = typename std::decay<Fun>::type;
556  ::new (static_cast<void*>(&data_.tiny)) FunT(static_cast<Fun&&>(fun));
557  call_ = &Traits::template callSmall<FunT>;
558  exec_ = &detail::function::execSmall<FunT>;
559  }
560  }
bool execSmall(Op o, Data *src, Data *dst)
Definition: Function.h:483
PskType type
bool isNullPtrFn(T *p)
Definition: Function.h:277
detail::function::FunctionTraits< FunctionType > Traits
Definition: Function.h:528
std::aligned_storage< 6 *sizeof(void *)>::type tiny
Definition: Function.h:255
void fun()
template<typename FunctionType>
template<typename Fun >
folly::Function< FunctionType >::Function ( Fun &&  fun,
HeapTag   
)
inlineprivate

Definition at line 563 of file Function.h.

563  {
564  using FunT = typename std::decay<Fun>::type;
565  data_.big = new FunT(static_cast<Fun&&>(fun));
566  call_ = &Traits::template callBig<FunT>;
567  exec_ = &detail::function::execBig<FunT>;
568  }
PskType type
void fun()
template<typename FunctionType>
template<typename Signature >
folly::Function< FunctionType >::Function ( Function< Signature > &&  that,
CoerceTag   
)
inlineprivate

Definition at line 571 of file Function.h.

572  : Function(static_cast<Function<Signature>&&>(that), HeapTag{}) {}
A polymorphic function wrapper that is not copyable and does not require the wrapped function to be c...
detail::function::HeapTag HeapTag
Definition: Function.h:525
Function()=default
template<typename FunctionType>
folly::Function< FunctionType >::Function ( Function< typename Traits::OtherSignature > &&  that,
CoerceTag   
)
inlineprivatenoexcept

Definition at line 574 of file Function.h.

575  : call_(that.call_), exec_(that.exec_) {
576  that.call_ = &Traits::uninitCall;
577  that.exec_ = nullptr;
578  exec(Op::MOVE, &that.data_, &data_);
579  }
bool exec(Op o, Data *src, Data *dst) const
Definition: Function.h:543
template<typename FunctionType>
folly::Function< FunctionType >::Function ( )
default

Default constructor. Constructs an empty Function.

template<typename FunctionType>
folly::Function< FunctionType >::Function ( const Function< FunctionType > &  )
delete
template<typename FunctionType>
folly::Function< FunctionType >::Function ( Function< FunctionType > &&  that)
inlinenoexcept

Move constructor

Definition at line 601 of file Function.h.

601  : call_(that.call_), exec_(that.exec_) {
602  // that must be uninitialized before exec() call in the case of self move
603  that.call_ = &Traits::uninitCall;
604  that.exec_ = nullptr;
605  exec(Op::MOVE, &that.data_, &data_);
606  }
bool exec(Op o, Data *src, Data *dst) const
Definition: Function.h:543
template<typename FunctionType>
folly::Function< FunctionType >::Function ( std::nullptr_t  )
inlinenoexcept

Constructs an empty Function.

Definition at line 611 of file Function.h.

611 {}
template<typename FunctionType>
template<typename Fun , typename = detail::function::EnableIfNotFunction<Fun>, typename = typename Traits::template ResultOf<Fun>>
folly::Function< FunctionType >::Function ( Fun  fun)
inlinenoexcept

Constructs a new Function from any callable object that is not a folly::Function. This handles function pointers, pointers to static member functions, std::reference_wrapper objects, std::function objects, and arbitrary objects that implement operator() if the parameter signature matches (i.e. it returns an object convertible to R when called with Args...).

Note
typename Traits::template ResultOf<Fun> prevents this overload from being selected by overload resolution when fun is not a compatible function.
The noexcept requires some explanation. IsSmall is true when the decayed type fits within the internal buffer and is noexcept-movable. But this ctor might copy, not move. What we need here, if this ctor does a copy, is that this ctor be noexcept when the copy is noexcept. That is not checked in IsSmall, and shouldn't be, because once the Function is constructed, the contained object is never copied. This check is for this ctor only, in the case that this ctor does a copy.

Definition at line 637 of file Function.h.

639  : Function(std::move(fun), IsSmall<Fun>{}) {}
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void fun()
Function()=default
template<typename FunctionType>
template<typename Signature , typename = typename Traits::template ResultOf<Function<Signature>>>
folly::Function< FunctionType >::Function ( Function< Signature > &&  that)
inlinenoexcept

For move-constructing from a folly::Function<X(Ys...) [const?]>. For a Function with a const function type, the object must be callable from a const-reference, i.e. implement operator() const. For a Function with a non-const function type, the object will be called from a non-const reference, which means that it will execute a non-const operator() if it is defined, and falls back to operator() const otherwise.

Definition at line 653 of file Function.h.

654  {})))
655  : Function(std::move(that), CoerceTag{}) {}
detail::function::CoerceTag CoerceTag
Definition: Function.h:526
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Function()=default
template<typename FunctionType>
template<typename Member , typename Class , typename = decltype(Function(std::mem_fn((Member Class::*)0)))>
folly::Function< FunctionType >::Function ( Member Class::*  ptr)
inlinenoexcept

If ptr is null, constructs an empty Function. Otherwise, this constructor is equivalent to Function(std::mem_fn(ptr)).

Definition at line 667 of file Function.h.

667  {
668  if (ptr) {
669  *this = std::mem_fn(ptr);
670  }
671  }
void * ptr
template<typename FunctionType>
folly::Function< FunctionType >::~Function ( )
inline

Definition at line 673 of file Function.h.

673  {
674  exec(Op::NUKE, &data_, nullptr);
675  }
bool exec(Op o, Data *src, Data *dst) const
Definition: Function.h:543

Member Function Documentation

template<typename FunctionType>
SharedProxy folly::Function< FunctionType >::asSharedProxy ( )
inline

Move this Function into a copyable callable object, of which all copies share the state.

Definition at line 801 of file Function.h.

801  {
802  return SharedProxy{std::move(*this)};
803  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<typename FunctionType>
std::function<typename Traits::NonConstSignature> folly::Function< FunctionType >::asStdFunction ( )
inline

Construct a std::function by moving in the contents of this Function. Note that the returned std::function will share its state (i.e. captured data) across all copies you make of it, so be very careful when copying.

Definition at line 810 of file Function.h.

810  {
811  return std::move(*this).asSharedProxy();
812  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<typename FunctionType>
bool folly::Function< FunctionType >::exec ( Op  o,
Data src,
Data dst 
) const
inlineprivate

Definition at line 543 of file Function.h.

543  {
544  return exec_ && exec_(o, src, dst);
545  }
template<typename FunctionType>
bool folly::Function< FunctionType >::hasAllocatedMemory ( ) const
inlinenoexcept

Returns true if this Function stores the callable on the heap. If false is returned, there has been no additional memory allocation and the callable is stored inside the Function object itself.

Definition at line 791 of file Function.h.

Referenced by TEST().

791  {
792  return exec(Op::HEAP, nullptr, nullptr);
793  }
bool exec(Op o, Data *src, Data *dst) const
Definition: Function.h:543
template<typename FunctionType>
folly::Function< FunctionType >::operator bool ( ) const
inlineexplicitnoexcept

Returns true if this Function contains a callable, i.e. is non-empty.

Definition at line 781 of file Function.h.

781  {
782  return exec_ != nullptr;
783  }
template<typename FunctionType>
Function& folly::Function< FunctionType >::operator= ( const Function< FunctionType > &  )
delete
template<typename FunctionType>
Function& folly::Function< FunctionType >::operator= ( Function< FunctionType > &&  that)
inlinenoexcept

Move assignment operator

Note
Leaves that in a valid but unspecified state. If &that == this then *this is left in a valid but unspecified state.

Definition at line 695 of file Function.h.

695  {
696  // Q: Why is it safe to destroy and reconstruct this object in place?
697  // A: Two reasons: First, `Function` is a final class, so in doing this
698  // we aren't slicing off any derived parts. And second, the move
699  // operation is guaranteed not to throw so we always leave the object
700  // in a valid state.
701  // In the case of self-move (this == &that), this leaves the object in
702  // a default-constructed state. First the object is destroyed, then we
703  // pass the destroyed object to the move constructor. The first thing the
704  // move constructor does is default-construct the object. That object is
705  // "moved" into itself, which is a no-op for a default-constructed Function.
706  this->~Function();
707  ::new (this) Function(std::move(that));
708  return *this;
709  }
A polymorphic function wrapper that is not copyable and does not require the wrapped function to be c...
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
template<typename FunctionType>
template<typename Fun , typename = decltype(Function(std::declval<Fun>()))>
Function& folly::Function< FunctionType >::operator= ( Fun  fun)
inlinenoexcept

Assigns a callable object to this Function. If the operation fails, *this is left unmodified.

Note
typename = decltype(Function(std::declval<Fun>())) prevents this overload from being selected by overload resolution when fun is not a compatible function.

Definition at line 720 of file Function.h.

721  {
722  // Doing this in place is more efficient when we can do so safely.
723  if (noexcept(/* implicit */ Function(std::declval<Fun>()))) {
724  // Q: Why is is safe to destroy and reconstruct this object in place?
725  // A: See the explanation in the move assignment operator.
726  this->~Function();
727  ::new (this) Function(std::move(fun));
728  } else {
729  // Construct a temporary and (nothrow) swap.
730  Function(std::move(fun)).swap(*this);
731  }
732  return *this;
733  }
A polymorphic function wrapper that is not copyable and does not require the wrapped function to be c...
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
requires E e noexcept(noexcept(s.error(std::move(e))))
void fun()
Function()=default
template<typename FunctionType>
template<typename Signature , typename = typename Traits::template ResultOf<Function<Signature>>>
Function& folly::Function< FunctionType >::operator= ( Function< Signature > &&  that)
inlinenoexcept

For assigning from a Function<X(Ys..) [const?]>.

Definition at line 741 of file Function.h.

742  {
743  return (*this = Function(std::move(that)));
744  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Function()=default
template<typename FunctionType>
Function& folly::Function< FunctionType >::operator= ( std::nullptr_t  )
inlinenoexcept

Clears this Function.

Definition at line 749 of file Function.h.

749  {
750  return (*this = Function());
751  }
Function()=default
template<typename FunctionType>
template<typename Member , typename Class >
auto folly::Function< FunctionType >::operator= ( Member Class::*  ptr) -> decltype(operator=(std::mem_fn(ptr)))
inlinenoexcept

If ptr is null, clears this Function. Otherwise, this assignment operator is equivalent to *this = std::mem_fn(ptr).

Definition at line 758 of file Function.h.

761  {
762  return ptr ? (*this = std::mem_fn(ptr)) : (*this = Function());
763  }
void * ptr
Function()=default
template<typename FunctionType>
void folly::Function< FunctionType >::swap ( Function< FunctionType > &  that)
inlinenoexcept

Exchanges the callable objects of *this and that.

Definition at line 773 of file Function.h.

Referenced by swap_test(), and TEST().

773  {
774  std::swap(*this, that);
775  }
void swap(Function< FunctionType > &lhs, Function< FunctionType > &rhs) noexcept
Definition: Function.h:816

Friends And Related Function Documentation

template<typename FunctionType>
Function<typename Traits::ConstSignature> folly::constCastFunction ( Function< typename Traits::NonConstSignature > &&  )
friend
template<typename FunctionType>
friend class Function< typename Traits::OtherSignature >
friend

Definition at line 550 of file Function.h.

Member Data Documentation

template<typename FunctionType>
Data folly::Function< FunctionType >::data_ {}
mutableprivate

Definition at line 539 of file Function.h.

template<typename FunctionType>
Exec folly::Function< FunctionType >::exec_ {nullptr}
private

Definition at line 541 of file Function.h.

template<typename FunctionType>
friend folly::Function< FunctionType >::Traits
private

Definition at line 547 of file Function.h.


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