Boost logo

Boost Users :

Subject: Re: [Boost-users] [shared_ptr] Is it possible for a shared_ptr to
From: Max (loadcom_at_[hidden])
Date: 2008-12-30 00:03:08


Sorry, the solution I presented here is absolutely incorrect.
Here is the corrected version. I've tested.

//______________SOLUTION_CODE_______________
template<class T>
class condition_selector : public std::unary_function<bool, T>
{
        T true_, false_;
public:
        condition_selector(T true__, T false__)
                : true_(true__), false_(false__) {}
        T operator()(bool condition)
        { return condition? true_ : false_; }
};
// helper function
template<class T>
condition_selector<T> make_condition_selector(T true_, T false_)
{ return condition_selector<T>(true_, false_); }


// if(CONDITION), I'll take care of deleting the ptr, or let it be otherwise
#define DECLARE_PTR_AUTO_DELETOR_CONDITIONAL(CLASS, VAR_PTR, CONDITION)\
        boost::scoped_ptr<CLASS> auto__deletor__for__ ## VAR_PTR (\
        make_condition_selector(VAR_PTR, static_cast<CLASS*>(0))(CONDITION))

// I don;t know why this version, using lambda, does not work
//#define DECLARE_PTR_AUTO_DELETOR_CONDITIONAL(CLASS, VAR_PTR, CONDITION)\
// boost::scoped_ptr<CLASS> auto__deletor__for__ ## VAR_PTR (\
// boost::lambda::ret<CLASS*>(boost::lambda::if_then_else(_1,\
// VAR_PTR, static_cast<CLASS*>(0)))(static_cast<bool>(CONDITION)))


//____________APPLICATION____________

AcDbCircle *pFace =new AcDbCircle (AcGePoint3d::kOrigin, AcGeVector3d::kZAxis, 1.0) ;
AcDbCircle *pLeftEye =new AcDbCircle (AcGePoint3d (0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbCircle *pRightEye =new AcDbCircle (AcGePoint3d (-0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbArc *pMouth =new AcDbArc (AcGePoint3d (0, 0.5, 0), 1.0, 3.141592 + (3.141592 * 0.3), 3.141592 + (3.141592 * 0.7)) ;

// Set the color property.
pFace->setColorIndex (2) ;
pLeftEye->setColorIndex (5) ;
pRightEye->setColorIndex (5) ;
pMouth->setColorIndex (1) ;
// add the entities to the new block table record
es = pBlockTableRecord->appendAcDbEntity (pFace)
DECLARE_PTR_AUTO_DELETOR(AcDbCircle, pFace, (es!=Acad::eOk));

if ( es != Acad::eOk ) {
//delete pFace ; // all these 'delete ...' not needed any more ...
//delete pLeftEye ;
//delete pRightEye ;
//delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pFace->close () ;

// ... ...

Sorry for the noise.

B/Rgds
Max


================================================================================
There seems to be a solution that can make the code a little bit
cleaner and easier to maintain.

#define DECLARE_PTR_AUTO_DELETOR(CLASS, VAR_PTR)\
        boost::scoped_ptr auto__deletor__for__ ## VAR_PTR (VAR_PTR)

AcDbCircle *pFace =new AcDbCircle (AcGePoint3d::kOrigin, AcGeVector3d::kZAxis, 1.0) ;
AcDbCircle *pLeftEye =new AcDbCircle (AcGePoint3d (0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbCircle *pRightEye =new AcDbCircle (AcGePoint3d (-0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbArc *pMouth =new AcDbArc (AcGePoint3d (0, 0.5, 0), 1.0, 3.141592 + (3.141592 * 0.3), 3.141592 + (3.141592 * 0.7)) ;

DECLARE_PTR_AUTO_DELETOR(AcDbCircle, pFace);
DECLARE_PTR_AUTO_DELETOR(AcDbCirclep, LeftEye);
DECLARE_PTR_AUTO_DELETOR(AcDbCircle, pRightEye);
DECLARE_PTR_AUTO_DELETOR(AcDbArc, pMouth);

// Set the color property.
pFace->setColorIndex (2) ;
pLeftEye->setColorIndex (5) ;
pRightEye->setColorIndex (5) ;
pMouth->setColorIndex (1) ;
// add the entities to the new block table record
if ( (es =pBlockTableRecord->appendAcDbEntity (pFace)) != Acad::eOk ) {
//delete pFace ; // all these 'delete ...' not needed any more ...
//delete pLeftEye ;
//delete pRightEye ;
//delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pFace->close () ;

if ( (es =pBlockTableRecord->appendAcDbEntity (pLeftEye)) != Acad::eOk ) {
//delete pLeftEye ;
//delete pRightEye ;
//delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pLeftEye->close () ;

if ( (es =pBlockTableRecord->appendAcDbEntity (pRightEye)) != Acad::eOk ) {
//delete pRightEye ;
//delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pRightEye->close () ;

if ( (es =pBlockTableRecord->appendAcDbEntity (pMouth)) != Acad::eOk ) {
//delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pMouth->close () ;


Thanks you all that hepled.

B/Rgds
Max

-------------------------------------------------------------------
ÐÂÀ˿ռ䡪¡ªÓëÅóÓÑ¿ªÐÄ·ÖÏíÍøÂçÐÂÉú»î£¡(http://space.sina.com.cn/ )


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