annotate win64-msvc/include/kj/one-of.h @ 56:af97cad61ff0

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