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