19 #include "hashGenerator.h" 20 #include "dcmsgtypes.h" 27 #include "pStatTimer.h" 40 _field_update_pcollector(
"DCField")
44 _default_value_stale =
true;
45 _has_default_value =
false;
49 _has_nested_fields =
true;
50 _num_nested_fields = 0;
51 _pack_type = PT_field;
53 _has_fixed_byte_size =
true;
55 _has_fixed_structure =
true;
64 DCField(
const string &name,
DCClass *dclass) :
69 _field_update_pcollector(dclass->_class_update_pcollector, name)
73 _has_default_value =
false;
74 _default_value_stale =
true;
78 _has_nested_fields =
true;
79 _num_nested_fields = 0;
80 _pack_type = PT_field;
82 _has_fixed_byte_size =
true;
84 _has_fixed_structure =
true;
180 as_parameter()
const {
261 pack_args(
DCPacker &packer, PyObject *sequence)
const {
265 packer.pack_object(sequence);
276 PyObject *exc_type = PyExc_Exception;
282 strm <<
"Incorrect arguments to field: " <<
get_name()
283 <<
" = " << get_pystr(sequence);
284 exc_type = PyExc_TypeError;
286 strm <<
"Value out of range on field: " <<
get_name()
287 <<
" = " << get_pystr(sequence);
288 exc_type = PyExc_ValueError;
294 PyObject *tuple = PySequence_Tuple(sequence);
295 if (tuple == (PyObject *)NULL) {
296 strm <<
"Value for " <<
get_name() <<
" not a sequence: " \
297 << get_pystr(sequence);
298 exc_type = PyExc_TypeError;
302 strm <<
"Incorrect arguments to field: " <<
get_name()
303 << get_pystr(sequence);
304 exc_type = PyExc_TypeError;
306 strm <<
"Value out of range on field: " <<
get_name()
307 << get_pystr(sequence);
308 exc_type = PyExc_ValueError;
315 string message = strm.str();
316 PyErr_SetString(exc_type, message.c_str());
320 #endif // HAVE_PYTHON 334 unpack_args(
DCPacker &packer)
const {
339 PyObject *
object = packer.unpack_object();
352 PyObject *exc_type = PyExc_Exception;
355 strm <<
"Data error unpacking field ";
358 strm <<
"\nGot data (" << (int)length <<
" bytes):\n";
362 strm <<
"Error detected on byte " << error_byte
363 <<
" (" << hex << error_byte << dec <<
" hex)";
365 exc_type = PyExc_RuntimeError;
367 strm <<
"Value outside specified range when unpacking field " 368 <<
get_name() <<
": " << get_pystr(
object);
369 exc_type = PyExc_ValueError;
372 string message = strm.str();
373 PyErr_SetString(exc_type, message.c_str());
379 #endif // HAVE_PYTHON 390 receive_update(
DCPacker &packer, PyObject *distobj)
const {
394 PyObject *value = unpack_args(packer);
395 if (value != (PyObject *)NULL) {
396 PyObject_SetAttrString(distobj, (
char *)_name.c_str(), value);
404 if (!PyObject_HasAttrString(distobj, (
char *)_name.c_str())) {
413 PyObject *args = unpack_args(packer);
415 if (args != (PyObject *)NULL) {
416 PyObject *func = PyObject_GetAttrString(distobj, (
char *)_name.c_str());
417 nassertv(func != (PyObject *)NULL);
424 result = PyObject_CallObject(func, args);
433 #endif // HAVE_PYTHON 444 client_format_update(DOID_TYPE do_id, PyObject *args)
const {
452 pack_args(packer, args);
459 #endif // HAVE_PYTHON 470 ai_format_update(DOID_TYPE do_id, CHANNEL_TYPE to_id, CHANNEL_TYPE from_id, PyObject *args)
const {
474 packer.RAW_PACK_CHANNEL(to_id);
475 packer.RAW_PACK_CHANNEL(from_id);
481 pack_args(packer, args);
488 #endif // HAVE_PYTHON 500 ai_format_update_msg_type(DOID_TYPE do_id, CHANNEL_TYPE to_id, CHANNEL_TYPE from_id,
int msg_type, PyObject *args)
const {
504 packer.RAW_PACK_CHANNEL(to_id);
505 packer.RAW_PACK_CHANNEL(from_id);
511 pack_args(packer, args);
518 #endif // HAVE_PYTHON 537 if (dc_multiple_inheritance) {
556 if (!_default_value_stale) {
557 pack_data.
append_data(_default_value.data(), _default_value.length());
572 if (_dclass != (
DCClass *)NULL) {
585 get_pystr(PyObject *value) {
590 PyObject *str = PyObject_Str(value);
592 #if PY_MAJOR_VERSION >= 3 593 string result = PyUnicode_AsUTF8(str);
595 string result = PyString_AsString(str);
601 PyObject *repr = PyObject_Repr(value);
603 #if PY_MAJOR_VERSION >= 3 604 string result = PyUnicode_AsUTF8(repr);
606 string result = PyString_AsString(repr);
612 if (value->ob_type != NULL) {
613 PyObject *typestr = PyObject_Str((PyObject *)(value->ob_type));
614 if (typestr != NULL) {
615 #if PY_MAJOR_VERSION >= 3 616 string result = PyUnicode_AsUTF8(typestr);
618 string result = PyString_AsString(typestr);
625 return "(invalid object)";
627 #endif // HAVE_PYTHON 636 refresh_default_value() {
641 cerr <<
"Error while packing default value for " <<
get_name() <<
"\n";
645 _default_value_stale =
false;
void add_string(const string &str)
Adds a string to the hash, by breaking it down into a sequence of integers.
bool had_error() const
Returns true if there has been any error (either a pack error or a range error) since the most recent...
This is a block of data that receives the results of DCPacker.
void append_data(const char *buffer, size_t size)
Adds the indicated bytes to the end of the data.
string format_data(const string &packed_data, bool show_field_names=true)
Given a blob that represents the packed data for this field, returns a string formatting it for human...
bool parse_and_pack(const string &formatted_object)
Parses an object's value according to the DC file syntax (e.g.
void add_int(int num)
Adds another integer to the hash so far.
string get_string() const
Returns the packed data buffer as a string.
size_t get_num_unpacked_bytes() const
Returns the number of bytes that have been unpacked so far, or after unpack_end(), the total number of bytes that were unpacked at all.
A single field of a Distributed Class, either atomic or molecular.
void dump_hex(ostream &out, unsigned int indent=0) const
Writes a representation of the entire datagram contents, as a sequence of hex (and ASCII) values...
bool has_assert_failed() const
Returns true if an assertion test has failed (and not been ignored) since the last call to clear_asse...
const DCPackerInterface * get_current_field() const
Returns the field that will be referenced by the next call to pack_*() or unpack_*().
void raw_pack_uint8(unsigned int value)
Packs the data into the buffer between packing sessions.
const string & get_name() const
Returns the name of this field, or empty string if the field is unnamed.
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...
virtual DCAtomicField * as_atomic_field()
Returns the same field pointer converted to an atomic field pointer, if this is in fact an atomic fie...
Defines a particular DistributedClass as read from an input .dc file.
bool had_pack_error() const
Returns true if there has been an packing error since the most recent call to begin(); in particular...
A lightweight class that can be used to automatically start and stop a PStatCollector around a sectio...
size_t get_length() const
Returns the current length of the buffer.
A single atomic field of a Distributed Class, as read from a .dc file.
virtual DCMolecularField * as_molecular_field()
Returns the same field pointer converted to a molecular field pointer, if this is in fact a molecular...
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this field into the hash.
void raw_pack_uint32(unsigned int value)
Packs the data into the buffer between packing sessions.
size_t get_unpack_length() const
Returns the total number of bytes in the unpack data buffer.
static Notify * ptr()
Returns the pointer to the global Notify object.
bool validate_ranges(const string &packed_data) const
Verifies that all of the packed values in the field data are within the specified ranges and that the...
Represents the type specification for a single parameter within a field specification.
string parse_string(const string &formatted_string)
Given a human-formatted string (for instance, as returned by format_data(), above) that represents th...
void pack_default_value()
Adds the default value for the current element into the stream.
void raw_pack_uint16(unsigned int value)
Packs the data into the buffer between packing sessions.
void output(ostream &out) const
Write a string representation of this instance to <out>.
const char * get_data() const
Returns the beginning of the data buffer.
This class generates an arbitrary hash number from a sequence of ints.
void mark_inherited_fields_stale()
Indicates that something has changed in one or more of the inheritance chains or the set of fields; t...
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...
void begin_pack(const DCPackerInterface *root)
Begins a packing session.
This class can be used for packing a series of numeric and string data into a binary stream...
virtual void set_name(const string &name)
Sets the name of this field.
void unpack_skip()
Skips the current field without unpacking it and advances to the next field.
virtual void set_name(const string &name)
Sets the name of this field.
bool end_pack()
Finishes a packing session.
void set_unpack_data(const string &data)
Sets up the unpack_data pointer.
const char * get_unpack_data() const
Returns a read pointer to the unpack data buffer.
A single molecular field of a Distributed Class, as read from a .dc file.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
This defines the internal interface for packing values into a DCField.
void begin_unpack(const DCPackerInterface *root)
Begins an unpacking session.
bool end_unpack()
Finishes the unpacking session.
void unpack_validate()
Internally unpacks the current numeric or string value and validates it against the type range limits...