Back to home page

EIC code displayed by LXR

 
 

    


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

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_Allocator_HeaderFile
0015 #define _NCollection_Allocator_HeaderFile
0016 
0017 #include <Standard.hxx>
0018 #include <NCollection_BaseAllocator.hxx>
0019 
0020 #include <utility>
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_Allocator<TopoDS_Shape> anSAllocator();
0030   std::list<TopoDS_Shape, NCollection_Allocator<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_Allocator
0037 {
0038 public:
0039   typedef ItemType value_type;
0040   typedef value_type* pointer;
0041   typedef const value_type* const_pointer;
0042   typedef value_type& reference;
0043   typedef const value_type& const_reference;
0044   typedef size_t size_type;
0045   typedef ptrdiff_t difference_type;
0046 
0047   template <typename OtherType>
0048   struct rebind
0049   {
0050     typedef NCollection_Allocator<OtherType> other;
0051   };
0052 
0053   //! Constructor.
0054   //! Creates an object using the default Open CASCADE allocation mechanism, i.e., which uses
0055   //! Standard::Allocate() and Standard::Free() underneath.
0056   NCollection_Allocator() noexcept
0057   {}
0058 
0059   //! Constructor.
0060   NCollection_Allocator(const Handle(NCollection_BaseAllocator)&) noexcept
0061   {}
0062 
0063   //! Assignment operator
0064   template <typename OtherType>
0065   NCollection_Allocator& operator=(const NCollection_Allocator<OtherType>&) noexcept
0066   {
0067     return *this;
0068   }
0069   //! Constructor.
0070   //! Creates an object using the default Open CASCADE allocation mechanism, i.e., which uses
0071   //! Standard::Allocate() and Standard::Free() underneath.
0072   template <typename OtherType>
0073   NCollection_Allocator(const NCollection_Allocator<OtherType>&) noexcept
0074   {}
0075 
0076   //! Returns an object address.
0077   pointer address(reference theItem) const
0078   {
0079     return &theItem;
0080   }
0081 
0082   //! Returns an object address.
0083   const_pointer address(const_reference theItem) const
0084   {
0085     return &theItem;
0086   }
0087 
0088   //! Allocates memory for theSize objects.
0089   pointer allocate(const size_type theSize, const void* /*hint*/ = 0) const
0090   {
0091     return static_cast<pointer>(Standard::AllocateOptimal(theSize * sizeof(ItemType)));
0092   }
0093 
0094   //! Frees previously allocated memory.
0095   void deallocate(pointer thePnt, const size_type) const
0096   {
0097     Standard::Free(static_cast<Standard_Address>(thePnt));
0098   }
0099 
0100   //! Reallocates memory for theSize objects.
0101   pointer reallocate(pointer thePnt, const size_type theSize) const
0102   {
0103     return static_cast<pointer>(Standard::Reallocate(thePnt, theSize * sizeof(ItemType)));
0104   }
0105 
0106   //! Constructs an object.
0107   //! Uses placement new operator and copy constructor to construct an object.
0108   template<class _Objty, class... _Types>
0109   void construct(_Objty* _Ptr, _Types&&... _Args)
0110   {
0111     ::new ((void*)_Ptr) _Objty(std::forward<_Types>(_Args)...);
0112   }
0113 
0114   //! Destroys the object.
0115   //! Uses the object destructor.
0116   void destroy(pointer thePnt)
0117   {
0118     (void)thePnt; thePnt->~value_type();
0119   }
0120 
0121   bool operator==(const NCollection_Allocator&) const
0122   {
0123     return true;
0124   }
0125 
0126   template<class U>
0127   bool operator==(const NCollection_Allocator<U>&) const noexcept
0128   {
0129     return true;
0130   }
0131 
0132   bool operator!=(const NCollection_Allocator&) const noexcept
0133   {
0134     return false;
0135   }
0136 
0137   template<class U>
0138   bool operator!=(const NCollection_Allocator<U>&) const noexcept
0139   {
0140     return false;
0141   }
0142 
0143 };
0144 
0145 template<class U, class V>
0146 bool operator==(const NCollection_Allocator<U>&, const NCollection_Allocator<V>&)
0147 {
0148   return true;
0149 }
0150 
0151 #endif