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