Boost logo

Boost :

From: Vaclav Vesely (vaclav.vesely_at_[hidden])
Date: 2006-03-29 13:41:32


Fernando Cacciola wrote:
> Vaclav Vesely wrote:
>> is there any reason for missing in_place_factory without arguments?
>>
> I just missed this one :)
> It will be added, just not in 1.34

I have added in_place_factory0 and typed_in_place_factory0. Also I've
written a missing test for all factories.

Regards,
Vaclav

Index: boost/utility/in_place_factory.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/utility/in_place_factory.hpp,v
retrieving revision 1.6
diff -c -r1.6 in_place_factory.hpp
*** boost/utility/in_place_factory.hpp 23 Sep 2004 17:03:54 -0000 1.6
--- boost/utility/in_place_factory.hpp 29 Mar 2006 17:22:02 -0000
***************
*** 50,55 ****
--- 50,74 ----
  
  BOOST_PP_REPEAT( BOOST_MAX_INPLACE_FACTORY_ARITY, BOOST_DEFINE_INPLACE_FACTORY_CLASS, BOOST_PP_EMPTY() )
  
+ class in_place_factory0:
+ public in_place_factory_base
+ {
+ public:
+ in_place_factory0()
+ {}
+
+ template<class T>
+ void apply(void* address BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) const
+ {
+ new (address) T();
+ }
+ };
+
+ in_place_factory0 in_place()
+ {
+ return in_place_factory0();
+ }
+
  } // namespace boost
  
  #include <boost/utility/detail/in_place_factory_suffix.hpp>
Index: boost/utility/typed_in_place_factory.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/utility/typed_in_place_factory.hpp,v
retrieving revision 1.5
diff -c -r1.5 typed_in_place_factory.hpp
*** boost/utility/typed_in_place_factory.hpp 23 Sep 2004 17:03:54 -0000 1.5
--- boost/utility/typed_in_place_factory.hpp 29 Mar 2006 18:18:18 -0000
***************
*** 19,53 ****
  class typed_in_place_factory_base {} ;
  
  #define BOOST_DEFINE_TYPED_INPLACE_FACTORY_CLASS(z,n,_) \
! template< class T, BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n),class A) > \
! class BOOST_PP_CAT(typed_in_place_factory, BOOST_PP_INC(n) ) : public typed_in_place_factory_base \
  { \
  public: \
  \
    typedef T value_type ; \
  \
! BOOST_PP_CAT(typed_in_place_factory, BOOST_PP_INC(n) ) ( BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_INC(n),A,const& a) ) \
! : \
! BOOST_PP_ENUM( BOOST_PP_INC(n), BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT, _ ) \
    {} \
  \
    void apply ( void* address ) const \
    { \
! new ( address ) T ( BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), m_a ) ) ; \
    } \
  \
! BOOST_PP_REPEAT( BOOST_PP_INC(n), BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL, _) \
  } ; \
  \
! template< class T, BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n),class A) > \
! BOOST_PP_CAT(typed_in_place_factory, BOOST_PP_INC(n) ) < T , BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), A ) > \
! in_place ( BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_INC(n),A, const& a) ) \
  { \
! return BOOST_PP_CAT(typed_in_place_factory, BOOST_PP_INC(n) ) < T, BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), A ) > \
! ( BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), a ) ) ; \
  } ; \
  
! BOOST_PP_REPEAT( BOOST_MAX_INPLACE_FACTORY_ARITY, BOOST_DEFINE_TYPED_INPLACE_FACTORY_CLASS, BOOST_PP_EMPTY() )
  
  } // namespace boost
  
--- 19,53 ----
  class typed_in_place_factory_base {} ;
  
  #define BOOST_DEFINE_TYPED_INPLACE_FACTORY_CLASS(z,n,_) \
! template< class T BOOST_PP_ENUM_TRAILING_PARAMS(n,class A) > \
! class BOOST_PP_CAT(typed_in_place_factory, n ) : public typed_in_place_factory_base \
  { \
  public: \
  \
    typedef T value_type ; \
  \
! BOOST_PP_CAT(typed_in_place_factory, n ) ( BOOST_PP_ENUM_BINARY_PARAMS(n,A,const& a) ) \
! BOOST_PP_IF(n , : , BOOST_PP_EMPTY() ) \
! BOOST_PP_ENUM( n, BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT, _ ) \
    {} \
  \
    void apply ( void* address ) const \
    { \
! new ( address ) T ( BOOST_PP_ENUM_PARAMS( n, m_a ) ) ; \
    } \
  \
! BOOST_PP_REPEAT( n, BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL, _) \
  } ; \
  \
