Panda3D

Loading...
Searching...
No Matches
dcSwitch.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file dcSwitch.h
10 * @author drose
11 * @date 2004-06-23
12 */
13
14#ifndef DCSWITCH_H
15#define DCSWITCH_H
16
17#include "dcbase.h"
18#include "dcDeclaration.h"
19#include "dcPackerInterface.h"
20
21 class DCParameter;
22 class HashGenerator;
23 class DCField;
24
25/**
26 * This represents a switch statement, which can appear inside a class body
27 * and represents two or more alternative unpacking schemes based on the first
28 * field read.
29 */
30 class EXPCL_DIRECT_DCPARSER DCSwitch : public DCDeclaration {
31 public:
32 DCSwitch(const std::string &name, DCField *key_parameter);
33 virtual ~DCSwitch();
34
35PUBLISHED:
36 virtual DCSwitch *as_switch();
37 virtual const DCSwitch *as_switch() const;
38
39 const std::string &get_name() const;
40 DCField *get_key_parameter() const;
41
42 int get_num_cases() const;
43 int get_case_by_value(const vector_uchar &case_value) const;
44 DCPackerInterface *get_case(int n) const;
45 DCPackerInterface *get_default_case() const;
46
47 vector_uchar get_value(int case_index) const;
48 int get_num_fields(int case_index) const;
49 DCField *get_field(int case_index, int n) const;
50 DCField *get_field_by_name(int case_index, const std::string &name) const;
51
52 public:
53 bool is_field_valid() const;
54 int add_case(const vector_uchar &value);
55 void add_invalid_case();
56 bool add_default();
57 bool add_field(DCField *field);
58 void add_break();
59
60 const DCPackerInterface *apply_switch(const char *value_data, size_t length) const;
61
62 virtual void output(std::ostream &out, bool brief) const;
63 virtual void write(std::ostream &out, bool brief, int indent_level) const;
64 void output_instance(std::ostream &out, bool brief, const std::string &prename,
65 const std::string &name, const std::string &postname) const;
66 void write_instance(std::ostream &out, bool brief, int indent_level,
67 const std::string &prename, const std::string &name,
68 const std::string &postname) const;
69 virtual void generate_hash(HashGenerator &hashgen) const;
70 virtual bool pack_default_value(DCPackData &pack_data, bool &pack_error) const;
71
72 bool do_check_match_switch(const DCSwitch *other) const;
73
74 public:
75 typedef pvector<DCField *> Fields;
76 typedef pmap<std::string, DCField *> FieldsByName;
77
78 class SwitchFields : public DCPackerInterface {
79 public:
80 SwitchFields(const std::string &name);
81 ~SwitchFields();
82 virtual DCPackerInterface *get_nested_field(int n) const;
83
84 bool add_field(DCField *field);
85 bool do_check_match_switch_case(const SwitchFields *other) const;
86
87 void output(std::ostream &out, bool brief) const;
88 void write(std::ostream &out, bool brief, int indent_level) const;
89
90 protected:
91 virtual bool do_check_match(const DCPackerInterface *other) const;
92
93 public:
94 Fields _fields;
95 FieldsByName _fields_by_name;
96 bool _has_default_value;
97 };
98
99 class SwitchCase {
100 public:
101 SwitchCase(const vector_uchar &value, SwitchFields *fields);
102 ~SwitchCase();
103
104 bool do_check_match_switch_case(const SwitchCase *other) const;
105
106 public:
107 vector_uchar _value;
108 SwitchFields *_fields;
109 };
110
111 private:
112 SwitchFields *start_new_case();
113
114 private:
115 std::string _name;
116 DCField *_key_parameter;
117
118 typedef pvector<SwitchCase *> Cases;
119 Cases _cases;
120 SwitchFields *_default_case;
121
122 // All SwitchFields created and used by the DCSwitch object are also stored
123 // here; this is the vector that "owns" the pointers.
124 typedef pvector<SwitchFields *> CaseFields;
125 CaseFields _case_fields;
126
127 // All nested DCField objects that have been added to one or more of the
128 // above SwitchFields are also recorded here; this is the vector that "owns"
129 // these pointers.
130 Fields _nested_fields;
131
132 // These are the SwitchFields that are currently being filled up during this
133 // stage of the parser. There might be more than one at a time, if we have
134 // multiple cases being introduced in the middle of a series of fields
135 // (without a break statement intervening).
136 CaseFields _current_fields;
137 bool _fields_added;
138
139 // This map indexes into the _cases vector, above.
140 typedef pmap<vector_uchar, int> CasesByValue;
141 CasesByValue _cases_by_value;
142};
143
144#endif
DCDeclaration
This is a common interface for a declaration in a DC file.
Definition dcDeclaration.h:29
DCDeclaration::output
virtual void output(std::ostream &out) const
Write a string representation of this instance to <out>.
Definition dcDeclaration.cxx:60
DCDeclaration::write
void write(std::ostream &out, int indent_level) const
Write a string representation of this instance to <out>.
Definition dcDeclaration.cxx:68
DCField
A single field of a Distributed Class, either atomic or molecular.
Definition dcField.h:37
DCPackData
This is a block of data that receives the results of DCPacker.
Definition dcPackData.h:22
DCPackerInterface
This defines the internal interface for packing values into a DCField.
Definition dcPackerInterface.h:67
DCParameter
Represents the type specification for a single parameter within a field specification.
Definition dcParameter.h:35
DCSwitch::SwitchCase::do_check_match_switch_case
bool do_check_match_switch_case(const SwitchCase *other) const
Returns true if this case matches the indicated case, false otherwise.
Definition dcSwitch.cxx:725
DCSwitch::SwitchFields
Definition dcSwitch.h:78
DCSwitch::SwitchFields::add_field
bool add_field(DCField *field)
Adds a field to this case.
Definition dcSwitch.cxx:604
DCSwitch::SwitchFields::do_check_match_switch_case
bool do_check_match_switch_case(const SwitchFields *other) const
Returns true if this case matches the indicated case, false otherwise.
Definition dcSwitch.cxx:641
DCSwitch::SwitchFields::get_nested_field
virtual DCPackerInterface * get_nested_field(int n) const
Returns the DCPackerInterface object that represents the nth nested field.
Definition dcSwitch.cxx:593
DCSwitch::get_name
const std::string & get_name() const
Returns the name of this switch.
Definition dcSwitch.cxx:84
DCSwitch::get_case
DCPackerInterface * get_case(int n) const
Returns the DCPackerInterface that packs the nth case.
Definition dcSwitch.cxx:126
DCSwitch::get_num_fields
int get_num_fields(int case_index) const
Returns the number of fields in the indicated case.
Definition dcSwitch.cxx:153
DCSwitch::add_default
bool add_default()
Adds a default case to the switch.
Definition dcSwitch.cxx:231
DCSwitch::get_field
DCField * get_field(int case_index, int n) const
Returns the nth field in the indicated case.
Definition dcSwitch.cxx:162
DCSwitch::get_case_by_value
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...
Definition dcSwitch.cxx:112
DCSwitch::get_value
std::vector< unsigned char > get_value(int case_index) const
Returns the packed value associated with the indicated case.
Definition dcSwitch.cxx:144
DCSwitch::add_field
bool add_field(DCField *field)
Adds a field to the currently active cases (those that have been added via add_case() or add_default(...
Definition dcSwitch.cxx:250
DCSwitch::is_field_valid
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 bee...
Definition dcSwitch.cxx:191
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 d...
Definition dcSwitch.cxx:29
DCSwitch::add_break
void add_break()
Adds a break statement to the switch.
Definition dcSwitch.cxx:273
DCSwitch::get_num_cases
int get_num_cases() const
Returns the number of different cases within the switch.
Definition dcSwitch.cxx:103
DCSwitch::write_instance
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.
Definition dcSwitch.cxx:367
DCSwitch::add_invalid_case
void add_invalid_case()
Adds a new case to the switch that will never be matched.
Definition dcSwitch.cxx:221
DCSwitch::apply_switch
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 give...
Definition dcSwitch.cxx:284
DCSwitch::output_instance
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.
Definition dcSwitch.cxx:323
DCSwitch::generate_hash
virtual void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of this switch into the hash.
Definition dcSwitch.cxx:416
DCSwitch::get_key_parameter
DCField * get_key_parameter() const
Returns the key parameter on which the switch is based.
Definition dcSwitch.cxx:94
DCSwitch::get_default_case
DCPackerInterface * get_default_case() const
Returns the DCPackerInterface that packs the default case, or NULL if there is no default case.
Definition dcSwitch.cxx:136
DCSwitch::do_check_match_switch
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 ...
Definition dcSwitch.cxx:499
DCSwitch::pack_default_value
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) ...
Definition dcSwitch.cxx:452
DCSwitch::add_case
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.
Definition dcSwitch.cxx:201
DCSwitch::get_field_by_name
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.
Definition dcSwitch.cxx:173
HashGenerator
This class generates an arbitrary hash number from a sequence of ints.
Definition hashGenerator.h:23
dcDeclaration.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dcPackerInterface.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dcbase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.