annotate osx/include/kj/function.h @ 134:41e769c91eca

Add Capnp and KJ builds for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 25 Oct 2016 14:48:23 +0100
parents
children 0994c39f1e94
rev   line source
cannam@134 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@134 2 // Licensed under the MIT License:
cannam@134 3 //
cannam@134 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@134 5 // of this software and associated documentation files (the "Software"), to deal
cannam@134 6 // in the Software without restriction, including without limitation the rights
cannam@134 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@134 8 // copies of the Software, and to permit persons to whom the Software is
cannam@134 9 // furnished to do so, subject to the following conditions:
cannam@134 10 //
cannam@134 11 // The above copyright notice and this permission notice shall be included in
cannam@134 12 // all copies or substantial portions of the Software.
cannam@134 13 //
cannam@134 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@134 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@134 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@134 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@134 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@134 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@134 20 // THE SOFTWARE.
cannam@134 21
cannam@134 22 #ifndef KJ_FUNCTION_H_
cannam@134 23 #define KJ_FUNCTION_H_
cannam@134 24
cannam@134 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@134 26 #pragma GCC system_header
cannam@134 27 #endif
cannam@134 28
cannam@134 29 #include "memory.h"
cannam@134 30
cannam@134 31 namespace kj {
cannam@134 32
cannam@134 33 template <typename Signature>
cannam@134 34 class Function;
cannam@134 35 // Function wrapper using virtual-based polymorphism. Use this when template polymorphism is
cannam@134 36 // not possible. You can, for example, accept a Function as a parameter:
cannam@134 37 //
cannam@134 38 // void setFilter(Function<bool(const Widget&)> filter);
cannam@134 39 //
cannam@134 40 // The caller of `setFilter()` may then pass any callable object as the parameter. The callable
cannam@134 41 // object does not have to have the exact signature specified, just one that is "compatible" --
cannam@134 42 // i.e. the return type is covariant and the parameters are contravariant.
cannam@134 43 //
cannam@134 44 // Unlike `std::function`, `kj::Function`s are movable but not copyable, just like `kj::Own`. This
cannam@134 45 // is to avoid unexpected heap allocation or slow atomic reference counting.
cannam@134 46 //
cannam@134 47 // When a `Function` is constructed from an lvalue, it captures only a reference to the value.
cannam@134 48 // When constructed from an rvalue, it invokes the value's move constructor. So, for example:
cannam@134 49 //
cannam@134 50 // struct AddN {
cannam@134 51 // int n;
cannam@134 52 // int operator(int i) { return i + n; }
cannam@134 53 // }
cannam@134 54 //
cannam@134 55 // Function<int(int, int)> f1 = AddN{2};
cannam@134 56 // // f1 owns an instance of AddN. It may safely be moved out
cannam@134 57 // // of the local scope.
cannam@134 58 //
cannam@134 59 // AddN adder(2);
cannam@134 60 // Function<int(int, int)> f2 = adder;
cannam@134 61 // // f2 contains a reference to `adder`. Thus, it becomes invalid
cannam@134 62 // // when `adder` goes out-of-scope.
cannam@134 63 //
cannam@134 64 // AddN adder2(2);
cannam@134 65 // Function<int(int, int)> f3 = kj::mv(adder2);
cannam@134 66 // // f3 owns an insatnce of AddN moved from `adder2`. f3 may safely
cannam@134 67 // // be moved out of the local scope.
cannam@134 68 //
cannam@134 69 // Additionally, a Function may be bound to a class method using KJ_BIND_METHOD(object, methodName).
cannam@134 70 // For example:
cannam@134 71 //
cannam@134 72 // class Printer {
cannam@134 73 // public:
cannam@134 74 // void print(int i);
cannam@134 75 // void print(kj::StringPtr s);
cannam@134 76 // };
cannam@134 77 //
cannam@134 78 // Printer p;
cannam@134 79 //
cannam@134 80 // Function<void(uint)> intPrinter = KJ_BIND_METHOD(p, print);
cannam@134 81 // // Will call Printer::print(int).
cannam@134 82 //
cannam@134 83 // Function<void(const char*)> strPrinter = KJ_BIND_METHOD(p, print);
cannam@134 84 // // Will call Printer::print(kj::StringPtr).
cannam@134 85 //
cannam@134 86 // Notice how KJ_BIND_METHOD is able to figure out which overload to use depending on the kind of
cannam@134 87 // Function it is binding to.
cannam@134 88
cannam@134 89 template <typename Signature>
cannam@134 90 class ConstFunction;
cannam@134 91 // Like Function, but wraps a "const" (i.e. thread-safe) call.
cannam@134 92
cannam@134 93 template <typename Return, typename... Params>
cannam@134 94 class Function<Return(Params...)> {
cannam@134 95 public:
cannam@134 96 template <typename F>
cannam@134 97 inline Function(F&& f): impl(heap<Impl<F>>(kj::fwd<F>(f))) {}
cannam@134 98 Function() = default;
cannam@134 99
cannam@134 100 // Make sure people don't accidentally end up wrapping a reference when they meant to return
cannam@134 101 // a function.
cannam@134 102 KJ_DISALLOW_COPY(Function);
cannam@134 103 Function(Function&) = delete;
cannam@134 104 Function& operator=(Function&) = delete;
cannam@134 105 template <typename T> Function(const Function<T>&) = delete;
cannam@134 106 template <typename T> Function& operator=(const Function<T>&) = delete;
cannam@134 107 template <typename T> Function(const ConstFunction<T>&) = delete;
cannam@134 108 template <typename T> Function& operator=(const ConstFunction<T>&) = delete;
cannam@134 109 Function(Function&&) = default;
cannam@134 110 Function& operator=(Function&&) = default;
cannam@134 111
cannam@134 112 inline Return operator()(Params... params) {
cannam@134 113 return (*impl)(kj::fwd<Params>(params)...);
cannam@134 114 }
cannam@134 115
cannam@134 116 Function reference() {
cannam@134 117 // Forms a new Function of the same type that delegates to this Function by reference.
cannam@134 118 // Therefore, this Function must outlive the returned Function, but otherwise they behave
cannam@134 119 // exactly the same.
cannam@134 120
cannam@134 121 return *impl;
cannam@134 122 }
cannam@134 123
cannam@134 124 private:
cannam@134 125 class Iface {
cannam@134 126 public:
cannam@134 127 virtual Return operator()(Params... params) = 0;
cannam@134 128 };
cannam@134 129
cannam@134 130 template <typename F>
cannam@134 131 class Impl final: public Iface {
cannam@134 132 public:
cannam@134 133 explicit Impl(F&& f): f(kj::fwd<F>(f)) {}
cannam@134 134
cannam@134 135 Return operator()(Params... params) override {
cannam@134 136 return f(kj::fwd<Params>(params)...);
cannam@134 137 }
cannam@134 138
cannam@134 139 private:
cannam@134 140 F f;
cannam@134 141 };
cannam@134 142
cannam@134 143 Own<Iface> impl;
cannam@134 144 };
cannam@134 145
cannam@134 146 template <typename Return, typename... Params>
cannam@134 147 class ConstFunction<Return(Params...)> {
cannam@134 148 public:
cannam@134 149 template <typename F>
cannam@134 150 inline ConstFunction(F&& f): impl(heap<Impl<F>>(kj::fwd<F>(f))) {}
cannam@134 151 ConstFunction() = default;
cannam@134 152
cannam@134 153 // Make sure people don't accidentally end up wrapping a reference when they meant to return
cannam@134 154 // a function.
cannam@134 155 KJ_DISALLOW_COPY(ConstFunction);
cannam@134 156 ConstFunction(ConstFunction&) = delete;
cannam@134 157 ConstFunction& operator=(ConstFunction&) = delete;
cannam@134 158 template <typename T> ConstFunction(const ConstFunction<T>&) = delete;
cannam@134 159 template <typename T> ConstFunction& operator=(const ConstFunction<T>&) = delete;
cannam@134 160 template <typename T> ConstFunction(const Function<T>&) = delete;
cannam@134 161 template <typename T> ConstFunction& operator=(const Function<T>&) = delete;
cannam@134 162 ConstFunction(ConstFunction&&) = default;
cannam@134 163 ConstFunction& operator=(ConstFunction&&) = default;
cannam@134 164
cannam@134 165 inline Return operator()(Params... params) const {
cannam@134 166 return (*impl)(kj::fwd<Params>(params)...);
cannam@134 167 }
cannam@134 168
cannam@134 169 ConstFunction reference() const {
cannam@134 170 // Forms a new ConstFunction of the same type that delegates to this ConstFunction by reference.
cannam@134 171 // Therefore, this ConstFunction must outlive the returned ConstFunction, but otherwise they
cannam@134 172 // behave exactly the same.
cannam@134 173
cannam@134 174 return *impl;
cannam@134 175 }
cannam@134 176
cannam@134 177 private:
cannam@134 178 class Iface {
cannam@134 179 public:
cannam@134 180 virtual Return operator()(Params... params) const = 0;
cannam@134 181 };
cannam@134 182
cannam@134 183 template <typename F>
cannam@134 184 class Impl final: public Iface {
cannam@134 185 public:
cannam@134 186 explicit Impl(F&& f): f(kj::fwd<F>(f)) {}
cannam@134 187
cannam@134 188 Return operator()(Params... params) const override {
cannam@134 189 return f(kj::fwd<Params>(params)...);
cannam@134 190 }
cannam@134 191
cannam@134 192 private:
cannam@134 193 F f;
cannam@134 194 };
cannam@134 195
cannam@134 196 Own<Iface> impl;
cannam@134 197 };
cannam@134 198
cannam@134 199 #if 1
cannam@134 200
cannam@134 201 namespace _ { // private
cannam@134 202
cannam@134 203 template <typename T, typename Signature, Signature method>
cannam@134 204 class BoundMethod;
cannam@134 205
cannam@134 206 template <typename T, typename Return, typename... Params, Return (Decay<T>::*method)(Params...)>
cannam@134 207 class BoundMethod<T, Return (Decay<T>::*)(Params...), method> {
cannam@134 208 public:
cannam@134 209 BoundMethod(T&& t): t(kj::fwd<T>(t)) {}
cannam@134 210
cannam@134 211 Return operator()(Params&&... params) {
cannam@134 212 return (t.*method)(kj::fwd<Params>(params)...);
cannam@134 213 }
cannam@134 214
cannam@134 215 private:
cannam@134 216 T t;
cannam@134 217 };
cannam@134 218
cannam@134 219 template <typename T, typename Return, typename... Params,
cannam@134 220 Return (Decay<T>::*method)(Params...) const>
cannam@134 221 class BoundMethod<T, Return (Decay<T>::*)(Params...) const, method> {
cannam@134 222 public:
cannam@134 223 BoundMethod(T&& t): t(kj::fwd<T>(t)) {}
cannam@134 224
cannam@134 225 Return operator()(Params&&... params) const {
cannam@134 226 return (t.*method)(kj::fwd<Params>(params)...);
cannam@134 227 }
cannam@134 228
cannam@134 229 private:
cannam@134 230 T t;
cannam@134 231 };
cannam@134 232
cannam@134 233 } // namespace _ (private)
cannam@134 234
cannam@134 235 #define KJ_BIND_METHOD(obj, method) \
cannam@134 236 ::kj::_::BoundMethod<KJ_DECLTYPE_REF(obj), \
cannam@134 237 decltype(&::kj::Decay<decltype(obj)>::method), \
cannam@134 238 &::kj::Decay<decltype(obj)>::method>(obj)
cannam@134 239 // Macro that produces a functor object which forwards to the method `obj.name`. If `obj` is an
cannam@134 240 // lvalue, the functor will hold a reference to it. If `obj` is an rvalue, the functor will
cannam@134 241 // contain a copy (by move) of it.
cannam@134 242 //
cannam@134 243 // The current implementation requires that the method is not overloaded.
cannam@134 244 //
cannam@134 245 // TODO(someday): C++14's generic lambdas may be able to simplify this code considerably, and
cannam@134 246 // probably make it work with overloaded methods.
cannam@134 247
cannam@134 248 #else
cannam@134 249 // Here's a better implementation of the above that doesn't work with GCC (but does with Clang)
cannam@134 250 // because it uses a local class with a template method. Sigh. This implementation supports
cannam@134 251 // overloaded methods.
cannam@134 252
cannam@134 253 #define KJ_BIND_METHOD(obj, method) \
cannam@134 254 ({ \
cannam@134 255 typedef KJ_DECLTYPE_REF(obj) T; \
cannam@134 256 class F { \
cannam@134 257 public: \
cannam@134 258 inline F(T&& t): t(::kj::fwd<T>(t)) {} \
cannam@134 259 template <typename... Params> \
cannam@134 260 auto operator()(Params&&... params) \
cannam@134 261 -> decltype(::kj::instance<T>().method(::kj::fwd<Params>(params)...)) { \
cannam@134 262 return t.method(::kj::fwd<Params>(params)...); \
cannam@134 263 } \
cannam@134 264 private: \
cannam@134 265 T t; \
cannam@134 266 }; \
cannam@134 267 (F(obj)); \
cannam@134 268 })
cannam@134 269 // Macro that produces a functor object which forwards to the method `obj.name`. If `obj` is an
cannam@134 270 // lvalue, the functor will hold a reference to it. If `obj` is an rvalue, the functor will
cannam@134 271 // contain a copy (by move) of it.
cannam@134 272
cannam@134 273 #endif
cannam@134 274
cannam@134 275 } // namespace kj
cannam@134 276
cannam@134 277 #endif // KJ_FUNCTION_H_