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