+//
+// wxVariant <-> wxAny conversion code
+//
+#if wxUSE_ANY
+
+#include "wx/any.h"
+
+// In order to convert wxAny to wxVariant, we need to be able to associate
+// wxAnyValueType with a wxVariantData factory function.
+typedef wxVariantData* (*wxVariantDataFactory)(const wxAny& any);
+
+// Actual Any-to-Variant registration must be postponed to a time when all
+// global variables have been initialized. Hence this arrangement.
+// wxAnyToVariantRegistration instances are kept in global scope and
+// wxAnyValueTypeGlobals in any.cpp will use their data when the time is
+// right.
+class WXDLLIMPEXP_BASE wxAnyToVariantRegistration
+{
+public:
+ wxAnyToVariantRegistration(wxVariantDataFactory factory);
+ virtual ~wxAnyToVariantRegistration();
+
+ virtual wxAnyValueType* GetAssociatedType() = 0;
+ wxVariantDataFactory GetFactory() const { return m_factory; }
+private:
+ wxVariantDataFactory m_factory;
+};
+
+template<typename T>
+class wxAnyToVariantRegistrationImpl : public wxAnyToVariantRegistration
+{
+public:
+ wxAnyToVariantRegistrationImpl(wxVariantDataFactory factory)
+ : wxAnyToVariantRegistration(factory)
+ {
+ }
+
+ virtual wxAnyValueType* GetAssociatedType()
+ {
+ return wxAnyValueTypeImpl<T>::GetInstance();
+ }
+private:
+};
+
+#define DECLARE_WXANY_CONVERSION() \
+virtual bool GetAsAny(wxAny* any) const; \
+static wxVariantData* VariantDataFactory(const wxAny& any);
+
+#define _REGISTER_WXANY_CONVERSION(T, CLASSNAME, FUNC) \
+static wxAnyToVariantRegistrationImpl<T> \
+ gs_##CLASSNAME##AnyToVariantRegistration = \
+ wxAnyToVariantRegistrationImpl<T>(&FUNC);
+
+#define REGISTER_WXANY_CONVERSION(T, CLASSNAME) \
+_REGISTER_WXANY_CONVERSION(T, CLASSNAME, CLASSNAME::VariantDataFactory)
+
+#define IMPLEMENT_TRIVIAL_WXANY_CONVERSION(T, CLASSNAME) \
+bool CLASSNAME::GetAsAny(wxAny* any) const \
+{ \
+ *any = m_value; \
+ return true; \
+} \
+wxVariantData* CLASSNAME::VariantDataFactory(const wxAny& any) \
+{ \
+ return new CLASSNAME(wxANY_AS(any, T)); \
+} \
+REGISTER_WXANY_CONVERSION(T, CLASSNAME)
+
+#else // if !wxUSE_ANY
+
+#define DECLARE_WXANY_CONVERSION()
+#define REGISTER_WXANY_CONVERSION(T, CLASSNAME)
+#define IMPLEMENT_TRIVIAL_WXANY_CONVERSION(T, CLASSNAME)
+
+#endif // wxUSE_ANY/!wxUSE_ANY
+
+
+#define DECLARE_VARIANT_OBJECT(classname) \
+ DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
+
+#define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
+expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
+expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
+
+#define IMPLEMENT_VARIANT_OBJECT(classname) \
+ IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
+
+#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
+class classname##VariantData: public wxVariantData \
+{ \
+public:\
+ classname##VariantData() {} \
+ classname##VariantData( const classname &value ) { m_value = value; } \
+\
+ classname &GetValue() { return m_value; } \
+\
+ virtual bool Eq(wxVariantData& data) const; \
+\
+ virtual wxString GetType() const; \
+ virtual wxClassInfo* GetValueClassInfo(); \
+\
+ virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
+\
+ DECLARE_WXANY_CONVERSION() \
+protected:\
+ classname m_value; \
+};\
+\
+wxString classname##VariantData::GetType() const\
+{\
+ return m_value.GetClassInfo()->GetClassName();\
+}\
+\
+wxClassInfo* classname##VariantData::GetValueClassInfo()\
+{\
+ return m_value.GetClassInfo();\
+}\
+\
+expdecl classname& operator << ( classname &value, const wxVariant &variant )\
+{\
+ wxASSERT( variant.GetType() == #classname );\
+ \
+ classname##VariantData *data = (classname##VariantData*) variant.GetData();\
+ value = data->GetValue();\
+ return value;\
+}\
+\
+expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
+{\
+ classname##VariantData *data = new classname##VariantData( value );\
+ variant.SetData( data );\
+ return variant;\
+} \
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(classname, classname##VariantData)
+
+// implements a wxVariantData-derived class using for the Eq() method the
+// operator== which must have been provided by "classname"
+#define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
+IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
+\
+bool classname##VariantData::Eq(wxVariantData& data) const \
+{\
+ wxASSERT( GetType() == data.GetType() );\
+\
+ classname##VariantData & otherData = (classname##VariantData &) data;\
+\
+ return otherData.m_value == m_value;\
+}\
+
+
+// implements a wxVariantData-derived class using for the Eq() method a shallow
+// comparison (through wxObject::IsSameAs function)
+#define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
+ IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
+#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
+IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
+\
+bool classname##VariantData::Eq(wxVariantData& data) const \
+{\
+ wxASSERT( GetType() == data.GetType() );\
+\
+ classname##VariantData & otherData = (classname##VariantData &) data;\
+\
+ return (otherData.m_value.IsSameAs(m_value));\
+}\
+
+
+// Since we want type safety wxVariant we need to fetch and dynamic_cast
+// in a seemingly safe way so the compiler can check, so we define
+// a dynamic_cast /wxDynamicCast analogue.
+
+#define wxGetVariantCast(var,classname) \
+ ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
+ var.GetWxObjectPtr() : NULL));
+
+// Replacement for using wxDynamicCast on a wxVariantData object
+#ifndef wxNO_RTTI
+ #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)