Panda3D
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 #include "vector_uchar.h"
20 
21 class DCFile;
22 class DCField;
23 class DCSimpleParameter;
24 class DCSwitchParameter;
25 class DCClassParameter;
26 class DCArrayParameter;
27 class DCAtomicField;
28 class DCMolecularField;
29 class DCPackData;
30 class DCPackerCatalog;
31 
32 BEGIN_PUBLISH
33 // This enumerated type is returned by get_pack_type() and represents the best
34 // choice for a subsequent call to pack_*() or unpack_*().
35 enum DCPackType {
36  // This one should never be returned in a normal situation.
37  PT_invalid,
38 
39  // These PackTypes are all fundamental types, and should be packed (or
40  // unpacked) with the corresponding call to pack_double(), pack_int(), etc.
41  // PT_blob is similar to PT_string, except that it contains arbitrary binary
42  // data instead of just UTF-8 text.
43  PT_double,
44  PT_int,
45  PT_uint,
46  PT_int64,
47  PT_uint64,
48  PT_string,
49  PT_blob,
50 
51  // The remaining PackTypes imply a need to call push() and pop(). They are
52  // all variants on the same thing: a list of nested fields, but the PackType
53  // provides a bit of a semantic context.
54  PT_array,
55  PT_field,
56  PT_class,
57  PT_switch,
58 };
59 END_PUBLISH
60 
61 /**
62  * This defines the internal interface for packing values into a DCField. The
63  * various different DC objects inherit from this.
64  *
65  * Normally these methods are called only by the DCPacker object; the user
66  * wouldn't normally call these directly.
67  */
68 class EXPCL_DIRECT_DCPARSER DCPackerInterface {
69 public:
70  DCPackerInterface(const std::string &name = std::string());
72  virtual ~DCPackerInterface();
73 
74 PUBLISHED:
75  INLINE const std::string &get_name() const;
76  int find_seek_index(const std::string &name) const;
77 
78  virtual DCField *as_field();
79  virtual const DCField *as_field() const;
80  virtual DCSwitchParameter *as_switch_parameter();
81  virtual const DCSwitchParameter *as_switch_parameter() const;
82  virtual DCClassParameter *as_class_parameter();
83  virtual const DCClassParameter *as_class_parameter() const;
84 
85  INLINE bool check_match(const DCPackerInterface *other) const;
86  bool check_match(const std::string &description, DCFile *dcfile = nullptr) const;
87 
88 public:
89  virtual void set_name(const std::string &name);
90  INLINE bool has_fixed_byte_size() const;
91  INLINE size_t get_fixed_byte_size() const;
92  INLINE bool has_fixed_structure() const;
93  INLINE bool has_range_limits() const;
94  INLINE size_t get_num_length_bytes() const;
95 
96  INLINE bool has_nested_fields() const;
97  INLINE int get_num_nested_fields() const;
98  virtual int calc_num_nested_fields(size_t length_bytes) const;
99  virtual DCPackerInterface *get_nested_field(int n) const;
100 
101  virtual bool validate_num_nested_fields(int num_nested_fields) const;
102 
103  INLINE DCPackType get_pack_type() const;
104 
105  virtual void pack_double(DCPackData &pack_data, double value,
106  bool &pack_error, bool &range_error) const;
107  virtual void pack_int(DCPackData &pack_data, int value,
108  bool &pack_error, bool &range_error) const;
109  virtual void pack_uint(DCPackData &pack_data, unsigned int value,
110  bool &pack_error, bool &range_error) const;
111  virtual void pack_int64(DCPackData &pack_data, int64_t value,
112  bool &pack_error, bool &range_error) const;
113  virtual void pack_uint64(DCPackData &pack_data, uint64_t value,
114  bool &pack_error, bool &range_error) const;
115  virtual void pack_string(DCPackData &pack_data, const std::string &value,
116  bool &pack_error, bool &range_error) const;
117  virtual void pack_blob(DCPackData &pack_data, const vector_uchar &value,
118  bool &pack_error, bool &range_error) const;
119  virtual bool pack_default_value(DCPackData &pack_data, bool &pack_error) const;
120 
121  virtual void unpack_double(const char *data, size_t length, size_t &p,
122  double &value, bool &pack_error, bool &range_error) const;
123  virtual void unpack_int(const char *data, size_t length, size_t &p,
124  int &value, bool &pack_error, bool &range_error) const;
125  virtual void unpack_uint(const char *data, size_t length, size_t &p,
126  unsigned int &value, bool &pack_error, bool &range_error) const;
127  virtual void unpack_int64(const char *data, size_t length, size_t &p,
128  int64_t &value, bool &pack_error, bool &range_error) const;
129  virtual void unpack_uint64(const char *data, size_t length, size_t &p,
130  uint64_t &value, bool &pack_error, bool &range_error) const;
131  virtual void unpack_string(const char *data, size_t length, size_t &p,
132  std::string &value, bool &pack_error, bool &range_error) const;
133  virtual void unpack_blob(const char *data, size_t length, size_t &p,
134  vector_uchar &value, bool &pack_error, bool &range_error) const;
135  virtual bool unpack_validate(const char *data, size_t length, size_t &p,
136  bool &pack_error, bool &range_error) const;
137  virtual bool unpack_skip(const char *data, size_t length, size_t &p,
138  bool &pack_error) const;
139 
140  // These are the low-level interfaces for packing and unpacking numbers from
141  // a buffer. You're responsible for making sure the buffer has enough room,
142  // and for incrementing the pointer.
143  INLINE static void do_pack_int8(char *buffer, int value);
144  INLINE static void do_pack_int16(char *buffer, int value);
145  INLINE static void do_pack_int32(char *buffer, int value);
146  INLINE static void do_pack_int64(char *buffer, int64_t value);
147  INLINE static void do_pack_uint8(char *buffer, unsigned int value);
148  INLINE static void do_pack_uint16(char *buffer, unsigned int value);
149  INLINE static void do_pack_uint32(char *buffer, unsigned int value);
150  INLINE static void do_pack_uint64(char *buffer, uint64_t value);
151  INLINE static void do_pack_float64(char *buffer, double value);
152 
153  INLINE static int do_unpack_int8(const char *buffer);
154  INLINE static int do_unpack_int16(const char *buffer);
155  INLINE static int do_unpack_int32(const char *buffer);
156  INLINE static int64_t do_unpack_int64(const char *buffer);
157  INLINE static unsigned int do_unpack_uint8(const char *buffer);
158  INLINE static unsigned int do_unpack_uint16(const char *buffer);
159  INLINE static unsigned int do_unpack_uint32(const char *buffer);
160  INLINE static uint64_t do_unpack_uint64(const char *buffer);
161  INLINE static double do_unpack_float64(const char *buffer);
162 
163  INLINE static void validate_int_limits(int value, int num_bits,
164  bool &range_error);
165  INLINE static void validate_int64_limits(int64_t value, int num_bits,
166  bool &range_error);
167  INLINE static void validate_uint_limits(unsigned int value, int num_bits,
168  bool &range_error);
169  INLINE static void validate_uint64_limits(uint64_t value, int num_bits,
170  bool &range_error);
171 
172  const DCPackerCatalog *get_catalog() const;
173 
174 protected:
175  virtual bool do_check_match(const DCPackerInterface *other) const=0;
176 
177 public:
178  // These are declared public just so the derived classes can call them
179  // easily. They're not intended to be called directly.
180 
181  virtual bool do_check_match_simple_parameter(const DCSimpleParameter *other) const;
182  virtual bool do_check_match_class_parameter(const DCClassParameter *other) const;
183  virtual bool do_check_match_switch_parameter(const DCSwitchParameter *other) const;
184  virtual bool do_check_match_array_parameter(const DCArrayParameter *other) const;
185  virtual bool do_check_match_atomic_field(const DCAtomicField *other) const;
186  virtual bool do_check_match_molecular_field(const DCMolecularField *other) const;
187 
188 private:
189  void make_catalog();
190 
191 protected:
192  std::string _name;
193  bool _has_fixed_byte_size;
194  size_t _fixed_byte_size;
195  bool _has_fixed_structure;
196  bool _has_range_limits;
197  size_t _num_length_bytes;
198  bool _has_nested_fields;
199  int _num_nested_fields;
200  DCPackType _pack_type;
201 
202 private:
203  DCPackerCatalog *_catalog;
204 };
205 
206 #include "dcPackerInterface.I"
207 
208 #endif
This is a block of data that receives the results of DCPacker.
Definition: dcPackData.h:22
This represents a class (or struct) object used as a parameter itself.
A single field of a Distributed Class, either atomic or molecular.
Definition: dcField.h:37
This is the most fundamental kind of parameter type: a single number or string, one of the DCSubatomi...
This represents a switch object used as a parameter itself, which packs the appropriate fields of the...
A single atomic field of a Distributed Class, as read from a .dc file.
Definition: dcAtomicField.h:30
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents the complete list of Distributed Class descriptions as read from a .dc file.
Definition: dcFile.h:32
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This represents an array of some other kind of object, meaning this parameter type accepts an arbitra...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This object contains the names of all of the nested fields available within a particular field.
A single molecular field of a Distributed Class, as read from a .dc file.
This defines the internal interface for packing values into a DCField.