annotate win32-mingw/include/kj/function.h @ 50:37d53a7e8262

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