annotate win64-msvc/include/kj/function.h @ 148:b4bfdf10c4b3

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