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