Panda3D

Loading...
Searching...
No Matches
dcPackerInterface.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 dcPackerInterface.h
10 * @author drose
11 * @date 2004-06-15
12 */
13
14#ifndef DCPACKERINTERFACE_H
15#define DCPACKERINTERFACE_H
16
17#include "dcbase.h"
18#include "dcSubatomicType.h"
19
20 class DCFile;
21 class DCField;
22 class DCSimpleParameter;
23 class DCSwitchParameter;
24 class DCClassParameter;
25 class DCArrayParameter;
26 class DCAtomicField;
27 class DCMolecularField;
28 class DCPackData;
29 class DCPackerCatalog;
30
31BEGIN_PUBLISH
32// This enumerated type is returned by get_pack_type() and represents the best
33// choice for a subsequent call to pack_*() or unpack_*().
34 enum DCPackType {
35 // This one should never be returned in a normal situation.
36 PT_invalid,
37
38 // These PackTypes are all fundamental types, and should be packed (or
39 // unpacked) with the corresponding call to pack_double(), pack_int(), etc.
40 // PT_blob is similar to PT_string, except that it contains arbitrary binary
41 // data instead of just UTF-8 text.
42 PT_double,
43 PT_int,
44 PT_uint,
45 PT_int64,
46 PT_uint64,
47 PT_string,
48 PT_blob,
49
50 // The remaining PackTypes imply a need to call push() and pop(). They are
51 // all variants on the same thing: a list of nested fields, but the PackType
52 // provides a bit of a semantic context.
53 PT_array,
54 PT_field,
55 PT_class,
56 PT_switch,
57};
58END_PUBLISH
59
60/**
61 * This defines the internal interface for packing values into a DCField. The
62 * various different DC objects inherit from this.
63 *
64 * Normally these methods are called only by the DCPacker object; the user
65 * wouldn't normally call these directly.
66 */
67 class EXPCL_DIRECT_DCPARSER DCPackerInterface {
68 public:
69 DCPackerInterface(const std::string &name = std::string());
70 DCPackerInterface(const DCPackerInterface &copy);
71 virtual ~DCPackerInterface();
72
73PUBLISHED:
74 INLINE const std::string &get_name() const;
75 int find_seek_index(const std::string &name) const;
76
77 virtual DCField *as_field();
78 virtual const DCField *as_field() const;
79 virtual DCSwitchParameter *as_switch_parameter();
80 virtual const DCSwitchParameter *as_switch_parameter() const;
81 virtual DCClassParameter *as_class_parameter();
82 virtual const DCClassParameter *as_class_parameter() const;
83
84 INLINE bool check_match(const DCPackerInterface *other) const;
85 bool check_match(const std::string &description, DCFile *dcfile = nullptr) const;
86
87 public:
88 virtual void set_name(const std::string &name);
89 INLINE bool has_fixed_byte_size() const;
90 INLINE size_t get_fixed_byte_size() const;
91 INLINE bool has_fixed_structure() const;
92 INLINE bool has_range_limits() const;
93 INLINE size_t get_num_length_bytes() const;
94
95 INLINE bool has_nested_fields() const;
96 INLINE int get_num_nested_fields() const;
97 virtual int calc_num_nested_fields(size_t length_bytes) const;
98 virtual DCPackerInterface *get_nested_field(int n) const;
99
100 virtual bool validate_num_nested_fields(int num_nested_fields) const;
101
102 INLINE DCPackType get_pack_type() const;
103
104 virtual void pack_double(DCPackData &pack_data, double value,
105 bool &pack_error, bool &range_error) const;
106 virtual void pack_int(DCPackData &pack_data, int value,
107 bool &pack_error, bool &range_error) const;
108 virtual void pack_uint(DCPackData &pack_data, unsigned int value,
109 bool &pack_error, bool &range_error) const;
110 virtual void pack_int64(DCPackData &pack_data, int64_t value,
111 bool &pack_error, bool &range_error) const;
112 virtual void pack_uint64(DCPackData &pack_data, uint64_t value,
113 bool &pack_error, bool &range_error) const;
114 virtual void pack_string(DCPackData &pack_data, const std::string &value,
115 bool &pack_error, bool &range_error) const;
116 virtual void pack_blob(DCPackData &pack_data, const vector_uchar &value,
117 bool &pack_error, bool &range_error) const;
118 virtual bool pack_default_value(DCPackData &pack_data, bool &pack_error) const;
119
120 virtual void unpack_double(const char *data, size_t length, size_t &p,
121 double &value, bool &pack_error, bool &range_error) const;
122 virtual void unpack_int(const char *data, size_t length, size_t &p,
123 int &value, bool &pack_error, bool &range_error) const;
124 virtual void unpack_uint(const char *data, size_t length, size_t &p,
125 unsigned int &value, bool &pack_error, bool &range_error) const;
126 virtual void unpack_int64(const char *data, size_t length, size_t &p,
127 int64_t &value, bool &pack_error, bool &range_error) const;
128 virtual void unpack_uint64(const char *data, size_t length, size_t &p,
129 uint64_t &value, bool &pack_error, bool &range_error) const;
130 virtual void unpack_string(const char *data, size_t length, size_t &p,
131 std::string &value, bool &pack_error, bool &range_error) const;
132 virtual void unpack_blob(const char *data, size_t length, size_t &p,
133 vector_uchar &value, bool &pack_error, bool &range_error) const;
134 virtual bool unpack_validate(const char *data, size_t length, size_t &p,
135 bool &pack_error, bool &range_error) const;
136 virtual bool unpack_skip(const char *data, size_t length, size_t &p,
137 bool &pack_error) const;
138
139 // These are the low-level interfaces for packing and unpacking numbers from
140 // a buffer. You're responsible for making sure the buffer has enough room,
141 // and for incrementing the pointer.
142 INLINE static void do_pack_int8(char *buffer, int value);
143 INLINE static void do_pack_int16(char *buffer, int value);
144 INLINE static void do_pack_int32(char *buffer, int value);
145 INLINE static void do_pack_int64(char *buffer, int64_t value);
146 INLINE static void do_pack_uint8(char *buffer, unsigned int value);
147 INLINE static void do_pack_uint16(char *buffer, unsigned int value);
148 INLINE static void do_pack_uint32(char *buffer, unsigned int value);
149 INLINE static void do_pack_uint64(char *buffer, uint64_t value);
150 INLINE static void do_pack_float64(char *buffer, double value);
151
152 INLINE static int do_unpack_int8(const char *buffer);
153 INLINE static int do_unpack_int16(const char *buffer);
154 INLINE static int do_unpack_int32(const char *buffer);
155 INLINE static int64_t do_unpack_int64(const char *buffer);
156 INLINE static unsigned int do_unpack_uint8(const char *buffer);
157 INLINE static unsigned int do_unpack_uint16(const char *buffer);
158 INLINE static unsigned int do_unpack_uint32(const char *buffer);
159 INLINE static uint64_t do_unpack_uint64(const char *buffer);
160 INLINE static double do_unpack_float64(const char *buffer);
161
162 INLINE static void validate_int_limits(int value, int num_bits,
163 bool &range_error);
164 INLINE static void validate_int64_limits(int64_t value, int num_bits,
165 bool &range_error);
166 INLINE static void validate_uint_limits(unsigned int value, int num_bits,
167 bool &range_error);
168 INLINE static void validate_uint64_limits(uint64_t value, int num_bits,
169 bool &range_error);
170
171 const DCPackerCatalog *get_catalog() const;
172
173 protected:
174 virtual bool do_check_match(const DCPackerInterface *other) const=0;
175
176 public:
177 // These are declared public just so the derived classes can call them
178 // easily. They're not intended to be called directly.
179
180 virtual bool do_check_match_simple_parameter(const DCSimpleParameter *other) const;
181 virtual bool do_check_match_class_parameter(const DCClassParameter *other) const;
182 virtual bool do_check_match_switch_parameter(const DCSwitchParameter *other) const;
183 virtual bool do_check_match_array_parameter(const DCArrayParameter *other) const;
184 virtual bool do_check_match_atomic_field(const DCAtomicField *other) const;
185 virtual bool do_check_match_molecular_field(const DCMolecularField *other) const;
186
187 private:
188 void make_catalog();
189
190 protected:
191 std::string _name;
192 bool _has_fixed_byte_size;
193 size_t _fixed_byte_size;
194 bool _has_fixed_structure;
195 bool _has_range_limits;
196 size_t _num_length_bytes;
197 bool _has_nested_fields;
198 int _num_nested_fields;
199 DCPackType _pack_type;
200
201 private:
202 DCPackerCatalog *_catalog;
203};
204
205#include "dcPackerInterface.I"
206
207#endif
DCArrayParameter
This represents an array of some other kind of object, meaning this parameter type accepts an arbitra...
Definition dcArrayParameter.h:26
DCAtomicField
A single atomic field of a Distributed Class, as read from a .dc file.
Definition dcAtomicField.h:30
DCClassParameter
This represents a class (or struct) object used as a parameter itself.
Definition dcClassParameter.h:26
DCField
A single field of a Distributed Class, either atomic or molecular.
Definition dcField.h:37
DCFile
Represents the complete list of Distributed Class descriptions as read from a .dc file.
Definition dcFile.h:32
DCMolecularField
A single molecular field of a Distributed Class, as read from a .dc file.
Definition dcMolecularField.h:28
DCPackData
This is a block of data that receives the results of DCPacker.
Definition dcPackData.h:22
DCPackerCatalog
This object contains the names of all of the nested fields available within a particular field.
Definition dcPackerCatalog.h:29
DCPackerInterface::validate_uint_limits
static void validate_uint_limits(unsigned int value, int num_bits, bool &range_error)
Confirms that the unsigned value fits within num_bits bits.
Definition dcPackerInterface.I:359
DCPackerInterface::has_fixed_structure
bool has_fixed_structure() const
Returns true if this field type always has the same structure regardless of the data in the stream,...
Definition dcPackerInterface.I:59
DCPackerInterface::get_catalog
const DCPackerCatalog * get_catalog() const
Returns the DCPackerCatalog associated with this field, listing all of the nested fields by name.
Definition dcPackerInterface.cxx:387
DCPackerInterface::get_name
const std::string & get_name() const
Returns the name of this field, or empty string if the field is unnamed.
Definition dcPackerInterface.I:18
DCPackerInterface::pack_int64
virtual void pack_int64(DCPackData &pack_data, int64_t value, bool &pack_error, bool &range_error) const
Packs the indicated numeric or string value into the stream.
Definition dcPackerInterface.cxx:231
DCPackerInterface::unpack_blob
virtual void unpack_blob(const char *data, size_t length, size_t &p, std::vector< unsigned char > &value, bool &pack_error, bool &range_error) const
Unpacks the current numeric or string value from the stream.
Definition dcPackerInterface.cxx:321
DCPackerInterface::has_nested_fields
bool has_nested_fields() const
Returns true if this field type has any nested fields (and thus expects a push() .
Definition dcPackerInterface.I:91
DCPackerInterface::get_fixed_byte_size
size_t get_fixed_byte_size() const
If has_fixed_byte_size() returns true, this returns the number of bytes this field type will use.
Definition dcPackerInterface.I:46
DCPackerInterface::get_num_nested_fields
int get_num_nested_fields() const
Returns the number of nested fields required by this field type.
Definition dcPackerInterface.I:101
DCPackerInterface::pack_uint
virtual void pack_uint(DCPackData &pack_data, unsigned int value, bool &pack_error, bool &range_error) const
Packs the indicated numeric or string value into the stream.
Definition dcPackerInterface.cxx:223
DCPackerInterface::unpack_uint64
virtual void unpack_uint64(const char *data, size_t length, size_t &p, uint64_t &value, bool &pack_error, bool &range_error) const
Unpacks the current numeric or string value from the stream.
Definition dcPackerInterface.cxx:305
DCPackerInterface::unpack_double
virtual void unpack_double(const char *data, size_t length, size_t &p, double &value, bool &pack_error, bool &range_error) const
Unpacks the current numeric or string value from the stream.
Definition dcPackerInterface.cxx:273
DCPackerInterface::pack_uint64
virtual void pack_uint64(DCPackData &pack_data, uint64_t value, bool &pack_error, bool &range_error) const
Packs the indicated numeric or string value into the stream.
Definition dcPackerInterface.cxx:239
DCPackerInterface::unpack_validate
virtual bool unpack_validate(const char *data, size_t length, size_t &p, bool &pack_error, bool &range_error) const
Internally unpacks the current numeric or string value and validates it against the type range limits...
Definition dcPackerInterface.cxx:332
DCPackerInterface::has_fixed_byte_size
bool has_fixed_byte_size() const
Returns true if this field type always packs to the same number of bytes, false if it is variable.
Definition dcPackerInterface.I:37
DCPackerInterface::calc_num_nested_fields
virtual int calc_num_nested_fields(size_t length_bytes) const
This flavor of get_num_nested_fields is used during unpacking.
Definition dcPackerInterface.cxx:178
DCPackerInterface::do_check_match_simple_parameter
virtual bool do_check_match_simple_parameter(const DCSimpleParameter *other) const
Returns true if this field matches the indicated simple parameter, false otherwise.
Definition dcPackerInterface.cxx:399
DCPackerInterface::unpack_uint
virtual void unpack_uint(const char *data, size_t length, size_t &p, unsigned int &value, bool &pack_error, bool &range_error) const
Unpacks the current numeric or string value from the stream.
Definition dcPackerInterface.cxx:289
DCPackerInterface::pack_string
virtual void pack_string(DCPackData &pack_data, const std::string &value, bool &pack_error, bool &range_error) const
Packs the indicated numeric or string value into the stream.
Definition dcPackerInterface.cxx:247
DCPackerInterface::get_num_length_bytes
size_t get_num_length_bytes() const
Returns the number of bytes that should be written into the stream on a push() to record the number o...
Definition dcPackerInterface.I:79
DCPackerInterface::unpack_string
virtual void unpack_string(const char *data, size_t length, size_t &p, std::string &value, bool &pack_error, bool &range_error) const
Unpacks the current numeric or string value from the stream.
Definition dcPackerInterface.cxx:313
DCPackerInterface::validate_int_limits
static void validate_int_limits(int value, int num_bits, bool &range_error)
Confirms that the signed value fits within num_bits bits.
Definition dcPackerInterface.I:325
DCPackerInterface::validate_uint64_limits
static void validate_uint64_limits(uint64_t value, int num_bits, bool &range_error)
Confirms that the unsigned value fits within num_bits bits.
Definition dcPackerInterface.I:376
DCPackerInterface::unpack_int
virtual void unpack_int(const char *data, size_t length, size_t &p, int &value, bool &pack_error, bool &range_error) const
Unpacks the current numeric or string value from the stream.
Definition dcPackerInterface.cxx:281
DCPackerInterface::validate_int64_limits
static void validate_int64_limits(int64_t value, int num_bits, bool &range_error)
Confirms that the signed value fits within num_bits bits.
Definition dcPackerInterface.I:345
DCPackerInterface::do_check_match_atomic_field
virtual bool do_check_match_atomic_field(const DCAtomicField *other) const
Returns true if this field matches the indicated atomic field, false otherwise.
Definition dcPackerInterface.cxx:435
DCPackerInterface::pack_int
virtual void pack_int(DCPackData &pack_data, int value, bool &pack_error, bool &range_error) const
Packs the indicated numeric or string value into the stream.
Definition dcPackerInterface.cxx:215
DCPackerInterface::validate_num_nested_fields
virtual bool validate_num_nested_fields(int num_nested_fields) const
After a number of fields have been packed via push() .
Definition dcPackerInterface.cxx:199
DCPackerInterface::check_match
bool check_match(const DCPackerInterface *other) const
Returns true if the other interface is bitwise the same as this one–that is, a uint32 only matches a ...
Definition dcPackerInterface.I:28
DCPackerInterface::do_check_match_switch_parameter
virtual bool do_check_match_switch_parameter(const DCSwitchParameter *other) const
Returns true if this field matches the indicated switch parameter, false otherwise.
Definition dcPackerInterface.cxx:417
DCPackerInterface::pack_double
virtual void pack_double(DCPackData &pack_data, double value, bool &pack_error, bool &range_error) const
Packs the indicated numeric or string value into the stream.
Definition dcPackerInterface.cxx:207
DCPackerInterface::unpack_int64
virtual void unpack_int64(const char *data, size_t length, size_t &p, int64_t &value, bool &pack_error, bool &range_error) const
Unpacks the current numeric or string value from the stream.
Definition dcPackerInterface.cxx:297
DCPackerInterface::has_range_limits
bool has_range_limits() const
Returns true if this field, or any sub-field of this field, has a limit imposed in the DC file on its...
Definition dcPackerInterface.I:69
DCPackerInterface::get_nested_field
virtual DCPackerInterface * get_nested_field(int n) const
Returns the DCPackerInterface object that represents the nth nested field.
Definition dcPackerInterface.cxx:188
DCPackerInterface::do_check_match_class_parameter
virtual bool do_check_match_class_parameter(const DCClassParameter *other) const
Returns true if this field matches the indicated class parameter, false otherwise.
Definition dcPackerInterface.cxx:408
DCPackerInterface::set_name
virtual void set_name(const std::string &name)
Sets the name of this field.
Definition dcPackerInterface.cxx:167
DCPackerInterface::get_pack_type
DCPackType get_pack_type() const
Returns the type of value expected by this field.
Definition dcPackerInterface.I:109
DCPackerInterface::do_check_match_molecular_field
virtual bool do_check_match_molecular_field(const DCMolecularField *other) const
Returns true if this field matches the indicated molecular field, false otherwise.
Definition dcPackerInterface.cxx:444
DCPackerInterface::do_check_match_array_parameter
virtual bool do_check_match_array_parameter(const DCArrayParameter *other) const
Returns true if this field matches the indicated array parameter, false otherwise.
Definition dcPackerInterface.cxx:426
DCPackerInterface::pack_blob
virtual void pack_blob(DCPackData &pack_data, const std::vector< unsigned char > &value, bool &pack_error, bool &range_error) const
Packs the indicated numeric or string value into the stream.
Definition dcPackerInterface.cxx:255
DCPackerInterface::unpack_skip
virtual bool unpack_skip(const char *data, size_t length, size_t &p, bool &pack_error) const
Increments p to the end of the current field without actually unpacking any data or performing any ra...
Definition dcPackerInterface.cxx:346
DCPackerInterface::pack_default_value
virtual bool pack_default_value(DCPackData &pack_data, bool &pack_error) const
Packs the field's specified default value (or a sensible default if no value is specified) into the s...
Definition dcPackerInterface.cxx:265
DCPackerInterface::find_seek_index
int find_seek_index(const std::string &name) const
Returns the index number to be passed to a future call to DCPacker::seek() to seek directly to the na...
Definition dcPackerInterface.cxx:79
DCSimpleParameter
This is the most fundamental kind of parameter type: a single number or string, one of the DCSubatomi...
Definition dcSimpleParameter.h:28
DCSwitchParameter
This represents a switch object used as a parameter itself, which packs the appropriate fields of the...
Definition dcSwitchParameter.h:26
dcPackerInterface.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dcSubatomicType.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dcbase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.