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 std::vector< DCField * > | Fields |
typedef std::map< std::string, DCField * > | FieldsByName |
Public Member Functions | |
DCSwitch (const std::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 std::vector< unsigned char > &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 std::vector< unsigned char > &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 std::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 std::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. | |
std::vector< unsigned char > | 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 (std::ostream &out, bool brief) const |
Write a string representation of this instance to <out>. | |
void | output_instance (std::ostream &out, bool brief, const std::string &prename, const std::string &name, const std::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 (std::ostream &out, bool brief, int indent_level) const |
Generates a parseable description of the object to the indicated output stream. | |
void | write_instance (std::ostream &out, bool brief, int indent_level, const std::string &prename, const std::string &name, const std::string &postname) const |
Generates a parseable description of the object to the indicated output stream. | |
![]() | |
virtual DCClass * | as_class () |
virtual const DCClass * | as_class () const |
virtual void | output (std::ostream &out) const |
Write a string representation of this instance to <out>. | |
void | write (std::ostream &out, int indent_level) const |
Write a string representation of this instance to <out>. | |
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 30 of file dcSwitch.h.
typedef std::vector<DCField *> DCSwitch::Fields |
Definition at line 75 of file dcSwitch.h.
typedef std::map<std::string, DCField *> DCSwitch::FieldsByName |
Definition at line 76 of file dcSwitch.h.
DCSwitch::DCSwitch | ( | const std::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 28 of file dcSwitch.cxx.
|
virtual |
Definition at line 40 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 272 of file dcSwitch.cxx.
int DCSwitch::add_case | ( | const std::vector< unsigned char > & | 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 200 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 230 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 249 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 220 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 283 of file dcSwitch.cxx.
Referenced by DCSwitchParameter::apply_switch().
|
virtual |
Reimplemented from DCDeclaration.
Definition at line 67 of file dcSwitch.cxx.
|
virtual |
Reimplemented from DCDeclaration.
Definition at line 75 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 498 of file dcSwitch.cxx.
References DCPackerInterface::check_match(), and DCSwitch::SwitchCase::do_check_match_switch_case().
|
virtual |
Accumulates the properties of this switch into the hash.
Definition at line 415 of file dcSwitch.cxx.
References HashGenerator::add_blob(), 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 125 of file dcSwitch.cxx.
int DCSwitch::get_case_by_value | ( | const std::vector< unsigned char > & | 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 111 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 135 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 161 of file dcSwitch.cxx.
DCField * DCSwitch::get_field_by_name | ( | int | case_index, |
const std::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 172 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 93 of file dcSwitch.cxx.
Referenced by DCSwitchParameter::get_nested_field().
const string & DCSwitch::get_name | ( | ) | const |
Returns the name of this switch.
Definition at line 83 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 102 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 152 of file dcSwitch.cxx.
std::vector< unsigned char > DCSwitch::get_value | ( | int | case_index | ) | const |
Returns the packed value associated with the indicated case.
Definition at line 143 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 190 of file dcSwitch.cxx.
|
virtual |
Write a string representation of this instance to <out>.
Implements DCDeclaration.
Definition at line 304 of file dcSwitch.cxx.
References output_instance().
void DCSwitch::output_instance | ( | std::ostream & | out, |
bool | brief, | ||
const std::string & | prename, | ||
const std::string & | name, | ||
const std::string & | postname ) const |
Generates a parseable description of the object to the indicated output stream.
Definition at line 322 of file dcSwitch.cxx.
References DCField::format_data(), and DCField::output().
Referenced by output(), and DCSwitchParameter::output_instance().
|
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 451 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().
|
virtual |
Generates a parseable description of the object to the indicated output stream.
Implements DCDeclaration.
Definition at line 313 of file dcSwitch.cxx.
References write_instance().
void DCSwitch::write_instance | ( | std::ostream & | out, |
bool | brief, | ||
int | indent_level, | ||
const std::string & | prename, | ||
const std::string & | name, | ||
const std::string & | postname ) const |
Generates a parseable description of the object to the indicated output stream.
Definition at line 366 of file dcSwitch.cxx.
References DCField::format_data(), indent(), and DCField::output().
Referenced by write(), and DCSwitchParameter::write_instance().