! template< class T BOOST_PP_ENUM_TRAILING_PARAMS(n,class A) > \
! BOOST_PP_CAT(typed_in_place_factory, n ) < T BOOST_PP_ENUM_TRAILING_PARAMS( n, A ) > \
! in_place ( BOOST_PP_ENUM_BINARY_PARAMS(n,A, const& a) ) \
  { \
! return BOOST_PP_CAT(typed_in_place_factory, n ) < T BOOST_PP_ENUM_TRAILING_PARAMS( n, A ) > \
! ( BOOST_PP_ENUM_PARAMS( n, a ) ) ; \
  } ; \
  
! BOOST_PP_REPEAT( BOOST_PP_INC(BOOST_MAX_INPLACE_FACTORY_ARITY), BOOST_DEFINE_TYPED_INPLACE_FACTORY_CLASS, BOOST_PP_EMPTY() )
  
  } // namespace boost
  
Index: boost/utility/detail/in_place_factory_prefix.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/utility/detail/in_place_factory_prefix.hpp,v
retrieving revision 1.1
diff -c -r1.1 in_place_factory_prefix.hpp
*** boost/utility/detail/in_place_factory_prefix.hpp 21 Sep 2004 14:54:32 -0000 1.1
--- boost/utility/detail/in_place_factory_prefix.hpp 29 Mar 2006 18:09:54 -0000
***************
*** 16,21 ****
--- 16,22 ----
  #include <boost/preprocessor/repetition/enum.hpp>
  #include <boost/preprocessor/repetition/enum_params.hpp>
  #include <boost/preprocessor/repetition/enum_binary_params.hpp>
+ #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  #include <boost/preprocessor/cat.hpp>
  #include <boost/preprocessor/arithmetic/inc.hpp>
  #include <boost/preprocessor/punctuation/paren.hpp>


// (C) Copyright Vaclav Vesely 2006.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#define BOOST_TEST_MAIN
#include <boost/ref.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/utility/in_place_factory.hpp>
#include <boost/utility/typed_in_place_factory.hpp>

//-----------------------------------------------------------------------------

static int g0 = 0;
static int g2 = 2;
static int g3 = 3;
static int g5 = 5;

struct X
{
    X(
        int a1 = 0,
        int* a2 = &g0,
        int& a3 = g0,
        std::string const& a4 = "",
        const int& a5 = g0,
        int a6 = 0,
        int a7 = 0,
        int a8 = 0,
        int a9 = 0,
        int a10 = 0)
    :
        m1(a1),
        m2(a2),
        m3(a3),
        m4(a4),
        m5(a5),
        m6(a6),
        m7(a7),
        m8(a8),
        m9(a9),
        m10(a10)
    {
    }

    bool operator==(X const& other) const
    {
        return m1 == other.m1
            && m2 == other.m2
            && m3 == other.m3
            && m4 == other.m4
            && m5 == other.m5
            && m6 == other.m6
            && m7 == other.m7
            && m8 == other.m8
            && m9 == other.m9
            && m10 == other.m10;
    }

    int m1;
    int* m2;
    int& m3;
    std::string m4;
    const int& m5;
    int m6;
    int m7;
    int m8;
    int m9;
    int m10;
};

std::ostream& operator<<(std::ostream& ostr, X const& x)
{
    ostr << "X("
        << x.m1 << ", "
        << x.m2 << ", "
        << x.m3 << ", "
        << x.m4 << ", "
        << x.m5 << ", "
        << x.m6 << ", "
        << x.m7 << ", "
        << x.m8 << ", "
        << x.m9 << ", "
        << x.m10 << ")";
    return ostr;
}

//-----------------------------------------------------------------------------

