Chris@63: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@63: // Licensed under the MIT License:
Chris@63: //
Chris@63: // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@63: // of this software and associated documentation files (the "Software"), to deal
Chris@63: // in the Software without restriction, including without limitation the rights
Chris@63: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@63: // copies of the Software, and to permit persons to whom the Software is
Chris@63: // furnished to do so, subject to the following conditions:
Chris@63: //
Chris@63: // The above copyright notice and this permission notice shall be included in
Chris@63: // all copies or substantial portions of the Software.
Chris@63: //
Chris@63: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@63: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@63: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@63: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@63: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@63: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@63: // THE SOFTWARE.
Chris@63: 
Chris@63: #ifndef KJ_FUNCTION_H_
Chris@63: #define KJ_FUNCTION_H_
Chris@63: 
Chris@63: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
Chris@63: #pragma GCC system_header
Chris@63: #endif
Chris@63: 
Chris@63: #include "memory.h"
Chris@63: 
Chris@63: namespace kj {
Chris@63: 
Chris@63: template <typename Signature>
Chris@63: class Function;
Chris@63: // Function wrapper using virtual-based polymorphism.  Use this when template polymorphism is
Chris@63: // not possible.  You can, for example, accept a Function as a parameter:
Chris@63: //
Chris@63: //     void setFilter(Function<bool(const Widget&)> filter);
Chris@63: //
Chris@63: // The caller of `setFilter()` may then pass any callable object as the parameter.  The callable
Chris@63: // object does not have to have the exact signature specified, just one that is "compatible" --
Chris@63: // i.e. the return type is covariant and the parameters are contravariant.
Chris@63: //
Chris@63: // Unlike `std::function`, `kj::Function`s are movable but not copyable, just like `kj::Own`.  This
Chris@63: // is to avoid unexpected heap allocation or slow atomic reference counting.
Chris@63: //
Chris@63: // When a `Function` is constructed from an lvalue, it captures only a reference to the value.
Chris@63: // When constructed from an rvalue, it invokes the value's move constructor.  So, for example:
Chris@63: //
Chris@63: //     struct AddN {
Chris@63: //       int n;
Chris@63: //       int operator(int i) { return i + n; }
Chris@63: //     }
Chris@63: //
Chris@63: //     Function<int(int, int)> f1 = AddN{2};
Chris@63: //     // f1 owns an instance of AddN.  It may safely be moved out
Chris@63: //     // of the local scope.
Chris@63: //
Chris@63: //     AddN adder(2);
Chris@63: //     Function<int(int, int)> f2 = adder;
Chris@63: //     // f2 contains a reference to `adder`.  Thus, it becomes invalid
Chris@63: //     // when `adder` goes out-of-scope.
Chris@63: //
Chris@63: //     AddN adder2(2);
Chris@63: //     Function<int(int, int)> f3 = kj::mv(adder2);
Chris@63: //     // f3 owns an insatnce of AddN moved from `adder2`.  f3 may safely
Chris@63: //     // be moved out of the local scope.
Chris@63: //
Chris@63: // Additionally, a Function may be bound to a class method using KJ_BIND_METHOD(object, methodName).
Chris@63: // For example:
Chris@63: //
Chris@63: //     class Printer {
Chris@63: //     public:
Chris@63: //       void print(int i);
Chris@63: //       void print(kj::StringPtr s);
Chris@63: //     };
Chris@63: //
Chris@63: //     Printer p;
Chris@63: //
Chris@63: //     Function<void(uint)> intPrinter = KJ_BIND_METHOD(p, print);
Chris@63: //     // Will call Printer::print(int).
Chris@63: //
Chris@63: //     Function<void(const char*)> strPrinter = KJ_BIND_METHOD(p, print);
Chris@63: //     // Will call Printer::print(kj::StringPtr).
Chris@63: //
Chris@63: // Notice how KJ_BIND_METHOD is able to figure out which overload to use depending on the kind of
Chris@63: // Function it is binding to.
Chris@63: 
Chris@63: template <typename Signature>
Chris@63: class ConstFunction;
Chris@63: // Like Function, but wraps a "const" (i.e. thread-safe) call.
Chris@63: 
Chris@63: template <typename Return, typename... Params>
Chris@63: class Function<Return(Params...)> {
Chris@63: public:
Chris@63:   template <typename F>
Chris@63:   inline Function(F&& f): impl(heap<Impl<F>>(kj::fwd<F>(f))) {}
Chris@63:   Function() = default;
Chris@63: 
Chris@63:   // Make sure people don't accidentally end up wrapping a reference when they meant to return
Chris@63:   // a function.
Chris@63:   KJ_DISALLOW_COPY(Function);
Chris@63:   Function(Function&) = delete;
Chris@63:   Function& operator=(Function&) = delete;
Chris@63:   template <typename T> Function(const Function<T>&) = delete;
Chris@63:   template <typename T> Function& operator=(const Function<T>&) = delete;
Chris@63:   template <typename T> Function(const ConstFunction<T>&) = delete;
Chris@63:   template <typename T> Function& operator=(const ConstFunction<T>&) = delete;
Chris@63:   Function(Function&&) = default;
Chris@63:   Function& operator=(Function&&) = default;
Chris@63: 
Chris@63:   inline Return operator()(Params... params) {
Chris@63:     return (*impl)(kj::fwd<Params>(params)...);
Chris@63:   }
Chris@63: 
Chris@63:   Function reference() {
Chris@63:     // Forms a new Function of the same type that delegates to this Function by reference.
Chris@63:     // Therefore, this Function must outlive the returned Function, but otherwise they behave
Chris@63:     // exactly the same.
Chris@63: 
Chris@63:     return *impl;
Chris@63:   }
Chris@63: 
Chris@63: private:
Chris@63:   class Iface {
Chris@63:   public:
Chris@63:     virtual Return operator()(Params... params) = 0;
Chris@63:   };
Chris@63: 
Chris@63:   template <typename F>
Chris@63:   class Impl final: public Iface {
Chris@63:   public:
Chris@63:     explicit Impl(F&& f): f(kj::fwd<F>(f)) {}
Chris@63: 
Chris@63:     Return operator()(Params... params) override {
Chris@63:       return f(kj::fwd<Params>(params)...);
Chris@63:     }
Chris@63: 
Chris@63:   private:
Chris@63:     F f;
Chris@63:   };
Chris@63: 
Chris@63:   Own<Iface> impl;
Chris@63: };
Chris@63: 
Chris@63: template <typename Return, typename... Params>
Chris@63: class ConstFunction<Return(Params...)> {
Chris@63: public:
Chris@63:   template <typename F>
Chris@63:   inline ConstFunction(F&& f): impl(heap<Impl<F>>(kj::fwd<F>(f))) {}
Chris@63:   ConstFunction() = default;
Chris@63: 
Chris@63:   // Make sure people don't accidentally end up wrapping a reference when they meant to return
Chris@63:   // a function.
Chris@63:   KJ_DISALLOW_COPY(ConstFunction);
Chris@63:   ConstFunction(ConstFunction&) = delete;
Chris@63:   ConstFunction& operator=(ConstFunction&) = delete;
Chris@63:   template <typename T> ConstFunction(const ConstFunction<T>&) = delete;
Chris@63:   template <typename T> ConstFunction& operator=(const ConstFunction<T>&) = delete;
Chris@63:   template <typename T> ConstFunction(const Function<T>&) = delete;
Chris@63:   template <typename T> ConstFunction& operator=(const Function<T>&) = delete;
Chris@63:   ConstFunction(ConstFunction&&) = default;
Chris@63:   ConstFunction& operator=(ConstFunction&&) = default;
Chris@63: 
Chris@63:   inline Return operator()(Params... params) const {
Chris@63:     return (*impl)(kj::fwd<Params>(params)...);
Chris@63:   }
Chris@63: 
Chris@63:   ConstFunction reference() const {
Chris@63:     // Forms a new ConstFunction of the same type that delegates to this ConstFunction by reference.
Chris@63:     // Therefore, this ConstFunction must outlive the returned ConstFunction, but otherwise they
Chris@63:     // behave exactly the same.
Chris@63: 
Chris@63:     return *impl;
Chris@63:   }
Chris@63: 
Chris@63: private:
Chris@63:   class Iface {
Chris@63:   public:
Chris@63:     virtual Return operator()(Params... params) const = 0;
Chris@63:   };
Chris@63: 
Chris@63:   template <typename F>
Chris@63:   class Impl final: public Iface {
Chris@63:   public:
Chris@63:     explicit Impl(F&& f): f(kj::fwd<F>(f)) {}
Chris@63: 
Chris@63:     Return operator()(Params... params) const override {
Chris@63:       return f(kj::fwd<Params>(params)...);
Chris@63:     }
Chris@63: 
Chris@63:   private:
Chris@63:     F f;
Chris@63:   };
Chris@63: 
Chris@63:   Own<Iface> impl;
Chris@63: };
Chris@63: 
Chris@63: #if 1
Chris@63: 
Chris@63: namespace _ {  // private
Chris@63: 
Chris@63: template <typename T, typename Signature, Signature method>
Chris@63: class BoundMethod;
Chris@63: 
Chris@63: template <typename T, typename Return, typename... Params, Return (Decay<T>::*method)(Params...)>
Chris@63: class BoundMethod<T, Return (Decay<T>::*)(Params...), method> {
Chris@63: public:
Chris@63:   BoundMethod(T&& t): t(kj::fwd<T>(t)) {}
Chris@63: 
Chris@63:   Return operator()(Params&&... params) {
Chris@63:     return (t.*method)(kj::fwd<Params>(params)...);
Chris@63:   }
Chris@63: 
Chris@63: private:
Chris@63:   T t;
Chris@63: };
Chris@63: 
Chris@63: template <typename T, typename Return, typename... Params,
Chris@63:           Return (Decay<T>::*method)(Params...) const>
Chris@63: class BoundMethod<T, Return (Decay<T>::*)(Params...) const, method> {
Chris@63: public:
Chris@63:   BoundMethod(T&& t): t(kj::fwd<T>(t)) {}
Chris@63: 
Chris@63:   Return operator()(Params&&... params) const {
Chris@63:     return (t.*method)(kj::fwd<Params>(params)...);
Chris@63:   }
Chris@63: 
Chris@63: private:
Chris@63:   T t;
Chris@63: };
Chris@63: 
Chris@63: }  // namespace _ (private)
Chris@63: 
Chris@63: #define KJ_BIND_METHOD(obj, method) \
Chris@63:   ::kj::_::BoundMethod<KJ_DECLTYPE_REF(obj), \
Chris@63:                        decltype(&::kj::Decay<decltype(obj)>::method), \
Chris@63:                        &::kj::Decay<decltype(obj)>::method>(obj)
Chris@63: // Macro that produces a functor object which forwards to the method `obj.name`.  If `obj` is an
Chris@63: // lvalue, the functor will hold a reference to it.  If `obj` is an rvalue, the functor will
Chris@63: // contain a copy (by move) of it.
Chris@63: //
Chris@63: // The current implementation requires that the method is not overloaded.
Chris@63: //
Chris@63: // TODO(someday):  C++14's generic lambdas may be able to simplify this code considerably, and
Chris@63: //   probably make it work with overloaded methods.
Chris@63: 
Chris@63: #else
Chris@63: // Here's a better implementation of the above that doesn't work with GCC (but does with Clang)
Chris@63: // because it uses a local class with a template method.  Sigh.  This implementation supports
Chris@63: // overloaded methods.
Chris@63: 
Chris@63: #define KJ_BIND_METHOD(obj, method) \
Chris@63:   ({ \
Chris@63:     typedef KJ_DECLTYPE_REF(obj) T; \
Chris@63:     class F { \
Chris@63:     public: \
Chris@63:       inline F(T&& t): t(::kj::fwd<T>(t)) {} \
Chris@63:       template <typename... Params> \
Chris@63:       auto operator()(Params&&... params) \
Chris@63:           -> decltype(::kj::instance<T>().method(::kj::fwd<Params>(params)...)) { \
Chris@63:         return t.method(::kj::fwd<Params>(params)...); \
Chris@63:       } \
Chris@63:     private: \
Chris@63:       T t; \
Chris@63:     }; \
Chris@63:     (F(obj)); \
Chris@63:   })
Chris@63: // Macro that produces a functor object which forwards to the method `obj.name`.  If `obj` is an
Chris@63: // lvalue, the functor will hold a reference to it.  If `obj` is an rvalue, the functor will
Chris@63: // contain a copy (by move) of it.
Chris@63: 
Chris@63: #endif
Chris@63: 
Chris@63: }  // namespace kj
Chris@63: 
Chris@63: #endif  // KJ_FUNCTION_H_