annotate win32-mingw/include/kj/function.h @ 149:279b18cc7785

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