Panda3D
|
This represents a switch statement, which can appear inside a class body and represents two or more alternative unpacking schemes based on the first field read. More...
#include "dcSwitch.h"
Classes | |
class | SwitchCase |
class | SwitchFields |
Public Types | |
typedef pvector< DCField * > | Fields |
typedef pmap< string, DCField * > | FieldsByName |
Public Member Functions | |
DCSwitch (const string &name, DCField *key_parameter) | |
The key_parameter must be recently allocated via new; it will be deleted via delete when the switch destructs. | |
void | add_break () |
Adds a break statement to the switch. | |
int | add_case (const string &value) |
Adds a new case to the switch with the indicated value, and returns the new case_index. | |
bool | add_default () |
Adds a default case to the switch. | |
bool | add_field (DCField *field) |
Adds a field to the currently active cases (those that have been added via add_case() or add_default(), since the last call to add_break()). | |
void | add_invalid_case () |
Adds a new case to the switch that will never be matched. | |
const DCPackerInterface * | apply_switch (const char *value_data, size_t length) const |
Returns the DCPackerInterface that presents the alternative fields for the case indicated by the given packed value string, or NULL if the value string does not match one of the expected cases. | |
virtual DCSwitch * | as_switch () |
virtual const DCSwitch * | as_switch () const |
bool | do_check_match_switch (const DCSwitch *other) const |
Returns true if this switch matches the indicated other switch--that is, the two switches are bitwise equivalent--false otherwise. | |
virtual void | generate_hash (HashGenerator &hashgen) const |
Accumulates the properties of this switch into the hash. | |
DCPackerInterface * | get_case (int n) const |
Returns the DCPackerInterface that packs the nth case. | |
int | get_case_by_value (const string &case_value) const |
Returns the index number of the case with the indicated packed value, or -1 if no case has this value. | |
DCPackerInterface * | get_default_case () const |
Returns the DCPackerInterface that packs the default case, or NULL if there is no default case. | |
DCField * | get_field (int case_index, int n) const |
Returns the nth field in the indicated case. | |
DCField * | get_field_by_name (int case_index, const string &name) const |
Returns the field with the given name from the indicated case, or NULL if no field has this name. | |
DCField * | get_key_parameter () const |
Returns the key parameter on which the switch is based. | |
const string & | get_name () const |
Returns the name of this switch. | |
int | get_num_cases () const |
Returns the number of different cases within the switch. | |
int | get_num_fields (int case_index) const |
Returns the number of fields in the indicated case. | |
string | get_value (int case_index) const |
Returns the packed value associated with the indicated case. | |
bool | is_field_valid () const |
Returns true if it is valid to add a new field at this point (implying that a case or default has been added already), or false if not. | |
virtual void | output (ostream &out, bool brief) const |
Write a string representation of this instance to <out>. | |
void | output_instance (ostream &out, bool brief, const string &prename, const string &name, const string &postname) const |
Generates a parseable description of the object to the indicated output stream. | |
virtual bool | pack_default_value (DCPackData &pack_data, bool &pack_error) const |
Packs the switchParameter's specified default value (or a sensible default if no value is specified) into the stream. | |
virtual void | write (ostream &out, bool brief, int indent_level) const |
Generates a parseable description of the object to the indicated output stream. | |
void | write_instance (ostream &out, bool brief, int indent_level, const string &prename, const string &name, const string &postname) const |
Generates a parseable description of the object to the indicated output stream. |
This represents a switch statement, which can appear inside a class body and represents two or more alternative unpacking schemes based on the first field read.
Definition at line 33 of file dcSwitch.h.
DCSwitch::DCSwitch | ( | const string & | name, |
DCField * | key_parameter | ||
) |
The key_parameter must be recently allocated via new; it will be deleted via delete when the switch destructs.
Definition at line 30 of file dcSwitch.cxx.
void DCSwitch::add_break | ( | ) |
Adds a break statement to the switch.
This closes the currently open cases and prepares for a new, unrelated case.
Definition at line 323 of file dcSwitch.cxx.
int DCSwitch::add_case | ( | const string & | value | ) |
Adds a new case to the switch with the indicated value, and returns the new case_index.
If the value has already been used for another case, returns -1. This is normally called only by the parser.
Definition at line 238 of file dcSwitch.cxx.
References add_invalid_case().
bool DCSwitch::add_default | ( | ) |
Adds a default case to the switch.
Returns true if the case is successfully added, or false if it had already been added. This is normally called only by the parser.
Definition at line 274 of file dcSwitch.cxx.
References add_invalid_case().
bool DCSwitch::add_field | ( | DCField * | field | ) |
Adds a field to the currently active cases (those that have been added via add_case() or add_default(), since the last call to add_break()).
Returns true if successful, false if the field duplicates a field already named within this case. It is an error to call this before calling add_case() or add_default(). This is normally called only by the parser.
Definition at line 297 of file dcSwitch.cxx.
References DCSwitch::SwitchFields::add_field().
void DCSwitch::add_invalid_case | ( | ) |
Adds a new case to the switch that will never be matched.
This is only used by the parser, to handle an error condition more gracefully without bitching the parsing (which behaves differently according to whether a case has been encountered or not).
Definition at line 261 of file dcSwitch.cxx.
Referenced by add_case(), and add_default().
const DCPackerInterface * DCSwitch::apply_switch | ( | const char * | value_data, |
size_t | length | ||
) | const |
Returns the DCPackerInterface that presents the alternative fields for the case indicated by the given packed value string, or NULL if the value string does not match one of the expected cases.
Definition at line 337 of file dcSwitch.cxx.
bool DCSwitch::do_check_match_switch | ( | const DCSwitch * | other | ) | const |
Returns true if this switch matches the indicated other switch--that is, the two switches are bitwise equivalent--false otherwise.
This is only intended to be called internally from DCSwitchParameter::do_check_match_switch_parameter().
Definition at line 570 of file dcSwitch.cxx.
References DCPackerInterface::check_match(), and DCSwitch::SwitchCase::do_check_match_switch_case().
void DCSwitch::generate_hash | ( | HashGenerator & | hashgen | ) | const [virtual] |
Accumulates the properties of this switch into the hash.
Definition at line 480 of file dcSwitch.cxx.
References HashGenerator::add_int(), HashGenerator::add_string(), and DCField::generate_hash().
Referenced by DCSwitchParameter::generate_hash().
DCPackerInterface * DCSwitch::get_case | ( | int | n | ) | const |
Returns the DCPackerInterface that packs the nth case.
Definition at line 146 of file dcSwitch.cxx.
int DCSwitch::get_case_by_value | ( | const string & | case_value | ) | const |
Returns the index number of the case with the indicated packed value, or -1 if no case has this value.
Definition at line 130 of file dcSwitch.cxx.
DCPackerInterface * DCSwitch::get_default_case | ( | ) | const |
Returns the DCPackerInterface that packs the default case, or NULL if there is no default case.
Definition at line 158 of file dcSwitch.cxx.
DCField * DCSwitch::get_field | ( | int | case_index, |
int | n | ||
) | const |
Returns the nth field in the indicated case.
Definition at line 191 of file dcSwitch.cxx.
DCField * DCSwitch::get_field_by_name | ( | int | case_index, |
const string & | name | ||
) | const |
Returns the field with the given name from the indicated case, or NULL if no field has this name.
Definition at line 204 of file dcSwitch.cxx.
DCField * DCSwitch::get_key_parameter | ( | ) | const |
Returns the key parameter on which the switch is based.
The value of this parameter in the record determines which one of the several cases within the switch will be used.
Definition at line 106 of file dcSwitch.cxx.
const string & DCSwitch::get_name | ( | ) | const |
Returns the name of this switch.
Definition at line 93 of file dcSwitch.cxx.
Referenced by DCFile::add_switch().
int DCSwitch::get_num_cases | ( | ) | const |
Returns the number of different cases within the switch.
The legal values for case_index range from 0 to get_num_cases() - 1.
Definition at line 118 of file dcSwitch.cxx.
int DCSwitch::get_num_fields | ( | int | case_index | ) | const |
Returns the number of fields in the indicated case.
Definition at line 180 of file dcSwitch.cxx.
string DCSwitch::get_value | ( | int | case_index | ) | const |
Returns the packed value associated with the indicated case.
Definition at line 169 of file dcSwitch.cxx.
bool DCSwitch::is_field_valid | ( | ) | const |
Returns true if it is valid to add a new field at this point (implying that a case or default has been added already), or false if not.
Definition at line 225 of file dcSwitch.cxx.
void DCSwitch::output | ( | ostream & | out, |
bool | brief | ||
) | const [virtual] |
Write a string representation of this instance to <out>.
Implements DCDeclaration.
Definition at line 360 of file dcSwitch.cxx.
References output_instance().
void DCSwitch::output_instance | ( | ostream & | out, |
bool | brief, | ||
const string & | prename, | ||
const string & | name, | ||
const string & | postname | ||
) | const |
Generates a parseable description of the object to the indicated output stream.
Definition at line 382 of file dcSwitch.cxx.
References DCField::format_data(), and DCField::output().
Referenced by output().
bool DCSwitch::pack_default_value | ( | DCPackData & | pack_data, |
bool & | pack_error | ||
) | const [virtual] |
Packs the switchParameter's specified default value (or a sensible default if no value is specified) into the stream.
Returns true if the default value is packed, false if the switchParameter doesn't know how to pack its default value.
Definition at line 519 of file dcSwitch.cxx.
References DCPackData::append_data(), DCPacker::begin_pack(), DCPacker::end_pack(), DCPacker::get_data(), DCPacker::get_length(), DCPacker::pack_default_value(), and DCPacker::pack_literal_value().
Referenced by DCSwitchParameter::pack_default_value().
void DCSwitch::write | ( | ostream & | out, |
bool | brief, | ||
int | indent_level | ||
) | const [virtual] |
Generates a parseable description of the object to the indicated output stream.
Implements DCDeclaration.
Definition at line 371 of file dcSwitch.cxx.
References write_instance().
void DCSwitch::write_instance | ( | ostream & | out, |
bool | brief, | ||
int | indent_level, | ||
const string & | prename, | ||
const string & | name, | ||
const string & | postname | ||
) | const |
Generates a parseable description of the object to the indicated output stream.
Definition at line 428 of file dcSwitch.cxx.
References DCField::format_data(), and DCField::output().
Referenced by write().