From 52896b097e8269afa4d85471302ad00a7e11f06d Mon Sep 17 00:00:00 2001 From: Fernando Cacciola Date: Mon, 15 Sep 2003 20:28:10 +0000 Subject: [PATCH] Typed in place construction added [SVN r20070] --- .../boost/detail/in_place_factory_prefix.hpp | 36 +++++++++++++ .../boost/detail/in_place_factory_suffix.hpp | 26 +++++++++ include/boost/optional.hpp | 20 ++++--- test/Jamfile | 1 + test/optional_test_inplace.cpp | 13 ++++- test/optional_test_inplace_fail2.cpp | 53 +++++++++++++++++++ 6 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 include/boost/detail/in_place_factory_prefix.hpp create mode 100644 include/boost/detail/in_place_factory_suffix.hpp create mode 100644 test/optional_test_inplace_fail2.cpp diff --git a/include/boost/detail/in_place_factory_prefix.hpp b/include/boost/detail/in_place_factory_prefix.hpp new file mode 100644 index 0000000..ba658d1 --- /dev/null +++ b/include/boost/detail/in_place_factory_prefix.hpp @@ -0,0 +1,36 @@ +// (C) 2002, Fernando Luis Cacciola Carballal. +// +// This material is provided "as is", with absolutely no warranty expressed +// or implied. Any use is at your own risk. +// +// Permission to use or copy this software for any purpose is hereby granted +// without fee, provided the above notices are retained on all copies. +// Permission to modify the code and to distribute modified code is granted, +// provided the above notices are retained, and a notice that the code was +// modified is included with the above copyright notice. +// +// You are welcome to contact the author at: +// fernando_cacciola@hotmail.com +// +#ifndef BOOST_UTILITY_INPLACE_FACTORY_PREFIX_25AGO2003_HPP +#define BOOST_UTILITY_INPLACE_FACTORY_PREFIX_25AGO2003_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +#define BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT(z,n,_) BOOST_PP_CAT(m_a,n) BOOST_PP_LPAREN() BOOST_PP_CAT(a,n) BOOST_PP_RPAREN() +#define BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL(z,n,_) BOOST_PP_CAT(A,n) const& BOOST_PP_CAT(m_a,n); +#define BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_ARG(z,n,_) BOOST_PP_CAT(m_a,n) + +#define BOOST_MAX_INPLACE_FACTORY_ARITY 10 + +#undef BOOST_UTILITY_INPLACE_FACTORY_SUFFIX_25AGO2003_HPP + +#endif + diff --git a/include/boost/detail/in_place_factory_suffix.hpp b/include/boost/detail/in_place_factory_suffix.hpp new file mode 100644 index 0000000..344748e --- /dev/null +++ b/include/boost/detail/in_place_factory_suffix.hpp @@ -0,0 +1,26 @@ +// (C) 2002, Fernando Luis Cacciola Carballal. +// +// This material is provided "as is", with absolutely no warranty expressed +// or implied. Any use is at your own risk. +// +// Permission to use or copy this software for any purpose is hereby granted +// without fee, provided the above notices are retained on all copies. +// Permission to modify the code and to distribute modified code is granted, +// provided the above notices are retained, and a notice that the code was +// modified is included with the above copyright notice. +// +// You are welcome to contact the author at: +// fernando_cacciola@hotmail.com +// +#ifndef BOOST_UTILITY_INPLACE_FACTORY_SUFFIX_25AGO2003_HPP +#define BOOST_UTILITY_INPLACE_FACTORY_SUFFIX_25AGO2003_HPP + +#undef BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT +#undef BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL +#undef BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_ARG +#undef BOOST_MAX_INPLACE_FACTORY_ARITY + +#undef BOOST_UTILITY_INPLACE_FACTORY_PREFIX_25AGO2003_HPP + +#endif + diff --git a/include/boost/optional.hpp b/include/boost/optional.hpp index 1c7d8f3..9027945 100644 --- a/include/boost/optional.hpp +++ b/include/boost/optional.hpp @@ -56,6 +56,7 @@ namespace boost { class InPlaceFactoryBase ; +class TypedInPlaceFactoryBase ; namespace optional_detail { @@ -235,10 +236,17 @@ class optional_base template void construct ( Expr const& factory, InPlaceFactoryBase const* ) { - BOOST_STATIC_ASSERT ( mpl::not_::value ) ; + BOOST_STATIC_ASSERT ( ::boost::mpl::not_::value ) ; + factory.BOOST_NESTED_TEMPLATE apply(m_storage.address()) ; + m_initialized = true ; + } - boost::type selector ; - factory(selector,m_storage.address()) ; + template + void construct ( Expr const& factory, TypedInPlaceFactoryBase const* ) + { + BOOST_STATIC_ASSERT ( ::boost::mpl::not_::value ) ; + + factory.apply(m_storage.address()) ; m_initialized = true ; } @@ -542,9 +550,9 @@ bool operator >= ( optional const& x, optional const& y ) // namespace optional_detail { -// GCC <= 3.2 gets the using declaration at namespace scope (FLC) -#if BOOST_WORKAROUND(__GNUC__, <= 3) && __GNUC_MINOR__ <= 2 - // workaround for GCC (JM): +// GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA) +#if BOOST_WORKAROUND(__GNUC__, < 3) \ + || BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2 using std::swap; #define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE #endif diff --git a/test/Jamfile b/test/Jamfile index f0fd7b6..1fd8060 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -41,5 +41,6 @@ DEPENDS all : test ; [ compile-fail libs/optional/test/optional_test_references_fail3.cpp ] [ compile-fail libs/optional/test/optional_test_references_fail4.cpp ] [ compile-fail libs/optional/test/optional_test_inplace_fail.cpp ] + [ compile-fail libs/optional/test/optional_test_inplace_fail2.cpp ] ; } diff --git a/test/optional_test_inplace.cpp b/test/optional_test_inplace.cpp index 858045e..23b7a9d 100644 --- a/test/optional_test_inplace.cpp +++ b/test/optional_test_inplace.cpp @@ -20,6 +20,7 @@ #include "boost/optional.hpp" #include "boost/utility/in_place_factory.hpp" +#include "boost/utility/typed_in_place_factory.hpp" #ifdef __BORLANDC__ #pragma hdrstop @@ -42,16 +43,24 @@ struct A int test_main( int, char* [] ) { - A a(3.14,"pi"); + double a0 = 3.14 ; + std::string a1("pi"); + + A a(a0,a1); boost::optional opt1(a); - boost::optional opt2 ( boost::in_place(3.14,"pi") ) ; + boost::optional opt2 ( boost::in_place(a0,a1) ) ; + + boost::optional opt3 ( boost::in_place(a0,a1) ) ; BOOST_CHECK( opt1 == opt2 ) ; + BOOST_CHECK( opt2 == opt2 ) ; BOOST_CHECK( *opt2 == a ) ; return 0; } + + diff --git a/test/optional_test_inplace_fail2.cpp b/test/optional_test_inplace_fail2.cpp new file mode 100644 index 0000000..65d4efb --- /dev/null +++ b/test/optional_test_inplace_fail2.cpp @@ -0,0 +1,53 @@ +// (C) 2003, Fernando Luis Cacciola Carballal. +// +// This material is provided "as is", with absolutely no warranty expressed +// or implied. Any use is at your own risk. +// +// Permission to use or copy this software for any purpose is hereby granted +// without fee, provided the above notices are retained on all copies. +// Permission to modify the code and to distribute modified code is granted, +// provided the above notices are retained, and a notice that the code was +// modified is included with the above copyright notice. +// +// You are welcome to contact the author at: +// fernando_cacciola@hotmail.com +// +#include +#include +#include + +#define BOOST_ENABLE_ASSERT_HANDLER + +#include "boost/optional.hpp" +#include "boost/utility/typed_in_place_factory.hpp" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#include "boost/test/minimal.hpp" + +#include "optional_test_common.cpp" + +struct A +{ + A ( double a0, std::string a1 ) : m_a0(a0), m_a1(a1) {} + + friend bool operator == ( A const& x, A const& y ) + { return x.m_a0 == y.m_a0 && x.m_a1 == y.m_a1 ; } + + double m_a0 ; + std::string m_a1 ; +} ; + +int test_main( int, char* [] ) +{ + // This must fail to compile. + // The first template argument to in_place<> is the target-type, + // not the first constructor parameter type. + boost::optional opt2 ( boost::in_place(3.14,"pi") ) ; + + return 0; +} + +