]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/variant.cpp
Allow retrieving the descent and external leading of empty strings.
[wxWidgets.git] / src / common / variant.cpp
index 12a9ded6d2bc1100fdf3980fdd66d218a1c2e0cb..6bafda604f6d80b78a7e7c5e5d5501b61a68e420 100644 (file)
     #endif
 #endif
 
-#if defined(__MWERKS__) && __MSL__ >= 0x6000
-namespace std {}
-using namespace std ;
-#endif
-
 #if wxUSE_STREAMS
     #include "wx/txtstrm.h"
 #endif
@@ -193,6 +188,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 +265,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 +433,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 +573,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 +714,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 +869,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 +981,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 +1062,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 +1184,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 +1299,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 +1432,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 +1462,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 +1482,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 +1501,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 +1568,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 +1767,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 +1960,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 +2353,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;