File: minheritance.cpp

package info (click to toggle)
fox 1.0.52-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,788 kB
  • ctags: 13,384
  • sloc: cpp: 96,482; sh: 8,338; ansic: 1,935; makefile: 1,010; perl: 32
file content (349 lines) | stat: -rw-r--r-- 8,954 bytes parent folder | download
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/********************************************************************************
*                                                                               *
*                           Multiple Inheritance Test                           *
*                                                                               *
*********************************************************************************
* Copyright (C) 1998 by Jeroen van der Zijp.   All Rights Reserved.             *
*********************************************************************************
* $Id: minheritance.cpp,v 1.12 2001/11/16 22:59:26 jeroen Exp $                 *
********************************************************************************/
#include "fx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*

  Test for proper code generation for the case of multiple inheritance.

  We have tabulated the following cases:

    | Message map where handler is found | Class which message handler is member of 
  --+------------------------------------+------------------------------------------
  1 |     derived class                  |        derived class                     
  2 |     one of the base classes        |        one of the base classes           
  3 |     derived class                  |        one of the base classes           
  4 |     one of the base classes        |        derived class                     


  Ad 1. The most common scenario, and typically presents no problem, as the
        "this" pointer and pointer-to-member match up.

  Ad 2. The "this" pointer needs to be adjusted properly so as to resolve to
        the correct struct offset in the compound object where one of the bases
        is located.

  Ad 3. A handler which is defined in the base class is entered into the
        message map of the derived class.  This is sometimes done to give an
        existing member function an additional binding to a different message-id.
        
        According to the C++ rules (contravariance rule, pp. 420 3rd ed. C++ book),
        one can assign a pointer to member of base class to a pointer to
        member of derived class, and thats what we do.

  Ad 4. This case is, of course, impossible!! I list it here for completeness
        sake, and to see if you're paying attention...


  To test properly, we try several orderings of the two base classes.  
  We print out the "this" address in the ctor and the handler so as to see
  if the correct offset is being added when the handler is actually being
  called.

  Note we're currently assuming only one of the base classes is a FOX class;
  this limitation could be removed by making more macros:

    FXIMPLEMENT2
    FXIMPLEMENT3
        ...
    ad nauseam    

  Nausea starts at N=2 for me...

*/
/*******************************************************************************/

// Non-FOX object
class Base1 {
protected:
  int a;
public:
  Base1();
  virtual ~Base1();
  };


// FOX object
class Base2 : public FXObject {
  FXDECLARE(Base2)
protected:
  int b;
public:
  enum {
    ID_BASE2=1,
    ID_LAST
    };
public:
  long onCmdBase2(FXObject*,FXSelector,void*);
public:
  Base2();
  virtual ~Base2();
  };

  
  
// Non-FOX object
class Base3 {
protected:
  int c;
public:
public:
  Base3();
  virtual ~Base3();
  };

  
// FOX object with mix-in
class TwoBaseOne : public Base1, public Base2 {
  FXDECLARE(TwoBaseOne)
protected:
  int d;
public:
  enum {
    ID_TWOBASEONE=Base2::ID_LAST,
    ID_LAST
    };
public:
  long onCmdTwoBaseOne(FXObject*,FXSelector,void*);
public:
  TwoBaseOne();
  virtual ~TwoBaseOne();
  };

  
  
// FOX object with mix-in
class TwoBaseTwo : public Base2, public Base1 {
  FXDECLARE(TwoBaseTwo)
protected:
  int e;
public:
  enum {
    ID_TWOBASETWO=Base2::ID_LAST,
    ID_LAST
    };
public:
  long onCmdTwoBaseTwo(FXObject*,FXSelector,void*);
public:
  TwoBaseTwo();
  virtual ~TwoBaseTwo();
  };
  
  
// FOX object with mix-in
class ThreeBase : public Base3, public TwoBaseOne {
  FXDECLARE(ThreeBase)
protected:
  int f;
public:
  enum {
    ID_THREEBASE=TwoBaseOne::ID_LAST,
    ID_TWOBASEONE,
    ID_BASE2,
    ID_LAST
    };
public:
  long onCmdThreeBase(FXObject*,FXSelector,void*);
public:
  ThreeBase();
  virtual ~ThreeBase();
  };


/*******************************************************************************/
  
Base1::Base1(){
  FXTRACE((100,"Base1::Base1 at %08lx\n",(unsigned long)this));
  a=1;
  }


Base1::~Base1(){
  FXTRACE((100,"Base1::~Base1\n"));
  }

/*******************************************************************************/


FXDEFMAP(Base2) Base2Map[]={
  FXMAPFUNC(SEL_COMMAND,Base2::ID_BASE2,Base2::onCmdBase2),
  };
  
FXIMPLEMENT(Base2,FXObject,Base2Map,ARRAYNUMBER(Base2Map))

Base2::Base2(){
  FXTRACE((100,"Base2::Base2 at %08lx\n",(unsigned long)this));
  b=2;
  }

long Base2::onCmdBase2(FXObject*,FXSelector,void*){
  FXTRACE((100,"Base2::onCmdBase2 at %08lx b=%d\n",(unsigned long)this,b));
  return 1;
  }

Base2::~Base2(){
  FXTRACE((100,"Base2::~Base2\n"));
  }


/*******************************************************************************/


Base3::Base3(){
  FXTRACE((100,"Base3::Base3 at %08lx\n",(unsigned long)this));
  c=3;
  }


Base3::~Base3(){
  FXTRACE((100,"Base3::~Base3\n"));
  }



/*******************************************************************************/


