soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SAutoBuf.h
Go to the documentation of this file.
1/**
2 * @file SAutoBuf.h
3 * @brief Header file for the SAutoBuf class, a smart buffer management class.
4 */
5
6 #ifndef __SAUTOBUF__H__
7 #define __SAUTOBUF__H__
8
9 #include <utilities-def.h>
10
11 SNSBEGIN
12
13 /**
14 * @class SAutoBuf
15 * @brief A smart buffer management class that automatically handles memory allocation and deallocation.
16 */
17 class UTILITIES_API SAutoBuf
18 {
19 public:
20 /**
21 * @brief Default constructor.
22 * Initializes an empty buffer.
23 */
24 SAutoBuf();
25
26 /**
27 * @brief Constructor with initial buffer size.
28 * Allocates a buffer of the specified size.
29 * @param nElements The number of elements (bytes) to allocate.
30 */
31 SAutoBuf(size_t nElements);
32
33 /**
34 * @brief Destructor.
35 * Frees the allocated buffer if it was not externally attached.
36 */
37 ~SAutoBuf();
38
39 /**
40 * @brief Conversion operator to char*.
41 * @return Pointer to the managed buffer.
42 */
43 operator char *() const;
44
45 /**
46 * @brief Arrow operator to access members of the managed buffer.
47 * @return Pointer to the managed buffer.
48 */
49 char* operator ->() const;
50
51 /**
52 * @brief Const array subscript operator.
53 * @param i Index of the element to access.
54 * @return Const reference to the element at the specified index.
55 */
56 const char & operator[] (int i) const;
57
58 /**
59 * @brief Array subscript operator.
60 * @param i Index of the element to access.
61 * @return Reference to the element at the specified index.
62 */
63 char & operator[] (int i);
64
65 /**
66 * @brief Attaches an external buffer to the SAutoBuf object.
67 * The buffer will not be freed by the SAutoBuf object.
68 * @param pBuf Pointer to the external buffer.
69 * @param size Size of the external buffer.
70 */
71 void Attach(char *pBuf, size_t size);
72
73 /**
74 * @brief Detaches the managed buffer from the SAutoBuf object.
75 * The caller is responsible for freeing the buffer using soui_mem_wrapper::SouiFree.
76 * @return Pointer to the detached buffer.
77 */
78 char *Detach();
79
80 /**
81 * @brief Allocates a buffer of the specified size.
82 * If a buffer is already allocated, it will be freed first.
83 * @param nElements The number of elements (bytes) to allocate.
84 * @return Pointer to the newly allocated buffer.
85 */
86 char* Allocate(size_t nElements);
87
88 /**
89 * @brief Returns the size of the managed buffer.
90 * @return The size of the managed buffer.
91 */
92 size_t size();
93
94 /**
95 * @brief Frees the managed buffer.
96 * The buffer will be set to nullptr after being freed.
97 */
98 void Free();
99
100 private:
101 char * m_pBuf; /**< Pointer to the managed buffer. */
102 size_t m_nSize; /**< Size of the managed buffer. */
103 bool m_bExternalBuf; /**< Flag indicating if the buffer is externally attached. */
104 };
105
106 SNSEND
107
108 #endif // __SAUTOBUF__H__
void Free()
Frees the managed buffer. The buffer will be set to nullptr after being freed.
Definition SAutoBuf.cpp:26
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
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