19 #include "hashGenerator.h"
20 #include "dcmsgtypes.h"
23 #include "pStatTimer.h"
36 _field_update_pcollector(
"DCField")
40 _default_value_stale =
true;
41 _has_default_value =
false;
45 _has_nested_fields =
true;
46 _num_nested_fields = 0;
47 _pack_type = PT_field;
49 _has_fixed_byte_size =
true;
51 _has_fixed_structure =
true;
60 DCField(
const string &name,
DCClass *dclass) :
65 _field_update_pcollector(dclass->_class_update_pcollector, name)
69 _has_default_value =
false;
70 _default_value_stale =
true;
74 _has_nested_fields =
true;
75 _num_nested_fields = 0;
76 _pack_type = PT_field;
78 _has_fixed_byte_size =
true;
80 _has_fixed_structure =
true;
176 as_parameter()
const {
257 pack_args(
DCPacker &packer, PyObject *sequence)
const {
261 packer.pack_object(sequence);
272 PyObject *exc_type = PyExc_Exception;
278 strm <<
"Incorrect arguments to field: " <<
get_name()
279 <<
" = " << get_pystr(sequence);
280 exc_type = PyExc_TypeError;
282 strm <<
"Value out of range on field: " <<
get_name()
283 <<
" = " << get_pystr(sequence);
284 exc_type = PyExc_ValueError;
290 PyObject *tuple = PySequence_Tuple(sequence);
291 if (tuple == (PyObject *)NULL) {
292 strm <<
"Value for " <<
get_name() <<
" not a sequence: " \
293 << get_pystr(sequence);
294 exc_type = PyExc_TypeError;
298 strm <<
"Incorrect arguments to field: " <<
get_name()
299 << get_pystr(sequence);
300 exc_type = PyExc_TypeError;
302 strm <<
"Value out of range on field: " <<
get_name()
303 << get_pystr(sequence);
304 exc_type = PyExc_ValueError;
311 string message = strm.str();
312 PyErr_SetString(exc_type, message.c_str());
316 #endif // HAVE_PYTHON
330 unpack_args(
DCPacker &packer)
const {
335 PyObject *
object = packer.unpack_object();
348 PyObject *exc_type = PyExc_Exception;
351 strm <<
"Data error unpacking field ";
354 strm <<
"\nGot data (" << (int)length <<
" bytes):\n";
358 strm <<
"Error detected on byte " << error_byte
359 <<
" (" << hex << error_byte << dec <<
" hex)";
361 exc_type = PyExc_RuntimeError;
363 strm <<
"Value outside specified range when unpacking field "
364 <<
get_name() <<
": " << get_pystr(
object);
365 exc_type = PyExc_ValueError;
368 string message = strm.str();
369 PyErr_SetString(exc_type, message.c_str());
375 #endif // HAVE_PYTHON
386 receive_update(
DCPacker &packer, PyObject *distobj)
const {
390 PyObject *value = unpack_args(packer);
391 if (value != (PyObject *)NULL) {
392 PyObject_SetAttrString(distobj, (
char *)_name.c_str(), value);
400 if (!PyObject_HasAttrString(distobj, (
char *)_name.c_str())) {
409 PyObject *args = unpack_args(packer);
411 if (args != (PyObject *)NULL) {
412 PyObject *func = PyObject_GetAttrString(distobj, (
char *)_name.c_str());
413 nassertv(func != (PyObject *)NULL);
420 result = PyObject_CallObject(func, args);
429 #endif // HAVE_PYTHON
440 client_format_update(DOID_TYPE do_id, PyObject *args)
const {
448 pack_args(packer, args);
455 #endif // HAVE_PYTHON
466 ai_format_update(DOID_TYPE do_id, CHANNEL_TYPE to_id, CHANNEL_TYPE from_id, PyObject *args)
const {
470 packer.RAW_PACK_CHANNEL(to_id);
471 packer.RAW_PACK_CHANNEL(from_id);
477 pack_args(packer, args);
484 #endif // HAVE_PYTHON
496 ai_format_update_msg_type(DOID_TYPE do_id, CHANNEL_TYPE to_id, CHANNEL_TYPE from_id,
int msg_type, PyObject *args)
const {
500 packer.RAW_PACK_CHANNEL(to_id);
501 packer.RAW_PACK_CHANNEL(from_id);
507 pack_args(packer, args);
514 #endif // HAVE_PYTHON
533 if (dc_multiple_inheritance) {
552 if (!_default_value_stale) {
553 pack_data.
append_data(_default_value.data(), _default_value.length());
568 if (_dclass != (
DCClass *)NULL) {
581 get_pystr(PyObject *value) {
586 PyObject *str = PyObject_Str(value);
588 #if PY_MAJOR_VERSION >= 3
589 string result = PyUnicode_AsUTF8(str);
591 string result = PyString_AsString(str);
597 PyObject *repr = PyObject_Repr(value);
599 #if PY_MAJOR_VERSION >= 3
600 string result = PyUnicode_AsUTF8(repr);
602 string result = PyString_AsString(repr);
608 if (value->ob_type != NULL) {
609 PyObject *typestr = PyObject_Str((PyObject *)(value->ob_type));
610 if (typestr != NULL) {
611 #if PY_MAJOR_VERSION >= 3
612 string result = PyUnicode_AsUTF8(typestr);
614 string result = PyString_AsString(typestr);
621 return "(invalid object)";
623 #endif // HAVE_PYTHON
632 refresh_default_value() {
637 cerr <<
"Error while packing default value for " <<
get_name() <<
"\n";
641 _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.
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this field into the hash.
const DCPackerInterface * get_current_field() const
Returns the field that will be referenced by the next call to pack_*() or unpack_*().
const string & get_name() const
Returns the name of this field, or empty string if the field is unnamed.
This is a block of data that receives the results of DCPacker.
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...
bool had_error() const
Returns true if there has been any error (either a pack error or a range error) since the most recent...
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.
A single field of a Distributed Class, either atomic or molecular.
void raw_pack_uint8(unsigned int value)
Packs the data into the buffer between packing sessions.
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.
A lightweight class that can be used to automatically start and stop a PStatCollector around a sectio...
bool has_assert_failed() const
Returns true if an assertion test has failed (and not been ignored) since the last call to clear_asse...
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...
size_t get_length() const
Returns the current length of the buffer.
void raw_pack_uint32(unsigned int value)
Packs the data into the buffer between packing sessions.
static Notify * ptr()
Returns the pointer to the global Notify object.
size_t get_unpack_length() const
Returns the total number of bytes in the unpack data buffer.
Represents the type specification for a single parameter within a field specification.
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...
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.
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...
string get_string() const
Returns the packed data buffer as a string.
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.
const char * get_data() const
Returns the beginning of the data buffer.
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.
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.
bool end_pack()
Finishes a packing session.
void set_unpack_data(const string &data)
Sets up the unpack_data pointer.
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.
const char * get_unpack_data() const
Returns a read pointer to the unpack data buffer.
bool had_pack_error() const
Returns true if there has been an packing error since the most recent call to begin(); in particular...
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...