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