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