forked from boostorg/utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_from_member_test.cpp
595 lines (498 loc) · 15.3 KB
/
base_from_member_test.cpp
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Boost test program for base-from-member class templates -----------------//
// Copyright 2001, 2003 Daryle Walker. Use, modification, and distribution are
// subject to the Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
// See <http://www.boost.org/libs/utility/> for the library's home page.
// Revision History
// 14 Jun 2003 Adjusted code for Boost.Test changes (Daryle Walker)
// 29 Aug 2001 Initial Version (Daryle Walker)
#include <boost/test/minimal.hpp> // for BOOST_CHECK, main
#include <boost/config.hpp> // for BOOST_NO_MEMBER_TEMPLATES
#include <boost/cstdlib.hpp> // for boost::exit_success
#include <boost/noncopyable.hpp> // for boost::noncopyable
#include <boost/utility/base_from_member.hpp> // for boost::base_from_member
#include <functional> // for std::binary_function, std::less
#include <iostream> // for std::cout (std::ostream, std::endl indirectly)
#include <set> // for std::set
#include <typeinfo> // for std::type_info
#include <utility> // for std::pair, std::make_pair
#include <vector> // for std::vector
// Control if extra information is printed
#ifndef CONTROL_EXTRA_PRINTING
#define CONTROL_EXTRA_PRINTING 1
#endif
// A (sub)object can be identified by its memory location and its type.
// Both are needed since an object can start at the same place as its
// first base class subobject and/or contained subobject.
typedef std::pair< void *, std::type_info const * > object_id;
// Object IDs need to be printed
std::ostream & operator <<( std::ostream &os, object_id const &oi );
// A way to generate an object ID
template < typename T >
object_id identify( T &obj );
// A custom comparison type is needed
struct object_id_compare
: std::binary_function<object_id, object_id, bool>
{
bool operator ()( object_id const &a, object_id const &b ) const;
}; // object_id_compare
// A singleton of this type coordinates the acknowledgements
// of objects being created and used.
class object_registrar
: private boost::noncopyable
{
public:
#ifndef BOOST_NO_MEMBER_TEMPLATES
template < typename T >
void register_object( T &obj )
{ this->register_object_imp( identify(obj) ); }
template < typename T, typename U >
void register_use( T &owner, U &owned )
{ this->register_use_imp( identify(owner), identify(owned) ); }
template < typename T, typename U >
void unregister_use( T &owner, U &owned )
{ this->unregister_use_imp( identify(owner), identify(owned) ); }
template < typename T >
void unregister_object( T &obj )
{ this->unregister_object_imp( identify(obj) ); }
#endif
void register_object_imp( object_id obj );
void register_use_imp( object_id owner, object_id owned );
void unregister_use_imp( object_id owner, object_id owned );
void unregister_object_imp( object_id obj );
typedef std::set<object_id, object_id_compare> set_type;
typedef std::vector<object_id> error_record_type;
typedef std::vector< std::pair<object_id, object_id> > error_pair_type;
set_type db_;
error_pair_type defrauders_in_, defrauders_out_;
error_record_type overeager_, overkilled_;
}; // object_registrar
// A sample type to be used by containing types
class base_or_member
{
public:
explicit base_or_member( int x = 1, double y = -0.25 );
~base_or_member();
}; // base_or_member
// A sample type that uses base_or_member, used
// as a base for the main demonstration classes
class base_class
{
public:
explicit base_class( base_or_member &x, base_or_member *y = 0,
base_or_member *z = 0 );
~base_class();
private:
base_or_member *x_, *y_, *z_;
}; // base_class
// This bad class demonstrates the direct method of a base class needing
// to be initialized by a member. This is improper since the member
// isn't initialized until after the base class.
class bad_class
: public base_class
{
public:
bad_class();
~bad_class();
private:
base_or_member x_;
}; // bad_class
// The first good class demonstrates the correct way to initialize a
// base class with a member. The member is changed to another base
// class, one that is initialized before the base that needs it.
class good_class_1
: private boost::base_from_member<base_or_member>
, public base_class
{
typedef boost::base_from_member<base_or_member> pbase_type;
typedef base_class base_type;
public:
good_class_1();
~good_class_1();
}; // good_class_1
// The second good class also demonstrates the correct way to initialize
// base classes with other subobjects. This class uses the other helpers
// in the library, and shows the technique of using two base subobjects
// of the "same" type.
class good_class_2
: private boost::base_from_member<base_or_member, 0>
, private boost::base_from_member<base_or_member, 1>
, private boost::base_from_member<base_or_member, 2>
, public base_class
{
typedef boost::base_from_member<base_or_member, 0> pbase_type0;
typedef boost::base_from_member<base_or_member, 1> pbase_type1;
typedef boost::base_from_member<base_or_member, 2> pbase_type2;
typedef base_class base_type;
public:
good_class_2();
~good_class_2();
}; // good_class_2
// Declare/define the single object registrar
object_registrar obj_reg;
// Main functionality
int
test_main( int , char * [] )
{
BOOST_CHECK( obj_reg.db_.empty() );
BOOST_CHECK( obj_reg.defrauders_in_.empty() );
BOOST_CHECK( obj_reg.defrauders_out_.empty() );
BOOST_CHECK( obj_reg.overeager_.empty() );
BOOST_CHECK( obj_reg.overkilled_.empty() );
// Make a separate block to examine pre- and post-effects
{
using std::cout;
using std::endl;
bad_class bc;
BOOST_CHECK( obj_reg.db_.size() == 3 );
BOOST_CHECK( obj_reg.defrauders_in_.size() == 1 );
good_class_1 gc1;
BOOST_CHECK( obj_reg.db_.size() == 6 );
BOOST_CHECK( obj_reg.defrauders_in_.size() == 1 );
good_class_2 gc2;
BOOST_CHECK( obj_reg.db_.size() == 11 );
BOOST_CHECK( obj_reg.defrauders_in_.size() == 1 );
BOOST_CHECK( obj_reg.defrauders_out_.empty() );
BOOST_CHECK( obj_reg.overeager_.empty() );
BOOST_CHECK( obj_reg.overkilled_.empty() );
// Getting the addresses of the objects ensure
// that they're used, and not optimized away.
cout << "Object 'bc' is at " << &bc << '.' << endl;
cout << "Object 'gc1' is at " << &gc1 << '.' << endl;
cout << "Object 'gc2' is at " << &gc2 << '.' << endl;
}
BOOST_CHECK( obj_reg.db_.empty() );
BOOST_CHECK( obj_reg.defrauders_in_.size() == 1 );
BOOST_CHECK( obj_reg.defrauders_out_.size() == 1 );
BOOST_CHECK( obj_reg.overeager_.empty() );
BOOST_CHECK( obj_reg.overkilled_.empty() );
return boost::exit_success;
}
// Print an object's ID
std::ostream &
operator <<
(
std::ostream & os,
object_id const & oi
)
{
// I had an std::ostringstream to help, but I did not need it since
// the program never screws around with formatting. Worse, using
// std::ostringstream is an issue with some compilers.
return os << '[' << ( oi.second ? oi.second->name() : "NOTHING" )
<< " at " << oi.first << ']';
}
// Get an object ID given an object
template < typename T >
inline
object_id
identify
(
T & obj
)
{
return std::make_pair( static_cast<void *>(&obj), &(typeid( obj )) );
}
// Compare two object IDs
bool
object_id_compare::operator ()
(
object_id const & a,
object_id const & b
) const
{
std::less<void *> vp_cmp;
if ( vp_cmp(a.first, b.first) )
{
return true;
}
else if ( vp_cmp(b.first, a.first) )
{
return false;
}
else
{
// object pointers are equal, compare the types
if ( a.second == b.second )
{
return false;
}
else if ( !a.second )
{
return true; // NULL preceeds anything else
}
else if ( !b.second )
{
return false; // NULL preceeds anything else
}
else
{
return a.second->before( *b.second ) != 0;
}
}
}
// Let an object register its existence
void
object_registrar::register_object_imp
(
object_id obj
)
{
if ( db_.count(obj) <= 0 )
{
db_.insert( obj );
#if CONTROL_EXTRA_PRINTING
std::cout << "Registered " << obj << '.' << std::endl;
#endif
}
else
{
overeager_.push_back( obj );
#if CONTROL_EXTRA_PRINTING
std::cout << "Attempted to register a non-existant " << obj
<< '.' << std::endl;
#endif
}
}
// Let an object register its use of another object
void
object_registrar::register_use_imp
(
object_id owner,
object_id owned
)
{
if ( db_.count(owned) > 0 )
{
// We don't care to record usage registrations
}
else
{
defrauders_in_.push_back( std::make_pair(owner, owned) );
#if CONTROL_EXTRA_PRINTING
std::cout << "Attempted to own a non-existant " << owned
<< " by " << owner << '.' << std::endl;
#endif
}
}
// Let an object un-register its use of another object
void
object_registrar::unregister_use_imp
(
object_id owner,
object_id owned
)
{
if ( db_.count(owned) > 0 )
{
// We don't care to record usage un-registrations
}
else
{
defrauders_out_.push_back( std::make_pair(owner, owned) );
#if CONTROL_EXTRA_PRINTING
std::cout << "Attempted to disown a non-existant " << owned
<< " by " << owner << '.' << std::endl;
#endif
}
}
// Let an object un-register its existence
void
object_registrar::unregister_object_imp
(
object_id obj
)
{
set_type::iterator const i = db_.find( obj );
if ( i != db_.end() )
{
db_.erase( i );
#if CONTROL_EXTRA_PRINTING
std::cout << "Unregistered " << obj << '.' << std::endl;
#endif
}
else
{
overkilled_.push_back( obj );
#if CONTROL_EXTRA_PRINTING
std::cout << "Attempted to unregister a non-existant " << obj
<< '.' << std::endl;
#endif
}
}
// Macros to abstract the registration of objects
#ifndef BOOST_NO_MEMBER_TEMPLATES
#define PRIVATE_REGISTER_BIRTH(o) obj_reg.register_object( (o) )
#define PRIVATE_REGISTER_DEATH(o) obj_reg.unregister_object( (o) )
#define PRIVATE_REGISTER_USE(o, w) obj_reg.register_use( (o), (w) )
#define PRIVATE_UNREGISTER_USE(o, w) obj_reg.unregister_use( (o), (w) )
#else
#define PRIVATE_REGISTER_BIRTH(o) obj_reg.register_object_imp( \
identify((o)) )
#define PRIVATE_REGISTER_DEATH(o) obj_reg.unregister_object_imp( \
identify((o)) )
#define PRIVATE_REGISTER_USE(o, w) obj_reg.register_use_imp( identify((o)), \
identify((w)) )
#define PRIVATE_UNREGISTER_USE(o, w) obj_reg.unregister_use_imp( \
identify((o)), identify((w)) )
#endif
// Create a base_or_member, with arguments to simulate member initializations
base_or_member::base_or_member
(
int x, // = 1
double y // = -0.25
)
{
PRIVATE_REGISTER_BIRTH( *this );
#if CONTROL_EXTRA_PRINTING
std::cout << "\tMy x-factor is " << x << " and my y-factor is " << y
<< '.' << std::endl;
#endif
}
// Destroy a base_or_member
inline
base_or_member::~base_or_member
(
)
{
PRIVATE_REGISTER_DEATH( *this );
}
// Create a base_class, registering any objects used
base_class::base_class
(
base_or_member & x,
base_or_member * y, // = 0
base_or_member * z // = 0
)
: x_( &x ), y_( y ), z_( z )
{
PRIVATE_REGISTER_BIRTH( *this );
#if CONTROL_EXTRA_PRINTING
std::cout << "\tMy x-factor is " << x_;
#endif
PRIVATE_REGISTER_USE( *this, *x_ );
if ( y_ )
{
#if CONTROL_EXTRA_PRINTING
std::cout << ", my y-factor is " << y_;
#endif
PRIVATE_REGISTER_USE( *this, *y_ );
}
if ( z_ )
{
#if CONTROL_EXTRA_PRINTING
std::cout << ", my z-factor is " << z_;
#endif
PRIVATE_REGISTER_USE( *this, *z_ );
}
#if CONTROL_EXTRA_PRINTING
std::cout << '.' << std::endl;
#endif
}
// Destroy a base_class, unregistering the objects it uses
base_class::~base_class
(
)
{
PRIVATE_REGISTER_DEATH( *this );
#if CONTROL_EXTRA_PRINTING
std::cout << "\tMy x-factor was " << x_;
#endif
PRIVATE_UNREGISTER_USE( *this, *x_ );
if ( y_ )
{
#if CONTROL_EXTRA_PRINTING
std::cout << ", my y-factor was " << y_;
#endif
PRIVATE_UNREGISTER_USE( *this, *y_ );
}
if ( z_ )
{
#if CONTROL_EXTRA_PRINTING
std::cout << ", my z-factor was " << z_;
#endif
PRIVATE_UNREGISTER_USE( *this, *z_ );
}
#if CONTROL_EXTRA_PRINTING
std::cout << '.' << std::endl;
#endif
}
// Create a bad_class, noting the improper construction order
bad_class::bad_class
(
)
: x_( -7, 16.75 ), base_class( x_ ) // this order doesn't matter
{
PRIVATE_REGISTER_BIRTH( *this );
#if CONTROL_EXTRA_PRINTING
std::cout << "\tMy factor is at " << &x_
<< " and my base is at " << static_cast<base_class *>(this) << '.'
<< std::endl;
#endif
}
// Destroy a bad_class, noting the improper destruction order
bad_class::~bad_class
(
)
{
PRIVATE_REGISTER_DEATH( *this );
#if CONTROL_EXTRA_PRINTING
std::cout << "\tMy factor was at " << &x_
<< " and my base was at " << static_cast<base_class *>(this)
<< '.' << std::endl;
#endif
}
// Create a good_class_1, noting the proper construction order
good_class_1::good_class_1
(
)
: pbase_type( 8 ), base_type( member )
{
PRIVATE_REGISTER_BIRTH( *this );
#if CONTROL_EXTRA_PRINTING
std::cout << "\tMy factor is at " << &member
<< " and my base is at " << static_cast<base_class *>(this) << '.'
<< std::endl;
#endif
}
// Destroy a good_class_1, noting the proper destruction order
good_class_1::~good_class_1
(
)
{
PRIVATE_REGISTER_DEATH( *this );
#if CONTROL_EXTRA_PRINTING
std::cout << "\tMy factor was at " << &member
<< " and my base was at " << static_cast<base_class *>(this)
<< '.' << std::endl;
#endif
}
// Create a good_class_2, noting the proper construction order
good_class_2::good_class_2
(
)
: pbase_type0(), pbase_type1(-16, 0.125), pbase_type2(2, -3)
, base_type( pbase_type1::member, &this->pbase_type0::member,
&this->pbase_type2::member )
{
PRIVATE_REGISTER_BIRTH( *this );
#if CONTROL_EXTRA_PRINTING
std::cout << "\tMy factors are at " << &this->pbase_type0::member
<< ", " << &this->pbase_type1::member << ", "
<< &this->pbase_type2::member << ", and my base is at "
<< static_cast<base_class *>(this) << '.' << std::endl;
#endif
}
// Destroy a good_class_2, noting the proper destruction order
good_class_2::~good_class_2
(
)
{
PRIVATE_REGISTER_DEATH( *this );
#if CONTROL_EXTRA_PRINTING
std::cout << "\tMy factors were at " << &this->pbase_type0::member
<< ", " << &this->pbase_type1::member << ", "
<< &this->pbase_type2::member << ", and my base was at "
<< static_cast<base_class *>(this) << '.' << std::endl;
#endif
}