From c1c83294037e2e41ca5d3f3fd71b69f9c9cd2241 Mon Sep 17 00:00:00 2001 From: Daryle Walker Date: Sun, 15 Jun 2003 01:16:49 +0000 Subject: [PATCH] Expanded base-from-member constructors to 10 arguments; did minor tweaks [SVN r18791] --- base_from_member.html | 28 ++++++++---- base_from_member_test.cpp | 50 +++++++++++----------- include/boost/utility/base_from_member.hpp | 48 +++++++++++++++++++-- include/boost/utility_fwd.hpp | 5 ++- 4 files changed, 93 insertions(+), 38 deletions(-) diff --git a/base_from_member.html b/base_from_member.html index 0376130..823bcb1 100644 --- a/base_from_member.html +++ b/base_from_member.html @@ -108,7 +108,13 @@ public: }; -

Other projects can use similar custom base classes. The technique is basic enough to make a template, with a sample template class in this library. The main template parameter is the type of the enclosed member. The template class has several (explicit) constructor member templates, which implicitly type the constructor arguments and pass them to the member. The template class uses implicit copy construction and assignment, cancelling them if the enclosed member is non-copyable.

+

Other projects can use similar custom base classes. The technique +is basic enough to make a template, with a sample template class in +this library. The main template parameter is the type of the enclosed +member. The template class has several (explicit) constructor member +templates, which implicitly type the constructor arguments and pass them +to the member. The template class uses implicit copy construction and +assignment, cancelling them if the enclosed member is non-copyable.

Manually coding a base class may be better if the construction and/or copying needs are too complex for the supplied template class, @@ -129,15 +135,21 @@ class boost::base_from_member protected: MemberType member; - explicit base_from_member(); + base_from_member(); template< typename T1 > explicit base_from_member( T1 x1 ); + template< typename T1, typename T2 > + base_from_member( T1 x1, T2 x2 ); + //... - template< typename T1, typename T2, typename T3 > - explicit base_from_member( T1 x1, T2 x2, T3 x3 ); + template< typename T1, typename T2, typename T3, typename T4, + typename T5, typename T6, typename T7, typename T8, typename T9, + typename T10 > + base_from_member( T1 x1, T2 x2, T3 x3, T4 x4, T5 x5, T6 x6, T7 x7, + T8 x8, T9 x9, T10 x10 ); }; @@ -152,7 +164,7 @@ for later base classes (or itself).

There is a default constructor and several constructor member templates. These constructor templates can take as many arguments -(currently up to three) as possible and pass them to a constructor of +(currently up to ten) as possible and pass them to a constructor of the data member. Since C++ does not allow any way to explicitly state the template parameters of a templated constructor, make sure that the arguments are already close as possible to the actual type used in @@ -329,10 +341,10 @@ with the exact pointer type used in switcher's constructor.


-

Revised: 22 August 2001

+

Revised: 14 June 2003

-

Copyright © boost.org 2001. Permission to copy, use, modify, -sell and distribute this document is granted provided this copyright +

Copyright © Daryle Walker 2001, 2003. Permission to copy, use, +modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.

