]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/variant.cpp
Clip drawing in wxRendererGeneric::DrawHeaderButtonContents().
[wxWidgets.git] / src / common / variant.cpp
index 12a9ded6d2bc1100fdf3980fdd66d218a1c2e0cb..1820f4b8d5ef41912126fc9a2e31fc4b0ec9b387 100644 (file)
@@ -193,6 +193,52 @@ bool wxVariant::IsValueKindOf(const wxClassInfo* type) const
     return info ? info->IsKindOf(type) : false ;
 }
 
+// -----------------------------------------------------------------
+// wxVariant <-> wxAny conversion code
+// -----------------------------------------------------------------
+
+#if wxUSE_ANY
+
+wxAnyToVariantRegistration::
+    wxAnyToVariantRegistration(wxVariantDataFactory factory)
+        : m_factory(factory)
+{
+    wxPreRegisterAnyToVariant(this);
+}
+
+wxAnyToVariantRegistration::~wxAnyToVariantRegistration()
+{
+}
+
+wxVariant::wxVariant(const wxAny& any)
+    : wxObject()
+{
+    wxVariant variant;
+    if ( !any.GetAs(&variant) )
+    {
+        wxFAIL_MSG("wxAny of this type cannot be converted to wxVariant");
+        return;
+    }
+
+    *this = variant;
+}
+
+wxAny wxVariant::GetAny() const
+{
+    if ( IsNull() )
+        return wxAny();
+
+    wxAny any;
+    wxVariantData* data = GetData();
+
+    if ( data->GetAsAny(&any) )
+        return any;
+
+    // If everything else fails, wrap the whole wxVariantData
+    return wxAny(data);
+}
+
+#endif // wxUSE_ANY
 
 // -----------------------------------------------------------------
 // wxVariantDataLong
@@ -224,10 +270,27 @@ public:
 
     virtual wxString GetType() const { return wxT("long"); }
 
+    // Since wxAny does not have separate type for integers shorter than
+    // longlong, we do not usually implement wxVariant->wxAny conversion
+    // here (but in wxVariantDataLongLong instead).
+#ifndef wxLongLong_t
+    DECLARE_WXANY_CONVERSION()
+#else
+    bool GetAsAny(wxAny* any) const
+    {
+        *any = m_value;
+        return true;
+    }
+#endif
+
 protected:
     long m_value;
 };
 
