0027960: Configuration - fix compilation of OSD_Directory with MinGW-w64
[occt.git] / src / NCollection / NCollection_BaseVector.hxx
index 1f1b30c..08c6a61 100755 (executable)
 #define NCollection_BaseVector_HeaderFile
 
 #include <Standard_TypeDef.hxx>
-#include <NCollection_BaseAllocator.hxx>
-#include <stddef.h>
-
 #include <Standard_OutOfRange.hxx>
+#include <NCollection_BaseAllocator.hxx>
+#include <NCollection_DefineAlloc.hxx>
 
-#if defined(_MSC_VER)
-  #pragma warning(push, 1)
-  #pragma warning(disable:4355)
-#endif
+#include <stddef.h>
 
 // this value defines the number of blocks that are reserved
 // when the capacity of vector is increased
@@ -37,6 +33,10 @@ inline Standard_Integer GetCapacity (const Standard_Integer theIncrement)
 //! Class NCollection_BaseVector - base for NCollection_Vector template
 class NCollection_BaseVector
 {
+public:
+  //! Memory allocation
+  DEFINE_STANDARD_ALLOC
+  DEFINE_NCOLLECTION_ALLOC
 
 protected:
 
@@ -112,14 +112,21 @@ protected:
       }
     }
 
-    virtual void offsetV (Standard_Integer theOffset)
+    void offsetV (Standard_Integer theOffset)
     {
       const Standard_Integer anIndex = myCurIndex + myICurBlock * myVector->myIncrement + theOffset;
       myICurBlock = anIndex / myVector->myIncrement;
       myCurIndex = anIndex % myVector->myIncrement;
+      if (myICurBlock > myIEndBlock)
+      {
+        // make sure that iterator produced by Offset()
+        // is equal to the end() iterator
+        --myICurBlock;
+        myCurIndex += myVector->myIncrement;
+      }
     }
 
-    virtual Standard_Integer differV (const Iterator& theOther) const
+    Standard_Integer differV (const Iterator& theOther) const
     {
       return (myCurIndex - theOther.myCurIndex) + (myICurBlock - theOther.myICurBlock) * myVector->myIncrement;
     }
@@ -144,19 +151,17 @@ protected: //! @name Block initializer
                                    const Standard_Integer  theSize);
 
   //! Allocate memory for array of memory blocks.
-  //! @param theAllocator  Memory allocator to use
   //! @param theCapacity   Number of memory blocks in array
   //! @param theSource     Original array of memory blocks, will be automatically deallocated
   //! @param theSourceSize Number of memory blocks in original array
-  Standard_EXPORT static MemBlock* allocMemBlocks (Handle(NCollection_BaseAllocator)& theAllocator,
-                                                   const Standard_Integer             theCapacity,
-                                                   MemBlock*                          theSource     = NULL,
-                                                   const Standard_Integer             theSourceSize = 0);
+  Standard_EXPORT MemBlock* allocMemBlocks (const Standard_Integer theCapacity,
+                                            MemBlock*              theSource     = NULL,
+                                            const Standard_Integer theSourceSize = 0);
 
 protected: //! @name protected methods
 
   //! Empty constructor
-  NCollection_BaseVector (Handle(NCollection_BaseAllocator)& theAllocator,
+  NCollection_BaseVector (const Handle(NCollection_BaseAllocator)& theAllocator,
                           initMemBlocks_t                    theInitBlocks,
                           const size_t                       theSize,
                           const Standard_Integer             theInc)
@@ -165,24 +170,32 @@ protected: //! @name protected methods
     myLength     (0),
     myCapacity   (GetCapacity (myIncrement)),
     myNBlocks    (0),
-    myData       (allocMemBlocks (theAllocator, myCapacity)),
-    myInitBlocks (theInitBlocks) {}
+    myInitBlocks (theInitBlocks)
+  {
+    myAllocator = (theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator);
+    myData = allocMemBlocks (myCapacity);
+  }
 
   //! Copy constructor
-  NCollection_BaseVector (Handle(NCollection_BaseAllocator)& theAllocator,
+  NCollection_BaseVector (const Handle(NCollection_BaseAllocator)& theAllocator,
                           initMemBlocks_t                    theInitBlocks,
                           const NCollection_BaseVector&      theOther)
   : myItemSize   (theOther.myItemSize),
     myIncrement  (theOther.myIncrement),
     myLength     (theOther.myLength),
     myCapacity   (GetCapacity(myIncrement) + theOther.myLength / theOther.myIncrement),
-    myNBlocks    (1 + (theOther.myLength - 1)/theOther.myIncrement),
-    myData       (allocMemBlocks (theAllocator, myCapacity)),
-    myInitBlocks (theInitBlocks) {}
+    myNBlocks    (theOther.myLength == 0 ? 0 : 1 + (theOther.myLength - 1)/theOther.myIncrement),
+    myInitBlocks (theInitBlocks)
+  {
+    myAllocator = (theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator);
+    myData = allocMemBlocks (myCapacity);
+  }
+
+  //! Destructor
+  virtual ~NCollection_BaseVector() {}
 
   //! @return pointer to memory where to put the new item
-  Standard_EXPORT void* expandV (Handle(NCollection_BaseAllocator)& theAllocator,
-                                 const Standard_Integer             theIndex);
+  Standard_EXPORT void* expandV (const Standard_Integer theIndex);
 
   //! Locate the memory holding the desired value
   inline void* findV (const Standard_Integer theIndex) const
@@ -197,9 +210,24 @@ public: //! @name public API
 
   //! Empty the vector of its objects
   Standard_EXPORT void Clear();
+  // to set the size of increment dynamically
+  void SetIncrement(const Standard_Integer aIncrement) {
+    if (aIncrement > 0) {
+      if (!myIncrement) {
+        myIncrement=aIncrement;
+      }
+    }
+  }
 
-protected: //! @name Private fields
+  //! Returns attached allocator
+  const Handle(NCollection_BaseAllocator)& Allocator() const
+  {
+    return myAllocator;
+  }
+
+protected: //! @name Protected fields
 
+  Handle(NCollection_BaseAllocator) myAllocator;
   size_t           myItemSize;
   Standard_Integer myIncrement;
   Standard_Integer myLength;
@@ -211,11 +239,6 @@ protected: //! @name Private fields
 protected:
 
   friend class Iterator;
-
 };
 
-#if defined(_MSC_VER)
-  #pragma warning(pop)
-#endif
-
 #endif // NCollection_BaseVector_HeaderFile