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