+#ifndef wxLongLong_t
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(long, wxVariantDataLong)
+#endif
+
 bool wxVariantDataLong::Eq(wxVariantData& data) const
 {
     wxASSERT_MSG( (data.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
@@ -373,10 +436,14 @@ public:
     virtual wxString GetType() const { return wxT("double"); }
 
     wxVariantData* Clone() const { return new wxVariantDoubleData(m_value); }
+
+    DECLARE_WXANY_CONVERSION()
 protected:
     double m_value;
 };
 
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(double, wxVariantDoubleData)
+
 bool wxVariantDoubleData::Eq(wxVariantData& data) const
 {
     wxASSERT_MSG( (data.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
@@ -509,10 +576,14 @@ public:
     virtual wxString GetType() const { return wxT("bool"); }
 
     wxVariantData* Clone() const { return new wxVariantDataBool(m_value); }
+
+    DECLARE_WXANY_CONVERSION()
 protected:
     bool m_value;
 };
 
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(bool, wxVariantDataBool)
+
 bool wxVariantDataBool::Eq(wxVariantData& data) const
 {
     wxASSERT_MSG( (data.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
@@ -646,10 +717,13 @@ public:
     virtual wxString GetType() const { return wxT("char"); }
     wxVariantData* Clone() const { return new wxVariantDataChar(m_value); }
 
+    DECLARE_WXANY_CONVERSION()
 protected:
     wxUniChar m_value;
 };
 
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxUniChar, wxVariantDataChar)
+
 bool wxVariantDataChar::Eq(wxVariantData& data) const
 {
     wxASSERT_MSG( (data.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
@@ -798,10 +872,33 @@ public:
     virtual wxString GetType() const { return wxT("string"); }
     wxVariantData* Clone() const { return new wxVariantDataString(m_value); }
 
+    DECLARE_WXANY_CONVERSION()
 protected:
     wxString m_value;
 };
 
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxString, wxVariantDataString)
+
+#if wxUSE_ANY
+// This allows converting string literal wxAnys to string variants
+wxVariantData* wxVariantDataFromConstCharPAny(const wxAny& any)
+{
+    return new wxVariantDataString(wxANY_AS(any, const char*));
+}
+
+wxVariantData* wxVariantDataFromConstWchar_tPAny(const wxAny& any)
+{
+    return new wxVariantDataString(wxANY_AS(any, const wchar_t*));
+}
+
+_REGISTER_WXANY_CONVERSION(const char*,
+                           ConstCharP,
+                           wxVariantDataFromConstCharPAny)
+_REGISTER_WXANY_CONVERSION(const wchar_t*,
+                           ConstWchar_tP,
+                           wxVariantDataFromConstWchar_tPAny)
+#endif
+
 bool wxVariantDataString::Eq(wxVariantData& data) const
 {
     wxASSERT_MSG( (data.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
@@ -954,10 +1051,13 @@ public:
 
     virtual wxClassInfo* GetValueClassInfo();
 
+    DECLARE_WXANY_CONVERSION()
 protected:
     wxObject* m_value;
 };
 
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxObject*, wxVariantDataWxObjectPtr)
+
 bool wxVariantDataWxObjectPtr::Eq(wxVariantData& data) const
 {
     wxASSERT_MSG( data.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
@@ -1073,10 +1173,13 @@ public:
     virtual wxString GetType() const { return wxT("void*"); }
     virtual wxVariantData* Clone() const { return new wxVariantDataVoidPtr(m_value); }
 
+    DECLARE_WXANY_CONVERSION()
 protected:
     void* m_value;
 };
 
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(void*, wxVariantDataVoidPtr)
+
 bool wxVariantDataVoidPtr::Eq(wxVariantData& data) const
 {
     wxASSERT_MSG( data.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
@@ -1185,10 +1288,12 @@ public:
     virtual wxString GetType() const { return wxT("datetime"); }
     virtual wxVariantData* Clone() const { return new wxVariantDataDateTime(m_value); }
 
+    DECLARE_WXANY_CONVERSION()
 protected:
     wxDateTime m_value;
 };
 
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxDateTime, wxVariantDataDateTime)
 
 bool wxVariantDataDateTime::Eq(wxVariantData& data) const
 {
@@ -1316,10 +1421,13 @@ public:
     virtual wxString GetType() const { return wxT("arrstring"); }
     virtual wxVariantData* Clone() const { return new wxVariantDataArrayString(m_value); }
 
+    DECLARE_WXANY_CONVERSION()
 protected:
     wxArrayString m_value;
 };
 
+IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxArrayString, wxVariantDataArrayString)
+
 bool wxVariantDataArrayString::Eq(wxVariantData& data) const
 {
     wxASSERT_MSG( data.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
@@ -1343,7 +1451,7 @@ bool wxVariantDataArrayString::Write(wxString& str) const
     for ( size_t n = 0; n < count; n++ )
     {
         if ( n )
-            str += _T(';');
+            str += wxT(';');
 
         str += m_value[n];
     }
@@ -1363,7 +1471,7 @@ bool wxVariantDataArrayString::Read(wxSTD istream& WXUNUSED(str))
 
 bool wxVariantDataArrayString::Read(wxString& str)
 {
-    wxStringTokenizer tk(str, _T(";"));
+    wxStringTokenizer tk(str, wxT(";"));
     while ( tk.HasMoreTokens() )
     {
         m_value.Add(tk.GetNextToken());
@@ -1382,7 +1490,7 @@ wxVariant::wxVariant(const wxArrayString& val, const wxString& name) // Strings
 
 bool wxVariant::operator==(const wxArrayString& WXUNUSED(value)) const
 {
-    wxFAIL_MSG( _T("TODO") );
+    wxFAIL_MSG( wxT("TODO") );
 
     return false;
 }
@@ -1449,13 +1557,51 @@ public:
 
     virtual wxString GetType() const { return wxS("longlong"); }
 
+    DECLARE_WXANY_CONVERSION()
 protected:
     wxLongLong m_value;
 };
 
+//
+// wxLongLong type requires customized wxAny conversion code
+//
+#if wxUSE_ANY
+#ifdef wxLongLong_t
+
+bool wxVariantDataLongLong::GetAsAny(wxAny* any) const
+{
+    *any = m_value.GetValue();
+    return true;
+}
+
+wxVariantData* wxVariantDataLongLong::VariantDataFactory(const wxAny& any)
+{
+    return new wxVariantDataLongLong(wxANY_AS(any, wxLongLong_t));
+}
+
+REGISTER_WXANY_CONVERSION(wxLongLong_t, wxVariantDataLongLong)
+
+#else // if !defined(wxLongLong_t)
+
+bool wxVariantDataLongLong::GetAsAny(wxAny* any) const
+{
+    *any = m_value;
+    return true;
+}
+
+wxVariantData* wxVariantDataLongLong::VariantDataFactory(const wxAny& any)
+{
+    return new wxVariantDataLongLong(wxANY_AS(any, wxLongLong));
+}
+
+REGISTER_WXANY_CONVERSION(wxLongLong, wxVariantDataLongLong)
+
+#endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
+#endif // wxUSE_ANY
+
 bool wxVariantDataLongLong::Eq(wxVariantData& data) const
 {
-    wxASSERT_MSG( (data.GetType() == wxS("longlong")), 
+    wxASSERT_MSG( (data.GetType() == wxS("longlong")),
                   "wxVariantDataLongLong::Eq: argument mismatch" );
 
     wxVariantDataLongLong& otherData = (wxVariantDataLongLong&) data;
@@ -1610,13 +1756,52 @@ public:
 
     virtual wxString GetType() const { return wxS("ulonglong"); }
 
+    DECLARE_WXANY_CONVERSION()
 protected:
     wxULongLong m_value;
 };
 
+//
+// wxULongLong type requires customized wxAny conversion code
+//
+#if wxUSE_ANY
+#ifdef wxLongLong_t
+
+bool wxVariantDataULongLong::GetAsAny(wxAny* any) const
+{
+    *any = m_value.GetValue();
+    return true;
+}
+
+wxVariantData* wxVariantDataULongLong::VariantDataFactory(const wxAny& any)
+{
+    return new wxVariantDataULongLong(wxANY_AS(any, wxULongLong_t));
+}
+
+REGISTER_WXANY_CONVERSION(wxULongLong_t, wxVariantDataULongLong)
+
+#else // if !defined(wxLongLong_t)
+
+bool wxVariantDataULongLong::GetAsAny(wxAny* any) const
+{
+    *any = m_value;
+    return true;
+}
+
+wxVariantData* wxVariantDataULongLong::VariantDataFactory(const wxAny& any)
+{
+    return new wxVariantDataULongLong(wxANY_AS(any, wxULongLong));
+}
+
+REGISTER_WXANY_CONVERSION(wxULongLong, wxVariantDataULongLong)
+
+#endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
+#endif // wxUSE_ANY
+
+
 bool wxVariantDataULongLong::Eq(wxVariantData& data) const
 {
-    wxASSERT_MSG( (data.GetType() == wxS("ulonglong")), 
+    wxASSERT_MSG( (data.GetType() == wxS("ulonglong")),
                   "wxVariantDataULongLong::Eq: argument mismatch" );
 
     wxVariantDataULongLong& otherData = (wxVariantDataULongLong&) data;
@@ -1764,10 +1949,54 @@ public:
     void Clear();
 
     wxVariantData* Clone() const { return new wxVariantDataList(m_value); }
+
+    DECLARE_WXANY_CONVERSION()
 protected:
     wxVariantList  m_value;
 };
 
+#if wxUSE_ANY
+
+//
+// Convert to/from list of wxAnys
+//
+
+WX_DEFINE_LIST(wxAnyList)
+
+bool wxVariantDataList::GetAsAny(wxAny* any) const
+{
+    wxAnyList dst;
+    wxVariantList::compatibility_iterator node = m_value.GetFirst();
+    while (node)
+    {
+        wxVariant* pVar = node->GetData();
+        dst.push_back(new wxAny(((const wxVariant&)*pVar)));
+        node = node->GetNext();
+    }
+
+    *any = dst;
+    return true;
+}
+
+wxVariantData* wxVariantDataList::VariantDataFactory(const wxAny& any)
+{
+    wxAnyList src = wxANY_AS(any, wxAnyList);
+    wxVariantList dst;
+    wxAnyList::compatibility_iterator node = src.GetFirst();
+    while (node)
+    {
+        wxAny* pAny = node->GetData();
+        dst.push_back(new wxVariant(*pAny));
+        node = node->GetNext();
+    }
+
+    return new wxVariantDataList(dst);
+}
+
+REGISTER_WXANY_CONVERSION(wxAnyList, wxVariantDataList)
+
+#endif // wxUSE_ANY
+
 wxVariantDataList::wxVariantDataList(const wxVariantList& list)
 {
     SetValue(list);