annotate win64-msvc/include/kj/one-of.h @ 81:7029a4916348

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