ca0c0b11 |
1 | // Created on: 2014-04-01 |
2 | // Created by: Kirill Gavrilov |
3 | // Copyright (c) 2014 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
16 | #ifndef _NCollection_Buffer_HeaderFile |
17 | #define _NCollection_Buffer_HeaderFile |
18 | |
19 | #include <NCollection_BaseAllocator.hxx> |
20 | #include <NCollection_Handle.hxx> |
21 | |
22 | //! Low-level buffer object. |
23 | class NCollection_Buffer |
24 | { |
25 | |
26 | public: |
27 | |
28 | //! Default constructor. |
29 | //! When theData is NULL but theSize is not 0 than buffer of specified size will be allocated. |
30 | //! @param theAlloc memory allocator |
31 | //! @param theSize buffer size |
32 | //! @param theData buffer data allocated by theAlloc |
33 | NCollection_Buffer (const Handle(NCollection_BaseAllocator)& theAlloc, |
34 | const Standard_Size theSize = 0, |
35 | Standard_Byte* theData = NULL) |
36 | : myData (NULL), |
37 | mySize (0), |
38 | myAllocator (theAlloc) |
39 | { |
40 | if (theData != NULL) |
41 | { |
42 | myData = theData; |
43 | mySize = theSize; |
44 | } |
45 | else |
46 | { |
47 | Allocate (theSize); |
48 | } |
49 | } |
50 | |
51 | //! Destructor. |
52 | virtual ~NCollection_Buffer() |
53 | { |
54 | Free(); |
55 | } |
56 | |
57 | //! @return buffer data |
58 | const Standard_Byte* Data() const |
59 | { |
60 | return myData; |
61 | } |
62 | |
63 | //! @return buffer data |
64 | Standard_Byte* ChangeData() |
65 | { |
66 | return myData; |
67 | } |
68 | |
69 | //! @return true if buffer is not allocated |
70 | bool IsEmpty() const |
71 | { |
72 | return myData == NULL; |
73 | } |
74 | |
75 | //! Return buffer length in bytes. |
76 | Standard_Size Size() const |
77 | { |
78 | return mySize; |
79 | } |
80 | |
81 | //! @return buffer allocator |
82 | const Handle(NCollection_BaseAllocator)& Allocator() const |
83 | { |
84 | return myAllocator; |
85 | } |
86 | |
87 | //! Assign new buffer allocator with de-allocation of buffer. |
88 | void SetAllocator (const Handle(NCollection_BaseAllocator)& theAlloc) |
89 | { |
90 | Free(); |
91 | myAllocator = theAlloc; |
92 | } |
93 | |
94 | //! Allocate the buffer. |
95 | //! @param theSize buffer length in bytes |
96 | bool Allocate (const Standard_Size theSize) |
97 | { |
98 | Free(); |
99 | mySize = theSize; |
100 | if (theSize != 0 |
101 | || !myAllocator.IsNull()) |
102 | { |
103 | myData = (Standard_Byte* )myAllocator->Allocate (theSize); |
104 | } |
105 | |
106 | if (myData == NULL) |
107 | { |
108 | mySize = 0; |
109 | return false; |
110 | } |
111 | return true; |
112 | } |
113 | |
114 | //! De-allocate buffer. |
115 | void Free() |
116 | { |
117 | if (!myAllocator.IsNull()) |
118 | { |
119 | myAllocator->Free (myData); |
120 | } |
121 | myData = NULL; |
122 | mySize = 0; |
123 | } |
124 | |
125 | protected: |
126 | |
127 | Standard_Byte* myData; //!< data pointer |
128 | Standard_Size mySize; //!< buffer length in bytes |
129 | Handle(NCollection_BaseAllocator) myAllocator; //!< buffer allocator |
130 | |
131 | }; |
132 | |
133 | typedef NCollection_Handle<NCollection_Buffer> Handle(NCollection_Buffer); |
134 | |
135 | #endif // _NCollection_Buffer_HeaderFile |