BOOST_AUTO_TEST_CASE(in_place_factory_test)
{
    X x0(1, &g2, g3, "4", g5, 6, 7, 8, 9, 1);
    boost::in_place().apply<X>(reinterpret_cast<void*>(&x0));
    BOOST_CHECK_EQUAL(x0, X());

    X x1;
    boost::in_place(1).apply<X>(reinterpret_cast<void*>(&x1));
    BOOST_CHECK_EQUAL(x1, X(1));

    X x2;
    boost::in_place(1, &g2).apply<X>(reinterpret_cast<void*>(&x2));
    BOOST_CHECK_EQUAL(x2, X(1, &g2));

    X x3;
    boost::in_place(1, &g2, boost::ref(g3))
        .apply<X>(reinterpret_cast<void*>(&x3));
    BOOST_CHECK_EQUAL(x3, X(1, &g2, g3));

    X x4;
    boost::in_place(1, &g2, boost::ref(g3), "4")
        .apply<X>(reinterpret_cast<void*>(&x4));
    BOOST_CHECK_EQUAL(x4, X(1, &g2, g3, "4"));

    X x5;
    boost::in_place(1, &g2, boost::ref(g3), "4", g5)
        .apply<X>(reinterpret_cast<void*>(&x5));
    BOOST_CHECK_EQUAL(x5, X(1, &g2, g3, "4", g5));

    X x6;
    boost::in_place(1, &g2, boost::ref(g3), "4", g5, 6)
        .apply<X>(reinterpret_cast<void*>(&x6));
    BOOST_CHECK_EQUAL(x6, X(1, &g2, g3, "4", g5, 6));

    X x7;
    boost::in_place(1, &g2, boost::ref(g3), "4", g5, 6, 7)
        .apply<X>(reinterpret_cast<void*>(&x7));
    BOOST_CHECK_EQUAL(x7, X(1, &g2, g3, "4", g5, 6, 7));

    X x8;
    boost::in_place(1, &g2, boost::ref(g3), "4", g5, 6, 7, 8)
        .apply<X>(reinterpret_cast<void*>(&x8));
    BOOST_CHECK_EQUAL(x8, X(1, &g2, g3, "4", g5, 6, 7, 8));

    X x9;
    boost::in_place(1, &g2, boost::ref(g3), "4", g5, 6, 7, 8, 9)
        .apply<X>(reinterpret_cast<void*>(&x9));
    BOOST_CHECK_EQUAL(x9, X(1, &g2, g3, "4", g5, 6, 7, 8, 9));

    X x10;
    boost::in_place(1, &g2, boost::ref(g3), "4", g5, 6, 7, 8, 9, 10)
        .apply<X>(reinterpret_cast<void*>(&x10));
    BOOST_CHECK_EQUAL(x10, X(1, &g2, g3, "4", g5, 6, 7, 8, 9, 10));
}

//-----------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(typed_in_place_factory_test)
{
    X x0(1, &g2, g3, "4", g5, 6, 7, 8, 9, 1);
    boost::in_place<X>().apply(reinterpret_cast<void*>(&x0));
    BOOST_CHECK_EQUAL(x0, X());

    X x1;
    boost::in_place<X>(1).apply(reinterpret_cast<void*>(&x1));
    BOOST_CHECK_EQUAL(x1, X(1));

    X x2;
    boost::in_place<X>(1, &g2).apply(reinterpret_cast<void*>(&x2));
    BOOST_CHECK_EQUAL(x2, X(1, &g2));

    X x3;
    boost::in_place<X>(1, &g2, boost::ref(g3))
        .apply(reinterpret_cast<void*>(&x3));
    BOOST_CHECK_EQUAL(x3, X(1, &g2, g3));

    X x4;
    boost::in_place<X>(1, &g2, boost::ref(g3), "4")
        .apply(reinterpret_cast<void*>(&x4));
    BOOST_CHECK_EQUAL(x4, X(1, &g2, g3, "4"));

    X x5;
    boost::in_place<X>(1, &g2, boost::ref(g3), "4", g5)
        .apply(reinterpret_cast<void*>(&x5));
    BOOST_CHECK_EQUAL(x5, X(1, &g2, g3, "4", g5));

    X x6;
    boost::in_place<X>(1, &g2, boost::ref(g3), "4", g5, 6)
        .apply(reinterpret_cast<void*>(&x6));
    BOOST_CHECK_EQUAL(x6, X(1, &g2, g3, "4", g5, 6));

    X x7;
    boost::in_place<X>(1, &g2, boost::ref(g3), "4", g5, 6, 7)
        .apply(reinterpret_cast<void*>(&x7));
    BOOST_CHECK_EQUAL(x7, X(1, &g2, g3, "4", g5, 6, 7));

    X x8;
    boost::in_place<X>(1, &g2, boost::ref(g3), "4", g5, 6, 7, 8)
        .apply(reinterpret_cast<void*>(&x8));
    BOOST_CHECK_EQUAL(x8, X(1, &g2, g3, "4", g5, 6, 7, 8));

    X x9;
    boost::in_place<X>(1, &g2, boost::ref(g3), "4", g5, 6, 7, 8, 9)
        .apply(reinterpret_cast<void*>(&x9));
    BOOST_CHECK_EQUAL(x9, X(1, &g2, g3, "4", g5, 6, 7, 8, 9));

    X x10;
    boost::in_place<X>(1, &g2, boost::ref(g3), "4", g5, 6, 7, 8, 9, 10)
        .apply(reinterpret_cast<void*>(&x10));
    BOOST_CHECK_EQUAL(x10, X(1, &g2, g3, "4", g5, 6, 7, 8, 9, 10));
}

//-----------------------------------------------------------------------------


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk