dcPackerInterface.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Filename: dcPackerInterface.h
  2. // Created by: drose (15Jun04)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef DCPACKERINTERFACE_H
  19. #define DCPACKERINTERFACE_H
  20. #include "dcbase.h"
  21. #include "dcSubatomicType.h"
  22. class DCFile;
  23. class DCField;
  24. class DCSimpleParameter;
  25. class DCSwitchParameter;
  26. class DCClassParameter;
  27. class DCArrayParameter;
  28. class DCAtomicField;
  29. class DCMolecularField;
  30. class DCPackData;
  31. class DCPackerCatalog;
  32. BEGIN_PUBLISH
  33. // This enumerated type is returned by get_pack_type() and represents
  34. // the best 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. // These PackTypes are all fundamental types, and should be packed
  39. // (or unpacked) with the corresponding call to pack_double(),
  40. // pack_int(), etc. PT_blob is the same as PT_string, but implies
  41. // that the string contains binary data.
  42. PT_double,
  43. PT_int,
  44. PT_uint,
  45. PT_int64,
  46. PT_uint64,
  47. PT_string,
  48. PT_blob,
  49. // The remaining PackTypes imply a need to call push() and pop().
  50. // They are all variants on the same thing: a list of nested fields,
  51. // but the PackType provides a bit of a semantic context.
  52. PT_array,
  53. PT_field,
  54. PT_class,
  55. PT_switch,
  56. };
  57. END_PUBLISH
  58. ////////////////////////////////////////////////////////////////////
  59. // Class : DCPackerInterface
  60. // Description : This defines the internal interface for packing
  61. // values into a DCField. The various different DC
  62. // objects inherit from this.
  63. //
  64. // Normally these methods are called only by the
  65. // DCPacker object; the user wouldn't normally call
  66. // these directly.
  67. ////////////////////////////////////////////////////////////////////
  68. class EXPCL_DIRECT DCPackerInterface {
  69. public:
  70. DCPackerInterface(const string &name = string());
  71. DCPackerInterface(const DCPackerInterface &copy);
  72. virtual ~DCPackerInterface();
  73. PUBLISHED:
  74. INLINE const string &get_name() const;
  75. int find_seek_index(const string &name) const;
  76. virtual DCField *as_field();
  77. virtual const DCField *as_field() const;
  78. virtual DCSwitchParameter *as_switch_parameter();
  79. virtual const DCSwitchParameter *as_switch_parameter() const;
  80. virtual DCClassParameter *as_class_parameter();
  81. virtual const DCClassParameter *as_class_parameter() const;
  82. INLINE bool check_match(const DCPackerInterface *other) const;
  83. bool check_match(const string &description, DCFile *dcfile = NULL) const;
  84. public:
  85. INLINE void set_name(const string &name);
  86. INLINE bool has_fixed_byte_size() const;
  87. INLINE size_t get_fixed_byte_size() const;
  88. INLINE bool has_fixed_structure() const;
  89. INLINE bool has_range_limits() const;
  90. INLINE size_t get_num_length_bytes() const;
  91. INLINE bool has_nested_fields() const;
  92. INLINE int get_num_nested_fields() const;
  93. virtual int calc_num_nested_fields(size_t length_bytes) const;
  94. virtual DCPackerInterface *get_nested_field(int n) const;
  95. virtual bool validate_num_nested_fields(int num_nested_fields) const;
  96. INLINE DCPackType get_pack_type() const;
  97. virtual void pack_double(DCPackData &pack_data, double value,
  98. bool &pack_error, bool &range_error) const;
  99. virtual void pack_int(DCPackData &pack_data, int value,
  100. bool &pack_error, bool &range_error) const;
  101. virtual void pack_uint(DCPackData &pack_data, unsigned int value,
  102. bool &pack_error, bool &range_error) const;
  103. virtual void pack_int64(DCPackData &pack_data, PN_int64 value,
  104. bool &pack_error, bool &range_error) const;
  105. virtual void pack_uint64(DCPackData &pack_data, PN_uint64 value,
  106. bool &pack_error, bool &range_error) const;
  107. virtual void pack_string(DCPackData &pack_data, const string &value,
  108. bool &pack_error, bool &range_error) const;
  109. virtual bool pack_default_value(DCPackData &pack_data, bool &pack_error) const;
  110. virtual void unpack_double(const char *data, size_t length, size_t &p,
  111. double &value, bool &pack_error, bool &range_error) const;
  112. virtual void unpack_int(const char *data, size_t length, size_t &p,
  113. int &value, bool &pack_error, bool &range_error) const;
  114. virtual void unpack_uint(const char *data, size_t length, size_t &p,
  115. unsigned int &value, bool &pack_error, bool &range_error) const;
  116. virtual void unpack_int64(const char *data, size_t length, size_t &p,
  117. PN_int64 &value, bool &pack_error, bool &range_error) const;
  118. virtual void unpack_uint64(const char *data, size_t length, size_t &p,
  119. PN_uint64 &value, bool &pack_error, bool &range_error) const;
  120. virtual void unpack_string(const char *data, size_t length, size_t &p,
  121. string &value, bool &pack_error, bool &range_error) const;
  122. virtual bool unpack_validate(const char *data, size_t length, size_t &p,
  123. bool &pack_error, bool &range_error) const;
  124. virtual bool unpack_skip(const char *data, size_t length, size_t &p,
  125. bool &pack_error) const;
  126. // These are the low-level interfaces for packing and unpacking
  127. // numbers from a buffer. You're responsible for making sure the
  128. // buffer has enough room, and for incrementing the pointer.
  129. INLINE static void do_pack_int8(char *buffer, int value);
  130. INLINE static void do_pack_int16(char *buffer, int value);
  131. INLINE static void do_pack_int32(char *buffer, int value);
  132. INLINE static void do_pack_int64(char *buffer, PN_int64 value);
  133. INLINE static void do_pack_uint8(char *buffer, unsigned int value);
  134. INLINE static void do_pack_uint16(char *buffer, unsigned int value);
  135. INLINE static void do_pack_uint32(char *buffer, unsigned int value);
  136. INLINE static void do_pack_uint64(char *buffer, PN_uint64 value);
  137. INLINE static void do_pack_float64(char *buffer, double value);
  138. INLINE static int do_unpack_int8(const char *buffer);
  139. INLINE static int do_unpack_int16(const char *buffer);
  140. INLINE static int do_unpack_int32(const char *buffer);
  141. INLINE static PN_int64 do_unpack_int64(const char *buffer);
  142. INLINE static unsigned int do_unpack_uint8(const char *buffer);
  143. INLINE static unsigned int do_unpack_uint16(const char *buffer);
  144. INLINE static unsigned int do_unpack_uint32(const char *buffer);
  145. INLINE static PN_uint64 do_unpack_uint64(const char *buffer);
  146. INLINE static double do_unpack_float64(const char *buffer);
  147. INLINE static void validate_int_limits(int value, int num_bits,
  148. bool &range_error);
  149. INLINE static void validate_int64_limits(PN_int64 value, int num_bits,
  150. bool &range_error);
  151. INLINE static void validate_uint_limits(unsigned int value, int num_bits,
  152. bool &range_error);
  153. INLINE static void validate_uint64_limits(PN_uint64 value, int num_bits,
  154. bool &range_error);
  155. const DCPackerCatalog *get_catalog() const;
  156. protected:
  157. virtual bool do_check_match(const DCPackerInterface *other) const=0;
  158. public:
  159. // These are declared public just so the derived classes can call
  160. // them easily. They're not intended to be called directly.
  161. virtual bool do_check_match_simple_parameter(const DCSimpleParameter *other) const;
  162. virtual bool do_check_match_class_parameter(const DCClassParameter *other) const;
  163. virtual bool do_check_match_switch_parameter(const DCSwitchParameter *other) const;
  164. virtual bool do_check_match_array_parameter(const DCArrayParameter *other) const;
  165. virtual bool do_check_match_atomic_field(const DCAtomicField *other) const;
  166. virtual bool do_check_match_molecular_field(const DCMolecularField *other) const;
  167. private:
  168. void make_catalog();
  169. protected:
  170. string _name;
  171. bool _has_fixed_byte_size;
  172. size_t _fixed_byte_size;
  173. bool _has_fixed_structure;
  174. bool _has_range_limits;
  175. size_t _num_length_bytes;
  176. bool _has_nested_fields;
  177. int _num_nested_fields;
  178. DCPackType _pack_type;
  179. private:
  180. DCPackerCatalog *_catalog;
  181. };
  182. #include "dcPackerInterface.I"
  183. #endif