Skip to content

Commit

Permalink
🔀 Add variant implementation
Browse files Browse the repository at this point in the history
This adds the variant implementation to Backport, complete with
proper constexpr, trivial destructor, and unit tests!

Currently absent is a proper implementation of 'visit' to work with
multiple variants -- to be added at a later time.

Closes #1
  • Loading branch information
bitwizeshift committed Apr 22, 2020
2 parents 4c91ed2 + 42788c2 commit 76980f9
Show file tree
Hide file tree
Showing 12 changed files with 4,515 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(header_files
"include/bpstd/detail/nth_type.hpp"
"include/bpstd/detail/variant_fwds.hpp"
"include/bpstd/detail/variant_union.hpp"
"include/bpstd/detail/variant_base.hpp"
"include/bpstd/detail/variant_traits.hpp"
"include/bpstd/detail/variant_visitors.hpp"
"include/bpstd/detail/move.hpp"
"include/bpstd/detail/invoke.hpp"
"include/bpstd/detail/proxy_iterator.hpp"
Expand All @@ -60,6 +66,7 @@ set(header_files
"include/bpstd/span.hpp"
"include/bpstd/chrono.hpp"
"include/bpstd/string.hpp"
"include/bpstd/variant.hpp"
)

add_library(${PROJECT_NAME} INTERFACE)
Expand All @@ -79,6 +86,9 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND
# Disable the ridiculous compatibility warnings, since it fails on files not
# ending in newlines
add_compile_options(-Wno-c++98-compat -Wno-c++98-compat-pedantic)

# This gives an unbelievable amount of false-positives spuriously. Ignore it.
add_compile_options(-Wno-unneeded-member-function)
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" )
add_compile_options(-Wall -Werror -Wextra -pedantic -pedantic-errors)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ C++17, and C++20.
|| `bpstd::apply` | [`N3915`][3915] |
|| `bpstd::make_from_tuple` | [`P0209R2`][02092] |
|| `bpstd::as_const` | [`P0007R1`][00071] |
| 🚧 | `bpstd::variant` | [`P0088R3`][00883]<br> [`P0032R3`][00323]<br> [`P0393R3`][03933] |
| | `bpstd::variant` | [`P0088R3`][00883]<br> [`P0032R3`][00323]<br> [`P0393R3`][03933] |
|| `bpstd::uncaught_exceptions` | [`N4152`][4152]<br> [`N4259`][4259] |
|| `bpstd::byte` | [`P0298R3`][02983] |
|| `bpstd::not_fn` | [`P0005R4`][00054] |
Expand Down
8 changes: 6 additions & 2 deletions include/bpstd/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@
# define BPSTD_MAY_ALIAS
#endif // defined __clang__ || defined __GNUC__

#if defined(__clang__) || defined(__GNUC__)
// When using 'clang-cl', don't forceinline -- since it results in code generation
// failures in 'variant'
#if defined(__clang__) && defined(_MSC_VER)
# define BPSTD_INLINE_VISIBILITY __attribute__((visibility("hidden"), no_instrument_function))
#elif defined(__clang__) || defined(__GNUC__)
# define BPSTD_INLINE_VISIBILITY __attribute__((visibility("hidden"), always_inline, no_instrument_function))
#elif defined(_MSC_VER)
# define BPSTD_INLINE_VISIBILITY __forceinline
#else
# define inline
# define BPSTD_INLINE_VISIBILITY
#endif

#endif /* BPSTD_DETAIL_CONFIG_HPP */
64 changes: 64 additions & 0 deletions include/bpstd/detail/nth_type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*****************************************************************************
* \file nth_type.hpp
*
* \brief This internal header provides the definition of the INVOKE overload
*****************************************************************************/

/*
The MIT License (MIT)
Copyright (c) 2020 Matthew Rodusek All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef BPSTD_DETAIL_NTH_TYPE_HPP
#define BPSTD_DETAIL_NTH_TYPE_HPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

#include <cstddef> // std::size_t

namespace bpstd {
namespace detail {

/// \brief Gets the nth type from a variadic pack of arguments
///
/// \tparam N the argument to retrieve
/// \tparam Args the arguments to extract from
template <std::size_t N, typename...Args>
struct nth_type;

template <std::size_t N, typename Arg0, typename...Args>
struct nth_type<N,Arg0,Args...> : nth_type<N-1,Args...>{};

template <typename Arg0, typename...Args>
struct nth_type<0,Arg0,Args...>
{
using type = Arg0;
};

template <std::size_t N, typename...Args>
using nth_type_t = typename nth_type<N,Args...>::type;

} // namespace detail
} // namespace bpstd

#endif /* BPSTD_DETAIL_NTH_TYPE_HPP */
227 changes: 227 additions & 0 deletions include/bpstd/detail/variant_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*****************************************************************************
* \file variant_base.hpp
*
* \brief This internal header provides the definition of a utility for
* variant, variant_base
*****************************************************************************/

/*
The MIT License (MIT)
Copyright (c) 2020 Matthew Rodusek All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef BPSTD_DETAIL_VARIANT_BASE_HPP
#define BPSTD_DETAIL_VARIANT_BASE_HPP

#include "config.hpp" // BPSTD_CPP14_CONSTEXPR
#include "variant_union.hpp" // detail::variant_union

#include <cstddef> // std::size_t
#include <utility> // std::forward

namespace bpstd {
namespace detail {

//==========================================================================
// class : variant_base
//==========================================================================

////////////////////////////////////////////////////////////////////////////
/// \brief The base class used by variant
////////////////////////////////////////////////////////////////////////////
template <bool IsTrivial, typename...Types>
class variant_base;

//==========================================================================
// class : variant_base<true, Types...>
//==========================================================================

template <typename...Types>
class variant_base<true,Types...>
{
//------------------------------------------------------------------------
// Constructors
//------------------------------------------------------------------------
public:

constexpr variant_base();

template <std::size_t N, typename...Args>
constexpr variant_base(variant_index_tag<N>, Args&&...args);

//------------------------------------------------------------------------
// Protected Members
//------------------------------------------------------------------------
protected:

variant_union<true,Types...> m_union;
std::size_t m_index;

//---------------------------------------------------------------------
// Protected Member Functions
//---------------------------------------------------------------------
protected:

void destroy_active_object();
};

//==========================================================================
// class : variant_base<false, Types...>
//==========================================================================

template <typename...Types>
class variant_base<false,Types...>
{
public:

constexpr variant_base();

template <std::size_t N, typename...Args>
constexpr variant_base(variant_index_tag<N>, Args&&...args);

~variant_base();

//---------------------------------------------------------------------
// Protected Members
//---------------------------------------------------------------------
protected:

variant_union<false,Types...> m_union;
std::size_t m_index;

//---------------------------------------------------------------------
// Protected Member Functions
//---------------------------------------------------------------------
protected:

void destroy_active_object();

//---------------------------------------------------------------------
// Private Static Member Functions
//---------------------------------------------------------------------
private:

struct destroy_visitor
{
template <typename T>
inline BPSTD_INLINE_VISIBILITY
void operator()(T& v) {
v.~T();
}
};
};

} // namespace detail
} // namespace bpstd

//==============================================================================
// class : variant_base<true, Types...>
//==============================================================================

//------------------------------------------------------------------------------
// Constructors
//------------------------------------------------------------------------------

template <typename...Types>
inline BPSTD_INLINE_VISIBILITY constexpr
bpstd::detail::variant_base<true,Types...>::variant_base()
: m_union{},
m_index{static_cast<std::size_t>(-1)}
{

}

template <typename...Types>
template <std::size_t N, typename...Args>
inline BPSTD_INLINE_VISIBILITY constexpr
bpstd::detail::variant_base<true,Types...>::variant_base(variant_index_tag<N>,
Args&&...args)
: m_union{variant_index_tag<N>{}, std::forward<Args>(args)...},
m_index{N}
{

}

//------------------------------------------------------------------------------
// Protected Members
//------------------------------------------------------------------------------

template <typename...Types>
inline BPSTD_INLINE_VISIBILITY
void bpstd::detail::variant_base<true,Types...>::destroy_active_object()
{
m_index = static_cast<std::size_t>(-1);
}

//==============================================================================
// class : variant_base<false, Types...>
//==============================================================================

//------------------------------------------------------------------------------
// Constructors / Destructor
//------------------------------------------------------------------------------

template <typename...Types>
inline BPSTD_INLINE_VISIBILITY constexpr
bpstd::detail::variant_base<false,Types...>::variant_base()
: m_union{},
m_index{static_cast<std::size_t>(-1)}
{

}

template <typename...Types>
template <std::size_t N, typename...Args>
inline BPSTD_INLINE_VISIBILITY constexpr
bpstd::detail::variant_base<false,Types...>::variant_base(variant_index_tag<N>,
Args&&...args)
: m_union{variant_index_tag<N>{}, std::forward<Args>(args)...},
m_index{N}
{

}

//------------------------------------------------------------------------------

template <typename...Types>
inline BPSTD_INLINE_VISIBILITY
bpstd::detail::variant_base<false,Types...>::~variant_base()
{
destroy_active_object();
}

//------------------------------------------------------------------------------
// Protected Members
//------------------------------------------------------------------------------

template <typename...Types>
inline BPSTD_INLINE_VISIBILITY
void bpstd::detail::variant_base<false,Types...>::destroy_active_object()
{
if (m_index == static_cast<std::size_t>(-1)) {
return;
}

visit_union(m_index, destroy_visitor{}, m_union);
m_index = static_cast<std::size_t>(-1);
}

#endif /* BPSTD_DETAIL_VARIANT_BASE_HPP */
Loading

0 comments on commit 76980f9

Please sign in to comment.