1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/any/anytest.cpp
3 // Purpose: Test the wxAny classes
4 // Author: Jaakko Salli
5 // Copyright: (c) the wxWidgets team
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
18 #include "wx/datetime.h"
19 #include "wx/object.h"
20 #include "wx/vector.h"
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 class wxAnyTestCase
: public CppUnit::TestCase
34 CPPUNIT_TEST_SUITE( wxAnyTestCase
);
35 CPPUNIT_TEST( CheckType
);
36 CPPUNIT_TEST( Equality
);
38 CPPUNIT_TEST( GetAs
);
40 CPPUNIT_TEST( wxVariantConversions
);
41 CPPUNIT_TEST( CustomTemplateSpecialization
);
43 CPPUNIT_TEST_SUITE_END();
50 void wxVariantConversions();
51 void CustomTemplateSpecialization();
54 wxDateTime m_testDateTime
;
56 wxAny m_anySignedChar1
;
57 wxAny m_anySignedShort1
;
58 wxAny m_anySignedInt1
;
59 wxAny m_anySignedLong1
;
60 wxAny m_anySignedLongLong1
;
61 wxAny m_anyUnsignedChar1
;
62 wxAny m_anyUnsignedShort1
;
63 wxAny m_anyUnsignedInt1
;
64 wxAny m_anyUnsignedLong1
;
65 wxAny m_anyUnsignedLongLong1
;
66 wxAny m_anyStringString1
;
67 wxAny m_anyCharString1
;
68 wxAny m_anyWcharString1
;
70 wxAny m_anyFloatDouble1
;
71 wxAny m_anyDoubleDouble1
;
72 wxAny m_anyWxObjectPtr1
;
77 wxAny m_anySignedChar2
;
78 wxAny m_anySignedShort2
;
79 wxAny m_anySignedInt2
;
80 wxAny m_anySignedLong2
;
81 wxAny m_anySignedLongLong2
;
82 wxAny m_anyUnsignedChar2
;
83 wxAny m_anyUnsignedShort2
;
84 wxAny m_anyUnsignedInt2
;
85 wxAny m_anyUnsignedLong2
;
86 wxAny m_anyUnsignedLongLong2
;
87 wxAny m_anyStringString2
;
88 wxAny m_anyCharString2
;
89 wxAny m_anyWcharString2
;
91 wxAny m_anyFloatDouble2
;
92 wxAny m_anyDoubleDouble2
;
93 wxAny m_anyWxObjectPtr2
;
97 DECLARE_NO_COPY_CLASS(wxAnyTestCase
)
100 // register in the unnamed registry so that these tests are run by default
101 CPPUNIT_TEST_SUITE_REGISTRATION( wxAnyTestCase
);
103 // also include in its own registry so that these tests can be run alone
104 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( wxAnyTestCase
, "wxAnyTestCase" );
106 // Let's use a number with first digit after decimal dot less than 5,
107 // so that we don't have to worry about whether conversion from float
108 // to int truncates or rounds.
109 const float TEST_FLOAT_CONST
= 123.456f
;
110 const double TEST_DOUBLE_CONST
= 123.456;
112 const double FEQ_DELTA
= 0.001;
114 wxObject
* dummyWxObjectPointer
= reinterpret_cast<wxObject
*>(1234);
115 void* dummyVoidPointer
= reinterpret_cast<void*>(1234);
119 // Test both 'creation' methods
120 wxAnyTestCase::wxAnyTestCase()
121 : m_anySignedChar1((signed char)15),
122 m_anySignedShort1((signed short)15),
123 m_anySignedInt1((signed int)15),
124 m_anySignedLong1((signed long)15),
126 m_anySignedLongLong1((wxLongLong_t
)15),
128 m_anyUnsignedChar1((unsigned char)15),
129 m_anyUnsignedShort1((unsigned short)15),
130 m_anyUnsignedInt1((unsigned int)15),
131 m_anyUnsignedLong1((unsigned long)15),
133 m_anyUnsignedLongLong1((wxULongLong_t
)15),
135 m_anyStringString1(wxString("abc")),
136 m_anyCharString1("abc"),
137 m_anyWcharString1(L
"abc"),
139 m_anyFloatDouble1(TEST_FLOAT_CONST
),
140 m_anyDoubleDouble1(TEST_DOUBLE_CONST
),
141 m_anyWxObjectPtr1(dummyWxObjectPointer
),
142 m_anyVoidPtr1(dummyVoidPointer
),
143 m_anyDateTime1(wxDateTime::Now())
145 m_testDateTime
= wxDateTime::Now();
146 m_anySignedChar2
= (signed char)15;
147 m_anySignedShort2
= (signed short)15;
148 m_anySignedInt2
= (signed int)15;
149 m_anySignedLong2
= (signed long)15;
151 m_anySignedLongLong2
= (wxLongLong_t
)15;
153 m_anyUnsignedChar2
= (unsigned char)15;
154 m_anyUnsignedShort2
= (unsigned short)15;
155 m_anyUnsignedInt2
= (unsigned int)15;
156 m_anyUnsignedLong2
= (unsigned long)15;
158 m_anyUnsignedLongLong2
= (wxULongLong_t
)15;
160 m_anyStringString2
= wxString("abc");
161 m_anyCharString2
= "abc";
162 m_anyWcharString2
= L
"abc";
164 m_anyFloatDouble2
= TEST_FLOAT_CONST
;
165 m_anyDoubleDouble2
= TEST_DOUBLE_CONST
;
166 m_anyDateTime2
= m_testDateTime
;
167 m_anyUniChar1
= wxUniChar('A');
168 m_anyWxObjectPtr2
= dummyWxObjectPointer
;
169 m_anyVoidPtr2
= dummyVoidPointer
;
172 void wxAnyTestCase::CheckType()
175 CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(nullAny
, wxString
));
177 CPPUNIT_ASSERT(wxANY_CHECK_TYPE(m_anyCharString2
, const char*));
178 CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyCharString2
, wxString
));
179 CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyCharString2
, const wchar_t*));
180 CPPUNIT_ASSERT(wxANY_CHECK_TYPE(m_anyWcharString2
, const wchar_t*));
181 CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyWcharString2
, wxString
));
182 CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyWcharString2
, const char*));
185 CPPUNIT_ASSERT( m_anyWcharString1
.HasSameType(m_anyWcharString2
) );
186 CPPUNIT_ASSERT( !m_anyWcharString1
.HasSameType(m_anyBool1
) );
189 void wxAnyTestCase::Equality()
192 // Currently this should work
193 CPPUNIT_ASSERT(m_anyUnsignedLong1
== 15L);
194 CPPUNIT_ASSERT(m_anyUnsignedLong1
!= 30L);
195 CPPUNIT_ASSERT(m_anyUnsignedLong1
== 15UL);
196 CPPUNIT_ASSERT(m_anyUnsignedLong1
!= 30UL);
197 CPPUNIT_ASSERT(m_anyStringString1
== wxString("abc"));
198 CPPUNIT_ASSERT(m_anyStringString1
!= wxString("ABC"));
199 CPPUNIT_ASSERT(m_anyStringString1
== "abc");
200 CPPUNIT_ASSERT(m_anyStringString1
!= "ABC");
201 CPPUNIT_ASSERT(m_anyStringString1
== L
"abc");
202 CPPUNIT_ASSERT(m_anyStringString1
!= L
"ABC");
203 CPPUNIT_ASSERT(m_anyBool1
== true);
204 CPPUNIT_ASSERT(m_anyBool1
!= false);
205 CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble1
, double),
206 wxANY_AS(m_anyDoubleDouble1
, double),
208 CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble1
, double),
211 CPPUNIT_ASSERT(wxANY_AS(m_anyWxObjectPtr1
, wxObject
*)
212 == dummyWxObjectPointer
);
213 CPPUNIT_ASSERT(wxANY_AS(m_anyVoidPtr1
, void*) == dummyVoidPointer
);
215 CPPUNIT_ASSERT(m_anySignedLong2
== 15);
216 CPPUNIT_ASSERT(m_anyStringString2
== wxString("abc"));
217 CPPUNIT_ASSERT(m_anyStringString2
== "abc");
218 CPPUNIT_ASSERT(m_anyStringString2
== L
"abc");
219 CPPUNIT_ASSERT(m_anyBool2
== true);
220 CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble2
, double),
221 wxANY_AS(m_anyDoubleDouble2
, double),
223 CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble2
, double),
226 CPPUNIT_ASSERT(wxANY_AS(m_anyWxObjectPtr2
, wxObject
*)
227 == dummyWxObjectPointer
);
228 CPPUNIT_ASSERT(wxANY_AS(m_anyVoidPtr2
, void*) == dummyVoidPointer
);
230 // Test sub-type system type compatibility
231 CPPUNIT_ASSERT(m_anySignedShort1
.GetType()->
232 IsSameType(m_anySignedLongLong1
.GetType()));
233 CPPUNIT_ASSERT(m_anyUnsignedShort1
.GetType()->
234 IsSameType(m_anyUnsignedLongLong1
.GetType()));
237 void wxAnyTestCase::As()
240 // Test getting C++ data from wxAny without dynamic conversion
241 signed char a
= wxANY_AS(m_anySignedChar1
, signed char);
242 CPPUNIT_ASSERT(a
== (signed int)15);
243 signed short b
= wxANY_AS(m_anySignedShort1
, signed short);
244 CPPUNIT_ASSERT(b
== (signed int)15);
245 signed int c
= wxANY_AS(m_anySignedInt1
, signed int);
246 CPPUNIT_ASSERT(c
== (signed int)15);
247 signed long d
= wxANY_AS(m_anySignedLong1
, signed long);
248 CPPUNIT_ASSERT(d
== (signed int)15);
250 wxLongLong_t e
= wxANY_AS(m_anySignedLongLong1
, wxLongLong_t
);
251 CPPUNIT_ASSERT(e
== (signed int)15);
253 unsigned char f
= wxANY_AS(m_anyUnsignedChar1
, unsigned char);
254 CPPUNIT_ASSERT(f
== (unsigned int)15);
255 unsigned short g
= wxANY_AS(m_anyUnsignedShort1
, unsigned short);
256 CPPUNIT_ASSERT(g
== (unsigned int)15);
257 unsigned int h
= wxANY_AS(m_anyUnsignedInt1
, unsigned int);
258 CPPUNIT_ASSERT(h
== (unsigned int)15);
259 unsigned long i
= wxANY_AS(m_anyUnsignedLong1
, unsigned long);
260 CPPUNIT_ASSERT(i
== (unsigned int)15);
262 wxULongLong_t j
= wxANY_AS(m_anyUnsignedLongLong1
, wxULongLong_t
);
263 CPPUNIT_ASSERT(j
== (unsigned int)15);
265 wxString k
= wxANY_AS(m_anyStringString1
, wxString
);
266 CPPUNIT_ASSERT(k
== "abc");
267 wxString l
= wxANY_AS(m_anyCharString1
, wxString
);
268 const char* cptr
= wxANY_AS(m_anyCharString1
, const char*);
269 CPPUNIT_ASSERT(l
== "abc");
270 CPPUNIT_ASSERT(cptr
);
271 wxString m
= wxANY_AS(m_anyWcharString1
, wxString
);
272 const wchar_t* wcptr
= wxANY_AS(m_anyWcharString1
, const wchar_t*);
273 CPPUNIT_ASSERT(wcptr
);
274 CPPUNIT_ASSERT(m
== "abc");
275 bool n
= wxANY_AS(m_anyBool1
, bool);
278 // Make sure the stored float that comes back is -identical-.
279 // So do not use delta comparison here.
280 float o
= wxANY_AS(m_anyFloatDouble1
, float);
281 CPPUNIT_ASSERT_EQUAL(o
, TEST_FLOAT_CONST
);
283 double p
= wxANY_AS(m_anyDoubleDouble1
, double);
284 CPPUNIT_ASSERT_EQUAL(p
, TEST_DOUBLE_CONST
);
286 wxUniChar chr
= wxANY_AS(m_anyUniChar1
, wxUniChar
);
287 CPPUNIT_ASSERT(chr
== 'A');
288 wxDateTime q
= wxANY_AS(m_anyDateTime1
, wxDateTime
);
289 CPPUNIT_ASSERT(q
== m_testDateTime
);
290 wxObject
* r
= wxANY_AS(m_anyWxObjectPtr1
, wxObject
*);
291 CPPUNIT_ASSERT(r
== dummyWxObjectPointer
);
292 void* s
= wxANY_AS(m_anyVoidPtr1
, void*);
293 CPPUNIT_ASSERT(s
== dummyVoidPointer
);
296 void wxAnyTestCase::Null()
299 CPPUNIT_ASSERT(a
.IsNull());
301 CPPUNIT_ASSERT(a
== -127);
303 CPPUNIT_ASSERT(a
.IsNull());
306 void wxAnyTestCase::GetAs()
309 // Test dynamic conversion
313 unsigned long ul
= 0;
315 // Let's test against float instead of double, since the former
316 // is not the native underlying type the code converts to, but
317 // should still work, all the same.
321 // Conversions from signed long type
322 // The first check should be enough to make sure that the sub-type system
324 res
= m_anySignedLong1
.GetAs(&si
);
326 CPPUNIT_ASSERT_EQUAL(si
, 15);
327 res
= m_anySignedLong1
.GetAs(&ul
);
329 CPPUNIT_ASSERT_EQUAL(ul
, 15UL);
330 res
= m_anySignedLong1
.GetAs(&s
);
332 CPPUNIT_ASSERT(s
== "15");
333 res
= m_anySignedLong1
.GetAs(&f
);
335 CPPUNIT_ASSERT_DOUBLES_EQUAL(f
, 15.0, FEQ_DELTA
);
336 res
= m_anySignedLong1
.GetAs(&b
);
338 CPPUNIT_ASSERT(b
== true);
340 // Conversions from unsigned long type
341 res
= m_anyUnsignedLong1
.GetAs(&l
);
343 CPPUNIT_ASSERT(l
== static_cast<signed long>(15));
344 res
= m_anyUnsignedLong1
.GetAs(&s
);
346 CPPUNIT_ASSERT(s
== "15");
347 res
= m_anyUnsignedLong1
.GetAs(&f
);
349 CPPUNIT_ASSERT_DOUBLES_EQUAL(f
, 15.0, FEQ_DELTA
);
350 res
= m_anyUnsignedLong1
.GetAs(&b
);
352 CPPUNIT_ASSERT(b
== true);
354 // Conversions from default "abc" string to other types
356 CPPUNIT_ASSERT(!m_anyStringString1
.GetAs(&l
));
357 CPPUNIT_ASSERT(!m_anyStringString1
.GetAs(&ul
));
358 CPPUNIT_ASSERT(!m_anyStringString1
.GetAs(&f
));
359 CPPUNIT_ASSERT(!m_anyStringString1
.GetAs(&b
));
361 // Let's test some other conversions from string that should work.
365 res
= anyString
.GetAs(&l
);
367 CPPUNIT_ASSERT(l
== static_cast<signed long>(15));
368 res
= anyString
.GetAs(&ul
);
370 CPPUNIT_ASSERT_EQUAL(ul
, static_cast<unsigned long>(15));
371 res
= anyString
.GetAs(&f
);
373 CPPUNIT_ASSERT_DOUBLES_EQUAL(f
, 15.0, FEQ_DELTA
);
375 res
= anyString
.GetAs(&b
);
377 CPPUNIT_ASSERT(b
== true);
379 res
= anyString
.GetAs(&b
);
381 CPPUNIT_ASSERT(b
== false);
383 // Conversions from bool type
384 res
= m_anyBool1
.GetAs(&l
);
386 CPPUNIT_ASSERT(l
== static_cast<signed long>(1));
387 res
= m_anyBool1
.GetAs(&ul
);
389 CPPUNIT_ASSERT_EQUAL(ul
, static_cast<unsigned long>(1));
390 res
= m_anyBool1
.GetAs(&s
);
392 CPPUNIT_ASSERT(s
== "true");
393 CPPUNIT_ASSERT(!m_anyBool1
.GetAs(&f
));
395 // Conversions from floating point type
396 res
= m_anyDoubleDouble1
.GetAs(&l
);
398 CPPUNIT_ASSERT(l
== static_cast<signed long>(123));
399 res
= m_anyDoubleDouble1
.GetAs(&ul
);
401 CPPUNIT_ASSERT_EQUAL(ul
, static_cast<unsigned long>(123));
402 res
= m_anyDoubleDouble1
.GetAs(&s
);
405 res
= s
.ToCDouble(&d2
);
407 CPPUNIT_ASSERT_DOUBLES_EQUAL(d2
, TEST_FLOAT_CONST
, FEQ_DELTA
);
412 // Test user data type for wxAnyValueTypeImpl specialization
413 // any hand-built wxVariantData. Also for inplace allocation
419 static wxVector
<MyClass
*> gs_myClassInstances
;
424 MyClass( int someValue
= 32768 )
427 m_someValue
= someValue
;
429 MyClass( const MyClass
& other
)
432 m_someValue
= other
.m_someValue
;
436 for ( size_t i
=0; i
<gs_myClassInstances
.size(); i
++ )
438 if ( gs_myClassInstances
[i
] == this )
440 gs_myClassInstances
.erase(gs_myClassInstances
.begin()+i
);
452 return wxString::Format("%i", m_someValue
);
458 // We use this for some sanity checking
459 gs_myClassInstances
.push_back(this);
468 // For testing purposes, create dummy variant data implementation
469 // that does not have wxAny conversion code
470 class wxMyVariantData
: public wxVariantData
473 wxMyVariantData(const MyClass
& value
)
478 virtual bool Eq(wxVariantData
& WXUNUSED(data
)) const
483 // What type is it? Return a string name.
484 virtual wxString
GetType() const { return "MyClass"; }
486 virtual wxVariantData
* Clone() const
488 return new wxMyVariantData(m_value
);
495 #endif // wxUSE_VARIANT
498 void wxAnyTestCase::wxVariantConversions()
502 // Test various conversions to and from wxVariant
506 // Prepare wxVariants
507 wxVariant
vLong(123L);
508 wxVariant
vString("ABC");
509 wxVariant
vDouble(TEST_FLOAT_CONST
);
510 wxVariant
vBool((bool)true);
511 wxVariant
vChar('A');
513 wxVariant
vLongLong(wxLongLong(wxLL(0xAABBBBCCCC)));
514 wxVariant
vULongLong(wxULongLong(wxULL(123456)));
516 wxArrayString arrstr
;
517 arrstr
.push_back("test string");
518 wxVariant
vArrayString(arrstr
);
519 wxVariant
vDateTime(m_testDateTime
);
520 wxVariant
vVoidPtr(dummyVoidPointer
);
521 wxVariant
vCustomType(new wxMyVariantData(MyClass(101)));
528 // Convert to wxAnys, and then back to wxVariants
532 CPPUNIT_ASSERT(any
== 123L);
533 res
= any
.GetAs(&variant
);
535 CPPUNIT_ASSERT(variant
== 123L);
537 // Make sure integer variant has correct type information
538 CPPUNIT_ASSERT(variant
.GetLong() == 123);
539 CPPUNIT_ASSERT(variant
.GetType() == "long");
541 // Unsigned long wxAny should convert to "ulonglong" wxVariant
543 res
= any
.GetAs(&variant
);
545 CPPUNIT_ASSERT(variant
.GetType() == "ulonglong");
546 CPPUNIT_ASSERT(variant
.GetLong() == 1000);
549 CPPUNIT_ASSERT(any
== "ABC");
550 res
= any
.GetAs(&variant
);
552 CPPUNIT_ASSERT(variant
.GetString() == "ABC");
554 // Must be able to build string wxVariant from wxAny built from
557 res
= any
.GetAs(&variant
);
559 CPPUNIT_ASSERT(variant
.GetType() == "string");
560 CPPUNIT_ASSERT(variant
.GetString() == "ABC");
562 res
= any
.GetAs(&variant
);
564 CPPUNIT_ASSERT(variant
.GetType() == "string");
566 CPPUNIT_ASSERT(variant
.GetString() == L
"ABC");
570 double d
= wxANY_AS(any
, double);
571 CPPUNIT_ASSERT_DOUBLES_EQUAL(d
, TEST_FLOAT_CONST
, FEQ_DELTA
);
572 res
= any
.GetAs(&variant
);
574 CPPUNIT_ASSERT_DOUBLES_EQUAL(variant
.GetDouble(),
579 CPPUNIT_ASSERT(wxANY_AS(any
, bool) == true);
580 res
= any
.GetAs(&variant
);
582 CPPUNIT_ASSERT(variant
.GetBool() == true);
585 //CPPUNIT_ASSERT(wxANY_AS(any, wxUniChar) == 'A');
586 res
= any
.GetAs(&variant
);
588 CPPUNIT_ASSERT(variant
.GetChar() == 'A');
591 any
= wxAny(vLongLong
);
592 CPPUNIT_ASSERT(any
== wxLL(0xAABBBBCCCC));
593 res
= any
.GetAs(&variant
);
595 CPPUNIT_ASSERT(variant
.GetType() == "longlong");
596 CPPUNIT_ASSERT(variant
.GetLongLong() == wxLongLong(wxLL(0xAABBBBCCCC)));
598 #if LONG_MAX == wxINT64_MAX
599 // As a sanity check, test that wxVariant of type 'long' converts
600 // seamlessly to 'longlong' (on some 64-bit systems)
602 res
= any
.GetAs(&variant
);
603 CPPUNIT_ASSERT(variant
.GetLongLong() == wxLongLong(wxLL(0xAABBBBCCCC)));
606 any
= wxAny(vULongLong
);
607 CPPUNIT_ASSERT(any
== wxLL(123456));
608 res
= any
.GetAs(&variant
);
610 CPPUNIT_ASSERT(variant
.GetType() == "ulonglong");
611 CPPUNIT_ASSERT(variant
.GetULongLong() == wxULongLong(wxULL(123456)));
614 // Cannot test equality for the rest, just test that they convert
616 any
= wxAny(vArrayString
);
617 res
= any
.GetAs(&variant
);
619 wxArrayString arrstr2
= variant
.GetArrayString();
620 CPPUNIT_ASSERT(arrstr2
== arrstr
);
622 any
= m_testDateTime
;
623 CPPUNIT_ASSERT(wxANY_AS(any
, wxDateTime
) == m_testDateTime
);
624 any
= wxAny(vDateTime
);
625 CPPUNIT_ASSERT(wxANY_AS(any
, wxDateTime
) == m_testDateTime
);
626 res
= any
.GetAs(&variant
);
628 CPPUNIT_ASSERT(variant
== m_testDateTime
);
630 any
= wxAny(vVoidPtr
);
631 res
= any
.GetAs(&variant
);
633 CPPUNIT_ASSERT(variant
.GetVoidPtr() == dummyVoidPointer
);
636 CPPUNIT_ASSERT(wxANY_CHECK_TYPE(any
, wxAnyList
));
637 wxAnyList anyList
= wxANY_AS(any
, wxAnyList
);
638 CPPUNIT_ASSERT(anyList
.GetCount() == 2);
639 CPPUNIT_ASSERT(wxANY_AS((*anyList
[0]), int) == 15);
640 CPPUNIT_ASSERT(wxANY_AS((*anyList
[1]), wxString
) == "abc");
641 res
= any
.GetAs(&variant
);
643 CPPUNIT_ASSERT(variant
.GetType() == "list");
644 CPPUNIT_ASSERT(variant
.GetCount() == 2);
645 CPPUNIT_ASSERT(variant
[0].GetLong() == 15);
646 CPPUNIT_ASSERT(variant
[1].GetString() == "abc");
648 any
= wxAny(vCustomType
);
649 CPPUNIT_ASSERT(wxANY_CHECK_TYPE(any
, wxVariantData
*));
650 res
= any
.GetAs(&variant
);
652 CPPUNIT_ASSERT(variant
.GetType() == "MyClass");
654 #endif // wxUSE_VARIANT
658 class wxAnyValueTypeImpl
<MyClass
> :
659 public wxAnyValueTypeImplBase
<MyClass
>
661 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl
<MyClass
>)
663 wxAnyValueTypeImpl() :
664 wxAnyValueTypeImplBase
<MyClass
>() { }
665 virtual ~wxAnyValueTypeImpl() { }
667 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
668 wxAnyValueType
* dstType
,
669 wxAnyValueBuffer
& dst
) const
671 MyClass value
= GetValue(src
);
673 if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType
, wxString
) )
675 wxString s
= value
.ToString();
676 wxAnyValueTypeImpl
<wxString
>::SetValue(s
, dst
);
686 // Following must be placed somewhere in your source code
687 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl
<MyClass
>)
689 void wxAnyTestCase::CustomTemplateSpecialization()
691 // Do only a minimal CheckType() test, as dynamic type conversion already
695 wxAny any
= myObject
;
697 CPPUNIT_ASSERT( wxANY_CHECK_TYPE(any
, MyClass
) );
698 MyClass myObject2
= wxANY_AS(any
, MyClass
);
699 wxUnusedVar(myObject2
);
702 res
= any
.GetAs(&str
);
704 CPPUNIT_ASSERT_EQUAL(str
, myObject
.ToString());
707 void wxAnyTestCase::Misc()
709 // Do some (inplace) allocation sanity checks
712 // Do it inside a scope so we can easily test instance count
714 MyClass
myObject(15);
715 wxAny any
= myObject
;
717 // There must be two instances - first in myObject,
718 // and second copied in any.
719 CPPUNIT_ASSERT_EQUAL(gs_myClassInstances
.size(), 2);
721 // Check that it is allocated in-place, as supposed
722 if ( sizeof(MyClass
) <= WX_ANY_VALUE_BUFFER_SIZE
)
724 // Memory block of the instance second must be inside the any
725 size_t anyBegin
= reinterpret_cast<size_t>(&any
);
726 size_t anyEnd
= anyBegin
+ sizeof(wxAny
);
727 size_t pos
= reinterpret_cast<size_t>(gs_myClassInstances
[1]);
728 CPPUNIT_ASSERT( pos
>= anyBegin
);
729 CPPUNIT_ASSERT( pos
< anyEnd
);
733 CPPUNIT_ASSERT( wxANY_AS(any2
, MyClass
).GetValue() == 15 );
736 // Make sure allocations and deallocations match
737 CPPUNIT_ASSERT_EQUAL(gs_myClassInstances
.size(), 0);