From b3b45375e9b441a732d6ace7e0478d0b534ca94a Mon Sep 17 00:00:00 2001 From: ageay Date: Wed, 20 Mar 2013 11:12:07 +0000 Subject: [PATCH] Put DataArray* destructors in private to prevent users to call delete directly. --- src/MEDCoupling/MEDCouplingMemArray.cxx | 8 ++--- src/MEDCoupling/MEDCouplingMemArray.hxx | 5 +++ src/MEDCoupling/MEDCouplingMemArrayChar.cxx | 39 +++++++++++++++++++++ src/MEDCoupling_Swig/MEDCouplingCommon.i | 8 +++-- 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index fc3e9e862..a4f968c6f 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -1507,11 +1507,11 @@ DataArrayDouble *DataArrayDouble::substr(int tupleIdBg, int tupleIdEnd) const th else trueEnd=nbt; int nbComp=getNumberOfComponents(); - DataArrayDouble *ret=DataArrayDouble::New(); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); ret->alloc(trueEnd-tupleIdBg,nbComp); ret->copyStringInfoFrom(*this); std::copy(getConstPointer()+tupleIdBg*nbComp,getConstPointer()+trueEnd*nbComp,ret->getPointer()); - return ret; + return ret.retn(); } /*! @@ -6544,11 +6544,11 @@ DataArrayInt *DataArrayInt::substr(int tupleIdBg, int tupleIdEnd) const throw(IN else trueEnd=nbt; int nbComp=getNumberOfComponents(); - DataArrayInt *ret=DataArrayInt::New(); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); ret->alloc(trueEnd-tupleIdBg,nbComp); ret->copyStringInfoFrom(*this); std::copy(getConstPointer()+tupleIdBg*nbComp,getConstPointer()+trueEnd*nbComp,ret->getPointer()); - return ret; + return ret.retn(); } /*! diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 00c5d4e43..def94305c 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -326,6 +326,7 @@ namespace ParaMEDMEM static void FindTupleIdsNearTuplesAlg(const BBTree& myTree, const double *pos, int nbOfTuples, double eps, DataArrayInt *c, DataArrayInt *cI); private: + ~DataArrayDouble() { } DataArrayDouble() { } private: MemArray _mem; @@ -540,6 +541,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT bool resizeForUnserialization(const std::vector& tinyInfoI); MEDCOUPLING_EXPORT void finishUnserialization(const std::vector& tinyInfoI, const std::vector& tinyInfoS); private: + ~DataArrayInt() { } DataArrayInt() { } private: MemArray _mem; @@ -615,6 +617,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayChar *selectByTupleId2(int bg, int end, int step) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT bool isUniform(char val) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void rearrange(int newNbOfCompo) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT DataArrayChar *substr(int tupleIdBg, int tupleIdEnd=-1) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayChar *changeNbOfComponents(int newNbOfComp, char dftValue) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayChar *keepSelectedComponents(const std::vector& compoIds) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void meldWith(const DataArrayChar *other) throw(INTERP_KERNEL::Exception); @@ -687,6 +690,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT void reprCppStream(const char *varName, std::ostream& stream) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT bool isEqualIfNotWhy(const DataArrayChar& other, std::string& reason) const throw(INTERP_KERNEL::Exception); private: + ~DataArrayByte() { } DataArrayByte() { } }; @@ -741,6 +745,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT void reprCppStream(const char *varName, std::ostream& stream) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT bool isEqualIfNotWhy(const DataArrayChar& other, std::string& reason) const throw(INTERP_KERNEL::Exception); private: + ~DataArrayAsciiChar() { } DataArrayAsciiChar() { } DataArrayAsciiChar(const std::string& st) throw(INTERP_KERNEL::Exception); DataArrayAsciiChar(const std::vector& vst, char defaultChar) throw(INTERP_KERNEL::Exception); diff --git a/src/MEDCoupling/MEDCouplingMemArrayChar.cxx b/src/MEDCoupling/MEDCouplingMemArrayChar.cxx index e52d0cafa..dc21b4dec 100644 --- a/src/MEDCoupling/MEDCouplingMemArrayChar.cxx +++ b/src/MEDCoupling/MEDCouplingMemArrayChar.cxx @@ -618,6 +618,45 @@ void DataArrayChar::rearrange(int newNbOfCompo) throw(INTERP_KERNEL::Exception) declareAsNew(); } +/*! + * Returns a shorten copy of \a this array. The new DataArrayChar contains all + * tuples starting from the \a tupleIdBg-th tuple and including all tuples located before + * the \a tupleIdEnd-th one. This methods has a similar behavior as std::string::substr(). + * This method is a specialization of selectByTupleId2(). + * \param [in] tupleIdBg - index of the first tuple to copy from \a this array. + * \param [in] tupleIdEnd - index of the tuple before which the tuples to copy are located. + * If \a tupleIdEnd == -1, all the tuples till the end of \a this array are copied. + * \return DataArrayChar * - the new instance of DataArrayChar that the caller + * is to delete using decrRef() as it is no more needed. + * \throw If \a tupleIdBg < 0. + * \throw If \a tupleIdBg > \a this->getNumberOfTuples(). + \throw If \a tupleIdEnd != -1 && \a tupleIdEnd < \a this->getNumberOfTuples(). + * \sa DataArrayChar::selectByTupleId2 + */ +DataArrayChar *DataArrayChar::substr(int tupleIdBg, int tupleIdEnd) const throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + int nbt=getNumberOfTuples(); + if(tupleIdBg<0) + throw INTERP_KERNEL::Exception("DataArrayChar::substr : The tupleIdBg parameter must be greater than 0 !"); + if(tupleIdBg>nbt) + throw INTERP_KERNEL::Exception("DataArrayChar::substr : The tupleIdBg parameter is greater than number of tuples !"); + int trueEnd=tupleIdEnd; + if(tupleIdEnd!=-1) + { + if(tupleIdEnd>nbt) + throw INTERP_KERNEL::Exception("DataArrayChar::substr : The tupleIdBg parameter is greater or equal than number of tuples !"); + } + else + trueEnd=nbt; + int nbComp=getNumberOfComponents(); + MEDCouplingAutoRefCountObjectPtr ret=buildEmptySpecializedDAChar(); + ret->alloc(trueEnd-tupleIdBg,nbComp); + ret->copyStringInfoFrom(*this); + std::copy(getConstPointer()+tupleIdBg*nbComp,getConstPointer()+trueEnd*nbComp,ret->getPointer()); + return ret.retn(); +} + /*! * Returns a shorten or extended copy of \a this array. If \a newNbOfComp is less * than \a this->getNumberOfComponents() then the result array is shorten as each tuple diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index ca75f6f6d..4fa474213 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -221,6 +221,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::DataArrayByte::deepCpy; %newobject ParaMEDMEM::DataArrayByte::performCpy; %newobject ParaMEDMEM::DataArrayByteTuple::buildDAByte; +%newobject ParaMEDMEM::DataArrayChar::substr; %newobject ParaMEDMEM::DataArrayAsciiChar::New; %newobject ParaMEDMEM::DataArrayAsciiChar::__iter__; %newobject ParaMEDMEM::DataArrayAsciiChar::deepCpy; @@ -372,6 +373,8 @@ using namespace INTERP_KERNEL; %feature("unref") MEDCouplingExtrudedMesh "$this->decrRef();" %feature("unref") MEDCouplingCMesh "$this->decrRef();" %feature("unref") DataArrayInt "$this->decrRef();" +%feature("unref") DataArrayAsciiChar "$this->decrRef();" +%feature("unref") DataArrayByte "$this->decrRef();" %feature("unref") MEDCouplingField "$this->decrRef();" %feature("unref") MEDCouplingFieldDiscretizationP0 "$this->decrRef();" %feature("unref") MEDCouplingFieldDiscretizationP1 "$this->decrRef();" @@ -1831,8 +1834,9 @@ namespace ParaMEDMEM std::vector stdvecTyyppArr; std::pair > sTyyppArr; ParaMEDMEM::DataArrayInt *daIntTyypp=0; - int nbOfCompo=self->getNumberOfTuples(); - convertObjToPossibleCpp2(obj,nbOfCompo,sw1,iTypppArr,stdvecTyyppArr,sTyyppArr,daIntTyypp); + int nbOfCompo=self->getNumberOfComponents(); + int nbOfTuples=self->getNumberOfTuples(); + convertObjToPossibleCpp2(obj,nbOfTuples,sw1,iTypppArr,stdvecTyyppArr,sTyyppArr,daIntTyypp); int sw2; char vc; std::string sc; std::vector vsc; DataArrayChar *dacc=0; convertObjToPossibleCpp6(value,sw2,vc,sc,vsc,dacc); -- 2.39.2