base_from_member_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // Boost test program for base-from-member class templates -----------------//
  2. // Copyright 2001, 2003 Daryle Walker. Use, modification, and distribution are
  3. // subject to the Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
  5. // See <http://www.boost.org/libs/utility/> for the library's home page.
  6. // Revision History
  7. // 14 Jun 2003 Adjusted code for Boost.Test changes (Daryle Walker)
  8. // 29 Aug 2001 Initial Version (Daryle Walker)
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/config.hpp> // for BOOST_NO_MEMBER_TEMPLATES
  11. #include <boost/noncopyable.hpp> // for boost::noncopyable
  12. #include <boost/utility/base_from_member.hpp> // for boost::base_from_member
  13. #include <functional> // for std::less
  14. #include <iostream> // for std::cout (std::ostream, std::endl indirectly)
  15. #include <set> // for std::set
  16. #include <typeinfo> // for std::type_info
  17. #include <utility> // for std::pair, std::make_pair
  18. #include <vector> // for std::vector
  19. // Control if extra information is printed
  20. #ifndef CONTROL_EXTRA_PRINTING
  21. #define CONTROL_EXTRA_PRINTING 1
  22. #endif
  23. // A (sub)object can be identified by its memory location and its type.
  24. // Both are needed since an object can start at the same place as its
  25. // first base class subobject and/or contained subobject.
  26. typedef std::pair< void *, std::type_info const * > object_id;
  27. // Object IDs need to be printed
  28. std::ostream & operator <<( std::ostream &os, object_id const &oi );
  29. // A way to generate an object ID
  30. template < typename T >
  31. object_id identify( T &obj );
  32. // A custom comparison type is needed
  33. struct object_id_compare
  34. {
  35. bool operator ()( object_id const &a, object_id const &b ) const;
  36. }; // object_id_compare
  37. // A singleton of this type coordinates the acknowledgements
  38. // of objects being created and used.
  39. class object_registrar
  40. : private boost::noncopyable
  41. {
  42. public:
  43. #ifndef BOOST_NO_MEMBER_TEMPLATES
  44. template < typename T >
  45. void register_object( T &obj )
  46. { this->register_object_imp( identify(obj) ); }
  47. template < typename T, typename U >
  48. void register_use( T &owner, U &owned )
  49. { this->register_use_imp( identify(owner), identify(owned) ); }
  50. template < typename T, typename U >
  51. void unregister_use( T &owner, U &owned )
  52. { this->unregister_use_imp( identify(owner), identify(owned) ); }
  53. template < typename T >
  54. void unregister_object( T &obj )
  55. { this->unregister_object_imp( identify(obj) ); }
  56. #endif
  57. void register_object_imp( object_id obj );
  58. void register_use_imp( object_id owner, object_id owned );
  59. void unregister_use_imp( object_id owner, object_id owned );
  60. void unregister_object_imp( object_id obj );
  61. typedef std::set<object_id, object_id_compare> set_type;
  62. typedef std::vector<object_id> error_record_type;
  63. typedef std::vector< std::pair<object_id, object_id> > error_pair_type;
  64. set_type db_;
  65. error_pair_type defrauders_in_, defrauders_out_;
  66. error_record_type overeager_, overkilled_;
  67. }; // object_registrar
  68. // A sample type to be used by containing types
  69. class base_or_member
  70. {
  71. public:
  72. explicit base_or_member( int x = 1, double y = -0.25 );
  73. ~base_or_member();
  74. }; // base_or_member
  75. // A sample type that uses base_or_member, used
  76. // as a base for the main demonstration classes
  77. class base_class
  78. {
  79. public:
  80. explicit base_class( base_or_member &x, base_or_member *y = 0,
  81. base_or_member *z = 0 );
  82. ~base_class();
  83. private:
  84. base_or_member *x_, *y_, *z_;
  85. }; // base_class
  86. // This bad class demonstrates the direct method of a base class needing
  87. // to be initialized by a member. This is improper since the member
  88. // isn't initialized until after the base class.
  89. class bad_class
  90. : public base_class
  91. {
  92. public:
  93. bad_class();
  94. ~bad_class();
  95. private:
  96. base_or_member x_;
  97. }; // bad_class
  98. // The first good class demonstrates the correct way to initialize a
  99. // base class with a member. The member is changed to another base
  100. // class, one that is initialized before the base that needs it.
  101. class good_class_1
  102. : private boost::base_from_member<base_or_member>
  103. , public base_class
  104. {
  105. typedef boost::base_from_member<base_or_member> pbase_type;
  106. typedef base_class base_type;
  107. public:
  108. good_class_1();
  109. ~good_class_1();
  110. }; // good_class_1
  111. // The second good class also demonstrates the correct way to initialize
  112. // base classes with other subobjects. This class uses the other helpers
  113. // in the library, and shows the technique of using two base subobjects
  114. // of the "same" type.
  115. class good_class_2
  116. : private boost::base_from_member<base_or_member, 0>
  117. , private boost::base_from_member<base_or_member, 1>
  118. , private boost::base_from_member<base_or_member, 2>
  119. , public base_class
  120. {
  121. typedef boost::base_from_member<base_or_member, 0> pbase_type0;
  122. typedef boost::base_from_member<base_or_member, 1> pbase_type1;
  123. typedef boost::base_from_member<base_or_member, 2> pbase_type2;
  124. typedef base_class base_type;
  125. public:
  126. good_class_2();
  127. ~good_class_2();
  128. }; // good_class_2
  129. // Declare/define the single object registrar
  130. object_registrar obj_reg;
  131. // Main functionality
  132. int
  133. main()
  134. {
  135. BOOST_TEST( obj_reg.db_.empty() );
  136. BOOST_TEST( obj_reg.defrauders_in_.empty() );
  137. BOOST_TEST( obj_reg.defrauders_out_.empty() );
  138. BOOST_TEST( obj_reg.overeager_.empty() );
  139. BOOST_TEST( obj_reg.overkilled_.empty() );
  140. // Make a separate block to examine pre- and post-effects
  141. {
  142. using std::cout;
  143. using std::endl;
  144. bad_class bc;
  145. BOOST_TEST( obj_reg.db_.size() == 3 );
  146. BOOST_TEST( obj_reg.defrauders_in_.size() == 1 );
  147. good_class_1 gc1;
  148. BOOST_TEST( obj_reg.db_.size() == 6 );
  149. BOOST_TEST( obj_reg.defrauders_in_.size() == 1 );
  150. good_class_2 gc2;
  151. BOOST_TEST( obj_reg.db_.size() == 11 );
  152. BOOST_TEST( obj_reg.defrauders_in_.size() == 1 );
  153. BOOST_TEST( obj_reg.defrauders_out_.empty() );
  154. BOOST_TEST( obj_reg.overeager_.empty() );
  155. BOOST_TEST( obj_reg.overkilled_.empty() );
  156. // Getting the addresses of the objects ensure
  157. // that they're used, and not optimized away.
  158. cout << "Object 'bc' is at " << &bc << '.' << endl;
  159. cout << "Object 'gc1' is at " << &gc1 << '.' << endl;
  160. cout << "Object 'gc2' is at " << &gc2 << '.' << endl;
  161. }
  162. BOOST_TEST( obj_reg.db_.empty() );
  163. BOOST_TEST( obj_reg.defrauders_in_.size() == 1 );
  164. BOOST_TEST( obj_reg.defrauders_out_.size() == 1 );
  165. BOOST_TEST( obj_reg.overeager_.empty() );
  166. BOOST_TEST( obj_reg.overkilled_.empty() );
  167. return boost::report_errors();
  168. }
  169. // Print an object's ID
  170. std::ostream &
  171. operator <<
  172. (
  173. std::ostream & os,
  174. object_id const & oi
  175. )
  176. {
  177. // I had an std::ostringstream to help, but I did not need it since
  178. // the program never screws around with formatting. Worse, using
  179. // std::ostringstream is an issue with some compilers.
  180. return os << '[' << ( oi.second ? oi.second->name() : "NOTHING" )
  181. << " at " << oi.first << ']';
  182. }
  183. // Get an object ID given an object
  184. template < typename T >
  185. inline
  186. object_id
  187. identify
  188. (
  189. T & obj
  190. )
  191. {
  192. return std::make_pair( static_cast<void *>(&obj), &(typeid( obj )) );
  193. }
  194. // Compare two object IDs
  195. bool
  196. object_id_compare::operator ()
  197. (
  198. object_id const & a,
  199. object_id const & b
  200. ) const
  201. {
  202. std::less<void *> vp_cmp;
  203. if ( vp_cmp(a.first, b.first) )
  204. {
  205. return true;
  206. }
  207. else if ( vp_cmp(b.first, a.first) )
  208. {
  209. return false;
  210. }
  211. else
  212. {
  213. // object pointers are equal, compare the types
  214. if ( a.second == b.second )
  215. {
  216. return false;
  217. }
  218. else if ( !a.second )
  219. {
  220. return true; // NULL preceeds anything else
  221. }
  222. else if ( !b.second )
  223. {
  224. return false; // NULL preceeds anything else
  225. }
  226. else
  227. {
  228. return a.second->before( *b.second ) != 0;
  229. }
  230. }
  231. }
  232. // Let an object register its existence
  233. void
  234. object_registrar::register_object_imp
  235. (
  236. object_id obj
  237. )
  238. {
  239. if ( db_.count(obj) <= 0 )
  240. {
  241. db_.insert( obj );
  242. #if CONTROL_EXTRA_PRINTING
  243. std::cout << "Registered " << obj << '.' << std::endl;
  244. #endif
  245. }
  246. else
  247. {
  248. overeager_.push_back( obj );
  249. #if CONTROL_EXTRA_PRINTING
  250. std::cout << "Attempted to register a non-existant " << obj
  251. << '.' << std::endl;
  252. #endif
  253. }
  254. }
  255. // Let an object register its use of another object
  256. void
  257. object_registrar::register_use_imp
  258. (
  259. object_id owner,
  260. object_id owned
  261. )
  262. {
  263. if ( db_.count(owned) > 0 )
  264. {
  265. // We don't care to record usage registrations
  266. }
  267. else
  268. {
  269. defrauders_in_.push_back( std::make_pair(owner, owned) );
  270. #if CONTROL_EXTRA_PRINTING
  271. std::cout << "Attempted to own a non-existant " << owned
  272. << " by " << owner << '.' << std::endl;
  273. #endif
  274. }
  275. }
  276. // Let an object un-register its use of another object
  277. void
  278. object_registrar::unregister_use_imp
  279. (
  280. object_id owner,
  281. object_id owned
  282. )
  283. {
  284. if ( db_.count(owned) > 0 )
  285. {
  286. // We don't care to record usage un-registrations
  287. }
  288. else
  289. {
  290. defrauders_out_.push_back( std::make_pair(owner, owned) );
  291. #if CONTROL_EXTRA_PRINTING
  292. std::cout << "Attempted to disown a non-existant " << owned
  293. << " by " << owner << '.' << std::endl;
  294. #endif
  295. }
  296. }
  297. // Let an object un-register its existence
  298. void
  299. object_registrar::unregister_object_imp
  300. (
  301. object_id obj
  302. )
  303. {
  304. set_type::iterator const i = db_.find( obj );
  305. if ( i != db_.end() )
  306. {
  307. db_.erase( i );
  308. #if CONTROL_EXTRA_PRINTING
  309. std::cout << "Unregistered " << obj << '.' << std::endl;
  310. #endif
  311. }
  312. else
  313. {
  314. overkilled_.push_back( obj );
  315. #if CONTROL_EXTRA_PRINTING
  316. std::cout << "Attempted to unregister a non-existant " << obj
  317. << '.' << std::endl;
  318. #endif
  319. }
  320. }
  321. // Macros to abstract the registration of objects
  322. #ifndef BOOST_NO_MEMBER_TEMPLATES
  323. #define PRIVATE_REGISTER_BIRTH(o) obj_reg.register_object( (o) )
  324. #define PRIVATE_REGISTER_DEATH(o) obj_reg.unregister_object( (o) )
  325. #define PRIVATE_REGISTER_USE(o, w) obj_reg.register_use( (o), (w) )
  326. #define PRIVATE_UNREGISTER_USE(o, w) obj_reg.unregister_use( (o), (w) )
  327. #else
  328. #define PRIVATE_REGISTER_BIRTH(o) obj_reg.register_object_imp( \
  329. identify((o)) )
  330. #define PRIVATE_REGISTER_DEATH(o) obj_reg.unregister_object_imp( \
  331. identify((o)) )
  332. #define PRIVATE_REGISTER_USE(o, w) obj_reg.register_use_imp( identify((o)), \
  333. identify((w)) )
  334. #define PRIVATE_UNREGISTER_USE(o, w) obj_reg.unregister_use_imp( \
  335. identify((o)), identify((w)) )
  336. #endif
  337. // Create a base_or_member, with arguments to simulate member initializations
  338. base_or_member::base_or_member
  339. (
  340. int x, // = 1
  341. double y // = -0.25
  342. )
  343. {
  344. PRIVATE_REGISTER_BIRTH( *this );
  345. #if CONTROL_EXTRA_PRINTING
  346. std::cout << "\tMy x-factor is " << x << " and my y-factor is " << y
  347. << '.' << std::endl;
  348. #endif
  349. }
  350. // Destroy a base_or_member
  351. inline
  352. base_or_member::~base_or_member
  353. (
  354. )
  355. {
  356. PRIVATE_REGISTER_DEATH( *this );
  357. }
  358. // Create a base_class, registering any objects used
  359. base_class::base_class
  360. (
  361. base_or_member & x,
  362. base_or_member * y, // = 0
  363. base_or_member * z // = 0
  364. )
  365. : x_( &x ), y_( y ), z_( z )
  366. {
  367. PRIVATE_REGISTER_BIRTH( *this );
  368. #if CONTROL_EXTRA_PRINTING
  369. std::cout << "\tMy x-factor is " << x_;
  370. #endif
  371. PRIVATE_REGISTER_USE( *this, *x_ );
  372. if ( y_ )
  373. {
  374. #if CONTROL_EXTRA_PRINTING
  375. std::cout << ", my y-factor is " << y_;
  376. #endif
  377. PRIVATE_REGISTER_USE( *this, *y_ );
  378. }
  379. if ( z_ )
  380. {
  381. #if CONTROL_EXTRA_PRINTING
  382. std::cout << ", my z-factor is " << z_;
  383. #endif
  384. PRIVATE_REGISTER_USE( *this, *z_ );
  385. }
  386. #if CONTROL_EXTRA_PRINTING
  387. std::cout << '.' << std::endl;
  388. #endif
  389. }
  390. // Destroy a base_class, unregistering the objects it uses
  391. base_class::~base_class
  392. (
  393. )
  394. {
  395. PRIVATE_REGISTER_DEATH( *this );
  396. #if CONTROL_EXTRA_PRINTING
  397. std::cout << "\tMy x-factor was " << x_;
  398. #endif
  399. PRIVATE_UNREGISTER_USE( *this, *x_ );
  400. if ( y_ )
  401. {
  402. #if CONTROL_EXTRA_PRINTING
  403. std::cout << ", my y-factor was " << y_;
  404. #endif
  405. PRIVATE_UNREGISTER_USE( *this, *y_ );
  406. }
  407. if ( z_ )
  408. {
  409. #if CONTROL_EXTRA_PRINTING
  410. std::cout << ", my z-factor was " << z_;
  411. #endif
  412. PRIVATE_UNREGISTER_USE( *this, *z_ );
  413. }
  414. #if CONTROL_EXTRA_PRINTING
  415. std::cout << '.' << std::endl;
  416. #endif
  417. }
  418. // Create a bad_class, noting the improper construction order
  419. bad_class::bad_class
  420. (
  421. )
  422. : x_( -7, 16.75 ), base_class( x_ ) // this order doesn't matter
  423. {
  424. PRIVATE_REGISTER_BIRTH( *this );
  425. #if CONTROL_EXTRA_PRINTING
  426. std::cout << "\tMy factor is at " << &x_
  427. << " and my base is at " << static_cast<base_class *>(this) << '.'
  428. << std::endl;
  429. #endif
  430. }
  431. // Destroy a bad_class, noting the improper destruction order
  432. bad_class::~bad_class
  433. (
  434. )
  435. {
  436. PRIVATE_REGISTER_DEATH( *this );
  437. #if CONTROL_EXTRA_PRINTING
  438. std::cout << "\tMy factor was at " << &x_
  439. << " and my base was at " << static_cast<base_class *>(this)
  440. << '.' << std::endl;
  441. #endif
  442. }
  443. // Create a good_class_1, noting the proper construction order
  444. good_class_1::good_class_1
  445. (
  446. )
  447. : pbase_type( 8 ), base_type( member )
  448. {
  449. PRIVATE_REGISTER_BIRTH( *this );
  450. #if CONTROL_EXTRA_PRINTING
  451. std::cout << "\tMy factor is at " << &member
  452. << " and my base is at " << static_cast<base_class *>(this) << '.'
  453. << std::endl;
  454. #endif
  455. }
  456. // Destroy a good_class_1, noting the proper destruction order
  457. good_class_1::~good_class_1
  458. (
  459. )
  460. {
  461. PRIVATE_REGISTER_DEATH( *this );
  462. #if CONTROL_EXTRA_PRINTING
  463. std::cout << "\tMy factor was at " << &member
  464. << " and my base was at " << static_cast<base_class *>(this)
  465. << '.' << std::endl;
  466. #endif
  467. }
  468. // Create a good_class_2, noting the proper construction order
  469. good_class_2::good_class_2
  470. (
  471. )
  472. : pbase_type0(), pbase_type1(-16, 0.125), pbase_type2(2, -3)
  473. , base_type( pbase_type1::member, &this->pbase_type0::member,
  474. &this->pbase_type2::member )
  475. {
  476. PRIVATE_REGISTER_BIRTH( *this );
  477. #if CONTROL_EXTRA_PRINTING
  478. std::cout << "\tMy factors are at " << &this->pbase_type0::member
  479. << ", " << &this->pbase_type1::member << ", "
  480. << &this->pbase_type2::member << ", and my base is at "
  481. << static_cast<base_class *>(this) << '.' << std::endl;
  482. #endif
  483. }
  484. // Destroy a good_class_2, noting the proper destruction order
  485. good_class_2::~good_class_2
  486. (
  487. )
  488. {
  489. PRIVATE_REGISTER_DEATH( *this );
  490. #if CONTROL_EXTRA_PRINTING
  491. std::cout << "\tMy factors were at " << &this->pbase_type0::member
  492. << ", " << &this->pbase_type1::member << ", "
  493. << &this->pbase_type2::member << ", and my base was at "
  494. << static_cast<base_class *>(this) << '.' << std::endl;
  495. #endif
  496. }