43#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>> );
98 A(
const A&) =
default;
101static_assert(std::is_copy_constructible_v<A>);
107 std::tuple<int, int>
t;
108 template<
class... Args>
109 combined(std::initializer_list<int> il, Args&&... args)
110 : v(il), t(
std::forward<Args>(args)...)
122 BOOST_CHECK(any_cast<int>(x) == 5);
123 any_cast<int&>(x) = 10;
124 BOOST_CHECK(any_cast<int>(x) == 10);
127 BOOST_CHECK(strcmp(any_cast<const char*>(x),
"Meow") == 0);
128 any_cast<const char*&>(x) =
"Harry";
129 BOOST_CHECK(strcmp(any_cast<const char*>(x),
"Harry") == 0);
132 string s, s2(
"Jane");
133 s = std::move(any_cast<string&>(x));
134 BOOST_CHECK(s ==
"Meow");
135 any_cast<string&>(x) = std::move(s2);
136 BOOST_CHECK(any_cast<const string&>(x) ==
"Jane");
140 BOOST_CHECK(any_cast<const string&>(y) == cat);
148 auto p = any_cast<double>(&x);
149 BOOST_CHECK(p ==
nullptr);
152 p = any_cast<double>(&x);
153 BOOST_CHECK(p !=
nullptr);
156 p = any_cast<double>(&x);
157 BOOST_CHECK(p ==
nullptr);
162 }
catch (
const bad_any_cast&) {
172 MoveEnabled(MoveEnabled&&)
176 MoveEnabled() =
default;
177 MoveEnabled(
const MoveEnabled&) =
default;
180 MoveEnabled
m2 = any_cast<MoveEnabled>(
any(m));
182 MoveEnabled&&
m3 = any_cast<MoveEnabled&&>(
any(m));
191 ExplicitCopy() =
default;
192 explicit ExplicitCopy(
const ExplicitCopy&) =
default;
194 any x = ExplicitCopy();
195 ExplicitCopy ec{any_cast<ExplicitCopy>(x)};
196 ExplicitCopy ec2{any_cast<ExplicitCopy>(std::move(x))};
203 noncopyable(noncopyable
const&) =
delete;
207 auto p = any_cast<noncopyable>(&a);
208 BOOST_CHECK( p ==
nullptr );
218 void (*p1)() = any_cast<
void()>(&a);
219 BOOST_CHECK( p1 ==
nullptr );
220 int (*p2)(int) = any_cast<
int(
int)>(&a);
221 BOOST_CHECK( p2 ==
nullptr );
222 int (*p3)() = any_cast<
int()>(&std::as_const(a));
223 BOOST_CHECK( p3 ==
nullptr );
226 any_cast<int(&)()>(a);
227 BOOST_CHECK(
false );
232 any_cast<int(&)()>(std::move(a));
233 BOOST_CHECK(
false );
238 any_cast<int(&)()>(std::as_const(a));
239 BOOST_CHECK(
false );
250 BOOST_CHECK( a.
type() ==
typeid(
int*) );
251 int (*p1)[3] = any_cast<int[3]>(&a);
252 BOOST_CHECK( a.
type() !=
typeid(
int[3]) );
253 BOOST_CHECK( p1 ==
nullptr );
254 int (*p2)[] = any_cast<int[]>(&a);
255 BOOST_CHECK( a.
type() !=
typeid(
int[]) );
256 BOOST_CHECK( p2 ==
nullptr );
257 const int (*p3)[] = any_cast<int[]>(&std::as_const(a));
258 BOOST_CHECK( p3 ==
nullptr );
273static_assert(std::is_nothrow_move_constructible<LocationAware>::value,
"");
274static_assert(!std::is_trivially_copyable<LocationAware>::value,
"");
290 any tmp = std::move(a);
309 auto o = ki::make_any<int>(i);
310 int& i2 = any_cast<int&>(o);
311 BOOST_CHECK( i2 == 42 );
312 BOOST_CHECK( &i2 != &i );
313 auto o2 = ki::make_any<std::tuple<int, int>>(1, 2);
314 std::tuple<int, int>& t = any_cast<std::tuple<int, int>&>(o2);
315 BOOST_CHECK( std::get<0>(t) == 1 && std::get<1>(t) == 2);
316 auto o3 = ki::make_any<std::vector<int>>({42, 666});
317 std::vector<int>& v = any_cast<std::vector<int>&>(o3);
318 BOOST_CHECK(v[0] == 42 && v[1] == 666);
319 auto o4 = ki::make_any<combined>({42, 666});
320 combined& c = any_cast<combined&>(o4);
321 BOOST_CHECK(c.v[0] == 42 && c.v[1] == 666
322 && std::get<0>(c.t) == 0 && std::get<1>(c.t) == 0 );
323 auto o5 = ki::make_any<combined>({1, 2}, 3, 4);
324 combined& c2 = any_cast<combined&>(o5);
325 BOOST_CHECK(c2.v[0] == 1 && c2.v[1] == 2
326 && std::get<0>(c2.t) == 3 && std::get<1>(c2.t) == 4 );
333 BOOST_CHECK( x.
type() ==
typeid(
void) );
335 BOOST_CHECK( x.
type() ==
typeid(
int) );
337 BOOST_CHECK( x.
type() ==
typeid(
void) );
370 any o(std::in_place_type<int>, i);
371 int& i2 = any_cast<int&>(o);
372 BOOST_CHECK( i2 == 42 );
373 BOOST_CHECK( &i2 != &i );
374 any o2(std::in_place_type<std::tuple<int, int>>, 1, 2);
375 std::tuple<int, int>& t = any_cast<std::tuple<int, int>&>(o2);
376 BOOST_CHECK( std::get<0>(t) == 1 && std::get<1>(t) == 2);
377 any o3(std::in_place_type<std::vector<int>>, {42, 666});
378 std::vector<int>& v = any_cast<std::vector<int>&>(o3);
379 BOOST_CHECK(v[0] == 42 && v[1] == 666);
380 any o4(std::in_place_type<combined>, {42, 666});
381 combined& c = any_cast<combined&>(o4);
382 BOOST_CHECK(c.v[0] == 42 && c.v[1] == 666
383 && std::get<0>(c.t) == 0 && std::get<1>(c.t) == 0 );
384 any o5(std::in_place_type<combined>, {1, 2}, 3, 4);
385 combined& c2 = any_cast<combined&>(o5);
386 BOOST_CHECK(c2.v[0] == 1 && c2.v[1] == 2
387 && std::get<0>(c2.t) == 3 && std::get<1>(c2.t) == 4 );
388 any o6(std::in_place_type<int&>, i);
390 any o7(std::in_place_type<
void()>,
nullptr);
391 any o8(std::in_place_type<
void(*)()>,
nullptr);
392 BOOST_CHECK(o7.
type() == o8.
type());
393 any o9(std::in_place_type<
char(&)[42]>,
nullptr);
394 any o10(std::in_place_type<char*>,
nullptr);
395 BOOST_CHECK(o9.
type() == o10.
type());
421 BOOST_CHECK(
moved ==
false);
422 any a2(std::move(x));
423 BOOST_CHECK(
moved ==
true);
432 BOOST_CHECK(
moved ==
false);
434 any a2(std::move(a1));
435 BOOST_CHECK(
copied ==
false);
444 BOOST_CHECK(
moved ==
false);
446 any a2(std::move(a1));
447 BOOST_CHECK(
copied ==
false);
448 BOOST_CHECK(
moved ==
true);
484 auto a =
any(std::in_place_type<any>, 5);
485 BOOST_CHECK( any_cast<int>(any_cast<any>(a)) == 5 );
487 auto b =
any(std::in_place_type<any>, {1});
488 (void) any_cast<std::initializer_list<int>>(any_cast<any>(b));
494 any p = std::pair<any, any>(1, 1);
495 auto pt = any_cast<std::pair<any, any>>(p);
496 BOOST_CHECK( any_cast<int>(pt.first) == 1 );
497 BOOST_CHECK( any_cast<int>(pt.second) == 1 );
499 any t = std::tuple<any>(1);
500 auto tt = any_cast<std::tuple<any>>(t);
501 BOOST_CHECK( any_cast<int>(std::get<0>(tt)) == 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);
521 X3& x = any_cast<X3&>(a);
525 char& c = any_cast<char&>(a);
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);
757 auto x = any_cast<Good>(a1);
770 auto x = any_cast<Good>(a1);
783 int& i2 = any_cast<int&>(o);
784 BOOST_CHECK( i2 == 42 );
785 BOOST_CHECK( &i2 != &i );
787 o2.
emplace<std::tuple<int, int>>(1, 2);
788 std::tuple<int, int>& t = any_cast<std::tuple<int, int>&>(o2);
789 BOOST_CHECK( std::get<0>(t) == 1 && std::get<1>(t) == 2);
791 o3.
emplace<std::vector<int>>({42, 666});
792 std::vector<int>& v = any_cast<std::vector<int>&>(o3);
793 BOOST_CHECK(v[0] == 42 && v[1] == 666);
796 combined& c = any_cast<combined&>(o4);
797 BOOST_CHECK(c.v[0] == 42 && c.v[1] == 666
798 && std::get<0>(c.t) == 0 && std::get<1>(c.t) == 0 );
801 combined& c2 = any_cast<combined&>(o5);
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());
818 BOOST_CHECK(&o11.
emplace<
int>(42) == &any_cast<int&>(o11));
819 BOOST_CHECK(&o11.
emplace<std::vector<int>>({1,2,3}) ==
820 &any_cast<std::vector<int>&>(o11));
A type-safe container of any type.
bool has_value() const noexcept
Reports 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.
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
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 })