Panda3D
|
00001 // Filename: dcPackerInterface.h 00002 // Created by: drose (15Jun04) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef DCPACKERINTERFACE_H 00016 #define DCPACKERINTERFACE_H 00017 00018 #include "dcbase.h" 00019 #include "dcSubatomicType.h" 00020 00021 class DCFile; 00022 class DCField; 00023 class DCSimpleParameter; 00024 class DCSwitchParameter; 00025 class DCClassParameter; 00026 class DCArrayParameter; 00027 class DCAtomicField; 00028 class DCMolecularField; 00029 class DCPackData; 00030 class DCPackerCatalog; 00031 00032 BEGIN_PUBLISH 00033 // This enumerated type is returned by get_pack_type() and represents 00034 // the best choice for a subsequent call to pack_*() or unpack_*(). 00035 enum DCPackType { 00036 // This one should never be returned in a normal situation. 00037 PT_invalid, 00038 00039 // These PackTypes are all fundamental types, and should be packed 00040 // (or unpacked) with the corresponding call to pack_double(), 00041 // pack_int(), etc. PT_blob is the same as PT_string, but implies 00042 // that the string contains binary data. 00043 PT_double, 00044 PT_int, 00045 PT_uint, 00046 PT_int64, 00047 PT_uint64, 00048 PT_string, 00049 PT_blob, 00050 00051 // The remaining PackTypes imply a need to call push() and pop(). 00052 // They are all variants on the same thing: a list of nested fields, 00053 // but the PackType provides a bit of a semantic context. 00054 PT_array, 00055 PT_field, 00056 PT_class, 00057 PT_switch, 00058 }; 00059 END_PUBLISH 00060 00061 //////////////////////////////////////////////////////////////////// 00062 // Class : DCPackerInterface 00063 // Description : This defines the internal interface for packing 00064 // values into a DCField. The various different DC 00065 // objects inherit from this. 00066 // 00067 // Normally these methods are called only by the 00068 // DCPacker object; the user wouldn't normally call 00069 // these directly. 00070 //////////////////////////////////////////////////////////////////// 00071 class EXPCL_DIRECT DCPackerInterface { 00072 public: 00073 DCPackerInterface(const string &name = string()); 00074 DCPackerInterface(const DCPackerInterface ©); 00075 virtual ~DCPackerInterface(); 00076 00077 PUBLISHED: 00078 INLINE const string &get_name() const; 00079 int find_seek_index(const string &name) const; 00080 00081 virtual DCField *as_field(); 00082 virtual const DCField *as_field() const; 00083 virtual DCSwitchParameter *as_switch_parameter(); 00084 virtual const DCSwitchParameter *as_switch_parameter() const; 00085 virtual DCClassParameter *as_class_parameter(); 00086 virtual const DCClassParameter *as_class_parameter() const; 00087 00088 INLINE bool check_match(const DCPackerInterface *other) const; 00089 bool check_match(const string &description, DCFile *dcfile = NULL) const; 00090 00091 public: 00092 virtual void set_name(const string &name); 00093 INLINE bool has_fixed_byte_size() const; 00094 INLINE size_t get_fixed_byte_size() const; 00095 INLINE bool has_fixed_structure() const; 00096 INLINE bool has_range_limits() const; 00097 INLINE size_t get_num_length_bytes() const; 00098 00099 INLINE bool has_nested_fields() const; 00100 INLINE int get_num_nested_fields() const; 00101 virtual int calc_num_nested_fields(size_t length_bytes) const; 00102 virtual DCPackerInterface *get_nested_field(int n) const; 00103 00104 virtual bool validate_num_nested_fields(int num_nested_fields) const; 00105 00106 INLINE DCPackType get_pack_type() const; 00107 00108 virtual void pack_double(DCPackData &pack_data, double value, 00109 bool &pack_error, bool &range_error) const; 00110 virtual void pack_int(DCPackData &pack_data, int value, 00111 bool &pack_error, bool &range_error) const; 00112 virtual void pack_uint(DCPackData &pack_data, unsigned int value, 00113 bool &pack_error, bool &range_error) const; 00114 virtual void pack_int64(DCPackData &pack_data, PN_int64 value, 00115 bool &pack_error, bool &range_error) const; 00116 virtual void pack_uint64(DCPackData &pack_data, PN_uint64 value, 00117 bool &pack_error, bool &range_error) const; 00118 virtual void pack_string(DCPackData &pack_data, const string &value, 00119 bool &pack_error, bool &range_error) const; 00120 virtual bool pack_default_value(DCPackData &pack_data, bool &pack_error) const; 00121 00122 virtual void unpack_double(const char *data, size_t length, size_t &p, 00123 double &value, bool &pack_error, bool &range_error) const; 00124 virtual void unpack_int(const char *data, size_t length, size_t &p, 00125 int &value, bool &pack_error, bool &range_error) const; 00126 virtual void unpack_uint(const char *data, size_t length, size_t &p, 00127 unsigned int &value, bool &pack_error, bool &range_error) const; 00128 virtual void unpack_int64(const char *data, size_t length, size_t &p, 00129 PN_int64 &value, bool &pack_error, bool &range_error) const; 00130 virtual void unpack_uint64(const char *data, size_t length, size_t &p, 00131 PN_uint64 &value, bool &pack_error, bool &range_error) const; 00132 virtual void unpack_string(const char *data, size_t length, size_t &p, 00133 string &value, bool &pack_error, bool &range_error) const; 00134 virtual bool unpack_validate(const char *data, size_t length, size_t &p, 00135 bool &pack_error, bool &range_error) const; 00136 virtual bool unpack_skip(const char *data, size_t length, size_t &p, 00137 bool &pack_error) const; 00138 00139 // These are the low-level interfaces for packing and unpacking 00140 // numbers from a buffer. You're responsible for making sure the 00141 // buffer has enough room, and for incrementing the pointer. 00142 INLINE static void do_pack_int8(char *buffer, int value); 00143 INLINE static void do_pack_int16(char *buffer, int value); 00144 INLINE static void do_pack_int32(char *buffer, int value); 00145 INLINE static void do_pack_int64(char *buffer, PN_int64 value); 00146 INLINE static void do_pack_uint8(char *buffer, unsigned int value); 00147 INLINE static void do_pack_uint16(char *buffer, unsigned int value); 00148 INLINE static void do_pack_uint32(char *buffer, unsigned int value); 00149 INLINE static void do_pack_uint64(char *buffer, PN_uint64 value); 00150 INLINE static void do_pack_float64(char *buffer, double value); 00151 00152 INLINE static int do_unpack_int8(const char *buffer); 00153 INLINE static int do_unpack_int16(const char *buffer); 00154 INLINE static int do_unpack_int32(const char *buffer); 00155 INLINE static PN_int64 do_unpack_int64(const char *buffer); 00156 INLINE static unsigned int do_unpack_uint8(const char *buffer); 00157 INLINE static unsigned int do_unpack_uint16(const char *buffer); 00158 INLINE static unsigned int do_unpack_uint32(const char *buffer); 00159 INLINE static PN_uint64 do_unpack_uint64(const char *buffer); 00160 INLINE static double do_unpack_float64(const char *buffer); 00161 00162 INLINE static void validate_int_limits(int value, int num_bits, 00163 bool &range_error); 00164 INLINE static void validate_int64_limits(PN_int64 value, int num_bits, 00165 bool &range_error); 00166 INLINE static void validate_uint_limits(unsigned int value, int num_bits, 00167 bool &range_error); 00168 INLINE static void validate_uint64_limits(PN_uint64 value, int num_bits, 00169 bool &range_error); 00170 00171 const DCPackerCatalog *get_catalog() const; 00172 00173 protected: 00174 virtual bool do_check_match(const DCPackerInterface *other) const=0; 00175 00176 public: 00177 // These are declared public just so the derived classes can call 00178 // them easily. They're not intended to be called directly. 00179 00180 virtual bool do_check_match_simple_parameter(const DCSimpleParameter *other) const; 00181 virtual bool do_check_match_class_parameter(const DCClassParameter *other) const; 00182 virtual bool do_check_match_switch_parameter(const DCSwitchParameter *other) const; 00183 virtual bool do_check_match_array_parameter(const DCArrayParameter *other) const; 00184 virtual bool do_check_match_atomic_field(const DCAtomicField *other) const; 00185 virtual bool do_check_match_molecular_field(const DCMolecularField *other) const; 00186 00187 private: 00188 void make_catalog(); 00189 00190 protected: 00191 string _name; 00192 bool _has_fixed_byte_size; 00193 size_t _fixed_byte_size; 00194 bool _has_fixed_structure; 00195 bool _has_range_limits; 00196 size_t _num_length_bytes; 00197 bool _has_nested_fields; 00198 int _num_nested_fields; 00199 DCPackType _pack_type; 00200 00201 private: 00202 DCPackerCatalog *_catalog; 00203 }; 00204 00205 #include "dcPackerInterface.I" 00206 00207 #endif