Salome HOME
Windows compilation.
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingAutoRefCountObjectPtr.hxx
index fc0c0f4e9a5de28061d285c7dea7c935cbdb0895..7e097ff570a2534e024733d42781ad927f6172a3 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2014  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
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// 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
@@ -22,6 +22,7 @@
 #define __PARAMEDMEM_MEDCOUPLINGAUTOREFCOUNTOBJECTPTR_HXX__
 
 #include "MEDCouplingRefCountObject.hxx"
+#include "InterpKernelException.hxx"
 
 namespace ParaMEDMEM
 {
@@ -41,12 +42,37 @@ namespace ParaMEDMEM
     const T& operator*() const { return *_ptr; }
     operator T *() { return _ptr; }
     operator const T *() const { return _ptr; }
+    T *retn() { if(_ptr) _ptr->incrRef(); return _ptr; }
   private:
     void referPtr(T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); }
     void destroyPtr() { if(_ptr) _ptr->decrRef(); }
   private:
     T *_ptr;
   };
+
+  template<class T, class U>
+  typename ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<U> DynamicCast(typename ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<T>& autoSubPtr) throw()
+  {
+    T *subPtr(autoSubPtr);
+    U *ptr(dynamic_cast<U *>(subPtr));
+    typename ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<U> ret(ptr);
+    if(ptr)
+      ptr->incrRef();
+    return ret;
+  }
+
+  template<class T, class U>
+  typename ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<U> DynamicCastSafe(typename ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<T>& autoSubPtr)
+  {
+    T *subPtr(autoSubPtr);
+    U *ptr(dynamic_cast<U *>(subPtr));
+    if(subPtr && !ptr)
+      throw INTERP_KERNEL::Exception("DynamicCastSafe : U is not a subtype of T !");
+    typename ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<U> ret(ptr);
+    if(ptr)
+      ptr->incrRef();
+    return ret;
+  }
 }
 
 #endif