X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2562c82329113d43b0de122c86ac23f0dca0e669..72366f68d1036084d1adc5971eeee4885f36fe5c:/src/common/variant.cpp?ds=sidebyside diff --git a/src/common/variant.cpp b/src/common/variant.cpp index 37dc2c3f41..870938c773 100644 --- a/src/common/variant.cpp +++ b/src/common/variant.cpp @@ -290,7 +290,7 @@ bool wxVariantDataLong::Read(wxString& str) return true; } -// wxVariant +// wxVariant wxVariant::wxVariant(long val, const wxString& name) { @@ -642,10 +642,10 @@ class WXDLLIMPEXP_BASE wxVariantDataChar: public wxVariantData DECLARE_DYNAMIC_CLASS(wxVariantDataChar) public: wxVariantDataChar() { m_value = 0; } - wxVariantDataChar(char value) { m_value = value; } + wxVariantDataChar(wxChar value) { m_value = value; } - inline char GetValue() const { return m_value; } - inline void SetValue(char value) { m_value = value; } + inline wxChar GetValue() const { return m_value; } + inline void SetValue(wxChar value) { m_value = value; } virtual bool Eq(wxVariantData& data) const; #if wxUSE_STD_IOSTREAM @@ -661,7 +661,7 @@ public: virtual wxString GetType() const { return wxT("char"); }; protected: - char m_value; + wxChar m_value; }; IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar, wxVariantData) @@ -695,7 +695,7 @@ bool wxVariantDataChar::Write(wxString& str) const bool wxVariantDataChar::Read(wxSTD istream& WXUNUSED(str)) { wxFAIL_MSG(wxT("Unimplemented")); -// str >> m_value; + return false; } #endif @@ -705,7 +705,8 @@ bool wxVariantDataChar::Write(wxOutputStream& str) const { wxTextOutputStream s(str); - s.Write8(m_value); + s << m_value; + return true; } @@ -713,38 +714,39 @@ bool wxVariantDataChar::Read(wxInputStream& str) { wxTextInputStream s(str); - m_value = s.Read8(); + s >> m_value; + return true; } #endif // wxUSE_STREAMS bool wxVariantDataChar::Read(wxString& str) { - m_value = str.ToAscii()[size_t(0)]; + m_value = str[size_t(0)]; return true; } -wxVariant::wxVariant(char val, const wxString& name) +wxVariant::wxVariant(wxChar val, const wxString& name) { m_data = new wxVariantDataChar(val); m_name = name; } -bool wxVariant::operator== (char value) const +bool wxVariant::operator== (wxChar value) const { - char thisValue; + wxChar thisValue; if (!Convert(&thisValue)) return false; else return (value == thisValue); } -bool wxVariant::operator!= (char value) const +bool wxVariant::operator!= (wxChar value) const { return (!((*this) == value)); } -void wxVariant::operator= (char value) +void wxVariant::operator= (wxChar value) { if (GetType() == wxT("char") && m_data->GetRefCount() == 1) @@ -758,9 +760,9 @@ void wxVariant::operator= (char value) } } -char wxVariant::GetChar() const +wxChar wxVariant::GetChar() const { - char value; + wxChar value; if (Convert(& value)) return value; else @@ -853,7 +855,7 @@ bool wxVariantDataString::Read(wxString& str) IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString, wxVariantData) // wxVariant **** - + wxVariant::wxVariant(const wxString& val, const wxString& name) { m_data = new wxVariantDataString(val); @@ -1699,72 +1701,6 @@ void wxVariant::ClearList() } } -// Treat a list variant as an array -wxVariant wxVariant::operator[] (size_t idx) const -{ -#if WXWIN_COMPATIBILITY_2_4 - wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") ); -#else - wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") ); -#endif - - if (GetType() == wxT("list")) - { - wxVariantDataList* data = (wxVariantDataList*) m_data; - wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); - return * (wxVariant*) (data->GetValue().Item(idx)->GetData()); - } -#if WXWIN_COMPATIBILITY_2_4 - else if (GetType() == wxT("stringlist")) - { - wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; - wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); - - wxString str( (const wxChar*) (data->GetValue().Item(idx)->GetData()) ); - wxVariant variant( str ); - return variant; - } -#endif - return wxNullVariant; -} - -wxVariant& wxVariant::operator[] (size_t idx) -{ - // We can't return a reference to a variant for a string list, since the string - // is actually stored as a char*, not a variant. - - wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") ); - - wxVariantDataList* data = (wxVariantDataList*) m_data; - wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); - - return * (wxVariant*) (data->GetValue().Item(idx)->GetData()); -} - -// Return the number of elements in a list -size_t wxVariant::GetCount() const -{ -#if WXWIN_COMPATIBILITY_2_4 - wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") ); -#else - wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") ); -#endif - - if (GetType() == wxT("list")) - { - wxVariantDataList* data = (wxVariantDataList*) m_data; - return data->GetValue().GetCount(); - } -#if WXWIN_COMPATIBILITY_2_4 - else if (GetType() == wxT("stringlist")) - { - wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; - return data->GetValue().GetCount(); - } -#endif - return 0; -} - #if WXWIN_COMPATIBILITY_2_4 // ---------------------------------------------------------------------------- @@ -1905,7 +1841,7 @@ void wxVariant::operator= (const wxStringList& value) } } -// wxVariant +// wxVariant wxStringList& wxVariant::GetStringList() const { @@ -1916,6 +1852,72 @@ wxStringList& wxVariant::GetStringList() const #endif +// Treat a list variant as an array +wxVariant wxVariant::operator[] (size_t idx) const +{ +#if WXWIN_COMPATIBILITY_2_4 + wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") ); +#else + wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") ); +#endif + + if (GetType() == wxT("list")) + { + wxVariantDataList* data = (wxVariantDataList*) m_data; + wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); + return * (wxVariant*) (data->GetValue().Item(idx)->GetData()); + } +#if WXWIN_COMPATIBILITY_2_4 + else if (GetType() == wxT("stringlist")) + { + wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; + wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); + + wxString str( (const wxChar*) (data->GetValue().Item(idx)->GetData()) ); + wxVariant variant( str ); + return variant; + } +#endif + return wxNullVariant; +} + +wxVariant& wxVariant::operator[] (size_t idx) +{ + // We can't return a reference to a variant for a string list, since the string + // is actually stored as a char*, not a variant. + + wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") ); + + wxVariantDataList* data = (wxVariantDataList*) m_data; + wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); + + return * (wxVariant*) (data->GetValue().Item(idx)->GetData()); +} + +// Return the number of elements in a list +size_t wxVariant::GetCount() const +{ +#if WXWIN_COMPATIBILITY_2_4 + wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") ); +#else + wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") ); +#endif + + if (GetType() == wxT("list")) + { + wxVariantDataList* data = (wxVariantDataList*) m_data; + return data->GetValue().GetCount(); + } +#if WXWIN_COMPATIBILITY_2_4 + else if (GetType() == wxT("stringlist")) + { + wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; + return data->GetValue().GetCount(); + } +#endif + return 0; +} + // ---------------------------------------------------------------------------- // Type conversion // ---------------------------------------------------------------------------- @@ -1986,7 +1988,7 @@ bool wxVariant::Convert(double* value) const return true; } -bool wxVariant::Convert(char* value) const +bool wxVariant::Convert(wxChar* value) const { wxString type(GetType()); if (type == wxT("char")) @@ -2025,7 +2027,4 @@ bool wxVariant::Convert(wxDateTime* value) const } #endif // wxUSE_DATETIME - - - #endif // wxUSE_VARIANT