Chris@63: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@63: // Licensed under the MIT License: Chris@63: // Chris@63: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@63: // of this software and associated documentation files (the "Software"), to deal Chris@63: // in the Software without restriction, including without limitation the rights Chris@63: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@63: // copies of the Software, and to permit persons to whom the Software is Chris@63: // furnished to do so, subject to the following conditions: Chris@63: // Chris@63: // The above copyright notice and this permission notice shall be included in Chris@63: // all copies or substantial portions of the Software. Chris@63: // Chris@63: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@63: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@63: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@63: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@63: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@63: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@63: // THE SOFTWARE. Chris@63: Chris@63: #ifndef KJ_ONE_OF_H_ Chris@63: #define KJ_ONE_OF_H_ Chris@63: Chris@63: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@63: #pragma GCC system_header Chris@63: #endif Chris@63: Chris@63: #include "common.h" Chris@63: Chris@63: namespace kj { Chris@63: Chris@63: namespace _ { // private Chris@63: Chris@63: template Chris@63: struct TypeIndex_ { static constexpr uint value = TypeIndex_::value; }; Chris@63: template Chris@63: struct TypeIndex_ { static constexpr uint value = i; }; Chris@63: Chris@63: } // namespace _ (private) Chris@63: Chris@63: template Chris@63: class OneOf { Chris@63: template Chris@63: static inline constexpr uint typeIndex() { return _::TypeIndex_<1, Key, Variants...>::value; } Chris@63: // Get the 1-based index of Key within the type list Types. Chris@63: Chris@63: public: Chris@63: inline OneOf(): tag(0) {} Chris@63: OneOf(const OneOf& other) { copyFrom(other); } Chris@63: OneOf(OneOf&& other) { moveFrom(other); } Chris@63: ~OneOf() { destroy(); } Chris@63: Chris@63: OneOf& operator=(const OneOf& other) { if (tag != 0) destroy(); copyFrom(other); return *this; } Chris@63: OneOf& operator=(OneOf&& other) { if (tag != 0) destroy(); moveFrom(other); return *this; } Chris@63: Chris@63: inline bool operator==(decltype(nullptr)) const { return tag == 0; } Chris@63: inline bool operator!=(decltype(nullptr)) const { return tag != 0; } Chris@63: Chris@63: template Chris@63: bool is() const { Chris@63: return tag == typeIndex(); Chris@63: } Chris@63: Chris@63: template Chris@63: T& get() { Chris@63: KJ_IREQUIRE(is(), "Must check OneOf::is() before calling get()."); Chris@63: return *reinterpret_cast(space); Chris@63: } Chris@63: template Chris@63: const T& get() const { Chris@63: KJ_IREQUIRE(is(), "Must check OneOf::is() before calling get()."); Chris@63: return *reinterpret_cast(space); Chris@63: } Chris@63: Chris@63: template Chris@63: T& init(Params&&... params) { Chris@63: if (tag != 0) destroy(); Chris@63: ctor(*reinterpret_cast(space), kj::fwd(params)...); Chris@63: tag = typeIndex(); Chris@63: return *reinterpret_cast(space); Chris@63: } Chris@63: Chris@63: private: Chris@63: uint tag; Chris@63: Chris@63: static inline constexpr size_t maxSize(size_t a) { Chris@63: return a; Chris@63: } Chris@63: template Chris@63: static inline constexpr size_t maxSize(size_t a, size_t b, Rest... rest) { Chris@63: return maxSize(kj::max(a, b), rest...); Chris@63: } Chris@63: // Returns the maximum of all the parameters. Chris@63: // TODO(someday): Generalize the above template and make it common. I tried, but C++ decided to Chris@63: // be difficult so I cut my losses. Chris@63: Chris@63: static constexpr auto spaceSize = maxSize(sizeof(Variants)...); Chris@63: // TODO(msvc): This constant could just as well go directly inside space's bracket's, where it's Chris@63: // used, but MSVC suffers a parse error on `...`. Chris@63: Chris@63: union { Chris@63: byte space[spaceSize]; Chris@63: Chris@63: void* forceAligned; Chris@63: // TODO(someday): Use C++11 alignas() once we require GCC 4.8 / Clang 3.3. Chris@63: }; Chris@63: Chris@63: template Chris@63: inline void doAll(T... t) {} Chris@63: Chris@63: template Chris@63: inline bool destroyVariant() { Chris@63: if (tag == typeIndex()) { Chris@63: tag = 0; Chris@63: dtor(*reinterpret_cast(space)); Chris@63: } Chris@63: return false; Chris@63: } Chris@63: void destroy() { Chris@63: doAll(destroyVariant()...); Chris@63: } Chris@63: Chris@63: template Chris@63: inline bool copyVariantFrom(const OneOf& other) { Chris@63: if (other.is()) { Chris@63: ctor(*reinterpret_cast(space), other.get()); Chris@63: } Chris@63: return false; Chris@63: } Chris@63: void copyFrom(const OneOf& other) { Chris@63: // Initialize as a copy of `other`. Expects that `this` starts out uninitialized, so the tag Chris@63: // is invalid. Chris@63: tag = other.tag; Chris@63: doAll(copyVariantFrom(other)...); Chris@63: } Chris@63: Chris@63: template Chris@63: inline bool moveVariantFrom(OneOf& other) { Chris@63: if (other.is()) { Chris@63: ctor(*reinterpret_cast(space), kj::mv(other.get())); Chris@63: } Chris@63: return false; Chris@63: } Chris@63: void moveFrom(OneOf& other) { Chris@63: // Initialize as a copy of `other`. Expects that `this` starts out uninitialized, so the tag Chris@63: // is invalid. Chris@63: tag = other.tag; Chris@63: doAll(moveVariantFrom(other)...); Chris@63: } Chris@63: }; Chris@63: Chris@63: } // namespace kj Chris@63: Chris@63: #endif // KJ_ONE_OF_H_