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