-/////////////////////////////////////////////////////////////////////////////\r
-// Name: src/common/xtistrm.cpp\r
-// Purpose: streaming runtime metadata information\r
-// Author: Stefan Csomor\r
-// Modified by:\r
-// Created: 27/07/03\r
-// RCS-ID: $Id: xtistrm.cpp 47828 2007-07-31 19:26:56Z FM $\r
-// Copyright: (c) 2003 Stefan Csomor\r
-// Licence: wxWindows licence\r
-/////////////////////////////////////////////////////////////////////////////\r
-\r
-// For compilers that support precompilation, includes "wx.h".\r
-#include "wx/wxprec.h"\r
-\r
-#ifdef __BORLANDC__\r
- #pragma hdrstop\r
-#endif\r
-\r
-#include "wx/xtistrm.h"\r
-\r
-#ifndef WX_PRECOMP\r
- #include "wx/object.h"\r
- #include "wx/hash.h"\r
- #include "wx/event.h"\r
-#endif\r
-\r
-#include <map>\r
-#include <vector>\r
-#include <string>\r
-using namespace std;\r
-\r
-#include "wx/tokenzr.h"\r
-#include "wx/txtstrm.h"\r
-#include "codereadercallback.h"\r
-\r
-#if !wxUSE_EXTENDED_RTTI\r
- #error This sample requires XTI (eXtended RTTI) enabled\r
-#endif\r
-\r
-// ----------------------------------------------------------------------------\r
-// wxObjectCodeReaderCallback - depersisting to code\r
-// ----------------------------------------------------------------------------\r
-\r
-struct wxObjectCodeReaderCallback::wxObjectCodeReaderCallbackInternal\r
-{\r
-#if wxUSE_UNICODE\r
- map<int,wstring> m_objectNames;\r
-#else\r
- map<int,string> m_objectNames;\r
-#endif\r
-\r
- void SetObjectName(int objectID, const wxString &name )\r
- {\r
- if ( m_objectNames.find(objectID) != m_objectNames.end() )\r
- {\r
- wxLogError( _("Passing a already registered object to SetObjectName") );\r
- return ;\r
- }\r
- m_objectNames[objectID] = (const wxChar *)name;\r
- }\r
-\r
- wxString GetObjectName( int objectID )\r
- {\r
- if ( objectID == wxNullObjectID )\r
- return wxT("NULL");\r
-\r
- if ( m_objectNames.find(objectID) == m_objectNames.end() )\r
- {\r
- wxLogError( _("Passing an unkown object to GetObject") );\r
- return wxEmptyString;\r
- }\r
- return wxString( m_objectNames[objectID].c_str() );\r
- }\r
-};\r
-\r
-wxObjectCodeReaderCallback::wxObjectCodeReaderCallback(wxTextOutputStream *out)\r
-: m_fp(out)\r
-{\r
- m_data = new wxObjectCodeReaderCallbackInternal;\r
-}\r
-\r
-wxObjectCodeReaderCallback::~wxObjectCodeReaderCallback()\r
-{\r
- delete m_data;\r
-}\r
-\r
-void wxObjectCodeReaderCallback::AllocateObject(int objectID, wxClassInfo *classInfo,\r
- wxVariantBaseArray &WXUNUSED(metadata))\r
-{\r
- wxString objectName = wxString::Format( wxT("LocalObject_%d"), objectID );\r
- m_fp->WriteString( wxString::Format( wxT("\t%s *%s = new %s;\n"),\r
- classInfo->GetClassName(),\r
- objectName.c_str(),\r
- classInfo->GetClassName()) );\r
- m_data->SetObjectName( objectID, objectName );\r
-}\r
-\r
-void wxObjectCodeReaderCallback::DestroyObject(int objectID, wxClassInfo *WXUNUSED(classInfo))\r
-{\r
- m_fp->WriteString( wxString::Format( wxT("\tdelete %s;\n"),\r
- m_data->GetObjectName( objectID).c_str() ) );\r
-}\r
-\r
-wxString wxObjectCodeReaderCallback::ValueAsCode( const wxVariantBase ¶m )\r
-{\r
- wxString value;\r
- const wxTypeInfo* type = param.GetTypeInfo();\r
- if ( type->GetKind() == wxT_CUSTOM )\r
- {\r
- const wxCustomTypeInfo* cti = wx_dynamic_cast(const wxCustomTypeInfo*, type);\r
- if ( cti )\r
- {\r
- value.Printf( wxT("%s(%s)"), cti->GetTypeName().c_str(),\r
- param.GetAsString().c_str() );\r
- }\r
- else\r
- {\r
- wxLogError ( _("Internal error, illegal wxCustomTypeInfo") );\r
- }\r
- }\r
- else if ( type->GetKind() == wxT_STRING )\r
- {\r
- value.Printf( wxT("\"%s\""),param.GetAsString().c_str() );\r
- }\r
- else\r
- {\r
- value.Printf( wxT("%s"), param.GetAsString().c_str() );\r
- }\r
- return value;\r
-}\r
-\r
-void wxObjectCodeReaderCallback::CreateObject(int objectID,\r
- const wxClassInfo *WXUNUSED(classInfo),\r
- int paramCount,\r
- wxVariantBase *params,\r
- int *objectIDValues,\r
- const wxClassInfo **WXUNUSED(objectClassInfos),\r
- wxVariantBaseArray &WXUNUSED(metadata)\r
- )\r
-{\r
- int i;\r
- m_fp->WriteString( wxString::Format( wxT("\t%s->Create("), \r
- m_data->GetObjectName(objectID).c_str() ) );\r
- for (i = 0; i < paramCount; i++)\r
- {\r
- if ( objectIDValues[i] != wxInvalidObjectID )\r
- {\r
- wxString str = \r
- wxString::Format( wxT("%s"), \r
- m_data->GetObjectName( objectIDValues[i] ).c_str() );\r
- m_fp->WriteString( str );\r
- }\r
- else\r
- {\r
- m_fp->WriteString( \r
- wxString::Format( wxT("%s"), ValueAsCode(params[i]).c_str() ) );\r
- }\r
- if (i < paramCount - 1)\r
- m_fp->WriteString( wxT(", "));\r
- }\r
- m_fp->WriteString( wxT(");\n") );\r
-}\r
-\r
-void wxObjectCodeReaderCallback::ConstructObject(int objectID,\r
- const wxClassInfo *classInfo,\r
- int paramCount,\r
- wxVariantBase *params,\r
- int *objectIDValues,\r
- const wxClassInfo **WXUNUSED(objectClassInfos),\r
- wxVariantBaseArray &WXUNUSED(metadata)\r
- )\r
-{\r
- wxString objectName = wxString::Format( wxT("LocalObject_%d"), objectID );\r
- m_fp->WriteString( wxString::Format( wxT("\t%s *%s = new %s("),\r
- classInfo->GetClassName(),\r
- objectName.c_str(),\r
- classInfo->GetClassName()) );\r
- m_data->SetObjectName( objectID, objectName );\r
-\r
- int i;\r
- for (i = 0; i < paramCount; i++)\r
- {\r
- if ( objectIDValues[i] != wxInvalidObjectID )\r
- m_fp->WriteString( wxString::Format( wxT("%s"), \r
- m_data->GetObjectName( objectIDValues[i] ).c_str() ) );\r
- else\r
- {\r
- m_fp->WriteString( \r
- wxString::Format( wxT("%s"), ValueAsCode(params[i]).c_str() ) );\r
- }\r
- if (i < paramCount - 1)\r
- m_fp->WriteString( wxT(", ") );\r
- }\r
- m_fp->WriteString( wxT(");\n") );\r
-}\r
-\r
-void wxObjectCodeReaderCallback::SetProperty(int objectID,\r
- const wxClassInfo *WXUNUSED(classInfo),\r
- const wxPropertyInfo* propertyInfo,\r
- const wxVariantBase &value)\r
-{\r
- m_fp->WriteString( wxString::Format( wxT("\t%s->%s(%s);\n"),\r
- m_data->GetObjectName(objectID).c_str(),\r
- propertyInfo->GetAccessor()->GetSetterName().c_str(),\r
- ValueAsCode(value).c_str()) );\r
-}\r
-\r
-void wxObjectCodeReaderCallback::SetPropertyAsObject(int objectID,\r
- const wxClassInfo *WXUNUSED(classInfo),\r
- const wxPropertyInfo* propertyInfo,\r
- int valueObjectId)\r
-{\r
- if ( propertyInfo->GetTypeInfo()->GetKind() == wxT_OBJECT )\r
- m_fp->WriteString( wxString::Format( wxT("\t%s->%s(*%s);\n"),\r
- m_data->GetObjectName(objectID).c_str(),\r
- propertyInfo->GetAccessor()->GetSetterName().c_str(),\r
- m_data->GetObjectName( valueObjectId).c_str() ) );\r
- else\r
- m_fp->WriteString( wxString::Format( wxT("\t%s->%s(%s);\n"),\r
- m_data->GetObjectName(objectID).c_str(),\r
- propertyInfo->GetAccessor()->GetSetterName().c_str(),\r
- m_data->GetObjectName( valueObjectId).c_str() ) );\r
-}\r
-\r
-void wxObjectCodeReaderCallback::AddToPropertyCollection( int objectID,\r
- const wxClassInfo *WXUNUSED(classInfo),\r
- const wxPropertyInfo* propertyInfo,\r
- const wxVariantBase &value)\r
-{\r
- m_fp->WriteString( wxString::Format( wxT("\t%s->%s(%s);\n"),\r
- m_data->GetObjectName(objectID).c_str(),\r
- propertyInfo->GetAccessor()->GetAdderName().c_str(),\r
- ValueAsCode(value).c_str()) );\r
-}\r
-\r
-// sets the corresponding property (value is an object)\r
-void wxObjectCodeReaderCallback::\r
- AddToPropertyCollectionAsObject(int WXUNUSED(objectID),\r
- const wxClassInfo *WXUNUSED(classInfo),\r
- const wxPropertyInfo* WXUNUSED(propertyInfo),\r
- int WXUNUSED(valueObjectId))\r
-{\r
- // TODO\r
-}\r
-\r
-void wxObjectCodeReaderCallback::SetConnect(int eventSourceObjectID,\r
- const wxClassInfo *WXUNUSED(eventSourceClassInfo),\r
- const wxPropertyInfo *delegateInfo,\r
- const wxClassInfo *eventSinkClassInfo,\r
- const wxHandlerInfo* handlerInfo,\r
- int eventSinkObjectID )\r
-{\r
- wxString ehsource = m_data->GetObjectName( eventSourceObjectID );\r
- wxString ehsink = m_data->GetObjectName(eventSinkObjectID);\r
- wxString ehsinkClass = eventSinkClassInfo->GetClassName();\r
- const wxEventSourceTypeInfo *delegateTypeInfo = \r
- wx_dynamic_cast(const wxEventSourceTypeInfo*, delegateInfo->GetTypeInfo());\r
- if ( delegateTypeInfo )\r
- {\r
- int eventType = delegateTypeInfo->GetEventType();\r
- wxString handlerName = handlerInfo->GetName();\r
-\r
- wxString code =\r
- wxString::Format( \r
- wxT("\t%s->Connect( %s->GetId(), %d, ")\r
- wxT("(wxObjectEventFunction)(wxEventFunction) & %s::%s, NULL, %s );"),\r
- ehsource.c_str(), ehsource.c_str(), eventType, ehsinkClass.c_str(),\r
- handlerName.c_str(), ehsink.c_str() );\r
-\r
- m_fp->WriteString( code );\r
- }\r
- else\r
- {\r
- wxLogError(_("delegate has no type info"));\r
- }\r
-}\r
+/////////////////////////////////////////////////////////////////////////////
+// Name: src/common/xtistrm.cpp
+// Purpose: streaming runtime metadata information
+// Author: Stefan Csomor
+// Modified by:
+// Created: 27/07/03
+// RCS-ID: $Id$
+// Copyright: (c) 2003 Stefan Csomor
+// Licence: wxWindows licence
+/////////////////////////////////////////////////////////////////////////////
+
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#ifdef __BORLANDC__
+ #pragma hdrstop
+#endif
+
+#include "wx/xtistrm.h"
+
+#ifndef WX_PRECOMP
+ #include "wx/object.h"
+ #include "wx/hash.h"
+ #include "wx/event.h"
+#endif
+
+#include <map>
+#include <vector>
+#include <string>
+using namespace std;
+
+#include "wx/tokenzr.h"
+#include "wx/txtstrm.h"
+#include "codereadercallback.h"
+
+#if !wxUSE_EXTENDED_RTTI
+ #error This sample requires XTI (eXtended RTTI) enabled
+#endif
+
+// ----------------------------------------------------------------------------
+// wxObjectCodeReaderCallback - depersisting to code
+// ----------------------------------------------------------------------------
+
+struct wxObjectCodeReaderCallback::wxObjectCodeReaderCallbackInternal
+{
+#if wxUSE_UNICODE
+ map<int,wstring> m_objectNames;
+#else
+ map<int,string> m_objectNames;
+#endif
+
+ void SetObjectName(int objectID, const wxString &name )
+ {
+ if ( m_objectNames.find(objectID) != m_objectNames.end() )
+ {
+ wxLogError( _("Passing a already registered object to SetObjectName") );
+ return ;
+ }
+ m_objectNames[objectID] = (const wxChar *)name;
+ }
+
+ wxString GetObjectName( int objectID )
+ {
+ if ( objectID == wxNullObjectID )
+ return wxT("NULL");
+
+ if ( m_objectNames.find(objectID) == m_objectNames.end() )
+ {
+ wxLogError( _("Passing an unkown object to GetObject") );
+ return wxEmptyString;
+ }
+ return wxString( m_objectNames[objectID].c_str() );
+ }
+};
+
+wxObjectCodeReaderCallback::wxObjectCodeReaderCallback(wxString& headerincludes, wxString &source)
+: m_headerincludes(headerincludes),m_source(source)
+{
+ m_data = new wxObjectCodeReaderCallbackInternal;
+}
+
+wxObjectCodeReaderCallback::~wxObjectCodeReaderCallback()
+{
+ delete m_data;
+}
+
+void wxObjectCodeReaderCallback::AllocateObject(int objectID, wxClassInfo *classInfo,
+ wxStringToAnyHashMap &WXUNUSED(metadata))
+{
+ if ( classInfo->GetIncludeName() != wxEmptyString)
+ {
+ // add corresponding header if not already included
+ wxString include;
+ include.Printf(wxT("#include \"%s\"\n"),classInfo->GetIncludeName());
+ if ( m_headerincludes.Find(include) == wxNOT_FOUND)
+ m_headerincludes += include;
+ }
+
+ wxString objectName = wxString::Format( wxT("LocalObject_%d"), objectID );
+ m_source += ( wxString::Format( wxT("\t%s *%s = new %s;\n"),
+ classInfo->GetClassName(),
+ objectName.c_str(),
+ classInfo->GetClassName()) );
+ m_data->SetObjectName( objectID, objectName );
+}
+
+void wxObjectCodeReaderCallback::DestroyObject(int objectID, wxClassInfo *WXUNUSED(classInfo))
+{
+ m_source += ( wxString::Format( wxT("\tdelete %s;\n"),
+ m_data->GetObjectName( objectID).c_str() ) );
+}
+
+class WXDLLIMPEXP_BASE wxObjectConstructorWriter: public wxObjectWriterFunctor
+{
+public:
+ wxObjectConstructorWriter(const wxClassTypeInfo* cti,
+ wxObjectCodeReaderCallback* writer) :
+ m_cti(cti),m_writer(writer)
+ {}
+
+ virtual void operator()(const wxObject *vobj)
+ {
+ const wxClassInfo* ci = m_cti->GetClassInfo();
+
+ for ( int i = 0; i < ci->GetCreateParamCount(); ++i )
+ {
+ wxString name = ci->GetCreateParamName(i);
+ const wxPropertyInfo* prop = ci->FindPropertyInfo(name);
+ if ( i > 0 )
+ m_constructor += ", ";
+ wxAny value;
+ prop->GetAccessor()->GetProperty(vobj, value);
+ m_constructor+= m_writer->ValueAsCode(value);
+ }
+ }
+
+ const wxString& GetConstructorString() const { return m_constructor;}
+private:
+ const wxClassTypeInfo* m_cti;
+ wxObjectCodeReaderCallback* m_writer;
+ wxString m_constructor;
+};
+
+wxString wxObjectCodeReaderCallback::ValueAsCode( const wxAny ¶m )
+{
+ wxString value;
+
+ const wxTypeInfo* type = param.GetTypeInfo();
+ if ( type->GetKind() == wxT_CUSTOM )
+ {
+ const wxCustomTypeInfo* cti = wx_dynamic_cast(const wxCustomTypeInfo*, type);
+ if ( cti )
+ {
+ value.Printf( wxT("%s(%s)"), cti->GetTypeName().c_str(),
+ wxAnyGetAsString(param).c_str() );
+ }
+ else
+ {
+ wxLogError ( _("Internal error, illegal wxCustomTypeInfo") );
+ }
+ }
+ else if ( type->GetKind() == wxT_STRING )
+ {
+ value.Printf( wxT("\"%s\""), wxAnyGetAsString(param).c_str() );
+ }
+ else if ( type->GetKind() == wxT_OBJECT )
+ {
+ const wxClassTypeInfo* ctype = wx_dynamic_cast(const wxClassTypeInfo*,type);
+ const wxClassInfo* ci = ctype->GetClassInfo();
+ if( ci->NeedsDirectConstruction())
+ {
+ wxObjectConstructorWriter cw(ctype,this);
+
+ ci->CallOnAny(param,&cw);
+
+ value.Printf( wxT("%s(%s)"), ctype->GetClassInfo()->GetClassName(),
+ cw.GetConstructorString() );
+ }
+ }
+ else
+ {
+ value.Printf( wxT("%s"), wxAnyGetAsString(param).c_str() );
+ }
+
+ return value;
+}
+
+void wxObjectCodeReaderCallback::CreateObject(int objectID,
+ const wxClassInfo *WXUNUSED(classInfo),
+ int paramCount,
+ wxAny *params,
+ int *objectIDValues,
+ const wxClassInfo **WXUNUSED(objectClassInfos),
+ wxStringToAnyHashMap &WXUNUSED(metadata)
+ )
+{
+ int i;
+ m_source += ( wxString::Format( wxT("\t%s->Create("),
+ m_data->GetObjectName(objectID).c_str() ) );
+ for (i = 0; i < paramCount; i++)
+ {
+ if ( objectIDValues[i] != wxInvalidObjectID )
+ {
+ wxString str =
+ wxString::Format( wxT("%s"),
+ m_data->GetObjectName( objectIDValues[i] ).c_str() );
+ m_source += ( str );
+ }
+ else
+ {
+ m_source += (
+ wxString::Format( wxT("%s"), ValueAsCode(params[i]).c_str() ) );
+ }
+ if (i < paramCount - 1)
+ m_source += ( wxT(", "));
+ }
+ m_source += ( wxT(");\n") );
+}
+
+void wxObjectCodeReaderCallback::ConstructObject(int objectID,
+ const wxClassInfo *classInfo,
+ int paramCount,
+ wxAny *params,
+ int *objectIDValues,
+ const wxClassInfo **WXUNUSED(objectClassInfos),
+ wxStringToAnyHashMap &WXUNUSED(metadata)
+ )
+{
+ wxString objectName = wxString::Format( wxT("LocalObject_%d"), objectID );
+ m_source += ( wxString::Format( wxT("\t%s *%s = new %s("),
+ classInfo->GetClassName(),
+ objectName.c_str(),
+ classInfo->GetClassName()) );
+ m_data->SetObjectName( objectID, objectName );
+
+ int i;
+ for (i = 0; i < paramCount; i++)
+ {
+ if ( objectIDValues[i] != wxInvalidObjectID )
+ m_source += ( wxString::Format( wxT("%s"),
+ m_data->GetObjectName( objectIDValues[i] ).c_str() ) );
+ else
+ {
+ m_source += (
+ wxString::Format( wxT("%s"), ValueAsCode(params[i]).c_str() ) );
+ }
+ if (i < paramCount - 1)
+ m_source += ( wxT(", ") );
+ }
+ m_source += ( wxT(");\n") );
+}
+
+void wxObjectCodeReaderCallback::SetProperty(int objectID,
+ const wxClassInfo *WXUNUSED(classInfo),
+ const wxPropertyInfo* propertyInfo,
+ const wxAny &value)
+{
+ m_source += ( wxString::Format( wxT("\t%s->%s(%s);\n"),
+ m_data->GetObjectName(objectID).c_str(),
+ propertyInfo->GetAccessor()->GetSetterName().c_str(),
+ ValueAsCode(value).c_str()) );
+}
+
+void wxObjectCodeReaderCallback::SetPropertyAsObject(int objectID,
+ const wxClassInfo *WXUNUSED(classInfo),
+ const wxPropertyInfo* propertyInfo,
+ int valueObjectId)
+{
+ if ( propertyInfo->GetTypeInfo()->GetKind() == wxT_OBJECT )
+ m_source += ( wxString::Format( wxT("\t%s->%s(*%s);\n"),
+ m_data->GetObjectName(objectID).c_str(),
+ propertyInfo->GetAccessor()->GetSetterName().c_str(),
+ m_data->GetObjectName( valueObjectId).c_str() ) );
+ else
+ m_source += ( wxString::Format( wxT("\t%s->%s(%s);\n"),
+ m_data->GetObjectName(objectID).c_str(),
+ propertyInfo->GetAccessor()->GetSetterName().c_str(),
+ m_data->GetObjectName( valueObjectId).c_str() ) );
+}
+
+void wxObjectCodeReaderCallback::AddToPropertyCollection( int objectID,
+ const wxClassInfo *WXUNUSED(classInfo),
+ const wxPropertyInfo* propertyInfo,
+ const wxAny &value)
+{
+ m_source += ( wxString::Format( wxT("\t%s->%s(%s);\n"),
+ m_data->GetObjectName(objectID).c_str(),
+ propertyInfo->GetAccessor()->GetAdderName().c_str(),
+ ValueAsCode(value).c_str()) );
+}
+
+// sets the corresponding property (value is an object)
+void wxObjectCodeReaderCallback::
+ AddToPropertyCollectionAsObject(int WXUNUSED(objectID),
+ const wxClassInfo *WXUNUSED(classInfo),
+ const wxPropertyInfo* WXUNUSED(propertyInfo),
+ int WXUNUSED(valueObjectId))
+{
+ // TODO
+}
+
+void wxObjectCodeReaderCallback::SetConnect(int eventSourceObjectID,
+ const wxClassInfo *WXUNUSED(eventSourceClassInfo),
+ const wxPropertyInfo *delegateInfo,
+ const wxClassInfo *eventSinkClassInfo,
+ const wxHandlerInfo* handlerInfo,
+ int eventSinkObjectID )
+{
+ wxString ehsource = m_data->GetObjectName( eventSourceObjectID );
+ wxString ehsink = m_data->GetObjectName(eventSinkObjectID);
+ wxString ehsinkClass = eventSinkClassInfo->GetClassName();
+ const wxEventSourceTypeInfo *delegateTypeInfo =
+ wx_dynamic_cast(const wxEventSourceTypeInfo*, delegateInfo->GetTypeInfo());
+ if ( delegateTypeInfo )
+ {
+ int eventType = delegateTypeInfo->GetEventType();
+ wxString handlerName = handlerInfo->GetName();
+
+ wxString code =
+ wxString::Format(
+ wxT("\t%s->Connect( %s->GetId(), %d, ")
+ wxT("(wxObjectEventFunction)(wxEventFunction) & %s::%s, NULL, %s );"),
+ ehsource.c_str(), ehsource.c_str(), eventType, ehsinkClass.c_str(),
+ handlerName.c_str(), ehsink.c_str() );
+
+ m_source += ( code );
+ }
+ else
+ {
+ wxLogError(_("delegate has no type info"));
+ }
+}