copies {\it value}, the application is still responsible for
deleting {\it value} and its contents.
-%Note: this constructor is currently disabled because it causes a C++ ambiguity.
-
\func{}{wxVariant}{\param{void*}{ value}, \param{const wxString\& }{name = ``"}}
Construction from a void pointer.
+\func{}{wxVariant}{\param{wxObject*}{ value}, \param{const wxString\& }{name = ``"}}
+
+Construction from a wxObject pointer.
+
\func{}{wxVariant}{\param{wxVariantData*}{ data}, \param{const wxString\& }{name = ``"}}
Construction from user-defined data. The variant holds on to the {\it data} pointer.
Gets the void pointer value.
+\membersection{wxVariant::GetWxObjectPtr}\label{wxvariantgetwxobjectptr}
+
+\constfunc{void*}{GetWxObjectPtr}{\void}
+
+Gets the wxObject pointer value.
+
\membersection{wxVariant::Insert}\label{wxvariantinsert}
\func{void}{Insert}{\param{const wxVariant\&}{ value}}
Returns true if {\it type} matches the type of the variant, false otherwise.
+\membersection{wxVariant::IsValueKindOf}\label{wxvariantisvaluekindof}
+
+\constfunc{bool}{IsValueKindOf}{\param{const wxClassInfo* type}{ type}}
+
+Returns true if the data is derived from the class described by {\it type}, false otherwise.
+
\membersection{wxVariant::MakeNull}\label{wxvariantmakenull}
\func{void}{MakeNull}{\void}
Returns the string type of the data.
+\membersection{wxVariantData::GetValueClassInfo}\label{wxvariantdatagetvalueclassinfo}
+
+\constfunc{wxClassInfo*}{GetValueClassInfo}{\void}
+
+If the data is a wxObject returns a pointer to the objects wxClassInfo structure, if
+the data isn't a wxObject the method returns NULL.
+
\membersection{wxVariantData::Read}\label{wxvariantdataread}
\func{bool}{Read}{\param{ostream\&}{ stream}}
Writes the data to {\it stream} or {\it string}.
+\membersection{wxGetVariantCast}\label{wxgetvariantcast}
+
+\func{classname *}{wxGetVariantCast}{wxVariant\&, classname}
+
+This macro returns the data stored in {\it variant} cast to the type {\it classname *} if
+the data is of this type (the check is done during the run-time) or
+{\tt NULL} otherwise.
+
+
+\wxheading{See also}
+\helpref{RTTI overview}{runtimeclassoverview}\\
+\helpref{wxDynamicCast}{wxdynamiccast}\\
virtual bool Read(wxString& str) = 0;
// What type is it? Return a string name.
virtual wxString GetType() const = 0;
+ // If it based on wxObject return the ClassInfo.
+ virtual wxClassInfo* GetValueClassInfo() { return NULL; }
};
/*
wxVariant(const wxStringList& val, const wxString& name = wxEmptyString);
wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants
wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose)
+ wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); //wxObject
wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data
//TODO: Need to document
#if wxUSE_DATETIME
inline operator long () const { return GetLong(); }
inline operator bool () const { return GetBool(); }
inline operator void* () const { return GetVoidPtr(); }
+ // No implicit conversion to wxObject, as that would really
+ // confuse people between conversion to our contained data
+ // and downcasting to see our base type.
//TODO: Need to document
#if wxUSE_DATETIME
inline operator wxDateTime () const { return GetDateTime(); }
wxString GetType() const;
bool IsType(const wxString& type) const;
+ bool IsValueKindOf(const wxClassInfo* type) const;
// Return the number of elements in a list
int GetCount() const;
wxStringList& GetStringList() const ;
void* GetVoidPtr() const ;
+ wxObject* GetWxObjectPtr() ;
//TODO: Need to document
#if wxUSE_DATETIME
wxDateTime GetDateTime() const ;
wxString m_name;
};
+//Since we want type safety wxVariant we need to fetch and dynamic_cast
+//in a seemingly safe way so the compiler can check, so we define
+//a dynamic_cast /wxDynamicCast analogue.
+
+#define wxGetVariantCast(var,classname) \
+ ((classname*)(var.IsValueKindOf(&classname::sm_class##classname) ?\
+ var.GetWxObjectPtr() : NULL));
+
extern wxVariant WXDLLEXPORT wxNullVariant;
#endif
{
textCtrl << _T("var1[") << (int) i << _T("] (type ") << var1[i].GetType() << _T(") = ") << var1[i].MakeString() << _T("\n");
}
+
+ var1 = wxVariant(new wxFont(wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT)));
+ textCtrl << _T("var1 = (wxfont)\"");
+ wxFont* font = wxGetVariantCast(var1,wxFont);
+ if (font) {
+ textCtrl << font->GetNativeFontInfoDesc() << _T("\"\n");
+ } else {
+ textCtrl << _T("(null)\"\n");
+ }
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
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().mb_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
*/
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
{
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
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
{