Boost logo

Boost Users :

From: Roman Perepelitsa (romka_at_[hidden])
Date: 2006-07-14 04:14:03


bringiton bringiton <kneeride <at> gmail.com> writes:
>
> On 7/14/06, Michael Nicolella <boost <at> mike-n.com> wrote:
> > What's wrong with
> >
> > boost::shared_ptr<Test> test( new Test );
> > boost::shared_ptr<Test> test( new Test( 100, 'a', "hello" ) );
> >
> > http://boost.org/libs/smart_ptr/shared_ptr.htm
> >
> > I'm not sure how NEW0 or NEW or New is better than new... Making a macro
> > that calls new doesn't encapsulate the call to new, it only obscures it and
> > makes it less flexible.
> >
> >
> >
> > -----Original Message-----
> > From: bringiton bringiton [mailto:kneeride <at> gmail.com]
> > Sent: Thursday, July 13, 2006 6:38 PM
> > To: boost-users <at> lists.boost.org
> > Subject: [Boost-users] Macro to contruct/allocate a shared_ptr?
> >
> > I'm trying to define a MACRO that creates a shared_ptr and hides memory
> > allocation. Here is what i have so far:
> >
> > #define NEW0(T) boost::shared_ptr<T>(new T()); #define NEW1(T, p1)
> > boost::shared_ptr<T>(new T(p1)); #define NEW2(T, p1, p2)
> > boost::shared_ptr<T>(new T(p1, p2)); #define NEW3(T, p1, p2, p3)
> > boost::shared_ptr<T>(new T(p1, p2, p3));
> >
> > ---------------------------------
> > Example of usage for the type Test:
> >
> > class Test {
> > public:
> > Test() {;}
> > Test(int p1) {;}
> > Test(int p1, char p2) {;}
> > Test(int p1, char p2, std::string p3) {;} };
> >
> > boost::shared_ptr<Test> p0 = NEW0(Test); boost::shared_ptr<Test> p1 =
> > NEW1(Test, 100); boost::shared_ptr<Test> p2 = NEW2(Test, 100, 'a');
> > boost::shared_ptr<Test> p3 = NEW3(Test, 100, 'a', "hello");
> >
> > ---------------------------------
> > Is there a better way to do this? I would prefer to use a function instead
> > of a MACRO. But I don't think it is possible because it is not know how
many
> > paramaters are in T's constructor.
> >
> > // not sure how to do this (or whether possible) // i am not sure how to
> > implement the variable parameters // (hense the .. in my syntax) template
> > <class T> boost::shared_ptr<T> New(...) {
> > boost::shared_ptr<T> p(new T(...));
> > return p;
> > }
> >
> > Any ideas? I often have problems with the c++ syntax when creating my own
> > containers because i want to allocate T within the implementation (instead
> > of passing pointers and having the user manage the memory).
> > _______________________________________________
> > Boost-users mailing list
> > Boost-users <at> lists.boost.org
> > http://lists.boost.org/mailman/listinfo.cgi/boost-users
> >
> >
> > _______________________________________________
> > Boost-users mailing list
> > Boost-users <at> lists.boost.org
> > http://lists.boost.org/mailman/listinfo.cgi/boost-users
> >
>
> I just have a problem with having the user manage the memory. ie I'd
> rather do the following:
>
> shared_ptr<Test> p(1, 'a', "hello");
> or:
> shared_ptr<Test> p = newptr<Test>(1, 'a', "hello");
>
> instead of:
>
> shared_ptr<Test> p(new p(1, 'a', "hello"));
>
> but i guess that's a limitation of the language. to get around this i
> do the following:
>
> class Test {
> private:
> Test(int p1, char p2, string p3) {;}
> public:
> static shared_ptr<Test> NewTest(int p1, char p2, string p3) {
> return p(new Test(p1, p2, p3);
> }
> ...
>
> // memory allocation hidden
> shared_ptr<Test> t = Test::NewTest(1, 'a', "hello");
>
> but this is a hassle. esp when many constructors...
>

You can try this:

#include <boost/shared_ptr.hpp>

template <class R>
boost::shared_ptr<R> newptr()
{
        return boost::shared_ptr<R>(new R());
}

template <class R, class T0>
boost::shared_ptr<R> newptr(T0 p0)
{
        return boost::shared_ptr<R>(new R(p0));
}

template <class R, class T0, class T1>
boost::shared_ptr<R> newptr(T0 p0, T1 p1)
{
        return boost::shared_ptr<R>(new R(p0, p1));
}

// and so on...

Also you can generate newptr functions with the boost preprocessor library.

#include <boost/preprocessor.hpp>

#define BOOST_PP_LOCAL_MACRO(N)\
template <class R BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_PARAMS(N, class T)>\
boost::shared_ptr<R> newptr(BOOST_PP_ENUM_BINARY_PARAMS(N, T, p))\
{\
        return boost::shared_ptr<R>(new R(BOOST_PP_ENUM_PARAMS(N, p)));\
}

#define BOOST_PP_LOCAL_LIMITS (0, 10)
#include BOOST_PP_LOCAL_ITERATE()

Now you can use newptr with by up to 10 arguments.

HTH,
Roman Perepelitsa.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net