annotate win32-mingw/include/kj/one-of.h @ 72:7b5216b54e42

Update exclusion list
author Chris Cannam
date Fri, 25 Jan 2019 13:49:22 +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_ONE_OF_H_
Chris@64 23 #define KJ_ONE_OF_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 "common.h"
Chris@64 30
Chris@64 31 namespace kj {
Chris@64 32
Chris@64 33 namespace _ { // private
Chris@64 34
Chris@64 35 template <uint i, typename Key, typename First, typename... Rest>
Chris@64 36 struct TypeIndex_ { static constexpr uint value = TypeIndex_<i + 1, Key, Rest...>::value; };
Chris@64 37 template <uint i, typename Key, typename... Rest>
Chris@64 38 struct TypeIndex_<i, Key, Key, Rest...> { static constexpr uint value = i; };
Chris@64 39
Chris@64 40 } // namespace _ (private)
Chris@64 41
Chris@64 42 template <typename... Variants>
Chris@64 43 class OneOf {
Chris@64 44 template <typename Key>
Chris@64 45 static inline constexpr uint typeIndex() { return _::TypeIndex_<1, Key, Variants...>::value; }
Chris@64 46 // Get the 1-based index of Key within the type list Types.
Chris@64 47
Chris@64 48 public:
Chris@64 49 inline OneOf(): tag(0) {}
Chris@64 50 OneOf(const OneOf& other) { copyFrom(other); }
Chris@64 51 OneOf(OneOf&& other) { moveFrom(other); }
Chris@64 52 ~OneOf() { destroy(); }
Chris@64 53
Chris@64 54 OneOf& operator=(const OneOf& other) { if (tag != 0) destroy(); copyFrom(other); return *this; }
Chris@64 55 OneOf& operator=(OneOf&& other) { if (tag != 0) destroy(); moveFrom(other); return *this; }
Chris@64 56
Chris@64 57 inline bool operator==(decltype(nullptr)) const { return tag == 0; }
Chris@64 58 inline bool operator!=(decltype(nullptr)) const { return tag != 0; }
Chris@64 59
Chris@64 60 template <typename T>
Chris@64 61 bool is() const {
Chris@64 62 return tag == typeIndex<T>();
Chris@64 63 }
Chris@64 64
Chris@64 65 template <typename T>
Chris@64 66 T& get() {
Chris@64 67 KJ_IREQUIRE(is<T>(), "Must check OneOf::is<T>() before calling get<T>().");
Chris@64 68 return *reinterpret_cast<T*>(space);
Chris@64 69 }
Chris@64 70 template <typename T>
Chris@64 71 const T& get() const {
Chris@64 72 KJ_IREQUIRE(is<T>(), "Must check OneOf::is<T>() before calling get<T>().");
Chris@64 73 return *reinterpret_cast<const T*>(space);
Chris@64 74 }
Chris@64 75
Chris@64 76 template <typename T, typename... Params>
Chris@64 77 T& init(Params&&... params) {
Chris@64 78 if (tag != 0) destroy();
Chris@64 79 ctor(*reinterpret_cast<T*>(space), kj::fwd<Params>(params)...);
Chris@64 80 tag = typeIndex<T>();
Chris@64 81 return *reinterpret_cast<T*>(space);
Chris@64 82 }
Chris@64 83
Chris@64 84 private:
Chris@64 85 uint tag;
Chris@64 86
Chris@64 87 static inline constexpr size_t maxSize(size_t a) {
Chris@64 88 return a;
Chris@64 89 }
Chris@64 90 template <typename... Rest>
Chris@64 91 static inline constexpr size_t maxSize(size_t a, size_t b, Rest... rest) {
Chris@64 92 return maxSize(kj::max(a, b), rest...);
Chris@64 93 }
Chris@64 94 // Returns the maximum of all the parameters.
Chris@64 95 // TODO(someday): Generalize the above template and make it common. I tried, but C++ decided to
Chris@64 96 // be difficult so I cut my losses.
Chris@64 97
Chris@64 98 static constexpr auto spaceSize = maxSize(sizeof(Variants)...);
Chris@64 99 // TODO(msvc): This constant could just as well go directly inside space's bracket's, where it's
Chris@64 100 // used, but MSVC suffers a parse error on `...`.
Chris@64 101
Chris@64 102 union {
Chris@64 103 byte space[spaceSize];
Chris@64 104
Chris@64 105 void* forceAligned;
Chris@64 106 // TODO(someday): Use C++11 alignas() once we require GCC 4.8 / Clang 3.3.
Chris@64 107 };
Chris@64 108
Chris@64 109 template <typename... T>
Chris@64 110 inline void doAll(T... t) {}
Chris@64 111
Chris@64 112 template <typename T>
Chris@64 113 inline bool destroyVariant() {
Chris@64 114 if (tag == typeIndex<T>()) {
Chris@64 115 tag = 0;
Chris@64 116 dtor(*reinterpret_cast<T*>(space));
Chris@64 117 }
Chris@64 118 return false;
Chris@64 119 }
Chris@64 120 void destroy() {
Chris@64 121 doAll(destroyVariant<Variants>()...);
Chris@64 122 }
Chris@64 123
Chris@64 124 template <typename T>
Chris@64 125 inline bool copyVariantFrom(const OneOf& other) {
Chris@64 126 if (other.is<T>()) {
Chris@64 127 ctor(*reinterpret_cast<T*>(space), other.get<T>());
Chris@64 128 }
Chris@64 129 return false;
Chris@64 130 }
Chris@64 131 void copyFrom(const OneOf& other) {
Chris@64 132 // Initialize as a copy of `other`. Expects that `this` starts out uninitialized, so the tag
Chris@64 133 // is invalid.
Chris@64 134 tag = other.tag;
Chris@64 135 doAll(copyVariantFrom<Variants>(other)...);
Chris@64 136 }
Chris@64 137
Chris@64 138 template <typename T>
Chris@64 139 inline bool moveVariantFrom(OneOf& other) {
Chris@64 140 if (other.is<T>()) {
Chris@64 141 ctor(*reinterpret_cast<T*>(space), kj::mv(other.get<T>()));
Chris@64 142 }
Chris@64 143 return false;
Chris@64 144 }
Chris@64 145 void moveFrom(OneOf& other) {
Chris@64 146 // Initialize as a copy of `other`. Expects that `this` starts out uninitialized, so the tag
Chris@64 147 // is invalid.
Chris@64 148 tag = other.tag;
Chris@64 149 doAll(moveVariantFrom<Variants>(other)...);
Chris@64 150 }
Chris@64 151 };
Chris@64 152
Chris@64 153 } // namespace kj
Chris@64 154
Chris@64 155 #endif // KJ_ONE_OF_H_