diff --git a/base_from_member_test.cpp b/base_from_member_test.cpp index 840af10..e75eec8 100644 --- a/base_from_member_test.cpp +++ b/base_from_member_test.cpp @@ -1,7 +1,7 @@ // Boost test program for base-from-member class templates -----------------// -// (C) Copyright Daryle Walker 2001. Permission to copy, use, modify, sell -// and distribute this software is granted provided this copyright +// (C) Copyright Daryle Walker 2001, 2003. Permission to copy, use, modify, +// sell and distribute this software is granted provided this copyright // notice appears in all copies. This software is provided "as is" without // express or implied warranty, and with no claim as to its suitability for // any purpose. @@ -9,13 +9,13 @@ // See http://www.boost.org for most recent version including documentation. // Revision History +// 14 Jun 2003 Adjusted code for Boost.Test changes (Daryle Walker) // 29 Aug 2001 Initial Version (Daryle Walker) -#define BOOST_INCLUDE_MAIN -#include // for BOOST_TEST, main +#include // for BOOST_CHECK, main -#include // for BOOST_NO_MEMBER_TEMPLATES -#include // for boost::exit_success +#include // for BOOST_NO_MEMBER_TEMPLATES +#include // for boost::exit_success #include // for boost::noncopyable #include // for boost::base_from_member @@ -177,11 +177,11 @@ object_registrar obj_reg; int test_main( int , char * [] ) { - BOOST_TEST( obj_reg.db_.empty() ); - BOOST_TEST( obj_reg.defrauders_in_.empty() ); - BOOST_TEST( obj_reg.defrauders_out_.empty() ); - BOOST_TEST( obj_reg.overeager_.empty() ); - BOOST_TEST( obj_reg.overkilled_.empty() ); + 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 { @@ -189,20 +189,20 @@ test_main( int , char * [] ) using std::endl; bad_class bc; - BOOST_TEST( obj_reg.db_.size() == 3 ); - BOOST_TEST( obj_reg.defrauders_in_.size() == 1 ); + BOOST_CHECK( obj_reg.db_.size() == 3 ); + BOOST_CHECK( obj_reg.defrauders_in_.size() == 1 ); good_class_1 gc1; - BOOST_TEST( obj_reg.db_.size() == 6 ); - BOOST_TEST( obj_reg.defrauders_in_.size() == 1 ); + BOOST_CHECK( obj_reg.db_.size() == 6 ); + BOOST_CHECK( obj_reg.defrauders_in_.size() == 1 ); good_class_2 gc2; - BOOST_TEST( obj_reg.db_.size() == 11 ); - BOOST_TEST( obj_reg.defrauders_in_.size() == 1 ); + BOOST_CHECK( obj_reg.db_.size() == 11 ); + BOOST_CHECK( obj_reg.defrauders_in_.size() == 1 ); - BOOST_TEST( obj_reg.defrauders_out_.empty() ); - BOOST_TEST( obj_reg.overeager_.empty() ); - BOOST_TEST( obj_reg.overkilled_.empty() ); + 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. @@ -211,11 +211,11 @@ test_main( int , char * [] ) cout << "Object 'gc2' is at " << &gc2 << '.' << endl; } - BOOST_TEST( obj_reg.db_.empty() ); - BOOST_TEST( obj_reg.defrauders_in_.size() == 1 ); - BOOST_TEST( obj_reg.defrauders_out_.size() == 1 ); - BOOST_TEST( obj_reg.overeager_.empty() ); - BOOST_TEST( obj_reg.overkilled_.empty() ); + 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; } diff --git a/include/boost/utility/base_from_member.hpp b/include/boost/utility/base_from_member.hpp index 48d1d4a..d77ab1a 100644 --- a/include/boost/utility/base_from_member.hpp +++ b/include/boost/utility/base_from_member.hpp @@ -1,7 +1,7 @@ // boost utility/base_from_member.hpp header file --------------------------// -// (C) Copyright Daryle Walker 2001. Permission to copy, use, modify, sell -// and distribute this software is granted provided this copyright +// (C) Copyright Daryle Walker 2001, 2003. Permission to copy, use, modify, +// sell and distribute this software is granted provided this copyright // notice appears in all copies. This software is provided "as is" without // express or implied warranty, and with no claim as to its suitability for // any purpose. @@ -32,7 +32,7 @@ class base_from_member protected: MemberType member; - explicit base_from_member() + base_from_member() : member() {} @@ -51,6 +51,48 @@ protected: : member( x1, x2, x3 ) {} + template< typename T1, typename T2, typename T3, typename T4 > + base_from_member( T1 x1, T2 x2, T3 x3, T4 x4 ) + : member( x1, x2, x3, x4 ) + {} + + template< typename T1, typename T2, typename T3, typename T4, typename T5 > + base_from_member( T1 x1, T2 x2, T3 x3, T4 x4, T5 x5 ) + : member( x1, x2, x3, x4, x5 ) + {} + + template< typename T1, typename T2, typename T3, typename T4, typename T5, + typename T6 > + base_from_member( T1 x1, T2 x2, T3 x3, T4 x4, T5 x5, T6 x6 ) + : member( x1, x2, x3, x4, x5, x6 ) + {} + + template< typename T1, typename T2, typename T3, typename T4, typename T5, + typename T6, typename T7 > + base_from_member( T1 x1, T2 x2, T3 x3, T4 x4, T5 x5, T6 x6, T7 x7 ) + : member( x1, x2, x3, x4, x5, x6, x7 ) + {} + + template< typename T1, typename T2, typename T3, typename T4, typename T5, + typename T6, typename T7, typename T8 > + base_from_member( T1 x1, T2 x2, T3 x3, T4 x4, T5 x5, T6 x6, T7 x7, T8 x8 ) + : member( x1, x2, x3, x4, x5, x6, x7, x8 ) + {} + + template< typename T1, typename T2, typename T3, typename T4, typename T5, + typename T6, typename T7, typename T8, typename T9 > + base_from_member( T1 x1, T2 x2, T3 x3, T4 x4, T5 x5, T6 x6, T7 x7, T8 x8, + T9 x9 ) + : member( x1, x2, x3, x4, x5, x6, x7, x8, x9 ) + {} + + template< typename T1, typename T2, typename T3, typename T4, typename T5, + typename T6, typename T7, typename T8, typename T9, typename T10 > + base_from_member( T1 x1, T2 x2, T3 x3, T4 x4, T5 x5, T6 x6, T7 x7, T8 x8, + T9 x9, T10 x10 ) + : member( x1, x2, x3, x4, x5, x6, x7, x8, x9, x10 ) + {} + }; // boost::base_from_member } // namespace boost diff --git a/include/boost/utility_fwd.hpp b/include/boost/utility_fwd.hpp index 0f2762e..d5de6ab 100644 --- a/include/boost/utility_fwd.hpp +++ b/include/boost/utility_fwd.hpp @@ -1,6 +1,6 @@ // Boost utility_fwd.hpp header file ---------------------------------------// -// (C) Copyright boost.org 2001. Permission to copy, use, modify, sell +// (C) Copyright boost.org 2001, 2003. Permission to copy, use, modify, sell // and distribute this software is granted provided this copyright // notice appears in all copies. This software is provided "as is" without // express or implied warranty, and with no claim as to its suitability for @@ -22,12 +22,13 @@ template < typename MemberType, int UniqueID = 0 > class base_from_member; -// From ------------------------------------------------// +// From --------------------------------------------// class noncopyable; // Also has a few function templates + } // namespace boost