X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDWrapper%2FMED_SharedPtr.hxx;fp=src%2FMEDWrapper%2FMED_SharedPtr.hxx;h=09dc7ad9211e9bf2fa6564014db492c0792c3a39;hb=d3e1cabf77f67408ac401e2d387e721605cb3b10;hp=0000000000000000000000000000000000000000;hpb=d5029840731bccaa1718e65f0abf3b19198c7293;p=modules%2Fsmesh.git diff --git a/src/MEDWrapper/MED_SharedPtr.hxx b/src/MEDWrapper/MED_SharedPtr.hxx new file mode 100644 index 000000000..09dc7ad92 --- /dev/null +++ b/src/MEDWrapper/MED_SharedPtr.hxx @@ -0,0 +1,93 @@ +// Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#ifndef MED_SharedPtr_HeaderFile +#define MED_SharedPtr_HeaderFile + +#include + +namespace MED +{ + //! To extend the boost::shared_ptr to support such features automatic dynamic cast + /*! + All entities of the MEDWrapper package are handled as pointer. + This class was introduced to provide correct and flexible memory management + for all of the MEDWrapper objects. + */ + template class SharedPtr: public boost::shared_ptr + { + public: + //! Default constructor + SharedPtr() {} + + //! Construct the class by any type of a pointer + template + explicit SharedPtr(Y * p): + boost::shared_ptr(p) + {} + + //! Construct the class by any specialisation of the class + template + SharedPtr(SharedPtr const & r): + boost::shared_ptr(boost::dynamic_pointer_cast(r)) + {} + + //! Copy-constructor + template + SharedPtr& + operator=(SharedPtr const & r) + { + SharedPtr(r).swap(*this); + return *this; + } + + //! Introduce a flexible way to reset the wrapped pointer + template + SharedPtr& + operator()(Y * p) // Y must be complete + { + return operator=(SharedPtr(p)); + } + + //! Introduce a flexible way to reset the wrapped pointer + template + SharedPtr& + operator()(SharedPtr const & r) // Y must be complete + { + return operator=(SharedPtr(r)); + } + + //! To provide a flexible way to use reference to the wrapped pointer (const version) + operator const T& () const + { + return *(this->get()); + } + + //! To provide a flexible way to use reference to the wrapped pointer + operator T& () + { + return *(this->get()); + } + }; +} + +#endif // MED_SharedPtr_HeaderFile