43#include <boost/test/unit_test.hpp>
60static_assert(std::is_base_of<std::bad_cast, ki::bad_any_cast>::value,
61 "ki::bad_any_cast must derive from std::bad_cast");
67static_assert(std::is_assignable<any&, int>::value);
68static_assert(!std::is_assignable<any&, std::unique_ptr<int>>::value);
69static_assert(std::is_constructible<any, int>::value);
70static_assert(!std::is_constructible<any, std::unique_ptr<int>>::value);
71static_assert(!std::is_assignable<any&, const std::unique_ptr<int>&>::value);
72static_assert(!std::is_constructible<any&, const std::unique_ptr<int>&>::value);
73static_assert(!std::is_assignable<any&, std::unique_ptr<int>&>::value);
74static_assert(!std::is_constructible<any&, std::unique_ptr<int>&>::value);
81static_assert(!std::is_constructible_v<
any,
82 std::in_place_type_t<NoDefaultCtor>>);
84static_assert(!std::is_constructible_v<
any,
85 std::in_place_type_t<NoDefaultCtor>&>);
87static_assert(!std::is_constructible_v<
any,
88 std::in_place_type_t<NoDefaultCtor>&&>);
90static_assert(!std::is_constructible_v<
any,
91 const std::in_place_type_t<NoDefaultCtor>&>);
93static_assert(!std::is_constructible_v<
any,
94 const std::in_place_type_t<NoDefaultCtor>&&>);
96static_assert( std::is_copy_constructible_v<std::tuple<any>> );
99 A(
const A&) =
default;
102static_assert(std::is_copy_constructible_v<A>);
108 std::tuple<int, int>
t;
109 template<
class... Args>
110 combined(std::initializer_list<int> il, Args&&... args)
111 : v(il), t(
std::forward<Args>(args)...)
123 BOOST_CHECK(any_cast<int>(x) == 5);
124 any_cast<int&>(x) = 10;
125 BOOST_CHECK(any_cast<int>(x) == 10);
128 BOOST_CHECK(strcmp(any_cast<const char*>(x),
"Meow") == 0);
129 any_cast<const char*&>(x) =
"Harry";
130 BOOST_CHECK(strcmp(any_cast<const char*>(x),
"Harry") == 0);
133 string s, s2(
"Jane");
134 s = std::move(any_cast<string&>(x));
135 BOOST_CHECK(s ==
"Meow");
136 any_cast<string&>(x) = std::move(s2);
137 BOOST_CHECK(any_cast<const string&>(x) ==
"Jane");
141 BOOST_CHECK(any_cast<const string&>(y) == cat);
149 auto p = any_cast<double>(&x);
150 BOOST_CHECK(p ==
nullptr);
153 p = any_cast<double>(&x);
154 BOOST_CHECK(p !=
nullptr);
157 p = any_cast<double>(&x);
158 BOOST_CHECK(p ==
nullptr);
163 }
catch (
const bad_any_cast&) {
173 MoveEnabled(MoveEnabled&&)
177 MoveEnabled() =
default;
178 MoveEnabled(
const MoveEnabled&) =
default;
181 MoveEnabled
m2 = any_cast<MoveEnabled>(
any(m));
183 MoveEnabled&&
m3 = any_cast<MoveEnabled&&>(
any(m));
192 ExplicitCopy() =
default;
193 explicit ExplicitCopy(
const ExplicitCopy&) =
default;
195 any x = ExplicitCopy();
196 ExplicitCopy ec{any_cast<ExplicitCopy>(x)};
197 ExplicitCopy ec2{any_cast<ExplicitCopy>(std::move(x))};
204 noncopyable(noncopyable
const&) =
delete;
208 auto p = any_cast<noncopyable>(&a);
209 BOOST_CHECK( p ==
nullptr );
219 void (*p1)() = any_cast<
void()>(&a);
220 BOOST_CHECK( p1 ==
nullptr );
221 int (*p2)(int) = any_cast<
int(
int)>(&a);
222 BOOST_CHECK( p2 ==
nullptr );
223 int (*p3)() = any_cast<
int()>(&std::as_const(a));
224 BOOST_CHECK( p3 ==
nullptr );
227 any_cast<int(&)()>(a);
228 BOOST_CHECK(
false );
233 any_cast<int(&)()>(std::move(a));
234 BOOST_CHECK(
false );
239 any_cast<int(&)()>(std::as_const(a));
240 BOOST_CHECK(
false );
251 BOOST_CHECK( a.
type() ==
typeid(
int*) );
252 int (*p1)[3] = any_cast<int[3]>(&a);
253 BOOST_CHECK( a.
type() !=
typeid(
int[3]) );
254 BOOST_CHECK( p1 ==
nullptr );
255 int (*p2)[] = any_cast<int[]>(&a);
256 BOOST_CHECK( a.
type() !=
typeid(
int[]) );
257 BOOST_CHECK( p2 ==
nullptr );
258 const int (*p3)[] = any_cast<int[]>(&std::as_const(a));
259 BOOST_CHECK( p3 ==
nullptr );
274static_assert(std::is_nothrow_move_constructible<LocationAware>::value,
"");
275static_assert(!std::is_trivially_copyable<LocationAware>::value,
"");
291 any tmp = std::move(a);
310 auto o = ki::make_any<int>(i);
311 int& i2 = any_cast<int&>(o);
312 BOOST_CHECK( i2 == 42 );
313 BOOST_CHECK( &i2 != &i );
314 auto o2 = ki::make_any<std::tuple<int, int>>(1, 2);
315 std::tuple<int, int>& t = any_cast<std::tuple<int, int>&>(o2);
316 BOOST_CHECK( std::get<0>(t) == 1 && std::get<1>(t) == 2);
317 auto o3 = ki::make_any<std::vector<int>>({42, 666});
318 std::vector<int>& v = any_cast<std::vector<int>&>(o3);
319 BOOST_CHECK(v[0] == 42 && v[1] == 666);
320 auto o4 = ki::make_any<combined>({42, 666});
321 combined& c = any_cast<combined&>(o4);
322 BOOST_CHECK(c.v[0] == 42 && c.v[1] == 666
323 && std::get<0>(c.t) == 0 && std::get<1>(c.t) == 0 );
324 auto o5 = ki::make_any<combined>({1, 2}, 3, 4);
325 combined& c2 = any_cast<combined&>(o5);
326 BOOST_CHECK(c2.v[0] == 1 && c2.v[1] == 2
327 && std::get<0>(c2.t) == 3 && std::get<1>(c2.t) == 4 );
334 BOOST_CHECK( x.
type() ==
typeid(
void) );
336 BOOST_CHECK( x.
type() ==
typeid(
int) );
338 BOOST_CHECK( x.
type() ==
typeid(
void) );
371 any o(std::in_place_type<int>, i);
372 int& i2 = any_cast<int&>(o);
373 BOOST_CHECK( i2 == 42 );
374 BOOST_CHECK( &i2 != &i );
375 any o2(std::in_place_type<std::tuple<int, int>>, 1, 2);
376 std::tuple<int, int>& t = any_cast<std::tuple<int, int>&>(o2);
377 BOOST_CHECK( std::get<0>(t) == 1 && std::get<1>(t) == 2);
378 any o3(std::in_place_type<std::vector<int>>, {42, 666});
379 std::vector<int>& v = any_cast<std::vector<int>&>(o3);
380 BOOST_CHECK(v[0] == 42 && v[1] == 666);
381 any o4(std::in_place_type<combined>, {42, 666});
382 combined& c = any_cast<combined&>(o4);
383 BOOST_CHECK(c.v[0] == 42 && c.v[1] == 666
384 && std::get<0>(c.t) == 0 && std::get<1>(c.t) == 0 );
385 any o5(std::in_place_type<combined>, {1, 2}, 3, 4);
386 combined& c2 = any_cast<combined&>(o5);
387 BOOST_CHECK(c2.v[0] == 1 && c2.v[1] == 2
388 && std::get<0>(c2.t) == 3 && std::get<1>(c2.t) == 4 );
389 any o6(std::in_place_type<int&>, i);
391 any o7(std::in_place_type<
void()>,
nullptr);
392 any o8(std::in_place_type<
void(*)()>,
nullptr);
393 BOOST_CHECK(o7.
type() == o8.
type());
394 any o9(std::in_place_type<
char(&)[42]>,
nullptr);
395 any o10(std::in_place_type<char*>,
nullptr);
396 BOOST_CHECK(o9.
type() == o10.
type());
422 BOOST_CHECK(
moved ==
false);
423 any a2(std::move(x));
424 BOOST_CHECK(
moved ==
true);
433 BOOST_CHECK(
moved ==
false);
435 any a2(std::move(a1));
436 BOOST_CHECK(
copied ==
false);
445 BOOST_CHECK(
moved ==
false);
447 any a2(std::move(a1));
448 BOOST_CHECK(
copied ==
false);
449 BOOST_CHECK(
moved ==
true);
485 auto a =
any(std::in_place_type<any>, 5);
486 BOOST_CHECK( any_cast<int>(any_cast<any>(a)) == 5 );
492 any p = std::pair<any, any>(1, 1);
493 auto pt = any_cast<std::pair<any, any>>(p);
494 BOOST_CHECK( any_cast<int>(pt.first) == 1 );
495 BOOST_CHECK( any_cast<int>(pt.second) == 1 );
497 any t = std::tuple<any>(1);
498 auto tt = any_cast<std::tuple<any>>(t);
499 BOOST_CHECK( any_cast<int>(std::get<0>(tt)) == 1 );
504struct alignas(2 * alignof(void*))
X3 { };
509 std::uintptr_t a_addr =
reinterpret_cast<std::uintptr_t
>(&a);
510 std::uintptr_t a_end = a_addr +
sizeof(a);
511 std::uintptr_t obj_addr =
reinterpret_cast<std::uintptr_t
>(obj);
512 return (a_addr <= obj_addr) && (obj_addr < a_end);
519 X3& x = any_cast<X3&>(a);
523 char& c = any_cast<char&>(a);
620 BOOST_CHECK( !x.a.has_value() );
624 BOOST_CHECK( x.a.has_value() );
652 BOOST_CHECK(
moved ==
false);
656 BOOST_CHECK(
moved ==
true);
657 BOOST_CHECK(
copied ==
false);
667 BOOST_CHECK(
moved ==
false);
671 BOOST_CHECK(
moved ==
false);
672 BOOST_CHECK(
copied ==
false);
687 BOOST_CHECK(
moved ==
true);
688 BOOST_CHECK(
copied ==
false);
755 auto x = any_cast<Good>(a1);
768 auto x = any_cast<Good>(a1);
781 int& i2 = any_cast<int&>(o);
782 BOOST_CHECK( i2 == 42 );
783 BOOST_CHECK( &i2 != &i );
785 o2.
emplace<std::tuple<int, int>>(1, 2);
786 std::tuple<int, int>& t = any_cast<std::tuple<int, int>&>(o2);
787 BOOST_CHECK( std::get<0>(t) == 1 && std::get<1>(t) == 2);
789 o3.
emplace<std::vector<int>>({42, 666});
790 std::vector<int>& v = any_cast<std::vector<int>&>(o3);
791 BOOST_CHECK(v[0] == 42 && v[1] == 666);
794 combined& c = any_cast<combined&>(o4);
795 BOOST_CHECK(c.v[0] == 42 && c.v[1] == 666
796 && std::get<0>(c.t) == 0 && std::get<1>(c.t) == 0 );
799 combined& c2 = any_cast<combined&>(o5);
800 BOOST_CHECK(c2.v[0] == 1 && c2.v[1] == 2
801 && std::get<0>(c2.t) == 3 && std::get<1>(c2.t) == 4 );
808 o8.
emplace<void(*)()>(
nullptr);
809 BOOST_CHECK(o7.
type() == o8.
type());
811 o9.
emplace<char(&)[42]>(
nullptr);
814 BOOST_CHECK(o9.
type() == o10.
type());
816 BOOST_CHECK(&o11.
emplace<
int>(42) == &any_cast<int&>(o11));
817 BOOST_CHECK(&o11.
emplace<std::vector<int>>({1,2,3}) ==
818 &any_cast<std::vector<int>&>(o11));
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.
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 })