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
{
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(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(boost::lambda::if_then_else(_1,\
// VAR_PTR, static_cast(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/ )