mirror of
https://github.com/boostorg/optional.git
synced 2025-07-30 20:47:18 +02:00
Typed in place construction added
[SVN r20070]
This commit is contained in:
36
include/boost/detail/in_place_factory_prefix.hpp
Normal file
36
include/boost/detail/in_place_factory_prefix.hpp
Normal file
@ -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 <boost/config.hpp>
|
||||||
|
#include <boost/preprocessor/repetition/enum.hpp>
|
||||||
|
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||||
|
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
|
||||||
|
#include <boost/preprocessor/cat.hpp>
|
||||||
|
#include <boost/preprocessor/arithmetic/inc.hpp>
|
||||||
|
#include <boost/preprocessor/punctuation/paren.hpp>
|
||||||
|
#include <boost/preprocessor/facilities/empty.hpp>
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
26
include/boost/detail/in_place_factory_suffix.hpp
Normal file
26
include/boost/detail/in_place_factory_suffix.hpp
Normal file
@ -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
|
||||||
|
|
@ -56,6 +56,7 @@
|
|||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
class InPlaceFactoryBase ;
|
class InPlaceFactoryBase ;
|
||||||
|
class TypedInPlaceFactoryBase ;
|
||||||
|
|
||||||
namespace optional_detail {
|
namespace optional_detail {
|
||||||
|
|
||||||
@ -235,10 +236,17 @@ class optional_base
|
|||||||
template<class Expr>
|
template<class Expr>
|
||||||
void construct ( Expr const& factory, InPlaceFactoryBase const* )
|
void construct ( Expr const& factory, InPlaceFactoryBase const* )
|
||||||
{
|
{
|
||||||
BOOST_STATIC_ASSERT ( mpl::not_<is_reference_predicate>::value ) ;
|
BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
|
||||||
|
factory.BOOST_NESTED_TEMPLATE apply<T>(m_storage.address()) ;
|
||||||
|
m_initialized = true ;
|
||||||
|
}
|
||||||
|
|
||||||
boost::type<T> selector ;
|
template<class Expr>
|
||||||
factory(selector,m_storage.address()) ;
|
void construct ( Expr const& factory, TypedInPlaceFactoryBase const* )
|
||||||
|
{
|
||||||
|
BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
|
||||||
|
|
||||||
|
factory.apply(m_storage.address()) ;
|
||||||
m_initialized = true ;
|
m_initialized = true ;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -542,9 +550,9 @@ bool operator >= ( optional<T> const& x, optional<T> const& y )
|
|||||||
//
|
//
|
||||||
namespace optional_detail {
|
namespace optional_detail {
|
||||||
|
|
||||||
// GCC <= 3.2 gets the using declaration at namespace scope (FLC)
|
// GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA)
|
||||||
#if BOOST_WORKAROUND(__GNUC__, <= 3) && __GNUC_MINOR__ <= 2
|
#if BOOST_WORKAROUND(__GNUC__, < 3) \
|
||||||
// workaround for GCC (JM):
|
|| BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2
|
||||||
using std::swap;
|
using std::swap;
|
||||||
#define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
|
#define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
|
||||||
#endif
|
#endif
|
||||||
|
@ -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_fail3.cpp ]
|
||||||
[ compile-fail libs/optional/test/optional_test_references_fail4.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_fail.cpp ]
|
||||||
|
[ compile-fail libs/optional/test/optional_test_inplace_fail2.cpp ]
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
#include "boost/optional.hpp"
|
#include "boost/optional.hpp"
|
||||||
#include "boost/utility/in_place_factory.hpp"
|
#include "boost/utility/in_place_factory.hpp"
|
||||||
|
#include "boost/utility/typed_in_place_factory.hpp"
|
||||||
|
|
||||||
#ifdef __BORLANDC__
|
#ifdef __BORLANDC__
|
||||||
#pragma hdrstop
|
#pragma hdrstop
|
||||||
@ -42,16 +43,24 @@ struct A
|
|||||||
|
|
||||||
int test_main( int, char* [] )
|
int test_main( int, char* [] )
|
||||||
{
|
{
|
||||||
A a(3.14,"pi");
|
double a0 = 3.14 ;
|
||||||
|
std::string a1("pi");
|
||||||
|
|
||||||
|
A a(a0,a1);
|
||||||
|
|
||||||
boost::optional<A> opt1(a);
|
boost::optional<A> opt1(a);
|
||||||
|
|
||||||
boost::optional<A> opt2 ( boost::in_place(3.14,"pi") ) ;
|
boost::optional<A> opt2 ( boost::in_place(a0,a1) ) ;
|
||||||
|
|
||||||
|
boost::optional<A> opt3 ( boost::in_place<A>(a0,a1) ) ;
|
||||||
|
|
||||||
BOOST_CHECK( opt1 == opt2 ) ;
|
BOOST_CHECK( opt1 == opt2 ) ;
|
||||||
|
BOOST_CHECK( opt2 == opt2 ) ;
|
||||||
BOOST_CHECK( *opt2 == a ) ;
|
BOOST_CHECK( *opt2 == a ) ;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
53
test/optional_test_inplace_fail2.cpp
Normal file
53
test/optional_test_inplace_fail2.cpp
Normal file
@ -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<iostream>
|
||||||
|
#include<stdexcept>
|
||||||
|
#include<string>
|
||||||
|
|
||||||
|
#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<A> opt2 ( boost::in_place<int>(3.14,"pi") ) ;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user