blob: 066b6f8d2a2926d123291289aa6b0aa8d59a87a7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/*
* Copyright (C) 2013-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#pragma once
#include <stddef.h> // for size_t
namespace XUTILS
{
class auto_buffer
{
public:
/**
* Create buffer with zero size
*/
auto_buffer(void) = default;
/**
* Create buffer with specified size
* @param size of created buffer
*/
explicit auto_buffer(size_t size);
~auto_buffer();
/**
* Allocate specified size for buffer, discarding current buffer content
* @param size of buffer to allocate
* @return reference to itself
*/
auto_buffer& allocate(size_t size);
/**
* Resize current buffer to new size. Buffer will be extended or truncated at the end.
* @param newSize of buffer
* @return reference to itself
*/
auto_buffer& resize(size_t newSize);
/**
* Reset buffer to zero size
* @return reference to itself
*/
auto_buffer& clear(void);
/**
* Get pointer to buffer content
* @return pointer to buffer content or NULL if buffer is zero size
*/
inline char* get(void) { return static_cast<char*>(p); }
/**
* Get constant pointer to buffer content
* @return constant pointer to buffer content
*/
inline const char* get(void) const { return static_cast<char*>(p); }
/**
* Get size of the buffer
* @return size of the buffer
*/
inline size_t size(void) const { return s; }
/**
* Get size of the buffer
* @return size of the buffer
*/
inline size_t length(void) const { return s; }
/**
* Attach malloc'ed pointer to the buffer, discarding current buffer content
* Pointer must be acquired by malloc() or realloc().
* Pointer will be automatically freed on destroy of the buffer.
* @param pointer to attach
* @param size of new memory region pointed by pointer
* @return reference to itself
*/
auto_buffer& attach(void* pointer, size_t size);
/**
* Detach current buffer content from the buffer, reset buffer to zero size
* Caller is responsible to free memory by calling free() for returned pointer
* when pointer in not needed anymore
* @return detached from buffer pointer to content
*/
void* detach(void);
private:
auto_buffer(const auto_buffer& other) = delete; // disallow copy constructor
auto_buffer& operator=(const auto_buffer& other) = delete; // disallow assignment
void* p = 0;
size_t s = 0;
};
}
|