xtistrm.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 852 行 · 第 1/3 页
CPP
852 行
/////////////////////////////////////////////////////////////////////////////
// Name: src/common/xtistrm.cpp
// Purpose: streaming runtime metadata information
// Author: Stefan Csomor
// Modified by:
// Created: 27/07/03
// RCS-ID: $Id: xtistrm.cpp,v 1.25 2004/09/24 14:32:35 ABX Exp $
// Copyright: (c) 2003 Stefan Csomor
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "xtistrm.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/hash.h"
#include "wx/object.h"
#endif
#include "wx/tokenzr.h"
#include "wx/txtstrm.h"
#include "wx/event.h"
#if wxUSE_EXTENDED_RTTI
#include "wx/xtistrm.h"
#include "wx/beforestd.h"
#include <map>
#include <vector>
#include <string>
#include "wx/afterstd.h"
using namespace std ;
struct wxWriter::wxWriterInternal
{
map< const wxObject* , int > m_writtenObjects ;
int m_nextId ;
} ;
wxWriter::wxWriter()
{
m_data = new wxWriterInternal ;
m_data->m_nextId = 0 ;
}
wxWriter::~wxWriter()
{
delete m_data ;
}
struct wxWriter::wxWriterInternalPropertiesData
{
char nothing ;
} ;
void wxWriter::ClearObjectContext()
{
delete m_data ;
m_data = new wxWriterInternal() ;
m_data->m_nextId = 0 ;
}
void wxWriter::WriteObject(const wxObject *object, const wxClassInfo *classInfo , wxPersister *persister , const wxString &name , wxxVariantArray &metadata )
{
DoBeginWriteTopLevelEntry( name ) ;
WriteObject( object , classInfo , persister , false , metadata) ;
DoEndWriteTopLevelEntry( name ) ;
}
void wxWriter::WriteObject(const wxObject *object, const wxClassInfo *classInfo , wxPersister *persister , bool isEmbedded, wxxVariantArray &metadata )
{
if ( !classInfo->BeforeWriteObject( object , this , persister , metadata) )
return ;
if ( persister->BeforeWriteObject( this , object , classInfo , metadata) )
{
if ( object == NULL )
DoWriteNullObject() ;
else if ( IsObjectKnown( object ) )
DoWriteRepeatedObject( GetObjectID(object) ) ;
else
{
int oid = m_data->m_nextId++ ;
if ( !isEmbedded )
m_data->m_writtenObjects[object] = oid ;
// in case this object is a wxDynamicObject we also have to insert is superclass
// instance with the same id, so that object relations are streamed out correctly
const wxDynamicObject* dynobj = dynamic_cast<const wxDynamicObject *>( object ) ;
if ( !isEmbedded && dynobj )
m_data->m_writtenObjects[dynobj->GetSuperClassInstance()] = oid ;
DoBeginWriteObject( object , classInfo , oid , metadata ) ;
wxWriterInternalPropertiesData data ;
WriteAllProperties( object , classInfo , persister , &data ) ;
DoEndWriteObject( object , classInfo , oid ) ;
}
persister->AfterWriteObject( this ,object , classInfo ) ;
}
}
void wxWriter::FindConnectEntry(const wxEvtHandler * evSource,const wxDelegateTypeInfo* dti, const wxObject* &sink , const wxHandlerInfo *&handler)
{
wxList *dynamicEvents = evSource->GetDynamicEventTable() ;
if ( dynamicEvents )
{
wxList::compatibility_iterator node = dynamicEvents->GetFirst();
while (node)
{
wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
// find the match
if ( entry->m_fn &&
(dti->GetEventType() == entry->m_eventType) &&
(entry->m_id == -1 ) &&
(entry->m_eventSink != NULL ) )
{
sink = entry->m_eventSink ;
const wxClassInfo* sinkClassInfo = sink->GetClassInfo() ;
const wxHandlerInfo* sinkHandler = sinkClassInfo->GetFirstHandler() ;
while ( sinkHandler )
{
if ( sinkHandler->GetEventFunction() == entry->m_fn )
{
handler = sinkHandler ;
break ;
}
sinkHandler = sinkHandler->GetNext() ;
}
break ;
}
node = node->GetNext();
}
}
}
void wxWriter::WriteAllProperties( const wxObject * obj , const wxClassInfo* ci , wxPersister *persister, wxWriterInternalPropertiesData * data )
{
wxPropertyInfoMap map ;
ci->GetProperties( map ) ;
for ( int i = 0 ; i < ci->GetCreateParamCount() ; ++i )
{
wxString name = ci->GetCreateParamName(i) ;
const wxPropertyInfo* prop = map.find(name)->second ;
if ( prop )
{
WriteOneProperty( obj , prop->GetDeclaringClass() , prop , persister , data ) ;
}
else
{
wxLogError( _("Create Parameter not found in declared RTTI Parameters") ) ;
}
map.erase( name ) ;
}
{ // Extra block for broken compilers
for( wxPropertyInfoMap::iterator iter = map.begin() ; iter != map.end() ; ++iter )
{
const wxPropertyInfo* prop = iter->second ;
if ( prop->GetFlags() & wxPROP_OBJECT_GRAPH )
{
WriteOneProperty( obj , prop->GetDeclaringClass() , prop , persister , data ) ;
}
}
}
{ // Extra block for broken compilers
for( wxPropertyInfoMap::iterator iter = map.begin() ; iter != map.end() ; ++iter )
{
const wxPropertyInfo* prop = iter->second ;
if ( !(prop->GetFlags() & wxPROP_OBJECT_GRAPH) )
{
WriteOneProperty( obj , prop->GetDeclaringClass() , prop , persister , data ) ;
}
}
}
}
void wxWriter::WriteOneProperty( const wxObject *obj , const wxClassInfo* ci , const wxPropertyInfo* pi , wxPersister *persister , wxWriterInternalPropertiesData *WXUNUSED(data) )
{
if ( pi->GetFlags() & wxPROP_DONT_STREAM )
return ;
// make sure that we are picking the correct object for accessing the property
const wxDynamicObject* dynobj = dynamic_cast< const wxDynamicObject* > (obj ) ;
if ( dynobj && (dynamic_cast<const wxDynamicClassInfo*>(ci) == NULL) )
obj = dynobj->GetSuperClassInstance() ;
if ( pi->GetTypeInfo()->GetKind() == wxT_COLLECTION )
{
wxxVariantArray data ;
pi->GetAccessor()->GetPropertyCollection(obj, data) ;
const wxTypeInfo * elementType = dynamic_cast< const wxCollectionTypeInfo* >( pi->GetTypeInfo() )->GetElementType() ;
for ( size_t i = 0 ; i < data.GetCount() ; ++i )
{
if ( i == 0 )
DoBeginWriteProperty( pi ) ;
DoBeginWriteElement() ;
wxxVariant value = data[i] ;
if ( persister->BeforeWriteProperty( this , obj, pi , value ) )
{
const wxClassTypeInfo* cti = dynamic_cast< const wxClassTypeInfo* > ( elementType ) ;
if ( cti )
{
const wxClassInfo* pci = cti->GetClassInfo() ;
wxObject *vobj = pci->VariantToInstance( value ) ;
wxxVariantArray md ;
WriteObject( vobj , (vobj ? vobj->GetClassInfo() : pci ) , persister , cti->GetKind()== wxT_OBJECT , md ) ;
}
else
{
DoWriteSimpleType( value ) ;
}
}
DoEndWriteElement() ;
if ( i == data.GetCount() - 1 )
DoEndWriteProperty( pi ) ;
}
}
else
{
const wxDelegateTypeInfo* dti = dynamic_cast< const wxDelegateTypeInfo* > ( pi->GetTypeInfo() ) ;
if ( dti )
{
const wxObject* sink = NULL ;
const wxHandlerInfo *handler = NULL ;
const wxEvtHandler * evSource = dynamic_cast<const wxEvtHandler *>(obj) ;
if ( evSource )
{
FindConnectEntry( evSource , dti , sink , handler ) ;
if ( persister->BeforeWriteDelegate( this , obj , ci , pi , sink , handler ) )
{
if ( sink != NULL && handler != NULL )
{
DoBeginWriteProperty( pi ) ;
if ( IsObjectKnown( sink ) )
{
DoWriteDelegate( obj , ci , pi , sink , GetObjectID( sink ) , sink->GetClassInfo() , handler ) ;
DoEndWriteProperty( pi ) ;
}
else
{
wxLogError( _("Streaming delegates for not already streamed objects not yet supported") ) ;
}
}
}
}
else
{
wxLogError(_("Illegal Object Class (Non-wxEvtHandler) as Event Source") ) ;
}
}
else
{
wxxVariant value ;
pi->GetAccessor()->GetProperty(obj, value) ;
// avoid streaming out void objects
if( value.IsEmpty() )
return ;
if ( pi->GetFlags() & wxPROP_ENUM_STORE_LONG )
{
const wxEnumTypeInfo *eti = dynamic_cast<const wxEnumTypeInfo*>( pi->GetTypeInfo() ) ;
if ( eti )
{
eti->ConvertFromLong( value.wxTEMPLATED_MEMBER_CALL(Get , long) , value ) ;
}
else
{
wxLogError( _("Type must have enum - long conversion") ) ;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?