annotate osx/include/kj/one-of.h @ 138:eb184393b244

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