Panda3D

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
35 PUBLISHED:
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
Definition: dcSwitch.h:99
DCSwitch::SwitchFields
Definition: dcSwitch.h:78
DCSwitch
This represents a switch statement, which can appear inside a class body and represents two or more a...
Definition: dcSwitch.h:30
HashGenerator
This class generates an arbitrary hash number from a sequence of ints.
Definition: hashGenerator.h:23
pmap
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
pvector
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
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.