Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / GeomAPI / GeomAPI_Interface.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef GeomAPI_Interface_H_
22 #define GeomAPI_Interface_H_
23
24 #include <GeomAPI.h>
25
26 #include <memory>
27
28 /**\class GeomAPI_Interface
29  * \ingroup DataModel
30  * \brief General base class for all interfaces in this package
31  */
32
33 template <typename T>
34 void GeomAPI_deleter( void* p ) {
35    delete reinterpret_cast<T*>(p);
36 }
37
38 class GeomAPI_Interface
39 {
40  private:
41   std::shared_ptr<char> myImpl;  ///< pointer to the internal impl object
42
43  public:
44   /// None - constructor
45   GEOMAPI_EXPORT GeomAPI_Interface();
46
47   /// Constructor by the impl pointer (used for internal needs)
48   template<class T> explicit GeomAPI_Interface(T* theImpl)
49   {
50     myImpl = std::shared_ptr<char>(reinterpret_cast<char*>(theImpl), GeomAPI_deleter<T>);
51   }
52
53   /// Destructor
54   GEOMAPI_EXPORT virtual ~GeomAPI_Interface();
55
56   /// Returns the pointer to the impl
57   template<class T> inline T* implPtr()
58   {
59     return reinterpret_cast<T*>(myImpl.get());
60   }
61   /// Returns the pointer to the impl
62   template<class T> inline const T* implPtr() const
63   {
64     return reinterpret_cast<T*>(myImpl.get());
65   }
66   /// Returns the reference object of the impl
67   template<class T> inline const T& impl() const
68   {
69     return *(reinterpret_cast<T*>(myImpl.get()));
70   }
71   /// Updates the impl (deletes the old one)
72   template<class T> inline void setImpl(T* theImpl)
73   {
74     myImpl = std::shared_ptr<char>(reinterpret_cast<char*>(theImpl), GeomAPI_deleter<T>);
75   }
76
77   /// Returns true if the impl is empty
78   GEOMAPI_EXPORT bool empty() const;
79 };
80
81 #endif