FXDEFMAP(TwoBaseOne) TwoBaseOneMap[]={
  FXMAPFUNC(SEL_COMMAND,TwoBaseOne::ID_TWOBASEONE,TwoBaseOne::onCmdTwoBaseOne),
  };
  
FXIMPLEMENT(TwoBaseOne,Base2,TwoBaseOneMap,ARRAYNUMBER(TwoBaseOneMap))

TwoBaseOne::TwoBaseOne(){
  FXTRACE((100,"TwoBaseOne::TwoBaseOne at %08lx\n",(unsigned long)this));
  d=4;
  }

long TwoBaseOne::onCmdTwoBaseOne(FXObject*,FXSelector,void*){
  FXTRACE((100,"TwoBaseOne::onCmdTwoBaseOne at %08lx d=%d\n",(unsigned long)this,d));
  return 1;
  }

TwoBaseOne::~TwoBaseOne(){
  FXTRACE((100,"TwoBaseOne::~TwoBaseOne\n"));
  }

/*******************************************************************************/


FXDEFMAP(TwoBaseTwo) TwoBaseTwoMap[]={
  FXMAPFUNC(SEL_COMMAND,TwoBaseTwo::ID_TWOBASETWO,TwoBaseTwo::onCmdTwoBaseTwo),
  };
  
FXIMPLEMENT(TwoBaseTwo,Base2,TwoBaseTwoMap,ARRAYNUMBER(TwoBaseTwoMap))

TwoBaseTwo::TwoBaseTwo(){
  FXTRACE((100,"TwoBaseTwo::TwoBaseTwo at %08lx\n",(unsigned long)this));
  e=4;
  }

long TwoBaseTwo::onCmdTwoBaseTwo(FXObject*,FXSelector,void*){
  FXTRACE((100,"TwoBaseTwo::onCmdTwoBaseTwo at %08lx e=%d\n",(unsigned long)this,e));
  return 1;
  }

TwoBaseTwo::~TwoBaseTwo(){
  FXTRACE((100,"TwoBaseTwo::~TwoBaseTwo\n"));
  }


/*******************************************************************************/


FXDEFMAP(ThreeBase) ThreeBaseMap[]={
  FXMAPFUNC(SEL_COMMAND,ThreeBase::ID_THREEBASE,ThreeBase::onCmdThreeBase),
  FXMAPFUNC(SEL_COMMAND,ThreeBase::ID_TWOBASEONE,ThreeBase::onCmdTwoBaseOne),
  FXMAPFUNC(SEL_COMMAND,ThreeBase::ID_BASE2,ThreeBase::onCmdBase2),
  };
  
FXIMPLEMENT(ThreeBase,TwoBaseOne,ThreeBaseMap,ARRAYNUMBER(ThreeBaseMap))

ThreeBase::ThreeBase(){
  FXTRACE((100,"ThreeBase::ThreeBase at %08lx\n",(unsigned long)this));
  f=5;
  }

long ThreeBase::onCmdThreeBase(FXObject*,FXSelector,void*){
  FXTRACE((100,"ThreeBase::onCmdThreeBase at %08lx f=%d\n",(unsigned long)this,f));
  return 1;
  }

ThreeBase::~ThreeBase(){
  FXTRACE((100,"ThreeBase::~ThreeBase\n"));
  }


/*******************************************************************************/


// Start the whole thing
int main(int,char**){
  fxTraceLevel=101;

  {
  TwoBaseOne twobase1;
  
  // Found in TwoBaseOne
  FXTRACE((100,"calling TwoBaseOne\n"));
  twobase1.handle(NULL,MKUINT(TwoBaseOne::ID_TWOBASEONE,SEL_COMMAND),NULL);
  
  // Found in Base2
  FXTRACE((100,"calling Base2\n"));
  twobase1.handle(NULL,MKUINT(Base2::ID_BASE2,SEL_COMMAND),NULL);
  }

  FXTRACE((100,"=============\n"));

  {
  TwoBaseTwo twobase2;

  // Found in TwoBaseTwo
  FXTRACE((100,"calling TwoBaseTwo\n"));
  twobase2.handle(NULL,MKUINT(TwoBaseTwo::ID_TWOBASETWO,SEL_COMMAND),NULL);

  // Found in Base2
  FXTRACE((100,"calling Base2\n"));
  twobase2.handle(NULL,MKUINT(Base2::ID_BASE2,SEL_COMMAND),NULL);
  }

  FXTRACE((100,"=============\n"));

  {
  ThreeBase threebase;
  
  // Found in ThreeBase
  FXTRACE((100,"calling ThreeBase\n"));
  threebase.handle(NULL,MKUINT(ThreeBase::ID_THREEBASE,SEL_COMMAND),NULL);
  
  // Found in TwoBaseOne
  FXTRACE((100,"calling TwoBaseOne\n"));
  threebase.handle(NULL,MKUINT(TwoBaseOne::ID_TWOBASEONE,SEL_COMMAND),NULL);
  
  // Found in Base2
  FXTRACE((100,"calling Base2\n"));
  threebase.handle(NULL,MKUINT(Base2::ID_BASE2,SEL_COMMAND),NULL);
  
  // Found in TwoBaseOne
  FXTRACE((100,"calling TwoBaseOne via ThreeBase\n"));
  threebase.handle(NULL,MKUINT(ThreeBase::ID_TWOBASEONE,SEL_COMMAND),NULL);
  
  // Found in Base2
  FXTRACE((100,"calling Base2 via ThreeBase\n"));
  threebase.handle(NULL,MKUINT(ThreeBase::ID_BASE2,SEL_COMMAND),NULL);
  }

  return 1;
  }