Advertisement
mantielero

Standard_Handle_mod.hxx

Sep 26th, 2021
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.49 KB | None | 0 0
  1. // Copyright (c) 2014 OPEN CASCADE SAS
  2. //
  3. // This file is part of Open CASCADE Technology software library.
  4. //
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License version 2.1 as published
  7. // by the Free Software Foundation, with special exception defined in the file
  8. // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
  9. // distribution for complete text of the license and disclaimer of any warranty.
  10. //
  11. // Alternatively, this file may be used under the terms of Open CASCADE
  12. // commercial license or contractual agreement.
  13.  
  14. #ifndef _Standard_Handle_HeaderFile
  15. #define _Standard_Handle_HeaderFile
  16.  
  17. #include <Standard_Address.hxx>
  18. #include <Standard_Std.hxx>
  19. #include <Standard_Stream.hxx>
  20. #include <Standard_Transient.hxx>
  21.  
  22. class Standard_Transient;
  23.  
  24. //! Namespace opencascade is intended for low-level template classes and functions
  25. namespace opencascade {
  26.  
  27.   //! Intrusive smart pointer for use with Standard_Transient class and its descendants.
  28.   //!
  29.   //! This class is similar to boost::intrusive_ptr<>. The reference counter
  30.   //! is part of the base class (Standard_Transient), thus creation of a handle
  31.   //! does not require allocation of additional memory for the counter.
  32.   //! All handles to the same object share the common counter; object is deleted
  33.   //! when the last handle pointing on it is destroyed. It is safe to create a new
  34.   //! handle from plain C pointer to the object already pointed by another handle.
  35.   //! The same object can be referenced by handles of different types (as soon as
  36.   //! they are compatible with the object type).
  37.   //!
  38.   //! Handle has type cast operator to const reference to handle to the base
  39.   //! types, which allows it to be passed by reference in functions accepting
  40.   //! reference to handle to base class, without copying.
  41.   //!
  42.   //! By default, the type cast operator is provided also for non-const reference.
  43.   //! These casts (potentially unsafe) can be disabled by defining macro
  44.   //! OCCT_HANDLE_NOCAST; if it is defined, generalized copy constructor
  45.   //! and assignment operators are defined allowing to initialize handle
  46.   //! of base type from handle to derived type.
  47.   //!
  48.   //! Weak pointers are not supported.
  49.   template <class T>
  50.   class handle
  51.   {
  52.   public:
  53.     //! STL-compliant typedef of contained type
  54.     typedef T element_type;
  55.  
  56.   public:
  57.  
  58.     //! Empty constructor
  59.     handle () : entity(0) {}
  60.  
  61.     //! Constructor from pointer to new object
  62.     handle (const T *thePtr) : entity(const_cast<T*>(thePtr))
  63.     {
  64.       BeginScope();
  65.     }
  66.  
  67.     //! Copy constructor
  68.     handle (const handle& theHandle) : entity(theHandle.entity)
  69.     {
  70.       BeginScope();
  71.     }
  72.  
  73. #ifndef OCCT_NO_RVALUE_REFERENCE
  74.     handle (handle&& theHandle) : entity(theHandle.entity)
  75.     {
  76.       theHandle.entity = 0;
  77.     }
  78. #endif
  79.  
  80.     //! Destructor
  81.     ~handle ()
  82.     {
  83.       EndScope();
  84.     }
  85.  
  86.     //! Nullify the handle
  87.     void Nullify()
  88.     {
  89.       EndScope();
  90.     }
  91.  
  92.     //! Check for being null
  93.     bool IsNull() const { return entity == 0; }
  94.  
  95.     //! Reset by new pointer
  96.     void reset (T* thePtr)
  97.     {
  98.       Assign (thePtr);
  99.     }
  100.  
  101.     //! Assignment operator
  102.     handle& operator= (const handle& theHandle)
  103.     {
  104.       Assign (theHandle.entity);
  105.       return *this;
  106.     }
  107.  
  108.     //! Assignment to pointer
  109.     handle& operator= (const T* thePtr)
  110.     {
  111.       Assign (const_cast<T*>(thePtr));
  112.       return *this;
  113.     }
  114.  
  115. #ifndef OCCT_NO_RVALUE_REFERENCE
  116.     handle& operator= (handle&& theHandle)
  117.     {
  118.       std::swap (this->entity, theHandle.entity);
  119.       return *this;
  120.     }
  121. #endif
  122.  
  123.     //! STL-like cast to pointer to referred object (note non-const).
  124.     //! @sa std::shared_ptr::get()
  125.     T* get() const { return static_cast<T*>(this->entity); }
  126.  
  127.     //! Member access operator (note non-const)
  128.     T* operator-> () const { return static_cast<T*>(this->entity); }
  129.  
  130.     //! Dereferencing operator (note non-const)
  131.     T& operator* () const { return *get(); }
  132.  
  133.     //! Check for equality
  134.     template <class T2>
  135.     bool operator== (const handle<T2>& theHandle) const
  136.     {
  137.       return get() == theHandle.get();
  138.     }
  139.  
  140.     //! Check for equality
  141.     template <class T2>
  142.     bool operator== (const T2 *thePtr) const
  143.     {
  144.       return get() == thePtr;
  145.     }
  146.  
  147.     //! Check for equality
  148.     template <class T2>
  149.     friend bool operator== (const T2 *left, const handle& right)
  150.  
  151.  
  152.     template <class T2>
  153.     bool operator!= (const handle<T2>& theHandle) const
  154.  
  155.  
  156.  
  157.     template <class T2>
  158.     bool operator!= (const T2 *thePtr) const
  159.  
  160.  
  161.  
  162.     template <class T2>
  163.     friend bool operator!= (const T2 *left, const handle& right)
  164.  
  165.  
  166.  
  167.     template <class T2>
  168.     bool operator< (const handle<T2>& theHandle) const
  169.  
  170.  
  171.  
  172.     template <class T2>
  173.     static typename opencascade::std::enable_if<is_base_but_not_same<T2, T>::value, handle>::type
  174.       DownCast (const handle<T2>& theObject)
  175.  
  176.  
  177.     template <class T2>
  178.     static typename opencascade::std::enable_if<is_base_but_not_same<T2, T>::value, handle>::type
  179.       DownCast (const T2* thePtr)
  180.  
  181.  
  182.  
  183.     template <class T2>
  184.     Standard_DEPRECATED("down-casting from object of the same or unrelated type is meaningless")
  185.     static handle DownCast (const handle<T2>& theObject, typename opencascade::std::enable_if<!is_base_but_not_same<T2, T>::value, void*>::type = 0)
  186.  
  187.  
  188.     template <class T2>
  189.     Standard_DEPRECATED("down-casting from object of the same or unrelated type is meaningless")
  190.     static handle DownCast (const T2* thePtr, typename opencascade::std::enable_if<!is_base_but_not_same<T2, T>::value, void*>::type = 0)
  191.  
  192.  
  193. #if (defined(__clang__)) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1300) || \
  194.     (defined(_MSC_VER) && _MSC_VER >= 1800) || \
  195.     (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
  196.  
  197.     //! Conversion to bool for use in conditional expressions
  198.     explicit operator bool () const
  199.  
  200.  
  201. #else /* fallback version for compilers not supporting explicit conversion operators (VC10, VC11, GCC below 4.5) */
  202.  
  203.     //! Conversion to bool-compatible type for use in conditional expressions
  204.     operator Standard_Transient* handle::* () const
  205.  
  206.  
  207. #endif
  208.  
  209.     // Support of conversions to handle of base type:
  210.     // - copy and move constructors and assignment operators if OCCT_HANDLE_NOCAST is defined
  211.     // - operators of upcast to const reference to base type otherwise
  212. #if (defined(__clang__)) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1206) || \
  213.     (defined(_MSC_VER) && _MSC_VER >= 1800) || \
  214.     (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
  215.  
  216. #ifdef OCCT_HANDLE_NOCAST
  217.  
  218.     //! Generalized copy constructor.
  219.     //! Constructs handle holding entity of base type (T) from the one which holds entity of derived type (T2).
  220.     template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
  221.     handle (const handle<T2>& theHandle) :   entity(theHandle.entity)
  222.  
  223.  
  224.     template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
  225.     handle (handle<T2>&& theHandle) : entity(theHandle.entity)
  226.  
  227.  
  228.     template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
  229.     handle operator = (const handle<T2>& theHandle)
  230.  
  231.  
  232.  
  233.     template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
  234.     handle& operator= (handle<T2>&& theHandle)
  235.  
  236.  
  237. #else
  238.  
  239.     //! Upcast to const reference to base type.
  240.     template <class T2, typename = typename std::enable_if<is_base_but_not_same<T2, T>::value>::type>
  241.     operator const handle<T2>& () const
  242.  
  243.  
  244.  
  245.     template <class T2, typename = typename std::enable_if<is_base_but_not_same<T2, T>::value>::type>
  246.     operator handle<T2>& ()
  247.  
  248.  
  249. #endif /* OCCT_HANDLE_NOCAST */
  250.  
  251. #else /* fallback version for compilers not supporting default arguments of function templates (VC10, VC11, GCC below 4.3) */
  252.  
  253. #ifdef OCCT_HANDLE_NOCAST
  254.  
  255.     //! Generalized copy constructor.
  256.     //! Constructs handle holding entity of base type (T) from the one which holds entity of derived type (T2).
  257.     template <class T2>
  258.     handle (const handle<T2>& theHandle, typename std::enable_if <is_base_but_not_same <T, T2>::value>::type* = nullptr) :
  259.       entity(theHandle.entity)
  260.  
  261.  
  262. #ifndef OCCT_NO_RVALUE_REFERENCE
  263.     //! Generalized move constructor
  264.     template <class T2>
  265.     handle (handle<T2>&& theHandle, typename std::enable_if <is_base_but_not_same <T, T2>::value>::type* = nullptr)
  266.       : entity(theHandle.entity)
  267.  
  268. #endif
  269.  
  270.     //! Generalized assignment operator.
  271.     template <class T2>
  272.     handle operator = (const handle<T2>& theHandle)
  273.  
  274.  
  275. #ifndef OCCT_NO_RVALUE_REFERENCE
  276.     //! Generalized move operator
  277.     template <class T2>
  278.     handle& operator= (handle<T2>&& theHandle)
  279.  
  280. #endif
  281.  
  282. #else
  283.  
  284.     //! Upcast to const reference to base type.
  285.     //! NB: this implementation will cause ambiguity errors on calls to overloaded
  286.     //! functions accepting handles to different types, since compatibility is
  287.     //! checked in the cast code rather than ensured by SFINAE (possible with C++11)
  288.     template <class T2>
  289.     operator const handle<T2>& () const
  290.  
  291.     template <class T2>
  292.     Standard_DEPRECATED("Passing non-const reference to handle of base type in function is unsafe; use variable of exact type")
  293.     operator handle<T2>& ()
  294.  
  295.  
  296. #endif /* OCCT_HANDLE_NOCAST */
  297.  
  298. #endif /* compiler switch */
  299.  
  300.   private:
  301.  
  302.     //! Assignment
  303.     void Assign (Standard_Transient *thePtr)
  304.  
  305.  
  306.     //! Increment reference counter of referred object
  307.     void BeginScope()
  308.  
  309.     void EndScope()
  310.  
  311.     template <class T2> friend class handle;
  312.  
  313.   private:
  314.     Standard_Transient* entity;
  315.   };
  316.  
  317. } // namespace opencascade
  318.  
  319.  
  320. #define Handle(Class) opencascade::handle<Class>
  321.  
  322. //! Computes a hash code for the standard handle, in the range [1, theUpperBound]
  323. //! @param theHandle the handle which hash code is to be computed
  324. //! @param theUpperBound the upper bound of the range a computing hash code must be within
  325. //! @return a computed hash code, in the range [1, theUpperBound]
  326. template <class TheTransientType>
  327. Standard_Integer HashCode (const Handle (TheTransientType) & theHandle, const Standard_Integer theUpperBound);
  328.  
  329.  
  330.  
  331. #if (defined(_MSC_VER) && _MSC_VER >= 1800)
  332. //! For Visual Studio 2013+, define Handle_Class as non-template class to allow exporting this type in C++/CLI.
  333. #define DEFINE_STANDARD_HANDLECLASS(C1,C2,BC) class C1; class Handle_##C1 : public Handle(C1) \
  334. { \
  335. public: \
  336.   Handle_##C1() {} \
  337.   Handle_##C1(Handle(C1)&& theHandle) : Handle(C1)(theHandle) {} \
  338.   template <class T2, typename = typename std::enable_if <std::is_base_of <C1,T2>::value>::type> \
  339.   inline Handle_##C1(const opencascade::handle<T2>& theOther) : Handle(C1)(theOther) {} \
  340.   template <class T2, typename = typename std::enable_if <std::is_base_of <C1,T2>::value>::type> \
  341.   inline Handle_##C1(const T2* theOther) : Handle(C1)(theOther) {} \
  342.   template<typename T> inline Handle_##C1& operator=(T theOther) { Handle(C1)::operator=(theOther); return *this; } \
  343. };
  344. #else
  345. #define DEFINE_STANDARD_HANDLECLASS(C1,C2,BC) class C1; typedef Handle(C1) Handle_##C1;
  346. #endif
  347.  
  348. #define DEFINE_STANDARD_HANDLE(C1,C2) DEFINE_STANDARD_HANDLECLASS(C1,C2,Standard_Transient)
  349. #define DEFINE_STANDARD_PHANDLE(C1,C2) DEFINE_STANDARD_HANDLECLASS(C1,C2,Standard_Persistent)
  350.  
  351. #endif
  352.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement