|
| 1 | +/***************************************************************************** |
| 2 | + * \file variant_base.hpp |
| 3 | + * |
| 4 | + * \brief This internal header provides the definition of a utility for |
| 5 | + * variant, variant_base |
| 6 | + *****************************************************************************/ |
| 7 | + |
| 8 | +/* |
| 9 | + The MIT License (MIT) |
| 10 | +
|
| 11 | + Copyright (c) 2020 Matthew Rodusek All rights reserved. |
| 12 | +
|
| 13 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | + of this software and associated documentation files (the "Software"), to deal |
| 15 | + in the Software without restriction, including without limitation the rights |
| 16 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | + copies of the Software, and to permit persons to whom the Software is |
| 18 | + furnished to do so, subject to the following conditions: |
| 19 | +
|
| 20 | + The above copyright notice and this permission notice shall be included in |
| 21 | + all copies or substantial portions of the Software. |
| 22 | +
|
| 23 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | + SOFTWARE. |
| 30 | +*/ |
| 31 | +#ifndef BPSTD_DETAIL_VARIANT_BASE_HPP |
| 32 | +#define BPSTD_DETAIL_VARIANT_BASE_HPP |
| 33 | + |
| 34 | +#include "config.hpp" // BPSTD_CPP14_CONSTEXPR |
| 35 | +#include "variant_union.hpp" // detail::variant_union |
| 36 | + |
| 37 | +#include <cstddef> // std::size_t |
| 38 | +#include <utility> // std::forward |
| 39 | + |
| 40 | +namespace bpstd { |
| 41 | + namespace detail { |
| 42 | + |
| 43 | + //========================================================================== |
| 44 | + // class : variant_base |
| 45 | + //========================================================================== |
| 46 | + |
| 47 | + //////////////////////////////////////////////////////////////////////////// |
| 48 | + /// \brief The base class used by variant |
| 49 | + //////////////////////////////////////////////////////////////////////////// |
| 50 | + template <bool IsTrivial, typename...Types> |
| 51 | + class variant_base; |
| 52 | + |
| 53 | + //========================================================================== |
| 54 | + // class : variant_base<true, Types...> |
| 55 | + //========================================================================== |
| 56 | + |
| 57 | + template <typename...Types> |
| 58 | + class variant_base<true,Types...> |
| 59 | + { |
| 60 | + //------------------------------------------------------------------------ |
| 61 | + // Constructors |
| 62 | + //------------------------------------------------------------------------ |
| 63 | + public: |
| 64 | + |
| 65 | + constexpr variant_base(); |
| 66 | + |
| 67 | + template <std::size_t N, typename...Args> |
| 68 | + constexpr variant_base(variant_index_tag<N>, Args&&...args); |
| 69 | + |
| 70 | + //------------------------------------------------------------------------ |
| 71 | + // Protected Members |
| 72 | + //------------------------------------------------------------------------ |
| 73 | + protected: |
| 74 | + |
| 75 | + variant_union<true,Types...> m_union; |
| 76 | + std::size_t m_index; |
| 77 | + |
| 78 | + //--------------------------------------------------------------------- |
| 79 | + // Protected Member Functions |
| 80 | + //--------------------------------------------------------------------- |
| 81 | + protected: |
| 82 | + |
| 83 | + void destroy_active_object(); |
| 84 | + }; |
| 85 | + |
| 86 | + //========================================================================== |
| 87 | + // class : variant_base<false, Types...> |
| 88 | + //========================================================================== |
| 89 | + |
| 90 | + template <typename...Types> |
| 91 | + class variant_base<false,Types...> |
| 92 | + { |
| 93 | + public: |
| 94 | + |
| 95 | + constexpr variant_base(); |
| 96 | + |
| 97 | + template <std::size_t N, typename...Args> |
| 98 | + constexpr variant_base(variant_index_tag<N>, Args&&...args); |
| 99 | + |
| 100 | + ~variant_base(); |
| 101 | + |
| 102 | + //--------------------------------------------------------------------- |
| 103 | + // Protected Members |
| 104 | + //--------------------------------------------------------------------- |
| 105 | + protected: |
| 106 | + |
| 107 | + variant_union<false,Types...> m_union; |
| 108 | + std::size_t m_index; |
| 109 | + |
| 110 | + //--------------------------------------------------------------------- |
| 111 | + // Protected Member Functions |
| 112 | + //--------------------------------------------------------------------- |
| 113 | + protected: |
| 114 | + |
| 115 | + void destroy_active_object(); |
| 116 | + |
| 117 | + //--------------------------------------------------------------------- |
| 118 | + // Private Static Member Functions |
| 119 | + //--------------------------------------------------------------------- |
| 120 | + private: |
| 121 | + |
| 122 | + struct destroy_visitor |
| 123 | + { |
| 124 | + template <typename T> |
| 125 | + inline BPSTD_INLINE_VISIBILITY |
| 126 | + void operator()(T& v) { |
| 127 | + v.~T(); |
| 128 | + } |
| 129 | + }; |
| 130 | + }; |
| 131 | + |
| 132 | + } // namespace detail |
| 133 | +} // namespace bpstd |
| 134 | + |
| 135 | +//============================================================================== |
| 136 | +// class : variant_base<true, Types...> |
| 137 | +//============================================================================== |
| 138 | + |
| 139 | +//------------------------------------------------------------------------------ |
| 140 | +// Constructors |
| 141 | +//------------------------------------------------------------------------------ |
| 142 | + |
| 143 | +template <typename...Types> |
| 144 | +inline BPSTD_INLINE_VISIBILITY constexpr |
| 145 | +bpstd::detail::variant_base<true,Types...>::variant_base() |
| 146 | + : m_union{}, |
| 147 | + m_index{static_cast<std::size_t>(-1)} |
| 148 | +{ |
| 149 | + |
| 150 | +} |
| 151 | + |
| 152 | +template <typename...Types> |
| 153 | +template <std::size_t N, typename...Args> |
| 154 | +inline BPSTD_INLINE_VISIBILITY constexpr |
| 155 | +bpstd::detail::variant_base<true,Types...>::variant_base(variant_index_tag<N>, |
| 156 | + Args&&...args) |
| 157 | + : m_union{variant_index_tag<N>{}, std::forward<Args>(args)...}, |
| 158 | + m_index{N} |
| 159 | +{ |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +//------------------------------------------------------------------------------ |
| 164 | +// Protected Members |
| 165 | +//------------------------------------------------------------------------------ |
| 166 | + |
| 167 | +template <typename...Types> |
| 168 | +inline BPSTD_INLINE_VISIBILITY |
| 169 | +void bpstd::detail::variant_base<true,Types...>::destroy_active_object() |
| 170 | +{ |
| 171 | + m_index = static_cast<std::size_t>(-1); |
| 172 | +} |
| 173 | + |
| 174 | +//============================================================================== |
| 175 | +// class : variant_base<false, Types...> |
| 176 | +//============================================================================== |
| 177 | + |
| 178 | +//------------------------------------------------------------------------------ |
| 179 | +// Constructors / Destructor |
| 180 | +//------------------------------------------------------------------------------ |
| 181 | + |
| 182 | +template <typename...Types> |
| 183 | +inline BPSTD_INLINE_VISIBILITY constexpr |
| 184 | +bpstd::detail::variant_base<false,Types...>::variant_base() |
| 185 | + : m_union{}, |
| 186 | + m_index{static_cast<std::size_t>(-1)} |
| 187 | +{ |
| 188 | + |
| 189 | +} |
| 190 | + |
| 191 | +template <typename...Types> |
| 192 | +template <std::size_t N, typename...Args> |
| 193 | +inline BPSTD_INLINE_VISIBILITY constexpr |
| 194 | +bpstd::detail::variant_base<false,Types...>::variant_base(variant_index_tag<N>, |
| 195 | + Args&&...args) |
| 196 | + : m_union{variant_index_tag<N>{}, std::forward<Args>(args)...}, |
| 197 | + m_index{N} |
| 198 | +{ |
| 199 | + |
| 200 | +} |
| 201 | + |
| 202 | +//------------------------------------------------------------------------------ |
| 203 | + |
| 204 | +template <typename...Types> |
| 205 | +inline BPSTD_INLINE_VISIBILITY |
| 206 | +bpstd::detail::variant_base<false,Types...>::~variant_base() |
| 207 | +{ |
| 208 | + destroy_active_object(); |
| 209 | +} |
| 210 | + |
| 211 | +//------------------------------------------------------------------------------ |
| 212 | +// Protected Members |
| 213 | +//------------------------------------------------------------------------------ |
| 214 | + |
| 215 | +template <typename...Types> |
| 216 | +inline BPSTD_INLINE_VISIBILITY |
| 217 | +void bpstd::detail::variant_base<false,Types...>::destroy_active_object() |
| 218 | +{ |
| 219 | + if (m_index == static_cast<std::size_t>(-1)) { |
| 220 | + return; |
| 221 | + } |
| 222 | + |
| 223 | + visit_union(m_index, destroy_visitor{}, m_union); |
| 224 | + m_index = static_cast<std::size_t>(-1); |
| 225 | +} |
| 226 | + |
| 227 | +#endif /* BPSTD_DETAIL_VARIANT_BASE_HPP */ |
0 commit comments