annotate osx/include/kj/one-of.h @ 49:3ab5a40c4e3b

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