X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/03647350fc7cd141953c72e0284e928847d30f44..b404a8f3b072129c107c6d9a5e0f6f53cd34807b:/src/common/variant.cpp?ds=sidebyside diff --git a/src/common/variant.cpp b/src/common/variant.cpp index b1c8838981..5f7daabe9a 100644 --- a/src/common/variant.cpp +++ b/src/common/variant.cpp @@ -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,29 @@ public: virtual wxString GetType() const { return wxT("long"); } +#if wxUSE_ANY + // 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 +#endif // wxUSE_ANY + 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 +438,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 +578,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 +719,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 +874,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") ); @@ -887,6 +986,20 @@ wxVariant::wxVariant(const wxScopedWCharBuffer& val, const wxString& name) m_name = name; } +#if wxUSE_STD_STRING +wxVariant::wxVariant(const std::string& val, const wxString& name) +{ + m_refData = new wxVariantDataString(wxString(val)); + m_name = name; +} + +wxVariant::wxVariant(const wxStdWideString& val, const wxString& name) +{ + m_refData = new wxVariantDataString(wxString(val)); + m_name = name; +} +#endif // wxUSE_STD_STRING + bool wxVariant::operator== (const wxString& value) const { wxString thisValue; @@ -954,10 +1067,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 +1189,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 +1304,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 +1437,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") ); @@ -1449,10 +1573,48 @@ 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")), @@ -1610,10 +1772,49 @@ 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")), @@ -1764,10 +1965,52 @@ 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 +// + +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); @@ -2115,6 +2358,15 @@ bool wxVariant::Convert(wxUniChar* value) const *value = (char) (((wxVariantDataLong*)GetData())->GetValue()); else if (type == wxT("bool")) *value = (char) (((wxVariantDataBool*)GetData())->GetValue()); + else if (type == wxS("string")) + { + // Also accept strings of length 1 + const wxString& str = (((wxVariantDataString*)GetData())->GetValue()); + if ( str.length() == 1 ) + *value = str[0]; + else + return false; + } else return false;