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