Salome HOME
gcc 4.9 compatibility: v2 (avoid memory leaks)
[modules/shaper.git] / src / GeomAPI / GeomAPI_Interface.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Interface.hxx
4 // Created:     23 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef GeomAPI_Interface_H_
8 #define GeomAPI_Interface_H_
9
10 #include <GeomAPI.h>
11
12 #include <memory>
13
14 /**\class GeomAPI_Interface
15  * \ingroup DataModel
16  * \brief General base class for all interfaces in this package
17  */
18
19 template <typename T>
20 void GeomAPI_deleter( void* p ) {
21    delete reinterpret_cast<T*>(p);
22 }
23
24 class GeomAPI_Interface
25 {
26  private:
27   std::shared_ptr<char> myImpl;  ///< pointer to the internal impl object
28
29  public:
30   /// None - constructor
31   GEOMAPI_EXPORT GeomAPI_Interface();
32
33   /// Constructor by the impl pointer (used for internal needs)
34   template<class T> explicit GeomAPI_Interface(T* theImpl)
35   {
36     myImpl = std::shared_ptr<char>(reinterpret_cast<char*>(theImpl), GeomAPI_deleter<T>);
37   }
38
39   /// Destructor
40   GEOMAPI_EXPORT virtual ~GeomAPI_Interface();
41
42   /// Returns the pointer to the impl
43   template<class T> inline T* implPtr()
44   {
45     return reinterpret_cast<T*>(myImpl.get());
46   }
47   /// Returns the pointer to the impl
48   template<class T> inline const T* implPtr() const
49   {
50     return reinterpret_cast<T*>(myImpl.get());
51   }
52   /// Returns the reference object of the impl
53   template<class T> inline const T& impl() const
54   {
55     return *(reinterpret_cast<T*>(myImpl.get()));
56   }
57   /// Updates the impl (deletes the old one)
58   template<class T> inline void setImpl(T* theImpl)
59   {
60     myImpl = std::shared_ptr<char>(reinterpret_cast<char*>(theImpl), GeomAPI_deleter<T>);
61   }
62
63   /// Returns true if the impl is empty
64   GEOMAPI_EXPORT bool empty() const;
65 };
66
67 #endif