annotate osx/include/kj/one-of.h @ 83:ae30d91d2ffe

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