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