forked from boostorg/variant2
Compare commits
6 Commits
boost-1.78
...
esp-idf-co
Author | SHA1 | Date | |
---|---|---|---|
5c0274a76e | |||
d34658f4c8 | |||
5e2bce1baa | |||
573ef77382 | |||
f6cad272d4 | |||
3fc7540733 |
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@ -147,6 +147,10 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- toolset: msvc-14.0
|
||||
cxxstd: "14"
|
||||
addrmd: 32,64
|
||||
os: windows-2019
|
||||
- toolset: msvc-14.1
|
||||
cxxstd: "14,17,latest"
|
||||
addrmd: 32,64
|
||||
|
@ -3,6 +3,8 @@
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
if(NOT DEFINED IDF_TARGET)
|
||||
|
||||
cmake_minimum_required(VERSION 3.8...3.20)
|
||||
|
||||
project(boost_variant2 VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX)
|
||||
@ -26,3 +28,22 @@ if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt")
|
||||
add_subdirectory(test)
|
||||
|
||||
endif()
|
||||
|
||||
else()
|
||||
|
||||
FILE(GLOB_RECURSE headers include/*.h include/*.hpp)
|
||||
|
||||
idf_component_register(
|
||||
SRCS
|
||||
${headers}
|
||||
INCLUDE_DIRS
|
||||
include
|
||||
REQUIRES
|
||||
boost_assert
|
||||
boost_config
|
||||
boost_mp11
|
||||
)
|
||||
|
||||
endif()
|
||||
|
||||
|
||||
|
@ -8,6 +8,10 @@ https://www.boost.org/LICENSE_1_0.txt
|
||||
# Revision History
|
||||
:idprefix: changelog_
|
||||
|
||||
## Changes in 1.79.0
|
||||
|
||||
* Added `operator<<` for `monostate`.
|
||||
|
||||
## Changes in 1.78.0
|
||||
|
||||
* Added `<boost/variant2.hpp>`.
|
||||
|
@ -144,12 +144,6 @@ template<class... T>
|
||||
template<class... T>
|
||||
constexpr bool operator>=(const variant<T...>& v, const variant<T...>& w);
|
||||
|
||||
// stream insertion (extension)
|
||||
|
||||
template<class Ch, class Tr, class... T>
|
||||
std::basic_ostream<Ch, Tr>&
|
||||
operator<<( std::basic_ostream<Ch, Tr>& os, variant<T...> const& v );
|
||||
|
||||
// swap
|
||||
|
||||
template<class... T>
|
||||
@ -176,6 +170,16 @@ constexpr bool operator>(monostate, monostate) noexcept { return false; }
|
||||
constexpr bool operator<=(monostate, monostate) noexcept { return true; }
|
||||
constexpr bool operator>=(monostate, monostate) noexcept { return true; }
|
||||
|
||||
// stream insertion (extension)
|
||||
|
||||
template<class Ch, class Tr, class... T>
|
||||
std::basic_ostream<Ch, Tr>&
|
||||
operator<<( std::basic_ostream<Ch, Tr>& os, variant<T...> const& v );
|
||||
|
||||
template<class Ch, class Tr>
|
||||
std::basic_ostream<Ch, Tr>&
|
||||
operator<<( std::basic_ostream<Ch, Tr>& os, monostate const& v );
|
||||
|
||||
// bad_variant_access
|
||||
|
||||
class bad_variant_access;
|
||||
@ -886,20 +890,6 @@ template<class... T>
|
||||
Returns: ::
|
||||
`w \<= v`.
|
||||
|
||||
### Stream Insertion (extension)
|
||||
```
|
||||
template<class Ch, class Tr, class... T>
|
||||
std::basic_ostream<Ch, Tr>&
|
||||
operator<<( std::basic_ostream<Ch, Tr>& os, variant<T...> const& v );
|
||||
```
|
||||
[none]
|
||||
* {blank}
|
||||
+
|
||||
Requires: ::
|
||||
`sizeof...(T) != 0`.
|
||||
Returns: ::
|
||||
`os << get<I>(v)`, where `I` is `v.index()`.
|
||||
|
||||
### swap
|
||||
|
||||
```
|
||||
@ -946,6 +936,33 @@ Remarks: :: If `R` is given explicitly, as in `visit_by_index<int>`, the return
|
||||
of `Fi` to the corresponding variant alternatives must have the same return type
|
||||
for this deduction to succeed.
|
||||
|
||||
### Stream Insertion (extension)
|
||||
```
|
||||
template<class Ch, class Tr, class... T>
|
||||
std::basic_ostream<Ch, Tr>&
|
||||
operator<<( std::basic_ostream<Ch, Tr>& os, variant<T...> const& v );
|
||||
```
|
||||
[none]
|
||||
* {blank}
|
||||
+
|
||||
Requires: ::
|
||||
`sizeof...(T) != 0`.
|
||||
Returns: ::
|
||||
`os << get<I>(v)`, where `I` is `v.index()`.
|
||||
|
||||
```
|
||||
template<class Ch, class Tr>
|
||||
std::basic_ostream<Ch, Tr>&
|
||||
operator<<( std::basic_ostream<Ch, Tr>& os, monostate const& v );
|
||||
```
|
||||
[none]
|
||||
* {blank}
|
||||
+
|
||||
Effects: ::
|
||||
`os << "monostate"`.
|
||||
Returns: ::
|
||||
`os`.
|
||||
|
||||
### bad_variant_access
|
||||
|
||||
```
|
||||
|
@ -842,14 +842,14 @@ struct none {};
|
||||
// trivially destructible, single buffered
|
||||
template<class... T> struct variant_base_impl<true, true, T...>
|
||||
{
|
||||
unsigned ix_;
|
||||
variant_storage<none, T...> st_;
|
||||
unsigned ix_;
|
||||
|
||||
constexpr variant_base_impl(): ix_( 0 ), st_( mp11::mp_size_t<0>() )
|
||||
constexpr variant_base_impl(): st_( mp11::mp_size_t<0>() ), ix_( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... )
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... ), ix_( I::value + 1 )
|
||||
{
|
||||
}
|
||||
|
||||
@ -913,14 +913,14 @@ template<class... T> struct variant_base_impl<true, true, T...>
|
||||
// trivially destructible, double buffered
|
||||
template<class... T> struct variant_base_impl<true, false, T...>
|
||||
{
|
||||
unsigned ix_;
|
||||
variant_storage<none, T...> st_[ 2 ];
|
||||
unsigned ix_;
|
||||
|
||||
constexpr variant_base_impl(): ix_( 0 ), st_{ { mp11::mp_size_t<0>() }, { mp11::mp_size_t<0>() } }
|
||||
constexpr variant_base_impl(): st_{ { mp11::mp_size_t<0>() }, { mp11::mp_size_t<0>() } }, ix_( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): ix_( ( I::value + 1 ) * 2 ), st_{ { mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... }, { mp11::mp_size_t<0>() } }
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st_{ { mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... }, { mp11::mp_size_t<0>() } }, ix_( ( I::value + 1 ) * 2 )
|
||||
{
|
||||
}
|
||||
|
||||
@ -971,14 +971,14 @@ template<class... T> struct variant_base_impl<true, false, T...>
|
||||
// not trivially destructible, single buffered
|
||||
template<class... T> struct variant_base_impl<false, true, T...>
|
||||
{
|
||||
unsigned ix_;
|
||||
variant_storage<none, T...> st_;
|
||||
unsigned ix_;
|
||||
|
||||
constexpr variant_base_impl(): ix_( 0 ), st_( mp11::mp_size_t<0>() )
|
||||
constexpr variant_base_impl(): st_( mp11::mp_size_t<0>() ), ix_( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... )
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... ), ix_( I::value + 1 )
|
||||
{
|
||||
}
|
||||
|
||||
@ -1061,19 +1061,18 @@ template<class... T> struct variant_base_impl<false, true, T...>
|
||||
// not trivially destructible, double buffered
|
||||
template<class... T> struct variant_base_impl<false, false, T...>
|
||||
{
|
||||
unsigned ix_;
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ < 11 && !defined(__clang__) && !defined(__INTEL_COMPILER)
|
||||
|
||||
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63707 :-(
|
||||
|
||||
variant_storage<none, T...> st1_, st2_;
|
||||
unsigned ix_;
|
||||
|
||||
constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() ), st2_( mp11::mp_size_t<0>() )
|
||||
constexpr variant_base_impl(): st1_( mp11::mp_size_t<0>() ), st2_( mp11::mp_size_t<0>() ), ix_( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): ix_( ( I::value + 1 ) * 2 ), st1_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... ), st2_( mp11::mp_size_t<0>() )
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st1_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... ), st2_( mp11::mp_size_t<0>() ), ix_( ( I::value + 1 ) * 2 )
|
||||
{
|
||||
}
|
||||
|
||||
@ -1090,12 +1089,13 @@ template<class... T> struct variant_base_impl<false, false, T...>
|
||||
#else
|
||||
|
||||
variant_storage<none, T...> st_[ 2 ];
|
||||
unsigned ix_;
|
||||
|
||||
constexpr variant_base_impl(): ix_( 0 ), st_{ { mp11::mp_size_t<0>() }, { mp11::mp_size_t<0>() } }
|
||||
constexpr variant_base_impl(): st_{ { mp11::mp_size_t<0>() }, { mp11::mp_size_t<0>() } }, ix_( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): ix_( ( I::value + 1 ) * 2 ), st_{ { mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... }, { mp11::mp_size_t<0>() } }
|
||||
template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st_{ { mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... }, { mp11::mp_size_t<0>() } }, ix_( ( I::value + 1 ) * 2 )
|
||||
{
|
||||
}
|
||||
|
||||
@ -2253,9 +2253,26 @@ template<class Ch, class Tr, class... T> struct ostream_insert_L
|
||||
}
|
||||
};
|
||||
|
||||
template<class Os, class T, class E = void> struct is_output_streamable: std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
template<class Os, class T> struct is_output_streamable<Os, T, decltype( std::declval<Os&>() << std::declval<T const&>(), (void)0 )>: std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<class Ch, class Tr, class T1, class... T> std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, variant<T1, T...> const& v )
|
||||
template<class Ch, class Tr>
|
||||
std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, monostate const& )
|
||||
{
|
||||
os << "monostate";
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class Ch, class Tr, class T1, class... T,
|
||||
class E = typename std::enable_if< mp11::mp_all< detail::is_output_streamable<std::basic_ostream<Ch, Tr>, T>... >::value >::type >
|
||||
std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, variant<T1, T...> const& v )
|
||||
{
|
||||
return mp11::mp_with_index<1 + sizeof...(T)>( v.index(),
|
||||
detail::ostream_insert_L<Ch, Tr, T1, T...>{ os, v } );
|
||||
|
@ -125,3 +125,4 @@ compile variant_derived_construct.cpp ;
|
||||
run variant_visit_by_index.cpp ;
|
||||
|
||||
run variant_ostream_insert.cpp ;
|
||||
run is_output_streamable.cpp ;
|
||||
|
41
test/is_output_streamable.cpp
Normal file
41
test/is_output_streamable.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright 2021 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/variant2/variant.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
struct X
|
||||
{
|
||||
};
|
||||
|
||||
struct Y
|
||||
{
|
||||
};
|
||||
|
||||
std::ostream& operator<<( std::ostream& os, Y const& /*y*/ )
|
||||
{
|
||||
os << "Y()";
|
||||
return os;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable<std::ostream, int>));
|
||||
BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable<std::ostream, float>));
|
||||
BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable<std::ostream, std::string>));
|
||||
BOOST_TEST_TRAIT_FALSE((boost::variant2::detail::is_output_streamable<std::ostream, void>));
|
||||
BOOST_TEST_TRAIT_FALSE((boost::variant2::detail::is_output_streamable<std::ostream, X>));
|
||||
BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable<std::ostream, Y>));
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable<std::ostream, boost::variant2::monostate>));
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable<std::ostream, boost::variant2::variant<int, float, std::string>>));
|
||||
BOOST_TEST_TRAIT_FALSE((boost::variant2::detail::is_output_streamable<std::ostream, boost::variant2::variant<int, float, X>>));
|
||||
BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable<std::ostream, boost::variant2::variant<int, float, Y>>));
|
||||
BOOST_TEST_TRAIT_TRUE((boost::variant2::detail::is_output_streamable<std::ostream, boost::variant2::variant<boost::variant2::monostate, int, float>>));
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
@ -20,17 +20,24 @@ template<class T> std::string to_string( T const& t )
|
||||
|
||||
int main()
|
||||
{
|
||||
variant<int, float, std::string> v( 1 );
|
||||
{
|
||||
BOOST_TEST_EQ( to_string( monostate() ), "monostate" );
|
||||
}
|
||||
|
||||
{
|
||||
variant<monostate, int, float, std::string> v;
|
||||
|
||||
BOOST_TEST_EQ( to_string( v ), to_string( monostate() ) );
|
||||
|
||||
v = 1;
|
||||
BOOST_TEST_EQ( to_string( v ), to_string( 1 ) );
|
||||
|
||||
v = 3.14f;
|
||||
|
||||
BOOST_TEST_EQ( to_string( v ), to_string( 3.14f ) );
|
||||
|
||||
v = "test";
|
||||
|
||||
BOOST_TEST_EQ( to_string( v ), to_string( "test" ) );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
Reference in New Issue
Block a user