annotate win32-mingw/include/kj/function.h @ 69:7aeed7906520

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