annotate win32-mingw/include/kj/one-of.h @ 140:59a8758c56b1

Add source for PortAudio stable v190600_20161030
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 03 Jan 2017 13:44:07 +0000
parents 38d1c0e7850b
children eccd51b72864
rev   line source
cannam@135 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@135 2 // Licensed under the MIT License:
cannam@135 3 //
cannam@135 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@135 5 // of this software and associated documentation files (the "Software"), to deal
cannam@135 6 // in the Software without restriction, including without limitation the rights
cannam@135 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@135 8 // copies of the Software, and to permit persons to whom the Software is
cannam@135 9 // furnished to do so, subject to the following conditions:
cannam@135 10 //
cannam@135 11 // The above copyright notice and this permission notice shall be included in
cannam@135 12 // all copies or substantial portions of the Software.
cannam@135 13 //
cannam@135 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@135 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@135 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@135 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@135 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@135 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@135 20 // THE SOFTWARE.
cannam@135 21
cannam@135 22 #ifndef KJ_ONE_OF_H_
cannam@135 23 #define KJ_ONE_OF_H_
cannam@135 24
cannam@135 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@135 26 #pragma GCC system_header
cannam@135 27 #endif
cannam@135 28
cannam@135 29 #include "common.h"
cannam@135 30
cannam@135 31 namespace kj {
cannam@135 32
cannam@135 33 namespace _ { // private
cannam@135 34
cannam@135 35 template <uint i, typename Key, typename First, typename... Rest>
cannam@135 36 struct TypeIndex_ { static constexpr uint value = TypeIndex_<i + 1, Key, Rest...>::value; };
cannam@135 37 template <uint i, typename Key, typename... Rest>
cannam@135 38 struct TypeIndex_<i, Key, Key, Rest...> { static constexpr uint value = i; };
cannam@135 39
cannam@135 40 } // namespace _ (private)
cannam@135 41
cannam@135 42 template <typename... Variants>
cannam@135 43 class OneOf {
cannam@135 44 template <typename Key>
cannam@135 45 static inline constexpr uint typeIndex() { return _::TypeIndex_<1, Key, Variants...>::value; }
cannam@135 46 // Get the 1-based index of Key within the type list Types.
cannam@135 47
cannam@135 48 public:
cannam@135 49 inline OneOf(): tag(0) {}
cannam@135 50 OneOf(const OneOf& other) { copyFrom(other); }
cannam@135 51 OneOf(OneOf&& other) { moveFrom(other); }
cannam@135 52 ~OneOf() { destroy(); }
cannam@135 53
cannam@135 54 OneOf& operator=(const OneOf& other) { if (tag != 0) destroy(); copyFrom(other); return *this; }
cannam@135 55 OneOf& operator=(OneOf&& other) { if (tag != 0) destroy(); moveFrom(other); return *this; }
cannam@135 56
cannam@135 57 inline bool operator==(decltype(nullptr)) const { return tag == 0; }
cannam@135 58 inline bool operator!=(decltype(nullptr)) const { return tag != 0; }
cannam@135 59
cannam@135 60 template <typename T>
cannam@135 61 bool is() const {
cannam@135 62 return tag == typeIndex<T>();
cannam@135 63 }
cannam@135 64
cannam@135 65 template <typename T>
cannam@135 66 T& get() {
cannam@135 67 KJ_IREQUIRE(is<T>(), "Must check OneOf::is<T>() before calling get<T>().");
cannam@135 68 return *reinterpret_cast<T*>(space);
cannam@135 69 }
cannam@135 70 template <typename T>
cannam@135 71 const T& get() const {
cannam@135 72 KJ_IREQUIRE(is<T>(), "Must check OneOf::is<T>() before calling get<T>().");
cannam@135 73 return *reinterpret_cast<const T*>(space);
cannam@135 74 }
cannam@135 75
cannam@135 76 template <typename T, typename... Params>
cannam@135 77 void init(Params&&... params) {
cannam@135 78 if (tag != 0) destroy();
cannam@135 79 ctor(*reinterpret_cast<T*>(space), kj::fwd<Params>(params)...);
cannam@135 80 tag = typeIndex<T>();
cannam@135 81 }
cannam@135 82
cannam@135 83 private:
cannam@135 84 uint tag;
cannam@135 85
cannam@135 86 static inline constexpr size_t maxSize(size_t a) {
cannam@135 87 return a;
cannam@135 88 }
cannam@135 89 template <typename... Rest>
cannam@135 90 static inline constexpr size_t maxSize(size_t a, size_t b, Rest... rest) {
cannam@135 91 return maxSize(kj::max(a, b), rest...);
cannam@135 92 }
cannam@135 93 // Returns the maximum of all the parameters.
cannam@135 94 // TODO(someday): Generalize the above template and make it common. I tried, but C++ decided to
cannam@135 95 // be difficult so I cut my losses.
cannam@135 96
cannam@135 97 union {
cannam@135 98 byte space[maxSize(sizeof(Variants)...)];
cannam@135 99
cannam@135 100 void* forceAligned;
cannam@135 101 // TODO(someday): Use C++11 alignas() once we require GCC 4.8 / Clang 3.3.
cannam@135 102 };
cannam@135 103
cannam@135 104 template <typename... T>
cannam@135 105 inline void doAll(T... t) {}
cannam@135 106
cannam@135 107 template <typename T>
cannam@135 108 inline bool destroyVariant() {
cannam@135 109 if (tag == typeIndex<T>()) {
cannam@135 110 tag = 0;
cannam@135 111 dtor(*reinterpret_cast<T*>(space));
cannam@135 112 }
cannam@135 113 return false;
cannam@135 114 }
cannam@135 115 void destroy() {
cannam@135 116 doAll(destroyVariant<Variants>()...);
cannam@135 117 }
cannam@135 118
cannam@135 119 template <typename T>
cannam@135 120 inline bool copyVariantFrom(const OneOf& other) {
cannam@135 121 if (other.is<T>()) {
cannam@135 122 ctor(*reinterpret_cast<T*>(space), other.get<T>());
cannam@135 123 }
cannam@135 124 return false;
cannam@135 125 }
cannam@135 126 void copyFrom(const OneOf& other) {
cannam@135 127 // Initialize as a copy of `other`. Expects that `this` starts out uninitialized, so the tag
cannam@135 128 // is invalid.
cannam@135 129 tag = other.tag;
cannam@135 130 doAll(copyVariantFrom<Variants>(other)...);
cannam@135 131 }
cannam@135 132
cannam@135 133 template <typename T>
cannam@135 134 inline bool moveVariantFrom(OneOf& other) {
cannam@135 135 if (other.is<T>()) {
cannam@135 136 ctor(*reinterpret_cast<T*>(space), kj::mv(other.get<T>()));
cannam@135 137 }
cannam@135 138 return false;
cannam@135 139 }
cannam@135 140 void moveFrom(OneOf& other) {
cannam@135 141 // Initialize as a copy of `other`. Expects that `this` starts out uninitialized, so the tag
cannam@135 142 // is invalid.
cannam@135 143 tag = other.tag;
cannam@135 144 doAll(moveVariantFrom<Variants>(other)...);
cannam@135 145 }
cannam@135 146 };
cannam@135 147
cannam@135 148 } // namespace kj
cannam@135 149
cannam@135 150 #endif // KJ_ONE_OF_H_