42#include <boost/test/unit_test.hpp>
59static_assert(std::is_base_of<std::bad_cast, ki::bad_any_cast>::value,
60 "ki::bad_any_cast must derive from std::bad_cast");
66static_assert(std::is_assignable<any&, int>::value);
67static_assert(!std::is_assignable<any&, std::unique_ptr<int>>::value);
68static_assert(std::is_constructible<any, int>::value);
69static_assert(!std::is_constructible<any, std::unique_ptr<int>>::value);
70static_assert(!std::is_assignable<any&, const std::unique_ptr<int>&>::value);
71static_assert(!std::is_constructible<any&, const std::unique_ptr<int>&>::value);
72static_assert(!std::is_assignable<any&, std::unique_ptr<int>&>::value);
73static_assert(!std::is_constructible<any&, std::unique_ptr<int>&>::value);
80static_assert(!std::is_constructible_v<
any,
81 std::in_place_type_t<NoDefaultCtor>>);
83static_assert(!std::is_constructible_v<
any,
84 std::in_place_type_t<NoDefaultCtor>&>);
86static_assert(!std::is_constructible_v<
any,
87 std::in_place_type_t<NoDefaultCtor>&&>);
89static_assert(!std::is_constructible_v<
any,
90 const std::in_place_type_t<NoDefaultCtor>&>);
92static_assert(!std::is_constructible_v<
any,
93 const std::in_place_type_t<NoDefaultCtor>&&>);
95static_assert( std::is_copy_constructible_v<std::tuple<any>> );
100 A(
const A&) =
default;
101 explicit A(
any value);
104static_assert(std::is_copy_constructible_v<A>);
110 std::tuple<int, int>
t;
111 template<
class... Args>
112 combined(std::initializer_list<int> il, Args&&... args)
113 :
v(il),
t(
std::forward<Args>(args)...)
135 string s, s2(
"Jane");
137 BOOST_CHECK(s ==
"Meow");
152 BOOST_CHECK(p ==
nullptr);
156 BOOST_CHECK(p !=
nullptr);
160 BOOST_CHECK(p ==
nullptr);
165 }
catch (
const bad_any_cast&) {
175 MoveEnabled(MoveEnabled&&)
179 MoveEnabled() =
default;
180 MoveEnabled(
const MoveEnabled&) =
default;
194 ExplicitCopy() =
default;
195 explicit ExplicitCopy(
const ExplicitCopy&) =
default;
197 any x = ExplicitCopy();
206 noncopyable(noncopyable
const&) =
delete;
211 BOOST_CHECK( p ==
nullptr );
221 void (*p1)() =
any_cast<
void()>(&a);
222 BOOST_CHECK( p1 ==
nullptr );
223 int (*p2)(int) =
any_cast<
int(
int)>(&a);
224 BOOST_CHECK( p2 ==
nullptr );
225 int (*p3)() =
any_cast<
int()>(&std::as_const(a));
226 BOOST_CHECK( p3 ==
nullptr );
230 BOOST_CHECK(
false );
236 BOOST_CHECK(
false );
242 BOOST_CHECK(
false );
253 BOOST_CHECK( a.
type() ==
typeid(
int*) );
255 BOOST_CHECK( a.
type() !=
typeid(
int[3]) );
256 BOOST_CHECK( p1 ==
nullptr );
258 BOOST_CHECK( a.
type() !=
typeid(
int[]) );
259 BOOST_CHECK( p2 ==
nullptr );
261 BOOST_CHECK( p3 ==
nullptr );
276static_assert(std::is_nothrow_move_constructible<LocationAware>::value,
"");
277static_assert(!std::is_trivially_copyable<LocationAware>::value,
"");
293 any tmp = std::move(a);
314 BOOST_CHECK( i2 == 42 );
315 BOOST_CHECK( &i2 != &i );
318 BOOST_CHECK( std::get<0>(t) == 1 && std::get<1>(t) == 2);
321 BOOST_CHECK(v[0] == 42 && v[1] == 666);
324 BOOST_CHECK(c.v[0] == 42 && c.v[1] == 666
325 && std::get<0>(c.t) == 0 && std::get<1>(c.t) == 0 );
328 BOOST_CHECK(c2.v[0] == 1 && c2.v[1] == 2
329 && std::get<0>(c2.t) == 3 && std::get<1>(c2.t) == 4 );
336 BOOST_CHECK( x.
type() ==
typeid(
void) );
338 BOOST_CHECK( x.
type() ==
typeid(
int) );
340 BOOST_CHECK( x.
type() ==
typeid(
void) );
373 any o(std::in_place_type<int>, i);
375 BOOST_CHECK( i2 == 42 );
376 BOOST_CHECK( &i2 != &i );
377 any o2(std::in_place_type<std::tuple<int, int>>, 1, 2);
379 BOOST_CHECK( std::get<0>(t) == 1 && std::get<1>(t) == 2);
380 any o3(std::in_place_type<std::vector<int>>, {42, 666});
382 BOOST_CHECK(v[0] == 42 && v[1] == 666);
383 any o4(std::in_place_type<combined>, {42, 666});
385 BOOST_CHECK(c.v[0] == 42 && c.v[1] == 666
386 && std::get<0>(c.t) == 0 && std::get<1>(c.t) == 0 );
387 any o5(std::in_place_type<combined>, {1, 2}, 3, 4);
389 BOOST_CHECK(c2.v[0] == 1 && c2.v[1] == 2
390 && std::get<0>(c2.t) == 3 && std::get<1>(c2.t) == 4 );
391 any o6(std::in_place_type<int&>, i);
393 any o7(std::in_place_type<
void()>,
nullptr);
394 any o8(std::in_place_type<
void(*)()>,
nullptr);
395 BOOST_CHECK(o7.
type() == o8.
type());
396 any o9(std::in_place_type<
char(&)[42]>,
nullptr);
397 any o10(std::in_place_type<char*>,
nullptr);
398 BOOST_CHECK(o9.
type() == o10.
type());
424 BOOST_CHECK(
moved ==
false);
425 any a2(std::move(x));
426 BOOST_CHECK(
moved ==
true);
435 BOOST_CHECK(
moved ==
false);
437 any a2(std::move(a1));
438 BOOST_CHECK(
copied ==
false);
447 BOOST_CHECK(
moved ==
false);
449 any a2(std::move(a1));
450 BOOST_CHECK(
copied ==
false);
451 BOOST_CHECK(
moved ==
true);
487 auto a =
any(std::in_place_type<any>, 5);
494 any p = std::pair<any, any>(1, 1);
499 any t = std::tuple<any>(1);
506struct alignas(2 * alignof(void*))
X3 { };
511 std::uintptr_t a_addr =
reinterpret_cast<std::uintptr_t
>(&a);
512 std::uintptr_t a_end = a_addr +
sizeof(a);
513 std::uintptr_t obj_addr =
reinterpret_cast<std::uintptr_t
>(obj);
514 return (a_addr <= obj_addr) && (obj_addr < a_end);
622 BOOST_CHECK( !x.a.has_value() );
626 BOOST_CHECK( x.a.has_value() );
654 BOOST_CHECK(
moved ==
false);
658 BOOST_CHECK(
moved ==
true);
659 BOOST_CHECK(
copied ==
false);
669 BOOST_CHECK(
moved ==
false);
673 BOOST_CHECK(
moved ==
false);
674 BOOST_CHECK(
copied ==
false);
689 BOOST_CHECK(
moved ==
true);
690 BOOST_CHECK(
copied ==
false);
784 BOOST_CHECK( i2 == 42 );
785 BOOST_CHECK( &i2 != &i );
787 o2.
emplace<std::tuple<int, int>>(1, 2);
789 BOOST_CHECK( std::get<0>(t) == 1 && std::get<1>(t) == 2);
791 o3.
emplace<std::vector<int>>({42, 666});
793 BOOST_CHECK(v[0] == 42 && v[1] == 666);
797 BOOST_CHECK(c.v[0] == 42 && c.v[1] == 666
798 && std::get<0>(c.t) == 0 && std::get<1>(c.t) == 0 );
802 BOOST_CHECK(c2.v[0] == 1 && c2.v[1] == 2
803 && std::get<0>(c2.t) == 3 && std::get<1>(c2.t) == 4 );
810 o8.
emplace<void(*)()>(
nullptr);
811 BOOST_CHECK(o7.
type() == o8.
type());
813 o9.
emplace<char(&)[42]>(
nullptr);
816 BOOST_CHECK(o9.
type() == o10.
type());
819 BOOST_CHECK(&o11.
emplace<std::vector<int>>({1,2,3}) ==
A type-safe container of any type.
bool has_value() const noexcept
Report whether there is a contained object or not.
void reset() noexcept
If not empty, destroys the contained object.
void swap(any &rhs) noexcept
Exchange state with another object.
any_emplace_t< std::decay_t< T >, Args... > emplace(Args &&... args)
Emplace with an object created from args as the contained object.
const std::type_info & type() const noexcept
The typeid of the contained object, or typeid(void) if empty.
Exception class thrown by a failed any_cast.
An implementation of std::any_cast, which uses type_info::hash_code to check validity of cast types.
std::enable_if_t< std::is_constructible_v< any, std::in_place_type_t< T >, Args... >, any > make_any(Args &&... args)
Create a any holding a T constructed from args....
ValueType any_cast(const any &any)
Access the contained object.
Good(const Good &)=default
LocationAware & operator=(const LocationAware &)
LocationAware(LocationAware &&) noexcept
LocationAware(const LocationAware &)
LocationAware & operator=(LocationAware &&) noexcept
combined(std::initializer_list< int > il, Args &&... args)
wrapper(const wrapper &w)
auto & operator=(const any &t)
auto & operator=(const wrapper &w)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
bool stored_internally(void *obj, const ki::any &a)
BOOST_AUTO_TEST_CASE(AnyCast_1)
std::set< const void * > live_objects
ValueType any_cast(const any &any)
Access the contained object.
MATRIX3x3D m2(VECTOR3I{ 6, 6, 6 }, { 1, 1, 1 }, { 3, 3, 3 })
Test suite for KiCad math code.
MATRIX3x3D m3(VECTOR3I{ 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 })