Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "functorwrapper.hpp"
- // primary template (declaration)
- template <typename Signature>
- class Function;
- // partial class template specialization
- template <typename ReturnType, typename... Args>
- class Function<ReturnType(Args...)>
- {
- public:
- // constructors
- Function() : mFunctorWrapper(nullptr) {} // default constructor
- Function(const Function &); // copy constructor
- Function(Function &&); // move constructor
- template <typename Functor> Function(Functor &&); // generalized constructor
- // destructor
- ~Function() { delete mFunctorWrapper; }
- // copy/move operations
- Function &operator=(Function const &); // copy assignmaent operator
- Function &operator=(Function &&); // move assignment operator
- template <typename Functor> Function &operator=(Functor &&); // generalized assignment operator
- // overloaded function call operator
- ReturnType operator()(Args...);
- private:
- FunctorWrapperBase<ReturnType(Args...)> *mFunctorWrapper;
- };
- template <typename ReturnType, typename... Args>
- Function<ReturnType(Args...)>::Function(const Function &other) : mFunctorWrapper(nullptr)
- {
- if (other.mFunctorWrapper)
- mFunctorWrapper = other.mFunctorWrapper->Clone();
- }
- template <typename ReturnType, typename... Args>
- Function<ReturnType(Args...)>::Function(Function &&other) : mFunctorWrapper(other.mFunctorWrapper)
- {
- other.mFunctorWrapper = nullptr;
- }
- template <typename ReturnType, typename... Args>
- template <typename Functor>
- Function<ReturnType(Args...)>::Function(Functor &&functor)
- {
- // remove reference if l-value (template type argument deduced as Functor &)
- mFunctorWrapper = new FunctorWrapper<typename std::remove_reference<Functor>::type, ReturnType(Args...)>(std::forward<Functor>(functor));
- }
- template <typename ReturnType, typename... Args>
- Function<ReturnType(Args...)> &Function<ReturnType(Args...)>::operator=(const Function &other)
- {
- mFunctorWrapper = other.mFunctorWrapper->Clone();
- return *this;
- }
- template <typename ReturnType, typename... Args>
- Function<ReturnType(Args...)> &Function<ReturnType(Args...)>::operator=(Function &&other)
- {
- mFunctorWrapper = other.mFunctorWrapper;
- other.mFunctorWrapper = nullptr;
- return *this;
- }
- template <typename ReturnType, typename... Args>
- template <typename Functor>
- Function<ReturnType(Args...)> &Function<ReturnType(Args...)>::operator=(Functor &&functor)
- {
- mFunctorWrapper = new FunctorWrapper<typename std::remove_reference<Functor>::type, ReturnType(Args...)>(std::forward<Functor>(functor));
- }
- template <typename ReturnType, typename... Args>
- ReturnType Function<ReturnType(Args...)>::operator()(Args... args)
- {
- mFunctorWrapper->Invoke(args...);
- }
- Function<void(double)> fp4(&FreeFunction);
- fp4(1.2);
- Function<void(double)> fp5 = fp4; // copy construction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement