Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:19

0001 // Copyright (c) 2023 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _NCollection_OccAllocator_HeaderFile
0015 #define _NCollection_OccAllocator_HeaderFile
0016 
0017 #include <NCollection_BaseAllocator.hxx>
0018 #include <Standard.hxx>
0019 
0020 #include <memory>
0021 
0022 //! Implements allocator requirements as defined in ISO C++ Standard 2003, section 20.1.5.
0023 /*! The allocator uses a standard OCCT mechanism for memory
0024   allocation and deallocation. It can be used with standard
0025   containers (std::vector, std::map, etc.) to take advantage of OCCT memory optimizations.
0026 
0027   Example of use:
0028   \code
0029   NCollection_OccAllocator<TopoDS_Shape> anSAllocator();
0030   std::list<TopoDS_Shape, NCollection_OccAllocator<TopoDS_Shape>> aList(anSAllocator);
0031   TopoDS_Solid aSolid = BRepPrimAPI_MakeBox(10., 20., 30.);
0032   aList.push_back(aSolid);
0033   \endcode
0034 */
0035 template <typename ItemType>
0036 class NCollection_OccAllocator
0037 {
0038 public:
0039 
0040   typedef ItemType value_type;
0041   typedef value_type* pointer;
0042   typedef const value_type* const_pointer;
0043   typedef value_type& reference;
0044   typedef const value_type& const_reference;
0045   typedef size_t size_type;
0046   typedef ptrdiff_t difference_type;
0047   typedef std::false_type propagate_on_container_move_assignment; // std::false_type
0048 
0049   template <typename OtherType>
0050   struct rebind
0051   {
0052     typedef NCollection_OccAllocator<OtherType> other;
0053   };
0054 
0055   //! Constructor.
0056   //! Creates an object using the default Open CASCADE allocation mechanism, i.e., which uses
0057   //! Standard::Allocate() and Standard::Free() underneath.
0058   NCollection_OccAllocator() :
0059     myAllocator(nullptr)
0060   {}
0061 
0062   //! Constructor.
0063   NCollection_OccAllocator(const Handle(NCollection_BaseAllocator)& theAlloc) :
0064     myAllocator(theAlloc)
0065   {}
0066 
0067   //! Constructor.
0068   NCollection_OccAllocator(Handle(NCollection_BaseAllocator)&& theAlloc) :
0069     myAllocator(theAlloc)
0070   {}
0071 
0072   //! Constructor.
0073   NCollection_OccAllocator(const NCollection_OccAllocator& theOther) :
0074     myAllocator(theOther.myAllocator)
0075   {}
0076 
0077   //! Constructor.
0078   NCollection_OccAllocator(NCollection_OccAllocator&& theOther) noexcept :
0079     myAllocator(theOther.myAllocator)
0080   {}
0081 
0082   //! Assignment operator
0083   NCollection_OccAllocator& operator=(const NCollection_OccAllocator& theOther)
0084   {
0085     myAllocator = theOther.myAllocator;
0086     return *this;
0087   }
0088 
0089   //! Assignment operator
0090   NCollection_OccAllocator& operator=(NCollection_OccAllocator&& theOther) noexcept
0091   {
0092     myAllocator = theOther.myAllocator;
0093     return *this;
0094   }
0095 
0096   //! Assignment operator
0097   template <typename OtherType>
0098   NCollection_OccAllocator& operator=(const NCollection_OccAllocator<OtherType>& theOther)
0099   {
0100     myAllocator = theOther.myAllocator;
0101     return *this;
0102   }
0103 
0104   //! Constructor.
0105   //! Creates an object using the default Open CASCADE allocation mechanism, i.e., which uses
0106   //! Standard::Allocate() and Standard::Free() underneath.
0107   template <typename OtherType>
0108   NCollection_OccAllocator(const NCollection_OccAllocator<OtherType>& theOther) :
0109     myAllocator(theOther.Allocator())
0110   {}
0111 
0112   void SetAllocator(const Handle(NCollection_BaseAllocator)& theAlloc)
0113   {
0114     myAllocator = theAlloc;
0115   }
0116 
0117   const Handle(NCollection_BaseAllocator)& Allocator() const
0118   {
0119     return myAllocator;
0120   }
0121 
0122   //! Allocates memory for theSize objects.
0123   pointer allocate(size_type theSize, const void* = 0)
0124   {
0125     return static_cast<pointer> (myAllocator.IsNull() ?
0126                                  Standard::AllocateOptimal(theSize * sizeof(ItemType)) :
0127                                  myAllocator->AllocateOptimal(theSize * sizeof(ItemType)));
0128   }
0129 
0130   //! Template version of function Free(), nullifies the argument pointer
0131   //! @param thePtr - previously allocated memory block to be freed
0132   template <typename T>
0133   void deallocate(T* thePnt, size_type)
0134   {
0135     myAllocator.IsNull() ?
0136       Standard::Free(thePnt) :
0137       myAllocator->Free(thePnt);
0138   }
0139 
0140   //! Frees previously allocated memory.
0141   void deallocate(pointer thePnt, size_type)
0142   {
0143     myAllocator.IsNull() ?
0144       Standard::Free(thePnt) :
0145       myAllocator->Free(thePnt);
0146   }
0147 
0148   //! Constructs an object.
0149   //! Uses placement new operator and copy constructor to construct an object.
0150   template<class _Objty, class... _Types>
0151   void construct(_Objty* _Ptr, _Types&&... _Args)
0152   {
0153     ::new ((void*)_Ptr) _Objty(std::forward<_Types>(_Args)...);
0154   }
0155 
0156   //! Returns an object address.
0157   pointer address(reference theItem) const
0158   {
0159     return &theItem;
0160   }
0161 
0162   //! Returns an object address.
0163   const_pointer address(const_reference theItem) const
0164   {
0165     return &theItem;
0166   }
0167 
0168   //! Destroys the object.
0169   //! Uses the object destructor.
0170   template<class _Uty>
0171   void destroy(_Uty* _Ptr)
0172   {
0173     (void)_Ptr; _Ptr->~_Uty();
0174   }
0175 
0176   //! Estimate maximum array size
0177   size_t max_size() const noexcept
0178   {
0179     return ((size_t)(-1) / sizeof(ItemType));
0180   }
0181 
0182   bool operator==(const NCollection_OccAllocator& theOther) const
0183   {
0184     return theOther.Allocator() == myAllocator;
0185   }
0186 
0187   template<class U>
0188   bool operator==(const NCollection_OccAllocator<U>& theOther) const
0189   {
0190     return theOther.Allocator() == myAllocator;
0191   }
0192 
0193   bool operator!=(const NCollection_OccAllocator& theOther) const
0194   {
0195     return theOther.Allocator() != myAllocator;
0196   }
0197 
0198   template<class U>
0199   bool operator!=(const NCollection_OccAllocator<U>& theOther) const
0200   {
0201     return theOther.Allocator() != myAllocator;
0202   }
0203 
0204 private:
0205 
0206   Handle(NCollection_BaseAllocator) myAllocator;
0207 };
0208 
0209 template<class U, class V>
0210 bool operator==(const NCollection_OccAllocator<U>& theFirst, const NCollection_OccAllocator<V>& theSecond)
0211 {
0212   return theFirst.Allocator() == theSecond.Allocator();
0213 }
0214 
0215 #endif