0024971: Incomplete interface of NCollection classes
[occt.git] / src / NCollection / NCollection_LocalArray.hxx
1 // Created on: 2009-09-23
2 // Copyright (c) 2009-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _NCollection_LocalArray_HeaderFile
16 #define _NCollection_LocalArray_HeaderFile
17
18 #include <Standard.hxx>
19 #include <Standard_TypeDef.hxx>
20
21 //! Auxiliary class optimizing creation of array buffer 
22 //! (using stack allocation for small arrays).
23 template<class theItem> class NCollection_LocalArray
24 {
25 public:
26
27   // 1K * sizeof (theItem)
28   static const size_t MAX_ARRAY_SIZE = 1024;
29
30   NCollection_LocalArray (const size_t theSize)
31   : myPtr (myBuffer)
32   {
33     Allocate(theSize);
34   }
35
36   NCollection_LocalArray ()
37   : myPtr (myBuffer) {}
38
39   ~NCollection_LocalArray()
40   {
41     Deallocate();
42   }
43
44   void Allocate (const size_t theSize)
45   {
46     Deallocate();
47     if (theSize > MAX_ARRAY_SIZE)
48       myPtr = (theItem*)Standard::Allocate (theSize * sizeof(theItem));
49     else
50       myPtr = myBuffer;
51   }
52
53   operator theItem*() const
54   {
55     return myPtr;
56   }
57
58 private:
59
60   NCollection_LocalArray (const NCollection_LocalArray& );
61   NCollection_LocalArray& operator= (const NCollection_LocalArray& );
62
63 protected:
64
65   void Deallocate()
66   {
67     if (myPtr != myBuffer)
68       Standard::Free (myPtr);
69   }
70
71 protected:
72
73   theItem  myBuffer[MAX_ARRAY_SIZE];
74   theItem* myPtr;
75
76 };
77
78 #endif // _NCollection_LocalArray_HeaderFile