annotate win32-mingw/include/kj/one-of.h @ 54:5f67a29f0fc7

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