soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SAutoBuf.cpp
1//#include <Windows.h>
2#include <soui_mem_wrapper.h>
3#include <helper/SAutoBuf.h>
4#include <memory.h>
5#include <memory>
6
7SNSBEGIN
8SAutoBuf::SAutoBuf(size_t nElements)
9 : m_pBuf(0)
10 , m_nSize(0)
11 , m_bExternalBuf(false)
12 {
13 Allocate(nElements);
14 }
15
16 SAutoBuf::SAutoBuf() : m_pBuf(NULL),m_nSize(0),m_bExternalBuf(false)
17 {
18
19 }
20
22 {
23 Free();
24 }
25
27 {
28 if(!m_bExternalBuf)
29 soui_mem_wrapper::SouiFree(m_pBuf);
30 m_pBuf=NULL;
31 m_nSize=0;
32 m_bExternalBuf=false;
33 }
34
36 {
37 return m_nSize;
38 }
39
40 char* SAutoBuf::Allocate(size_t nBytes)
41 {
42 Free();
43 SASSERT(nBytes <= SIZE_MAX-1);
44 m_pBuf = static_cast<char*>(soui_mem_wrapper::SouiMalloc(nBytes+1));
45 if (m_pBuf)
46 {
47 m_nSize = nBytes;
48 memset(m_pBuf, 0, nBytes + 1);
49 m_bExternalBuf = false;
50 }
51 return m_pBuf;
52 }
53
54 void SAutoBuf::Attach(char *pBuf,size_t size)
55 {
56 Free();
57 m_pBuf=pBuf;
58 m_nSize=size;
59 m_bExternalBuf=true;
60 }
61
63 {
64 char *pRet = m_pBuf;
65 m_pBuf = NULL;
66 m_nSize = 0;
67 m_bExternalBuf=false;
68 return pRet;
69 }
70
72 {
73 SASSERT(m_pBuf != NULL);
74 return m_pBuf[i];
75 }
76
77 const char & SAutoBuf::operator[](int i) const
78 {
79 SASSERT(m_pBuf != NULL);
80 return m_pBuf[i];
81 }
82
84 {
85 SASSERT(m_pBuf != NULL);
86 return m_pBuf;
87 }
88
89 SAutoBuf::operator char *() const
90 {
91 return m_pBuf;
92 }
93
94SNSEND
Header file for the SAutoBuf class, a smart buffer management class.
const char & operator[](int i) const
Const array subscript operator.
Definition SAutoBuf.cpp:77
void Free()
Frees the managed buffer. The buffer will be set to nullptr after being freed.
Definition SAutoBuf.cpp:26
char * operator->() const
Arrow operator to access members of the managed buffer.
Definition SAutoBuf.cpp:83
void Attach(char *pBuf, size_t size)
Attaches an external buffer to the SAutoBuf object. The buffer will not be freed by the SAutoBuf obje...
Definition SAutoBuf.cpp:54
char * Allocate(size_t nElements)
Allocates a buffer of the specified size. If a buffer is already allocated, it will be freed first.
Definition SAutoBuf.cpp:40
~SAutoBuf()
Destructor. Frees the allocated buffer if it was not externally attached.
Definition SAutoBuf.cpp:21
size_t size()
Returns the size of the managed buffer.
Definition SAutoBuf.cpp:35
SAutoBuf()
Default constructor. Initializes an empty buffer.
Definition SAutoBuf.cpp:16
char * Detach()
Detaches the managed buffer from the SAutoBuf object. The caller is responsible for freeing the buffe...
Definition SAutoBuf.cpp:62