Panda3D
Loading...
Searching...
No Matches
dcPacker.h
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 dcPacker.h
10 * @author drose
11 * @date 2004-06-15
12 */
13
14#ifndef DCPACKER_H
15#define DCPACKER_H
16
17#include "dcbase.h"
18#include "dcPackerInterface.h"
19#include "dcSubatomicType.h"
20#include "dcPackData.h"
21#include "dcPackerCatalog.h"
22#include "dcPython.h"
23
24class DCClass;
26
27/**
28 * This class can be used for packing a series of numeric and string data into
29 * a binary stream, according to the DC specification.
30 *
31 * See also direct/src/doc/dcPacker.txt for a more complete description and
32 * examples of using this class.
33 */
34class EXPCL_DIRECT_DCPARSER DCPacker {
35PUBLISHED:
36 DCPacker();
37 ~DCPacker();
38
39 INLINE void clear_data();
40
41 void begin_pack(const DCPackerInterface *root);
42 bool end_pack();
43
44 void set_unpack_data(const vector_uchar &data);
45public:
46 void set_unpack_data(const char *unpack_data, size_t unpack_length,
47 bool owns_unpack_data);
48
49PUBLISHED:
50 void begin_unpack(const DCPackerInterface *root);
51 bool end_unpack();
52
53 void begin_repack(const DCPackerInterface *root);
54 bool end_repack();
55
56 bool seek(const std::string &field_name);
57 bool seek(int seek_index);
58
59 INLINE bool has_nested_fields() const;
60 INLINE int get_num_nested_fields() const;
61 INLINE bool more_nested_fields() const;
62
63 INLINE const DCPackerInterface *get_current_parent() const;
64 INLINE const DCPackerInterface *get_current_field() const;
65 INLINE const DCSwitchParameter *get_last_switch() const;
66 INLINE DCPackType get_pack_type() const;
67 INLINE std::string get_current_field_name() const;
68
69 void push();
70 void pop();
71
72 INLINE void pack_double(double value);
73 INLINE void pack_int(int value);
74 INLINE void pack_uint(unsigned int value);
75 INLINE void pack_int64(int64_t value);
76 INLINE void pack_uint64(uint64_t value);
77 INLINE void pack_string(const std::string &value);
78 INLINE void pack_blob(const vector_uchar &value);
79 INLINE void pack_literal_value(const vector_uchar &value);
80 void pack_default_value();
81
82 INLINE double unpack_double();
83 INLINE int unpack_int();
84 INLINE unsigned int unpack_uint();
85 INLINE int64_t unpack_int64();
86 INLINE uint64_t unpack_uint64();
87 INLINE std::string unpack_string();
88 INLINE vector_uchar unpack_blob();
89 INLINE vector_uchar unpack_literal_value();
90 void unpack_validate();
91 void unpack_skip();
92
93public:
94 // The following are variants on the above unpack() calls that pass the
95 // result back by reference instead of as a return value.
96 INLINE void unpack_double(double &value);
97 INLINE void unpack_int(int &value);
98 INLINE void unpack_uint(unsigned int &value);
99 INLINE void unpack_int64(int64_t &value);
100 INLINE void unpack_uint64(uint64_t &value);
101 INLINE void unpack_string(std::string &value);
102 INLINE void unpack_blob(vector_uchar &value);
103 INLINE void unpack_literal_value(vector_uchar &value);
104
105PUBLISHED:
106
107#ifdef HAVE_PYTHON
108 void pack_object(PyObject *object);
109 PyObject *unpack_object();
110#endif
111
112 bool parse_and_pack(const std::string &formatted_object);
113 bool parse_and_pack(std::istream &in);
114 std::string unpack_and_format(bool show_field_names = true);
115 void unpack_and_format(std::ostream &out, bool show_field_names = true);
116
117 INLINE bool had_parse_error() const;
118 INLINE bool had_pack_error() const;
119 INLINE bool had_range_error() const;
120 INLINE bool had_error() const;
121 INLINE size_t get_num_unpacked_bytes() const;
122
123 INLINE size_t get_length() const;
124 INLINE std::string get_string() const;
125 INLINE vector_uchar get_bytes() const;
126 INLINE size_t get_unpack_length() const;
127 INLINE std::string get_unpack_string() const;
128public:
129 INLINE void get_string(std::string &data) const;
130 INLINE const char *get_data() const;
131 INLINE char *take_data();
132
133 INLINE void append_data(const unsigned char *buffer, size_t size);
134 INLINE char *get_write_pointer(size_t size);
135
136 INLINE const char *get_unpack_data() const;
137
138PUBLISHED:
139 INLINE static int get_num_stack_elements_ever_allocated();
140
141 // The following methods are used only for packing (or unpacking) raw data
142 // into the buffer between packing sessions (e.g. between calls to
143 // end_pack() and the next begin_pack()).
144
145 INLINE void raw_pack_int8(int value);
146 INLINE void raw_pack_int16(int value);
147 INLINE void raw_pack_int32(int value);
148 INLINE void raw_pack_int64(int64_t value);
149 INLINE void raw_pack_uint8(unsigned int value);
150 INLINE void raw_pack_uint16(unsigned int value);
151 INLINE void raw_pack_uint32(unsigned int value);
152 INLINE void raw_pack_uint64(uint64_t value);
153 INLINE void raw_pack_float64(double value);
154 INLINE void raw_pack_string(const std::string &value);
155 INLINE void raw_pack_blob(const vector_uchar &value);
156
157// this is a hack to allw me to get in and out of 32bit Mode Faster need to
158// agree with channel_type in dcbase.h
159#define RAW_PACK_CHANNEL(in) raw_pack_uint64(in)
160#define RAW_UNPACK_CHANNEL() raw_unpack_uint64()
161
162
163 INLINE int raw_unpack_int8();
164 INLINE int raw_unpack_int16();
165 INLINE int raw_unpack_int32();
166 INLINE int64_t raw_unpack_int64();
167 INLINE unsigned int raw_unpack_uint8();
168 INLINE unsigned int raw_unpack_uint16();
169 INLINE unsigned int raw_unpack_uint32();
170 INLINE uint64_t raw_unpack_uint64();
171 INLINE double raw_unpack_float64();
172 INLINE std::string raw_unpack_string();
173 INLINE vector_uchar raw_unpack_blob();
174
175public:
176 INLINE void raw_unpack_int8(int &value);
177 INLINE void raw_unpack_int16(int &value);
178 INLINE void raw_unpack_int32(int &value);
179 INLINE void raw_unpack_int64(int64_t &value);
180 INLINE void raw_unpack_uint8(unsigned int &value);
181 INLINE void raw_unpack_uint16(unsigned int &value);
182 INLINE void raw_unpack_uint32(unsigned int &value);
183 INLINE void raw_unpack_uint64(uint64_t &value);
184 INLINE void raw_unpack_float64(double &value);
185 INLINE void raw_unpack_string(std::string &value);
186 INLINE void raw_unpack_blob(vector_uchar &value);
187
188public:
189 static void enquote_string(std::ostream &out, char quote_mark, const std::string &str);
190 static void output_hex_string(std::ostream &out, const vector_uchar &str);
191
192private:
193 INLINE void advance();
194 void handle_switch(const DCSwitchParameter *switch_parameter);
195 void clear();
196 void clear_stack();
197
198#ifdef HAVE_PYTHON
199 void pack_class_object(const DCClass *dclass, PyObject *object);
200 PyObject *unpack_class_object(const DCClass *dclass);
201 void set_class_element(PyObject *class_def, PyObject *&object,
202 const DCField *field);
203 void get_class_element(const DCClass *dclass, PyObject *object,
204 const DCField *field);
205#endif
206
207private:
208 enum Mode {
209 M_idle,
210 M_pack,
211 M_unpack,
212 M_repack,
213 };
214 Mode _mode;
215
216 DCPackData _pack_data;
217 const char *_unpack_data;
218 size_t _unpack_length;
219 bool _owns_unpack_data;
220 size_t _unpack_p;
221
222 const DCPackerInterface *_root;
223 const DCPackerCatalog *_catalog;
224 const DCPackerCatalog::LiveCatalog *_live_catalog;
225
226 class StackElement {
227 public:
228 // As an optimization, we implement operator new and delete here to
229 // minimize allocation overhead during push() and pop().
230 INLINE void *operator new(size_t size);
231 INLINE void operator delete(void *ptr);
232
233 const DCPackerInterface *_current_parent;
234 int _current_field_index;
235 size_t _push_marker;
236 size_t _pop_marker;
237 StackElement *_next;
238
239 static StackElement *_deleted_chain;
240 static int _num_ever_allocated;
241 };
242 StackElement *_stack;
243
244 const DCPackerInterface *_current_field;
245 const DCPackerInterface *_current_parent;
246 int _current_field_index;
247
248 // _push_marker marks the beginning of the push record (so we can go back
249 // and write in the length later, or figure out the switch parameter).
250 size_t _push_marker;
251 // _pop_marker is used in unpack mode with certain data structures (like
252 // dynamic arrays) to mark the end of the push record (so we know when we've
253 // reached the end). It is zero when it is not in use.
254 size_t _pop_marker;
255 int _num_nested_fields;
256 const DCSwitchParameter *_last_switch;
257
258 bool _parse_error;
259 bool _pack_error;
260 bool _range_error;
261};
262
263#include "dcPacker.I"
264
265#endif
Defines a particular DistributedClass as read from an input .dc file.
Definition dcClass.h:44
A single field of a Distributed Class, either atomic or molecular.
Definition dcField.h:37
This is a block of data that receives the results of DCPacker.
Definition dcPackData.h:22
This object contains the names of all of the nested fields available within a particular field.
This defines the internal interface for packing values into a DCField.
const DCPackerInterface * get_current_field() const
Returns the field that will be referenced by the next call to pack_*() or unpack_*().
Definition dcPacker.I:87
int raw_unpack_int32()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:825
std::vector< unsigned char > get_bytes() const
Returns the packed data buffer as a bytes object.
Definition dcPacker.I:599
void unpack_validate()
Internally unpacks the current numeric or string value and validates it against the type range limits...
Definition dcPacker.cxx:578
void push()
Marks the beginning of a nested series of fields.
Definition dcPacker.cxx:411
bool more_nested_fields() const
Returns true if there are more nested fields to pack or unpack in the current push sequence,...
Definition dcPacker.I:67
void raw_pack_int8(int value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:704
unsigned int raw_unpack_uint32()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:907
std::string unpack_string()
Unpacks the current numeric or string value from the stream.
Definition dcPacker.I:347
std::string raw_unpack_string()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:937
void clear_data()
Empties the data in the pack buffer and unpack buffer.
Definition dcPacker.I:20
void pack_blob(const std::vector< unsigned char > &value)
Packs the indicated numeric or string value into the stream.
Definition dcPacker.I:223
int unpack_int()
Unpacks the current numeric or string value from the stream.
Definition dcPacker.I:271
std::string get_string() const
Returns the packed data buffer as a string.
Definition dcPacker.I:591
std::vector< unsigned char > raw_unpack_blob()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:947
void append_data(const unsigned char *buffer, size_t size)
Adds the indicated bytes to the end of the data.
Definition dcPacker.I:664
void pack_uint(unsigned int value)
Packs the indicated numeric or string value into the stream.
Definition dcPacker.I:167
std::vector< unsigned char > unpack_literal_value()
Returns the literal string that represents the packed value of the current field, and advances the fi...
Definition dcPacker.I:386
void begin_pack(const DCPackerInterface *root)
Begins a packing session.
Definition dcPacker.cxx:73
bool had_range_error() const
Returns true if there has been an range validation error since the most recent call to begin(); in pa...
Definition dcPacker.I:553
void pack_int(int value)
Packs the indicated numeric or string value into the stream.
Definition dcPacker.I:153
void raw_pack_string(const std::string &value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:785
char * get_write_pointer(size_t size)
Adds the indicated number of bytes to the end of the data without initializing them,...
Definition dcPacker.I:675
void begin_unpack(const DCPackerInterface *root)
Begins an unpacking session.
Definition dcPacker.cxx:153
void begin_repack(const DCPackerInterface *root)
Begins a repacking session.
Definition dcPacker.cxx:213
bool end_repack()
Finishes the repacking session.
Definition dcPacker.cxx:248
DCPackType get_pack_type() const
Returns the type of value expected by the current field.
Definition dcPacker.I:114
bool end_unpack()
Finishes the unpacking session.
Definition dcPacker.cxx:179
const DCSwitchParameter * get_last_switch() const
Returns a pointer to the last DCSwitch instance that we have passed by and selected one case of durin...
Definition dcPacker.I:101
void raw_pack_float64(double value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:776
bool has_nested_fields() const
Returns true if the current field has any nested fields (and thus expects a push() .
Definition dcPacker.I:37
uint64_t raw_unpack_uint64()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:917
void raw_pack_uint32(unsigned int value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:758
void pack_uint64(uint64_t value)
Packs the indicated numeric or string value into the stream.
Definition dcPacker.I:195
bool had_error() const
Returns true if there has been any error (either a pack error or a range error) since the most recent...
Definition dcPacker.I:563
int get_num_nested_fields() const
Returns the number of nested fields associated with the current field, if has_nested_fields() returne...
Definition dcPacker.I:58
void raw_pack_uint16(unsigned int value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:749
double unpack_double()
Unpacks the current numeric or string value from the stream.
Definition dcPacker.I:252
void raw_pack_uint8(unsigned int value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:740
size_t get_unpack_length() const
Returns the total number of bytes in the unpack data buffer.
Definition dcPacker.I:610
void pop()
Marks the end of a nested series of fields.
Definition dcPacker.cxx:495
void pack_literal_value(const std::vector< unsigned char > &value)
Adds the indicated string value into the stream, representing a single pre- packed field element,...
Definition dcPacker.I:238
std::string get_unpack_string() const
Returns the unpack data buffer, as a string.
Definition dcPacker.I:620
bool had_pack_error() const
Returns true if there has been an packing error since the most recent call to begin(); in particular,...
Definition dcPacker.I:539
bool end_pack()
Finishes a packing session.
Definition dcPacker.cxx:98
void pack_int64(int64_t value)
Packs the indicated numeric or string value into the stream.
Definition dcPacker.I:181
const char * get_data() const
Returns the beginning of the data buffer.
Definition dcPacker.I:641
bool had_parse_error() const
Returns true if there has been an parse error since the most recent call to begin(); this can only ha...
Definition dcPacker.I:524
int raw_unpack_int16()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:815
double raw_unpack_float64()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:927
void unpack_skip()
Skips the current field without unpacking it and advances to the next field.
Definition dcPacker.cxx:604
static void output_hex_string(std::ostream &out, const std::vector< unsigned char > &str)
Outputs the indicated string as a hex constant.
bool seek(const std::string &field_name)
Sets the current unpack (or repack) position to the named field.
Definition dcPacker.cxx:270
const DCPackerInterface * get_current_parent() const
Returns the field that we left in our last call to push(): the owner of the current level of fields.
Definition dcPacker.I:77
void set_unpack_data(const std::vector< unsigned char > &data)
Sets up the unpack_data pointer.
Definition dcPacker.cxx:117
bool parse_and_pack(const std::string &formatted_object)
Parses an object's value according to the DC file syntax (e.g.
Definition dcPacker.cxx:960
void pack_default_value()
Adds the default value for the current element into the stream.
Definition dcPacker.cxx:552
int64_t raw_unpack_int64()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:835
void raw_pack_int64(int64_t value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:731
const char * get_unpack_data() const
Returns a read pointer to the unpack data buffer.
Definition dcPacker.I:686
size_t get_num_unpacked_bytes() const
Returns the number of bytes that have been unpacked so far, or after unpack_end(),...
Definition dcPacker.I:574
unsigned int raw_unpack_uint8()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:887
std::string unpack_and_format(bool show_field_names=true)
Unpacks an object and formats its value into a syntax suitable for parsing in the dc file (e....
Definition dcPacker.cxx:989
std::vector< unsigned char > unpack_blob()
Unpacks the current binary data value from the stream.
Definition dcPacker.I:366
unsigned int raw_unpack_uint16()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:897
void pack_double(double value)
Packs the indicated numeric or string value into the stream.
Definition dcPacker.I:139
size_t get_length() const
Returns the current length of the buffer.
Definition dcPacker.I:583
int64_t unpack_int64()
Unpacks the current numeric or string value from the stream.
Definition dcPacker.I:309
void raw_pack_blob(const std::vector< unsigned char > &value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:795
static int get_num_stack_elements_ever_allocated()
Returns the number of DCPacker::StackElement pointers ever simultaneously allocated; these are now ei...
Definition dcPacker.I:696
void raw_pack_uint64(uint64_t value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:767
int raw_unpack_int8()
Unpacks the data from the buffer between unpacking sessions.
Definition dcPacker.I:805
std::string get_current_field_name() const
Returns the name of the current field, if it has a name, or the empty string if the field does not ha...
Definition dcPacker.I:127
unsigned int unpack_uint()
Unpacks the current numeric or string value from the stream.
Definition dcPacker.I:290
uint64_t unpack_uint64()
Unpacks the current numeric or string value from the stream.
Definition dcPacker.I:328
void raw_pack_int32(int value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:722
char * take_data()
Returns the pointer to the beginning of the data buffer, and transfers ownership of the buffer to the...
Definition dcPacker.I:655
void raw_pack_int16(int value)
Packs the data into the buffer between packing sessions.
Definition dcPacker.I:713
void pack_string(const std::string &value)
Packs the indicated numeric or string value into the stream.
Definition dcPacker.I:209
This represents a switch object used as a parameter itself, which packs the appropriate fields of the...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
ostream & enquote_string(ostream &out, const string &str, int indent_level, bool always_quote)
Writes the string to the indicated output stream.