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