1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxAny class
4 // Author: Jaakko Salli
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
19 #include "wx/string.h"
20 #include "wx/meta/movable.h"
21 #include "wx/meta/if.h"
22 #include "wx/typeinfo.h"
25 // Size of the wxAny value buffer.
28 WX_ANY_VALUE_BUFFER_SIZE
= 16
31 union wxAnyValueBuffer
38 long double m_longDouble
;
39 void ( *m_funcPtr
)(void);
40 void ( wxAnyValueBuffer::*m_mFuncPtr
)(void);
44 wxByte m_buffer
[WX_ANY_VALUE_BUFFER_SIZE
];
48 // wxAnyValueType is base class for value type functionality for C++ data
49 // types used with wxAny. Usually the default template (wxAnyValueTypeImpl<>)
50 // will create a satisfactory wxAnyValueType implementation for a data type.
52 class WXDLLIMPEXP_BASE wxAnyValueType
54 WX_DECLARE_ABSTRACT_TYPEINFO(wxAnyValueType
)
64 virtual ~wxAnyValueType()
69 This function is used for internal type matching.
71 virtual bool IsSameType(const wxAnyValueType
* otherType
) const = 0;
74 This function is called every time the data in wxAny
75 buffer needs to be freed.
77 virtual void DeleteValue(wxAnyValueBuffer
& buf
) const = 0;
80 Implement this for buffer-to-buffer copy.
83 This is the source data buffer.
86 This is the destination data buffer that is in either
87 uninitialized or freed state.
89 virtual void CopyBuffer(const wxAnyValueBuffer
& src
,
90 wxAnyValueBuffer
& dst
) const = 0;
93 Convert value into buffer of different type. Return false if
96 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
97 wxAnyValueType
* dstType
,
98 wxAnyValueBuffer
& dst
) const = 0;
101 Use this template function for checking if wxAnyValueType represents
102 a specific C++ data type.
104 @remarks This template function does not work on some older compilers
105 (such as Visual C++ 6.0). For full compiler ccompatibility
106 please use wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) macro
109 @see wxAny::CheckType()
111 // FIXME-VC6: remove this hack when VC6 is no longer supported
112 template <typename T
>
113 bool CheckType(T
* reserved
= NULL
);
118 // This method of checking the type is compatible with VC6
119 #define wxANY_VALUE_TYPE_CHECK_TYPE(valueTypePtr, T) \
120 wxAnyValueTypeImpl<T>::IsSameClass(valueTypePtr)
124 Helper macro for defining user value types.
126 Even though C++ RTTI would be fully available to use, we'd have to to
127 facilitate sub-type system which allows, for instance, wxAny with
128 signed short '15' to be treated equal to wxAny with signed long long '15'.
129 Having sm_instance is important here.
131 #define WX_DECLARE_ANY_VALUE_TYPE(CLS) \
132 friend class wxAny; \
133 WX_DECLARE_TYPEINFO_INLINE(CLS) \
135 static bool IsSameClass(const wxAnyValueType* otherType) \
137 return wxTypeId(*sm_instance) == wxTypeId(*otherType); \
139 virtual bool IsSameType(const wxAnyValueType* otherType) const \
141 return IsSameClass(otherType); \
144 static CLS* sm_instance; \
146 static wxAnyValueType* GetInstance() \
148 return sm_instance; \
152 #define WX_IMPLEMENT_ANY_VALUE_TYPE(CLS) \
153 CLS* CLS::sm_instance = new CLS();
157 // "non dll-interface class 'xxx' used as base interface
158 #pragma warning (push)
159 #pragma warning (disable:4275)
163 Following are helper classes for the wxAnyValueTypeImplBase.
169 class wxAnyValueTypeOpsMovable
172 static void DeleteValue(wxAnyValueBuffer
& buf
)
177 static void SetValue(const T
& value
,
178 wxAnyValueBuffer
& buf
)
180 memcpy(buf
.m_buffer
, &value
, sizeof(T
));
183 static const T
& GetValue(const wxAnyValueBuffer
& buf
)
185 // Breaking this code into two lines should supress
186 // GCC's 'type-punned pointer will break strict-aliasing rules'
188 const T
* value
= reinterpret_cast<const T
*>(&buf
.m_buffer
[0]);
195 class wxAnyValueTypeOpsGeneric
198 template<typename T2
>
202 DataHolder(const T2
& value
)
206 virtual ~DataHolder() { }
210 wxDECLARE_NO_COPY_CLASS(DataHolder
);
213 static void DeleteValue(wxAnyValueBuffer
& buf
)
215 DataHolder
<T
>* holder
= static_cast<DataHolder
<T
>*>(buf
.m_ptr
);
219 static void SetValue(const T
& value
,
220 wxAnyValueBuffer
& buf
)
222 DataHolder
<T
>* holder
= new DataHolder
<T
>(value
);
226 static const T
& GetValue(const wxAnyValueBuffer
& buf
)
228 DataHolder
<T
>* holder
= static_cast<DataHolder
<T
>*>(buf
.m_ptr
);
229 return holder
->m_value
;
233 } // namespace wxPrivate
237 Intermediate template for the generic value type implementation.
238 We can derive from this same value type for multiple actual types
239 (for instance, we can have wxAnyValueTypeImplInt for all signed
240 integer types), and also easily implement specialized templates
241 with specific dynamic type conversion.
244 class wxAnyValueTypeImplBase
: public wxAnyValueType
246 typedef typename wxIf
< wxIsMovable
<T
>::value
&&
247 sizeof(T
) <= WX_ANY_VALUE_BUFFER_SIZE
,
248 wxPrivate::wxAnyValueTypeOpsMovable
<T
>,
249 wxPrivate::wxAnyValueTypeOpsGeneric
<T
> >::value
253 wxAnyValueTypeImplBase() : wxAnyValueType() { }
254 virtual ~wxAnyValueTypeImplBase() { }
256 virtual void DeleteValue(wxAnyValueBuffer
& buf
) const
258 Ops::DeleteValue(buf
);
261 virtual void CopyBuffer(const wxAnyValueBuffer
& src
,
262 wxAnyValueBuffer
& dst
) const
264 Ops::SetValue(Ops::GetValue(src
), dst
);
268 It is important to reimplement this in any specialized template
269 classes that inherit from wxAnyValueTypeImplBase.
271 static void SetValue(const T
& value
,
272 wxAnyValueBuffer
& buf
)
274 Ops::SetValue(value
, buf
);
278 It is important to reimplement this in any specialized template
279 classes that inherit from wxAnyValueTypeImplBase.
281 static const T
& GetValue(const wxAnyValueBuffer
& buf
)
283 return Ops::GetValue(buf
);
289 Generic value type template. Note that bulk of the implementation
290 resides in wxAnyValueTypeImplBase.
293 class wxAnyValueTypeImpl
: public wxAnyValueTypeImplBase
<T
>
295 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl
<T
>)
297 wxAnyValueTypeImpl() : wxAnyValueTypeImplBase
<T
>() { }
298 virtual ~wxAnyValueTypeImpl() { }
300 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
301 wxAnyValueType
* dstType
,
302 wxAnyValueBuffer
& dst
) const
305 wxUnusedVar(dstType
);
312 wxAnyValueTypeImpl
<T
>* wxAnyValueTypeImpl
<T
>::sm_instance
=
313 new wxAnyValueTypeImpl
<T
>();
317 // Helper macro for using same base value type implementation for multiple
318 // actual C++ data types.
320 #define WX_ANY_DEFINE_SUB_TYPE(T, CLSTYPE) \
322 class wxAnyValueTypeImpl<T> : public wxAnyValueTypeImpl##CLSTYPE \
324 typedef wxAnyBase##CLSTYPE##Type UseDataType; \
326 wxAnyValueTypeImpl() : wxAnyValueTypeImpl##CLSTYPE() { } \
327 virtual ~wxAnyValueTypeImpl() { } \
328 static void SetValue(const T& value, wxAnyValueBuffer& buf) \
330 void* voidPtr = reinterpret_cast<void*>(&buf.m_buffer[0]); \
331 UseDataType* dptr = reinterpret_cast<UseDataType*>(voidPtr); \
332 *dptr = static_cast<UseDataType>(value); \
334 static T GetValue(const wxAnyValueBuffer& buf) \
336 const void* voidPtr = \
337 reinterpret_cast<const void*>(&buf.m_buffer[0]); \
338 const UseDataType* sptr = \
339 reinterpret_cast<const UseDataType*>(voidPtr); \
340 return static_cast<T>(*sptr); \
346 // Integer value types
350 typedef wxLongLong_t wxAnyBaseIntType
;
351 typedef wxULongLong_t wxAnyBaseUintType
;
353 typedef long wxAnyBaseIntType
;
354 typedef unsigned long wxAnyBaseUintType
;
358 class WXDLLIMPEXP_BASE wxAnyValueTypeImplInt
:
359 public wxAnyValueTypeImplBase
<wxAnyBaseIntType
>
361 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplInt
)
363 wxAnyValueTypeImplInt() :
364 wxAnyValueTypeImplBase
<wxAnyBaseIntType
>() { }
365 virtual ~wxAnyValueTypeImplInt() { }
367 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
368 wxAnyValueType
* dstType
,
369 wxAnyValueBuffer
& dst
) const;
373 class WXDLLIMPEXP_BASE wxAnyValueTypeImplUint
:
374 public wxAnyValueTypeImplBase
<wxAnyBaseUintType
>
376 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplUint
)
378 wxAnyValueTypeImplUint() :
379 wxAnyValueTypeImplBase
<wxAnyBaseUintType
>() { }
380 virtual ~wxAnyValueTypeImplUint() { }
382 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
383 wxAnyValueType
* dstType
,
384 wxAnyValueBuffer
& dst
) const;
388 WX_ANY_DEFINE_SUB_TYPE(signed long, Int
)
389 WX_ANY_DEFINE_SUB_TYPE(signed int, Int
)
390 WX_ANY_DEFINE_SUB_TYPE(signed short, Int
)
391 WX_ANY_DEFINE_SUB_TYPE(signed char, Int
)
393 WX_ANY_DEFINE_SUB_TYPE(wxLongLong_t
, Int
)
396 WX_ANY_DEFINE_SUB_TYPE(unsigned long, Uint
)
397 WX_ANY_DEFINE_SUB_TYPE(unsigned int, Uint
)
398 WX_ANY_DEFINE_SUB_TYPE(unsigned short, Uint
)
399 WX_ANY_DEFINE_SUB_TYPE(unsigned char, Uint
)
401 WX_ANY_DEFINE_SUB_TYPE(wxULongLong_t
, Uint
)
408 class WXDLLIMPEXP_BASE wxAnyValueTypeImplString
:
409 public wxAnyValueTypeImplBase
<wxString
>
411 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplString
)
413 wxAnyValueTypeImplString() :
414 wxAnyValueTypeImplBase
<wxString
>() { }
415 virtual ~wxAnyValueTypeImplString() { }
418 Convert value into buffer of different type. Return false if
421 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
422 wxAnyValueType
* dstType
,
423 wxAnyValueBuffer
& dst
) const;
428 class wxAnyValueTypeImpl
<wxString
> : public wxAnyValueTypeImplString
431 wxAnyValueTypeImpl() : wxAnyValueTypeImplString() { }
432 virtual ~wxAnyValueTypeImpl() { }
440 class WXDLLIMPEXP_BASE wxAnyValueTypeImpl
<bool> :
441 public wxAnyValueTypeImplBase
<bool>
443 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl
<bool>)
445 wxAnyValueTypeImpl() :
446 wxAnyValueTypeImplBase
<bool>() { }
447 virtual ~wxAnyValueTypeImpl() { }
449 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
450 wxAnyValueType
* dstType
,
451 wxAnyValueBuffer
& dst
) const;
455 // Floating point value type
457 class WXDLLIMPEXP_BASE wxAnyValueTypeImplDouble
:
458 public wxAnyValueTypeImplBase
<double>
460 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImplDouble
)
462 wxAnyValueTypeImplDouble() :
463 wxAnyValueTypeImplBase
<double>() { }
464 virtual ~wxAnyValueTypeImplDouble() { }
466 virtual bool ConvertValue(const wxAnyValueBuffer
& src
,
467 wxAnyValueType
* dstType
,
468 wxAnyValueBuffer
& dst
) const;
471 // WX_ANY_DEFINE_SUB_TYPE requires this
472 typedef double wxAnyBaseDoubleType
;
474 WX_ANY_DEFINE_SUB_TYPE(float, Double
)
475 WX_ANY_DEFINE_SUB_TYPE(double, Double
)
479 // Defines a dummy wxAnyValueTypeImpl<> with given export
480 // declaration. This is needed if a class is used with
481 // wxAny in both user shared library and application.
483 #define wxDECLARE_ANY_TYPE(CLS, DECL) \
485 class DECL wxAnyValueTypeImpl<CLS> : \
486 public wxAnyValueTypeImplBase<CLS> \
488 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<CLS>) \
490 wxAnyValueTypeImpl() : \
491 wxAnyValueTypeImplBase<CLS>() { } \
492 virtual ~wxAnyValueTypeImpl() { } \
494 virtual bool ConvertValue(const wxAnyValueBuffer& src, \
495 wxAnyValueType* dstType, \
496 wxAnyValueBuffer& dst) const \
499 wxUnusedVar(dstType); \
506 // Make sure some of wx's own types get the right wxAnyValueType export
507 // (this is needed only for types that are referred to from wxBase.
508 // currently we may not use any of these types from there, but let's
509 // use the macro on at least one to make sure it compiles since we can't
510 // really test it properly in unittests since a separate DLL would
513 #include "wx/datetime.h"
514 wxDECLARE_ANY_TYPE(wxDateTime
, WXDLLIMPEXP_BASE
)
517 //#include "wx/object.h"
518 //wxDECLARE_ANY_TYPE(wxObject*, WXDLLIMPEXP_BASE)
520 //#include "wx/arrstr.h"
521 //wxDECLARE_ANY_TYPE(wxArrayString, WXDLLIMPEXP_BASE)
526 // Re-enable useless VC6 warnings
527 #pragma warning (pop)
532 Let's define a discrete Null value so we don't have to really
533 ever check if wxAny.m_type pointer is NULL or not. This is an
534 optimization, mostly. Implementation of this value type is
535 "hidden" in the source file.
537 extern WXDLLIMPEXP_DATA_BASE(wxAnyValueType
*) wxAnyNullValueType
;
541 // We need to implement custom signed/unsigned int equals operators
542 // for signed/unsigned (eg. wxAny(128UL) == 128L) comparisons to work.
543 #define WXANY_IMPLEMENT_INT_EQ_OP(TS, TUS) \
544 bool operator==(TS value) const \
546 if ( wxAnyValueTypeImpl<TS>::IsSameClass(m_type) ) \
547 return (value == static_cast<TS> \
548 (wxAnyValueTypeImpl<TS>::GetValue(m_buffer))); \
549 if ( wxAnyValueTypeImpl<TUS>::IsSameClass(m_type) ) \
550 return (value == static_cast<TS> \
551 (wxAnyValueTypeImpl<TUS>::GetValue(m_buffer))); \
554 bool operator==(TUS value) const \
556 if ( wxAnyValueTypeImpl<TUS>::IsSameClass(m_type) ) \
557 return (value == static_cast<TUS> \
558 (wxAnyValueTypeImpl<TUS>::GetValue(m_buffer))); \
559 if ( wxAnyValueTypeImpl<TS>::IsSameClass(m_type) ) \
560 return (value == static_cast<TUS> \
561 (wxAnyValueTypeImpl<TS>::GetValue(m_buffer))); \
567 // The wxAny class represents a container for any type. A variant's value
568 // can be changed at run time, possibly to a different type of value.
570 // As standard, wxAny can store value of almost any type, in a fairly
571 // optimal manner even.
581 m_type
= wxAnyNullValueType
;
589 m_type
->DeleteValue(m_buffer
);
594 Various constructors.
596 wxAny(const char* value
)
598 m_type
= wxAnyNullValueType
;
599 Assign(wxString(value
));
601 wxAny(const wchar_t* value
)
603 m_type
= wxAnyNullValueType
;
604 Assign(wxString(value
));
607 wxAny(const wxAny
& any
)
609 m_type
= wxAnyNullValueType
;
614 wxAny(const T
& value
)
616 m_type
= wxAnyValueTypeImpl
<T
>::sm_instance
;
617 wxAnyValueTypeImpl
<T
>::SetValue(value
, m_buffer
);
622 Use this template function for checking if this wxAny holds
623 a specific C++ data type.
625 @remarks This template function does not work on some older compilers
626 (such as Visual C++ 6.0). For full compiler ccompatibility
627 please use wxANY_CHECK_TYPE(any, T) macro instead.
629 @see wxAnyValueType::CheckType()
631 // FIXME-VC6: remove this hack when VC6 is no longer supported
632 template <typename T
>
633 bool CheckType(T
* = NULL
)
635 return m_type
->CheckType
<T
>();
639 Returns the value type as wxAnyValueType instance.
641 @remarks You cannot reliably test whether two wxAnys are of
642 same value type by simply comparing return values
643 of wxAny::GetType(). Instead use
644 wxAnyValueType::CheckType<T>() template function.
646 const wxAnyValueType
* GetType() const
652 Tests if wxAny is null (that is, whether there is data).
656 return (m_type
== wxAnyNullValueType
);
660 Makes wxAny null (that is, clears it).
664 m_type
->DeleteValue(m_buffer
);
665 m_type
= wxAnyNullValueType
;
670 Assignment operators.
672 wxAny
& operator=(const wxAny
&any
)
680 wxAny
& operator=(const T
&value
)
682 m_type
->DeleteValue(m_buffer
);
683 m_type
= wxAnyValueTypeImpl
<T
>::sm_instance
;
684 wxAnyValueTypeImpl
<T
>::SetValue(value
, m_buffer
);
688 wxAny
& operator=(const char* value
)
689 { Assign(wxString(value
)); return *this; }
690 wxAny
& operator=(const wchar_t* value
)
691 { Assign(wxString(value
)); return *this; }
698 bool operator==(const wxString
& value
) const
700 if ( !wxAnyValueTypeImpl
<wxString
>::IsSameClass(m_type
) )
704 static_cast<wxString
>
705 (wxAnyValueTypeImpl
<wxString
>::GetValue(m_buffer
));
708 bool operator==(const char* value
) const
709 { return (*this) == wxString(value
); }
710 bool operator==(const wchar_t* value
) const
711 { return (*this) == wxString(value
); }
714 // We need to implement custom signed/unsigned int equals operators
715 // for signed/unsigned (eg. wxAny(128UL) == 128L) comparisons to work.
716 WXANY_IMPLEMENT_INT_EQ_OP(signed char, unsigned char)
717 WXANY_IMPLEMENT_INT_EQ_OP(signed short, unsigned short)
718 WXANY_IMPLEMENT_INT_EQ_OP(signed int, unsigned int)
719 WXANY_IMPLEMENT_INT_EQ_OP(signed long, unsigned long)
721 WXANY_IMPLEMENT_INT_EQ_OP(wxLongLong_t
, wxULongLong_t
)
724 bool operator==(float value
) const
726 if ( !wxAnyValueTypeImpl
<float>::IsSameClass(m_type
) )
731 (wxAnyValueTypeImpl
<float>::GetValue(m_buffer
));
734 bool operator==(double value
) const
736 if ( !wxAnyValueTypeImpl
<double>::IsSameClass(m_type
) )
741 (wxAnyValueTypeImpl
<double>::GetValue(m_buffer
));
744 bool operator==(bool value
) const
746 if ( !wxAnyValueTypeImpl
<bool>::IsSameClass(m_type
) )
749 return value
== (wxAnyValueTypeImpl
<bool>::GetValue(m_buffer
));
756 Inequality operators (implement as template).
759 bool operator!=(const T
& value
) const
760 { return !((*this) == value
); }
764 This template function converts wxAny into given type. No dynamic
765 conversion is performed, so if the type is incorrect an assertion
766 failure will occur in debug builds, and a bogus value is returned
769 @remarks This template function does not work on some older compilers
770 (such as Visual C++ 6.0). For full compiler compatibility
771 please use wxANY_AS(any, T) macro instead.
773 // FIXME-VC6: remove this hack when VC6 is no longer supported
775 T
As(T
* = NULL
) const
777 if ( !wxAnyValueTypeImpl
<T
>::IsSameClass(m_type
) )
779 wxFAIL_MSG("Incorrect or non-convertible data type");
782 return static_cast<T
>(wxAnyValueTypeImpl
<T
>::GetValue(m_buffer
));
786 Template function that etrieves and converts the value of this
787 variant to the type that T* value is.
789 @return Returns @true if conversion was succesfull.
792 bool GetAs(T
* value
) const
794 if ( !wxAnyValueTypeImpl
<T
>::IsSameClass(m_type
) )
796 wxAnyValueType
* otherType
=
797 wxAnyValueTypeImpl
<T
>::sm_instance
;
798 wxAnyValueBuffer temp_buf
;
800 if ( !m_type
->ConvertValue(m_buffer
, otherType
, temp_buf
) )
804 static_cast<T
>(wxAnyValueTypeImpl
<T
>::GetValue(temp_buf
));
805 otherType
->DeleteValue(temp_buf
);
809 *value
= static_cast<T
>(wxAnyValueTypeImpl
<T
>::GetValue(m_buffer
));
814 // Assignment functions
815 void AssignAny(const wxAny
& any
)
817 // Must delete value - CopyBuffer() never does that
818 m_type
->DeleteValue(m_buffer
);
820 wxAnyValueType
* newType
= any
.m_type
;
822 if ( !newType
->IsSameType(m_type
) )
825 newType
->CopyBuffer(any
.m_buffer
, m_buffer
);
829 void Assign(const T
&value
)
831 m_type
->DeleteValue(m_buffer
);
832 m_type
= wxAnyValueTypeImpl
<T
>::sm_instance
;
833 wxAnyValueTypeImpl
<T
>::SetValue(value
, m_buffer
);
837 wxAnyValueBuffer m_buffer
;
838 wxAnyValueType
* m_type
;
843 // This method of checking the type is compatible with VC6
844 #define wxANY_CHECK_TYPE(any, T) \
845 wxANY_VALUE_TYPE_CHECK_TYPE(any.GetType(), T)
849 // This method of getting the value is compatible with VC6
850 #define wxANY_AS(any, T) \
851 any.As(static_cast<T*>(NULL))
855 inline bool wxAnyValueType::CheckType(T
* reserved
)
857 wxUnusedVar(reserved
);
858 return wxAnyValueTypeImpl
<T
>::IsSameClass(this);