X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDCoupling%2FMCAuto.hxx;h=3a64be546b9d73740fe4cd91f090ef36e09f433a;hb=b8616069f6667dcec3f90574a05bfc9e66d87fa5;hp=0fd3a97af004cee4ae4de7c19dbc9aec6509ea26;hpb=b3e28553eab4c76446ec4214ad4a8d71528905ab;p=tools%2Fmedcoupling.git diff --git a/src/MEDCoupling/MCAuto.hxx b/src/MEDCoupling/MCAuto.hxx index 0fd3a97af..3a64be546 100644 --- a/src/MEDCoupling/MCAuto.hxx +++ b/src/MEDCoupling/MCAuto.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2016 CEA/DEN, EDF R&D +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -18,24 +18,28 @@ // // Author : Anthony Geay (CEA/DEN) -#ifndef __PARAMEDMEM_MEDCOUPLINGAUTOREFCOUNTOBJECTPTR_HXX__ -#define __PARAMEDMEM_MEDCOUPLINGAUTOREFCOUNTOBJECTPTR_HXX__ +#pragma once #include "MEDCouplingRefCountObject.hxx" #include "InterpKernelException.hxx" +#include +#include + namespace MEDCoupling { template class MCAuto { public: - MCAuto(const MCAuto& other):_ptr(0) { referPtr(other._ptr); } - MCAuto(T *ptr=0):_ptr(ptr) { } + static MCAuto TakeRef(T *ptr) { MCAuto ret; ret.takeRef(ptr); return ret; } + MCAuto(const MCAuto& other):_ptr(nullptr) { referPtr(other._ptr); } + MCAuto(T *ptr=nullptr):_ptr(ptr) { } ~MCAuto() { destroyPtr(); } - bool isNull() const { return _ptr==0; } + void checkNotNull() const { if(!_ptr) throw INTERP_KERNEL::Exception("Pointer is nullptr !"); } + bool isNull() const { return _ptr==nullptr; } bool isNotNull() const { return !isNull(); } - void nullify() { destroyPtr(); _ptr=0; } + void nullify() { destroyPtr(); _ptr=nullptr; } bool operator==(const MCAuto& other) const { return _ptr==other._ptr; } bool operator==(const T *other) const { return _ptr==other; } MCAuto &operator=(const MCAuto& other) { if(_ptr!=other._ptr) { destroyPtr(); referPtr(other._ptr); } return *this; } @@ -48,6 +52,7 @@ namespace MEDCoupling operator T *() { return _ptr; } operator const T *() const { return _ptr; } T *retn() { if(_ptr) _ptr->incrRef(); return _ptr; } + T *retnConstCast() const { if(_ptr) _ptr->incrRef(); return _ptr; } T *iAmATrollConstCast() const { return _ptr; } private: void referPtr(T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); } @@ -56,8 +61,38 @@ namespace MEDCoupling T *_ptr; }; + template + std::vector FromVecAutoToVecOfConst(const std::vector>& inputVec) + { + std::size_t size(inputVec.size()); + std::vector< const T *> ret(size); + typename std::vector< const T *>::iterator itArrays(ret.begin()); + std::for_each(inputVec.begin(),inputVec.end(),[&itArrays](MCAuto elt) { *itArrays++=elt; }); + return ret; + } + + template + std::vector> FromVecToVecAuto(const std::vector& inputVec) + { + std::size_t size(inputVec.size()); + std::vector> ret(size); + typename std::vector>::iterator itArrays(ret.begin()); + std::for_each(inputVec.begin(),inputVec.end(),[&itArrays](T *elt) { (*itArrays).takeRef(elt); itArrays++; }); + return ret; + } + + template + std::vector> FromVecConstToVecAuto(const std::vector& inputVec) + { + std::size_t size(inputVec.size()); + std::vector> ret(size); + typename std::vector>::iterator itArrays(ret.begin()); + std::for_each(inputVec.begin(),inputVec.end(),[&itArrays](const T *elt) { (*itArrays).takeRef(const_cast(elt)); itArrays++; }); + return ret; + } + template - typename MEDCoupling::MCAuto DynamicCast(typename MEDCoupling::MCAuto& autoSubPtr) throw() + typename MEDCoupling::MCAuto DynamicCast(typename MEDCoupling::MCAuto& autoSubPtr) noexcept(true) { T *subPtr(autoSubPtr); U *ptr(dynamic_cast(subPtr)); @@ -80,11 +115,22 @@ namespace MEDCoupling return ret; } + template + typename std::vector ToConstVect(const typename std::vector< MCAuto >& vec) + { + std::size_t sz(vec.size()); + std::vector ret(sz); + for(std::size_t i=0;i class MCConstAuto { public: MCConstAuto(const MCConstAuto& other):_ptr(0) { referPtr(other._ptr); } + MCConstAuto(const typename MEDCoupling::MCAuto & other):_ptr(0) { referPtr( (const T*) other); } MCConstAuto(const T *ptr=0):_ptr(ptr) { } ~MCConstAuto() { destroyPtr(); } bool isNull() const { return _ptr==0; } @@ -100,6 +146,7 @@ namespace MEDCoupling const T& operator*() { return *_ptr; } const T& operator*() const { return *_ptr; } operator const T *() const { return _ptr; } + T *shameOnMeConstCast() const { return const_cast(_ptr); } private: void referPtr(const T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); } void destroyPtr() { if(_ptr) _ptr->decrRef(); } @@ -107,5 +154,3 @@ namespace MEDCoupling const T *_ptr; }; } - -#endif