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