Panda3D
dcPackData.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file dcPackData.I
10  * @author drose
11  * @date 2004-06-15
12  */
13 
14 /**
15  *
16  */
17 INLINE DCPackData::
18 DCPackData() {
19  _buffer = nullptr;
20  _allocated_size = 0;
21  _used_length = 0;
22 }
23 
24 /**
25  *
26  */
27 INLINE DCPackData::
28 ~DCPackData() {
29  if (_buffer != nullptr) {
30  delete[] _buffer;
31  }
32 }
33 
34 /**
35  * Empties the contents of the data (without necessarily freeing its allocated
36  * memory).
37  */
38 INLINE void DCPackData::
39 clear() {
40  _used_length = 0;
41 }
42 
43 /**
44  * Adds the indicated bytes to the end of the data.
45  */
46 INLINE void DCPackData::
47 append_data(const char *buffer, size_t size) {
48  set_used_length(_used_length + size);
49  memcpy(_buffer + _used_length - size, buffer, size);
50 }
51 
52 /**
53  * Adds the indicated number of bytes to the end of the data without
54  * initializing them, and returns a pointer to the beginning of the new data.
55  */
56 INLINE char *DCPackData::
57 get_write_pointer(size_t size) {
58  set_used_length(_used_length + size);
59  return _buffer + _used_length - size;
60 }
61 
62 /**
63  * Adds some uninitialized bytes to the end of the data.
64  */
65 INLINE void DCPackData::
66 append_junk(size_t size) {
67  set_used_length(_used_length + size);
68 }
69 
70 /**
71  * Changes the data at the indicated position to the given value. It is an
72  * error if there are not at least position + size bytes in the data.
73  */
74 INLINE void DCPackData::
75 rewrite_data(size_t position, const char *buffer, size_t size) {
76  nassertv(position + size <= _used_length);
77  memcpy(_buffer + position, buffer, size);
78 }
79 
80 /**
81  * Returns a pointer into the middle of the data at the indicated point.
82  */
83 INLINE char *DCPackData::
84 get_rewrite_pointer(size_t position, size_t size) {
85  nassertr(position + size <= _used_length, nullptr);
86  return _buffer + position;
87 }
88 
89 /**
90  * Returns the data buffer as a string. Also see get_data().
91  */
92 INLINE std::string DCPackData::
93 get_string() const {
94  return std::string(_buffer, _used_length);
95 }
96 
97 /**
98  * Returns the current length of the buffer. This is the number of useful
99  * bytes stored in the buffer, not the amount of memory it takes up.
100  */
101 INLINE size_t DCPackData::
102 get_length() const {
103  return _used_length;
104 }
105 
106 /**
107  * Returns the beginning of the data buffer. The buffer is not null-
108  * terminated, but see also get_string(). This may (or may not) return NULL if
109  * the buffer is empty.
110  *
111  * This may be used in conjunction with get_length() to copy all of the bytes
112  * out of the buffer.
113  */
114 INLINE const char *DCPackData::
115 get_data() const {
116  return _buffer;
117 }
118 
119 /**
120  * Returns the pointer to the beginning of the data buffer, and transfers
121  * ownership of the buffer to the caller. The caller is now responsible for
122  * ultimately freeing the returned pointer with delete[], if it is non-NULL.
123  * This may (or may not) return NULL if the buffer is empty.
124  *
125  * This also empties the DCPackData structure, and sets its length to zero (so
126  * you should call get_length() before calling this method).
127  */
128 INLINE char *DCPackData::
130  char *data = _buffer;
131 
132  _buffer = nullptr;
133  _allocated_size = 0;
134  _used_length = 0;
135 
136  return data;
137 }
char * get_write_pointer(size_t size)
Adds the indicated number of bytes to the end of the data without initializing them,...
Definition: dcPackData.I:57
void append_data(const char *buffer, size_t size)
Adds the indicated bytes to the end of the data.
Definition: dcPackData.I:47
void clear()
Empties the contents of the data (without necessarily freeing its allocated memory).
Definition: dcPackData.I:39
void append_junk(size_t size)
Adds some uninitialized bytes to the end of the data.
Definition: dcPackData.I:66
std::string get_string() const
Returns the data buffer as a string.
Definition: dcPackData.I:93
char * take_data()
Returns the pointer to the beginning of the data buffer, and transfers ownership of the buffer to the...
Definition: dcPackData.I:129
size_t get_length() const
Returns the current length of the buffer.
Definition: dcPackData.I:102
void rewrite_data(size_t position, const char *buffer, size_t size)
Changes the data at the indicated position to the given value.
Definition: dcPackData.I:75
const char * get_data() const
Returns the beginning of the data buffer.
Definition: dcPackData.I:115
char * get_rewrite_pointer(size_t position, size_t size)
Returns a pointer into the middle of the data at the indicated point.
Definition: dcPackData.I:84