annotate win32-mingw/include/kj/one-of.h @ 150:0a1a4a299a5d

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