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