File: codereadercallback.cpp

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (331 lines) | stat: -rw-r--r-- 11,645 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/////////////////////////////////////////////////////////////////////////////
// Name:        src/common/xtistrm.cpp
// Purpose:     streaming runtime metadata information
// Author:      Stefan Csomor
// Modified by:
// Created:     27/07/03
// 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 &param )
{
    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"));
    }
}