Panda3D
 All Classes Functions Variables Enumerations
dcKeywordList.cxx
1 // Filename: dcKeywordList.cxx
2 // Created by: drose (25Jul05)
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 #include "dcKeywordList.h"
16 #include "dcKeyword.h"
17 #include "hashGenerator.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: DCKeywordList::Constructor
21 // Access: Public
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 DCKeywordList::
25 DCKeywordList() :
26  _flags(0)
27 {
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: DCKeywordList::Copy Constructor
32 // Access: Public
33 // Description:
34 ////////////////////////////////////////////////////////////////////
35 DCKeywordList::
36 DCKeywordList(const DCKeywordList &copy) :
37  _keywords(copy._keywords),
38  _keywords_by_name(copy._keywords_by_name),
39  _flags(copy._flags)
40 {
41 }
42 
43 ////////////////////////////////////////////////////////////////////
44 // Function: DCKeywordList::Copy Assignment Operator
45 // Access: Public
46 // Description:
47 ////////////////////////////////////////////////////////////////////
48 void DCKeywordList::
49 operator = (const DCKeywordList &copy) {
50  _keywords = copy._keywords;
51  _keywords_by_name = copy._keywords_by_name;
52  _flags = copy._flags;
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: DCKeywordList::Destructor
57 // Access: Public
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 DCKeywordList::
61 ~DCKeywordList() {
62  nassertv(_keywords_by_name.size() == _keywords.size());
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: DCKeywordList::has_keyword
67 // Access: Published
68 // Description: Returns true if this list includes the indicated
69 // keyword, false otherwise.
70 ////////////////////////////////////////////////////////////////////
71 bool DCKeywordList::
72 has_keyword(const string &name) const {
73  return (_keywords_by_name.find(name) != _keywords_by_name.end());
74 }
75 
76 ////////////////////////////////////////////////////////////////////
77 // Function: DCKeywordList::has_keyword
78 // Access: Published
79 // Description: Returns true if this list includes the indicated
80 // keyword, false otherwise.
81 ////////////////////////////////////////////////////////////////////
82 bool DCKeywordList::
83 has_keyword(const DCKeyword *keyword) const {
84  return has_keyword(keyword->get_name());
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: DCKeywordList::get_num_keywords
89 // Access: Published
90 // Description: Returns the number of keywords in the list.
91 ////////////////////////////////////////////////////////////////////
94  nassertr(_keywords_by_name.size() == _keywords.size(), 0);
95  return _keywords.size();
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: DCKeywordList::get_keyword
100 // Access: Published
101 // Description: Returns the nth keyword in the list.
102 ////////////////////////////////////////////////////////////////////
104 get_keyword(int n) const {
105  nassertr(n >= 0 && n < (int)_keywords.size(), NULL);
106  return _keywords[n];
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: DCKeywordList::get_keyword_by_name
111 // Access: Published
112 // Description: Returns the keyword in the list with the indicated
113 // name, or NULL if there is no keyword in the list with
114 // that name.
115 ////////////////////////////////////////////////////////////////////
117 get_keyword_by_name(const string &name) const {
118  KeywordsByName::const_iterator ni;
119  ni = _keywords_by_name.find(name);
120  if (ni != _keywords_by_name.end()) {
121  return (*ni).second;
122  }
123 
124  return NULL;
125 }
126 
127 ////////////////////////////////////////////////////////////////////
128 // Function: DCKeywordList::compare_keywords
129 // Access: Published
130 // Description: Returns true if this list has the same keywords
131 // as the other list, false if some keywords differ.
132 // Order is not considered important.
133 ////////////////////////////////////////////////////////////////////
134 bool DCKeywordList::
135 compare_keywords(const DCKeywordList &other) const {
136  return _keywords_by_name == other._keywords_by_name;
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function: DCKeywordList::copy_keywords
141 // Access: Public
142 // Description: Replaces this keyword list with those from the other
143 // list.
144 ////////////////////////////////////////////////////////////////////
145 void DCKeywordList::
147  (*this) = other;
148 }
149 
150 ////////////////////////////////////////////////////////////////////
151 // Function: DCKeywordList::add_keyword
152 // Access: Public
153 // Description: Adds the indicated keyword to the list. Returns true
154 // if it is added, false if it was already there.
155 ////////////////////////////////////////////////////////////////////
156 bool DCKeywordList::
157 add_keyword(const DCKeyword *keyword) {
158  bool inserted = _keywords_by_name.insert(KeywordsByName::value_type(keyword->get_name(), keyword)).second;
159  if (inserted) {
160  _keywords.push_back(keyword);
161  _flags |= keyword->get_historical_flag();
162  }
163 
164  return inserted;
165 }
166 
167 ////////////////////////////////////////////////////////////////////
168 // Function: DCKeywordList::clear_keywords
169 // Access: Public
170 // Description: Removes all keywords from the field.
171 ////////////////////////////////////////////////////////////////////
172 void DCKeywordList::
174  _keywords.clear();
175  _keywords_by_name.clear();
176  _flags = 0;
177 }
178 
179 ////////////////////////////////////////////////////////////////////
180 // Function: DCKeywordList::output_keywords
181 // Access: Public
182 // Description:
183 ////////////////////////////////////////////////////////////////////
184 void DCKeywordList::
185 output_keywords(ostream &out) const {
186  Keywords::const_iterator ki;
187  for (ki = _keywords.begin(); ki != _keywords.end(); ++ki) {
188  out << " " << (*ki)->get_name();
189  }
190 }
191 
192 ////////////////////////////////////////////////////////////////////
193 // Function: DCKeywordList::generate_hash
194 // Access: Public
195 // Description: Accumulates the properties of these keywords into the
196 // hash.
197 ////////////////////////////////////////////////////////////////////
198 void DCKeywordList::
199 generate_hash(HashGenerator &hashgen) const {
200  if (_flags != ~0) {
201  // All of the flags are historical flags only, so add just the
202  // flags bitmask to keep the hash code the same as it has
203  // historically been.
204  hashgen.add_int(_flags);
205 
206  } else {
207  // There is at least one custom flag, so go ahead and make the
208  // hash code reflect it.
209 
210  hashgen.add_int(_keywords_by_name.size());
211  KeywordsByName::const_iterator ni;
212  for (ni = _keywords_by_name.begin(); ni != _keywords_by_name.end(); ++ni) {
213  (*ni).second->generate_hash(hashgen);
214  }
215  }
216 }
This represents a single keyword declaration in the dc file.
Definition: dcKeyword.h:31
const DCKeyword * get_keyword(int n) const
Returns the nth keyword in the list.
This is a list of keywords (see DCKeyword) that may be set on a particular field. ...
Definition: dcKeywordList.h:28
int get_num_keywords() const
Returns the number of keywords in the list.
void add_int(int num)
Adds another integer to the hash so far.
bool compare_keywords(const DCKeywordList &other) const
Returns true if this list has the same keywords as the other list, false if some keywords differ...
This class generates an arbitrary hash number from a sequence of ints.
Definition: hashGenerator.h:26
void copy_keywords(const DCKeywordList &other)
Replaces this keyword list with those from the other list.
bool add_keyword(const DCKeyword *keyword)
Adds the indicated keyword to the list.
int get_historical_flag() const
Returns the bitmask associated with this keyword, if any.
Definition: dcKeyword.cxx:61
void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of these keywords into the hash.
void clear_keywords()
Removes all keywords from the field.
const DCKeyword * get_keyword_by_name(const string &name) const
Returns the keyword in the list with the indicated name, or NULL if there is no keyword in the list w...
bool has_keyword(const string &name) const
Returns true if this list includes the indicated keyword, false otherwise.
const string & get_name() const
Returns the name of this keyword.
Definition: dcKeyword.cxx:46