]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/variant.cpp
Check for null pointer.
[wxWidgets.git] / src / common / variant.cpp
index 7994a92651673a8e1a81b69a108d01c10d4393c6..b7399182ca4ad54a82d1981449dd337079f9d4d0 100644 (file)
@@ -44,13 +44,13 @@ using namespace std ;
 
 IMPLEMENT_ABSTRACT_CLASS(wxVariantData, wxObject)
 
-wxVariant WXDLLEXPORT wxNullVariant;
+wxVariant WXDLLIMPEXP_BASE wxNullVariant;
 
 /*
  * wxVariantDataList
  */
 
-class WXDLLEXPORT wxVariantDataList: public wxVariantData
+class WXDLLIMPEXP_BASE wxVariantDataList: public wxVariantData
 {
 DECLARE_DYNAMIC_CLASS(wxVariantDataList)
 public:
@@ -198,7 +198,7 @@ bool wxVariantDataList::Read(wxString& WXUNUSED(str))
  * wxVariantDataStringList
  */
 
-class WXDLLEXPORT wxVariantDataStringList: public wxVariantData
+class WXDLLIMPEXP_BASE wxVariantDataStringList: public wxVariantData
 {
 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList)
 public:
@@ -306,7 +306,7 @@ bool wxVariantDataStringList::Read(wxString& WXUNUSED(str))
  * wxVariantDataLong
  */
 
-class WXDLLEXPORT wxVariantDataLong: public wxVariantData
+class WXDLLIMPEXP_BASE wxVariantDataLong: public wxVariantData
 {
 DECLARE_DYNAMIC_CLASS(wxVariantDataLong)
 public:
@@ -407,7 +407,7 @@ bool wxVariantDataLong::Read(wxString& str)
  * wxVariantDataReal
  */
 
-class WXDLLEXPORT wxVariantDataReal: public wxVariantData
+class WXDLLIMPEXP_BASE wxVariantDataReal: public wxVariantData
 {
 DECLARE_DYNAMIC_CLASS(wxVariantDataReal)
 public:
@@ -508,7 +508,7 @@ bool wxVariantDataReal::Read(wxString& str)
  * wxVariantDataBool
  */
 
-class WXDLLEXPORT wxVariantDataBool: public wxVariantData
+class WXDLLIMPEXP_BASE wxVariantDataBool: public wxVariantData
 {
 DECLARE_DYNAMIC_CLASS(wxVariantDataBool)
 public:
@@ -612,7 +612,7 @@ bool wxVariantDataBool::Read(wxString& str)
  * wxVariantDataChar
  */
 
-class WXDLLEXPORT wxVariantDataChar: public wxVariantData
+class WXDLLIMPEXP_BASE wxVariantDataChar: public wxVariantData
 {
 DECLARE_DYNAMIC_CLASS(wxVariantDataChar)
 public:
@@ -718,7 +718,7 @@ bool wxVariantDataChar::Read(wxString& str)
 #define wxVariantDataString wxVariantStringData
 #endif
 
-class WXDLLEXPORT wxVariantDataString: public wxVariantData
+class WXDLLIMPEXP_BASE wxVariantDataString: public wxVariantData
 {
 #if defined(__BORLANDC__) && defined(__WIN16__)
 DECLARE_DYNAMIC_CLASS(wxVariantStringData)
@@ -905,6 +905,111 @@ bool wxVariantDataVoidPtr::Read(wxString& WXUNUSED(str))
     return FALSE;
 }
 
+/*
+ * wxVariantDataWxObjectPtr
+ */
+
+class wxVariantDataWxObjectPtr: public wxVariantData
+{
+DECLARE_DYNAMIC_CLASS(wxVariantDataWxObjectPtr)
+public:
+    wxVariantDataWxObjectPtr() { }
+    wxVariantDataWxObjectPtr(wxObject* value) { m_value = value; }
+
+    inline wxObject* GetValue() const { return m_value; }
+    inline void SetValue(wxObject* value) { m_value = value; }
+
+    virtual void Copy(wxVariantData& data);
+    virtual bool Eq(wxVariantData& data) const;
+#if wxUSE_STD_IOSTREAM
+    virtual bool Write(wxSTD ostream& str) const;
+#endif
+    virtual bool Write(wxString& str) const;
+#if wxUSE_STD_IOSTREAM
+    virtual bool Read(wxSTD istream& str);
+#endif
+    virtual bool Read(wxString& str);
+    virtual wxString GetType() const ;
+    virtual wxVariantData* Clone() { return new wxVariantDataWxObjectPtr; }
+
+    virtual wxClassInfo* GetValueClassInfo() ; 
+protected:
+    wxObject* m_value;
+
+    DECLARE_NO_COPY_CLASS(wxVariantDataWxObjectPtr)
+};
+
+IMPLEMENT_DYNAMIC_CLASS(wxVariantDataWxObjectPtr, wxVariantData)
+
+void wxVariantDataWxObjectPtr::Copy(wxVariantData& data)
+{
+    wxASSERT_MSG(  wxIsKindOf((&data), wxVariantDataWxObjectPtr) ,\
+                   wxT("wxVariantDataWxObjectPtr::Copy: Can't copy to this type of data") \
+                 );
+
+    wxVariantDataWxObjectPtr& otherData = (wxVariantDataWxObjectPtr&) data;
+
+    otherData.m_value = m_value;
+}
+
+bool wxVariantDataWxObjectPtr::Eq(wxVariantData& data) const
+{
+    wxASSERT_MSG(   wxIsKindOf((&data), wxVariantDataWxObjectPtr), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
+
+    wxVariantDataWxObjectPtr& otherData = (wxVariantDataWxObjectPtr&) data;
+
+    return (otherData.m_value == m_value);
+}
+
+wxString wxVariantDataWxObjectPtr::GetType() const
+{
+    wxString returnVal(wxT("wxObject"));
+    if (m_value) {
+        returnVal = m_value->GetClassInfo()->GetClassName();
+    }
+    return returnVal;
+}
+
+wxClassInfo* wxVariantDataWxObjectPtr::GetValueClassInfo()
+{
+    wxClassInfo* returnVal=NULL;
+    
+    if (m_value) returnVal = m_value->GetClassInfo(); 
+
+    return returnVal;
+}
+
+#if wxUSE_STD_IOSTREAM
+bool wxVariantDataWxObjectPtr::Write(wxSTD ostream& str) const
+{
+    wxString s;
+    Write(s);
+    str << (const char*) s.mb_str();
+    return TRUE;
+}
+#endif
+
+bool wxVariantDataWxObjectPtr::Write(wxString& str) const
+{
+    str.Printf(wxT("%s(%ld)"), GetType().c_str(), (long) m_value);
+    return TRUE;
+}
+
+#if wxUSE_STD_IOSTREAM
+bool wxVariantDataWxObjectPtr::Read(wxSTD istream& WXUNUSED(str))
+{
+    // Not implemented
+    return FALSE;
+}
+#endif
+
+bool wxVariantDataWxObjectPtr::Read(wxString& WXUNUSED(str))
+{
+    // Not implemented
+    return FALSE;
+}
+
+
 /*
  * wxVariantDataDateTime
  */
@@ -1167,6 +1272,26 @@ wxVariant::wxVariant(const wxList& val, const wxString& name) // List of variant
     m_name = name;
 }
 
+wxVariant::wxVariant( void* val, const wxString& name)
+{
+   m_data = new wxVariantDataVoidPtr(val);
+   m_name = name;
+}
+
+wxVariant::wxVariant( wxObject* val, const wxString& name)
+{
+   m_data = new wxVariantDataWxObjectPtr(val);
+   m_name = name;
+}
+
+#if wxUSE_DATETIME
+wxVariant::wxVariant(const wxDateTime& val, const wxString& name) // Date
+{
+       m_data = new wxVariantDataDateTime(val);
+       m_name = name;
+}
+#endif // wxUSE_DATETIME    
+
 #if wxUSE_ODBC
 wxVariant::wxVariant(const TIME_STRUCT* valptr, const wxString& name) // Date
 {
@@ -1683,6 +1808,12 @@ bool wxVariant::IsType(const wxString& type) const
     return (GetType() == type);
 }
 
+bool wxVariant::IsValueKindOf(const wxClassInfo* type) const
+{
+    wxClassInfo* info=m_data->GetValueClassInfo(); 
+    return info ? info->IsKindOf(type) : false ;
+}
+
 
 // Value accessors
 double wxVariant::GetReal() const
@@ -1751,6 +1882,12 @@ void* wxVariant::GetVoidPtr() const
     return (void*) ((wxVariantDataVoidPtr*) m_data)->GetValue();
 }
 
+wxObject* wxVariant::GetWxObjectPtr() 
+{
+    wxASSERT(wxIsKindOf(m_data, wxVariantDataWxObjectPtr));
+    return (wxObject*) ((wxVariantDataWxObjectPtr*) m_data)->GetValue();
+}
+
 #if wxUSE_DATETIME
 wxDateTime wxVariant::GetDateTime() const
 {