X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cab1a6053dce9f0a2dcd87d8e2d12d8633f4785f..5429cf245532d124c1309ea956a2845eed195485:/src/common/variant.cpp?ds=sidebyside diff --git a/src/common/variant.cpp b/src/common/variant.cpp index d44908dd25..7afa1c3183 100644 --- a/src/common/variant.cpp +++ b/src/common/variant.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: variant.cpp +// Name: src/common/variant.cpp // Purpose: wxVariant class, container for any type // Author: Julian Smart // Modified by: @@ -9,10 +9,6 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma implementation "variant.h" -#endif - // For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" @@ -29,6 +25,7 @@ #endif #if defined(__MWERKS__) && __MSL__ >= 0x6000 +namespace std {} using namespace std ; #endif @@ -39,6 +36,7 @@ using namespace std ; #include "wx/string.h" #include "wx/tokenzr.h" +#include "wx/math.h" #include "wx/variant.h" @@ -58,7 +56,7 @@ public: wxVariantDataList(const wxList& list); ~wxVariantDataList(); - wxList& GetValue() const { return (wxList&) m_value; } + wxList& GetValue() { return m_value; } void SetValue(const wxList& value) ; virtual void Copy(wxVariantData& data); @@ -163,7 +161,7 @@ bool wxVariantDataList::Write(wxSTD ostream& str) const bool wxVariantDataList::Write(wxString& str) const { - str = wxT(""); + str = wxEmptyString; wxList::compatibility_iterator node = m_value.GetFirst(); while (node) { @@ -193,6 +191,7 @@ bool wxVariantDataList::Read(wxString& WXUNUSED(str)) // TODO return false; } +#if WXWIN_COMPATIBILITY_2_4 /* * wxVariantDataStringList @@ -302,6 +301,8 @@ bool wxVariantDataStringList::Read(wxString& WXUNUSED(str)) return false; } +#endif //2.4 compat + /* * wxVariantDataLong */ @@ -454,7 +455,7 @@ bool wxVariantDataReal::Eq(wxVariantData& data) const wxVariantDataReal& otherData = (wxVariantDataReal&) data; - return (otherData.m_value == m_value); + return wxIsSameDouble(otherData.m_value, m_value); } #if wxUSE_STD_IOSTREAM @@ -469,7 +470,7 @@ bool wxVariantDataReal::Write(wxSTD ostream& str) const bool wxVariantDataReal::Write(wxString& str) const { - str.Printf(wxT("%.4f"), m_value); + str.Printf(wxT("%.14g"), m_value); return true; } @@ -705,7 +706,7 @@ bool wxVariantDataChar::Read(wxInputStream& str) bool wxVariantDataChar::Read(wxString& str) { - m_value = str[(size_t)0]; + m_value = str.ToAscii()[size_t(0)]; return true; } @@ -874,7 +875,7 @@ bool wxVariantDataVoidPtr::Write(wxSTD ostream& str) const bool wxVariantDataVoidPtr::Write(wxString& str) const { - str.Printf(wxT("%ld"), (long) m_value); + str.Printf(wxT("%p"), m_value); return true; } @@ -978,7 +979,7 @@ bool wxVariantDataWxObjectPtr::Write(wxSTD ostream& str) const bool wxVariantDataWxObjectPtr::Write(wxString& str) const { - str.Printf(wxT("%s(%ld)"), GetType().c_str(), (long) m_value); + str.Printf(wxT("%s(%p)"), GetType().c_str(), m_value); return true; } @@ -1017,7 +1018,7 @@ public: { m_value = wxDateTime(valptr->day, (wxDateTime::Month) (valptr->month - 1),valptr->year); } wxVariantDataDateTime(const TIMESTAMP_STRUCT* valptr) { m_value = wxDateTime(valptr->day, (wxDateTime::Month) (valptr->month - 1), valptr->year, - valptr->hour, valptr->minute, valptr->second, valptr->fraction ); } + valptr->hour, valptr->minute, valptr->second, (wxDateTime::wxDateTime_t)valptr->fraction ); } #endif //ODBC inline wxDateTime GetValue() const { return m_value; } @@ -1064,7 +1065,7 @@ bool wxVariantDataDateTime::Eq(wxVariantData& data) const #if wxUSE_STD_IOSTREAM -bool wxVariantDataDateTime::Write(wxSTD ostream& str) const +bool wxVariantDataDateTime::Write(wxSTD ostream& WXUNUSED(str)) const { // Not implemented return false; @@ -1152,7 +1153,7 @@ bool wxVariantDataArrayString::Eq(wxVariantData& data) const #if wxUSE_STD_IOSTREAM -bool wxVariantDataArrayString::Write(wxSTD ostream& str) const +bool wxVariantDataArrayString::Write(wxSTD ostream& WXUNUSED(str)) const { // Not implemented return false; @@ -1247,12 +1248,16 @@ wxVariant::wxVariant(const wxChar* val, const wxString& name) m_name = name; } +#if WXWIN_COMPATIBILITY_2_4 + wxVariant::wxVariant(const wxStringList& val, const wxString& name) { m_data = new wxVariantDataStringList(val); m_name = name; } +#endif + wxVariant::wxVariant(const wxList& val, const wxString& name) // List of variants { m_data = new wxVariantDataList(val); @@ -1387,8 +1392,8 @@ bool wxVariant::operator== (double value) const double thisValue; if (!Convert(&thisValue)) return false; - else - return (value == thisValue); + + return wxIsSameDouble(value, thisValue); } bool wxVariant::operator!= (double value) const @@ -1538,6 +1543,8 @@ void wxVariant::operator= (const wxChar* value) } } +#if WXWIN_COMPATIBILITY_2_4 + bool wxVariant::operator== (const wxStringList& value) const { wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") ); @@ -1548,7 +1555,10 @@ bool wxVariant::operator== (const wxStringList& value) const bool wxVariant::operator!= (const wxStringList& value) const { - return (!((*this) == value)); + wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") ); + + wxVariantDataStringList other(value); + return !(m_data->Eq(other)); } void wxVariant::operator= (const wxStringList& value) @@ -1565,6 +1575,8 @@ void wxVariant::operator= (const wxStringList& value) } } +#endif + bool wxVariant::operator== (const wxList& value) const { wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") ); @@ -1709,22 +1721,29 @@ wxArrayString wxVariant::GetArrayString() const // 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 < (size_t) data->GetValue().GetCount()), wxT("Invalid index for array") ); + 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 < (size_t) data->GetValue().GetCount()), wxT("Invalid index for array") ); + wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); - wxVariant variant( wxString( (const wxChar*) (data->GetValue().Item(idx)->GetData()) )); + wxString str( (const wxChar*) (data->GetValue().Item(idx)->GetData()) ); + wxVariant variant( str ); return variant; } +#endif return wxNullVariant; } @@ -1736,26 +1755,32 @@ wxVariant& wxVariant::operator[] (size_t idx) wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") ); wxVariantDataList* data = (wxVariantDataList*) m_data; - wxASSERT_MSG( (idx < (size_t) data->GetValue().GetCount()), wxT("Invalid index for array") ); + 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 -int wxVariant::GetCount() const +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; } @@ -1767,7 +1792,7 @@ wxString wxVariant::MakeString() const if (GetData()->Write(str)) return str; } - return wxString(wxT("")); + return wxEmptyString; } // Accessors @@ -1780,7 +1805,7 @@ void wxVariant::SetData(wxVariantData* data) // Returns a string representing the type of the variant, -// e.g. "string", "bool", "stringlist", "list", "double", "long" +// e.g. "string", "bool", "list", "double", "long" wxString wxVariant::GetType() const { if (IsNull()) @@ -1895,6 +1920,8 @@ wxList& wxVariant::GetList() const return (wxList&) ((wxVariantDataList*) m_data)->GetValue(); } +#if WXWIN_COMPATIBILITY_2_4 + wxStringList& wxVariant::GetStringList() const { wxASSERT( (GetType() == wxT("stringlist")) ); @@ -1902,11 +1929,13 @@ wxStringList& wxVariant::GetStringList() const return (wxStringList&) ((wxVariantDataStringList*) m_data)->GetValue(); } +#endif + // Make empty list void wxVariant::NullList() { SetData(new wxVariantDataList()); -}; +} // Append to list void wxVariant::Append(const wxVariant& value) @@ -1941,11 +1970,11 @@ bool wxVariant::Member(const wxVariant& value) const } // Deletes the nth element of the list -bool wxVariant::Delete(int item) +bool wxVariant::Delete(size_t item) { wxList& list = GetList(); - wxASSERT_MSG( (item < (int) list.GetCount()), wxT("Invalid index to Delete") ); + wxASSERT_MSG( (item < list.GetCount()), wxT("Invalid index to Delete") ); wxList::compatibility_iterator node = list.Item(item); wxVariant* variant = (wxVariant*) node->GetData(); delete variant; @@ -1962,7 +1991,7 @@ void wxVariant::ClearList() } else { - if (GetType() != wxT("list")) + if (!GetType().IsSameAs(wxT("list"))) { delete m_data; m_data = NULL; @@ -2006,9 +2035,9 @@ bool wxVariant::Convert(bool* value) const { wxString val(((wxVariantDataString*)GetData())->GetValue()); val.MakeLower(); - if (val == wxT("true") || val == wxT("yes")) + if (val == wxT("true") || val == wxT("yes") || val == wxT('1') ) *value = true; - else if (val == wxT("false") || val == wxT("no")) + else if (val == wxT("false") || val == wxT("no") || val == wxT('0') ) *value = false; else return false; @@ -2072,7 +2101,7 @@ bool wxVariant::Convert(wxDateTime* value) const } // Fallback to string conversion wxString val; - return Convert(&val) && (value->ParseDate(val)); + return Convert(&val) && + (value->ParseDateTime(val) || value->ParseDate(val)); } #endif // wxUSE_DATETIME -