Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 2009-09-23 |
973c2be1 | 2 | // Copyright (c) 2009-2014 OPEN CASCADE SAS |
b311480e | 3 | // |
973c2be1 | 4 | // This file is part of Open CASCADE Technology software library. |
b311480e | 5 | // |
d5f74e42 | 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 | |
973c2be1 | 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. | |
b311480e | 11 | // |
973c2be1 | 12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. | |
41194117 | 14 | |
f7b4312f | 15 | #ifndef _NCollection_LocalArray_HeaderFile |
16 | #define _NCollection_LocalArray_HeaderFile | |
41194117 K |
17 | |
18 | #include <Standard.hxx> | |
19 | #include <Standard_TypeDef.hxx> | |
20 | ||
f7b4312f | 21 | //! Auxiliary class optimizing creation of array buffer |
22 | //! (using stack allocation for small arrays). | |
50bc8f96 | 23 | template<class theItem, Standard_Integer MAX_ARRAY_SIZE = 1024> class NCollection_LocalArray |
41194117 K |
24 | { |
25 | public: | |
26 | ||
b6a0525b | 27 | explicit NCollection_LocalArray (const size_t theSize) |
41194117 K |
28 | : myPtr (myBuffer) |
29 | { | |
30 | Allocate(theSize); | |
31 | } | |
32 | ||
f7b4312f | 33 | NCollection_LocalArray () |
50bc8f96 | 34 | : myPtr (myBuffer), mySize(0) {} |
41194117 | 35 | |
ddf2fe8e | 36 | ~NCollection_LocalArray() |
41194117 K |
37 | { |
38 | Deallocate(); | |
39 | } | |
40 | ||
41 | void Allocate (const size_t theSize) | |
42 | { | |
43 | Deallocate(); | |
44 | if (theSize > MAX_ARRAY_SIZE) | |
f7b4312f | 45 | myPtr = (theItem*)Standard::Allocate (theSize * sizeof(theItem)); |
41194117 K |
46 | else |
47 | myPtr = myBuffer; | |
50bc8f96 | 48 | |
49 | mySize = theSize; | |
50 | } | |
51 | ||
52 | size_t Size() const | |
53 | { | |
54 | return mySize; | |
41194117 K |
55 | } |
56 | ||
f7b4312f | 57 | operator theItem*() const |
41194117 K |
58 | { |
59 | return myPtr; | |
60 | } | |
61 | ||
62 | private: | |
63 | ||
f7b4312f | 64 | NCollection_LocalArray (const NCollection_LocalArray& ); |
65 | NCollection_LocalArray& operator= (const NCollection_LocalArray& ); | |
41194117 K |
66 | |
67 | protected: | |
68 | ||
69 | void Deallocate() | |
70 | { | |
71 | if (myPtr != myBuffer) | |
547702a1 | 72 | Standard::Free (myPtr); |
41194117 K |
73 | } |
74 | ||
75 | protected: | |
76 | ||
f7b4312f | 77 | theItem myBuffer[MAX_ARRAY_SIZE]; |
78 | theItem* myPtr; | |
50bc8f96 | 79 | size_t mySize; |
41194117 K |
80 | |
81 | }; | |
82 | ||
f7b4312f | 83 | #endif // _NCollection_LocalArray_HeaderFile |