Panda3D
Classes | Public Types | Public Member Functions | List of all members
DCSwitch Class Reference

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"

Inheritance diagram for DCSwitch:
DCDeclaration

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. More...
 
void add_break ()
 Adds a break statement to the switch. More...
 
int add_case (const vector_uchar &value)
 Adds a new case to the switch with the indicated value, and returns the new case_index. More...
 
bool add_default ()
 Adds a default case to the switch. More...
 
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()). More...
 
void add_invalid_case ()
 Adds a new case to the switch that will never be matched. More...
 
const DCPackerInterfaceapply_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. More...
 
virtual DCSwitchas_switch ()
 
virtual const DCSwitchas_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. More...
 
virtual void generate_hash (HashGenerator &hashgen) const
 Accumulates the properties of this switch into the hash. More...
 
DCPackerInterfaceget_case (int n) const
 Returns the DCPackerInterface that packs the nth case. More...
 
int get_case_by_value (const vector_uchar &case_value) const
 Returns the index number of the case with the indicated packed value, or -1 if no case has this value. More...
 
DCPackerInterfaceget_default_case () const
 Returns the DCPackerInterface that packs the default case, or NULL if there is no default case. More...
 
DCFieldget_field (int case_index, int n) const
 Returns the nth field in the indicated case. More...
 
DCFieldget_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. More...
 
DCFieldget_key_parameter () const
 Returns the key parameter on which the switch is based. More...
 
const std::string & get_name () const
 Returns the name of this switch. More...
 
int get_num_cases () const
 Returns the number of different cases within the switch. More...
 
int get_num_fields (int case_index) const
 Returns the number of fields in the indicated case. More...
 
vector_uchar get_value (int case_index) const
 Returns the packed value associated with the indicated case. More...
 
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. More...
 
virtual void output (std::ostream &out, bool brief) const
 Write a string representation of this instance to <out>. More...
 
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. More...
 
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. More...
 
virtual void write (std::ostream &out, bool brief, int indent_level) const
 Generates a parseable description of the object to the indicated output stream. More...
 
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. More...
 
- Public Member Functions inherited from DCDeclaration
virtual DCClassas_class ()
 
virtual const DCClassas_class () const
 
virtual void output (std::ostream &out) const
 Write a string representation of this instance to <out>. More...
 
void write (std::ostream &out, int indent_level) const
 Write a string representation of this instance to <out>. More...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ DCSwitch()

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 29 of file dcSwitch.cxx.

Member Function Documentation

◆ add_break()

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 273 of file dcSwitch.cxx.

◆ add_case()

int DCSwitch::add_case ( const vector_uchar &  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 201 of file dcSwitch.cxx.

References add_invalid_case().

◆ add_default()

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 231 of file dcSwitch.cxx.

References add_invalid_case().

◆ add_field()

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 250 of file dcSwitch.cxx.

◆ add_invalid_case()

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 221 of file dcSwitch.cxx.

Referenced by add_case(), and add_default().

◆ apply_switch()

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 284 of file dcSwitch.cxx.

◆ do_check_match_switch()

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 499 of file dcSwitch.cxx.

References DCPackerInterface::check_match().

◆ generate_hash()

void DCSwitch::generate_hash ( HashGenerator hashgen) const
virtual

Accumulates the properties of this switch into the hash.

Definition at line 416 of file dcSwitch.cxx.

References HashGenerator::add_blob(), HashGenerator::add_int(), HashGenerator::add_string(), and DCField::generate_hash().

◆ get_case()

DCPackerInterface * DCSwitch::get_case ( int  n) const

Returns the DCPackerInterface that packs the nth case.

Definition at line 126 of file dcSwitch.cxx.

◆ get_case_by_value()

int DCSwitch::get_case_by_value ( const vector_uchar &  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 112 of file dcSwitch.cxx.

◆ get_default_case()

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 136 of file dcSwitch.cxx.

◆ get_field()

DCField * DCSwitch::get_field ( int  case_index,
int  n 
) const

Returns the nth field in the indicated case.

Definition at line 162 of file dcSwitch.cxx.

◆ get_field_by_name()

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 173 of file dcSwitch.cxx.

◆ get_key_parameter()

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 94 of file dcSwitch.cxx.

◆ get_name()

const string & DCSwitch::get_name ( ) const

Returns the name of this switch.

Definition at line 84 of file dcSwitch.cxx.

Referenced by DCFile::add_switch().

◆ get_num_cases()

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 103 of file dcSwitch.cxx.

◆ get_num_fields()

int DCSwitch::get_num_fields ( int  case_index) const

Returns the number of fields in the indicated case.

Definition at line 153 of file dcSwitch.cxx.

◆ get_value()

vector_uchar DCSwitch::get_value ( int  case_index) const

Returns the packed value associated with the indicated case.

Definition at line 144 of file dcSwitch.cxx.

◆ is_field_valid()

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 191 of file dcSwitch.cxx.

◆ output()

void DCSwitch::output ( std::ostream &  out,
bool  brief 
) const
virtual

Write a string representation of this instance to <out>.

Implements DCDeclaration.

Definition at line 305 of file dcSwitch.cxx.

References output_instance().

◆ 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 323 of file dcSwitch.cxx.

References DCField::format_data(), and DCField::output().

Referenced by output().

◆ pack_default_value()

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 452 of file dcSwitch.cxx.

References DCPacker::begin_pack(), DCPacker::end_pack(), DCPacker::pack_default_value(), and DCPacker::pack_literal_value().

◆ write()

void DCSwitch::write ( std::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 314 of file dcSwitch.cxx.

References write_instance().

◆ 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 367 of file dcSwitch.cxx.

References DCField::format_data(), indent(), and DCField::output().

Referenced by write().


The documentation for this class was generated from the following files: