Panda3D
dcKeywordList.cxx
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 dcKeywordList.cxx
10  * @author drose
11  * @date 2005-07-25
12  */
13 
14 #include "dcKeywordList.h"
15 #include "dcKeyword.h"
16 #include "hashGenerator.h"
17 
18 /**
19  *
20  */
21 DCKeywordList::
22 DCKeywordList() :
23  _flags(0)
24 {
25 }
26 
27 /**
28  *
29  */
30 DCKeywordList::
31 DCKeywordList(const DCKeywordList &copy) :
32  _keywords(copy._keywords),
33  _keywords_by_name(copy._keywords_by_name),
34  _flags(copy._flags)
35 {
36 }
37 
38 /**
39  *
40  */
41 void DCKeywordList::
42 operator = (const DCKeywordList &copy) {
43  _keywords = copy._keywords;
44  _keywords_by_name = copy._keywords_by_name;
45  _flags = copy._flags;
46 }
47 
48 /**
49  *
50  */
51 DCKeywordList::
52 ~DCKeywordList() {
53  nassertv(_keywords_by_name.size() == _keywords.size());
54 }
55 
56 /**
57  * Returns true if this list includes the indicated keyword, false otherwise.
58  */
59 bool DCKeywordList::
60 has_keyword(const std::string &name) const {
61  return (_keywords_by_name.find(name) != _keywords_by_name.end());
62 }
63 
64 /**
65  * Returns true if this list includes the indicated keyword, false otherwise.
66  */
67 bool DCKeywordList::
68 has_keyword(const DCKeyword *keyword) const {
69  return has_keyword(keyword->get_name());
70 }
71 
72 /**
73  * Returns the number of keywords in the list.
74  */
77  nassertr(_keywords_by_name.size() == _keywords.size(), 0);
78  return _keywords.size();
79 }
80 
81 /**
82  * Returns the nth keyword in the list.
83  */
85 get_keyword(int n) const {
86  nassertr(n >= 0 && n < (int)_keywords.size(), nullptr);
87  return _keywords[n];
88 }
89 
90 /**
91  * Returns the keyword in the list with the indicated name, or NULL if there
92  * is no keyword in the list with that name.
93  */
95 get_keyword_by_name(const std::string &name) const {
96  KeywordsByName::const_iterator ni;
97  ni = _keywords_by_name.find(name);
98  if (ni != _keywords_by_name.end()) {
99  return (*ni).second;
100  }
101 
102  return nullptr;
103 }
104 
105 /**
106  * Returns true if this list has the same keywords as the other list, false if
107  * some keywords differ. Order is not considered important.
108  */
109 bool DCKeywordList::
110 compare_keywords(const DCKeywordList &other) const {
111  return _keywords_by_name == other._keywords_by_name;
112 }
113 
114 /**
115  * Replaces this keyword list with those from the other list.
116  */
117 void DCKeywordList::
119  (*this) = other;
120 }
121 
122 /**
123  * Adds the indicated keyword to the list. Returns true if it is added, false
124  * if it was already there.
125  */
126 bool DCKeywordList::
127 add_keyword(const DCKeyword *keyword) {
128  bool inserted = _keywords_by_name.insert(KeywordsByName::value_type(keyword->get_name(), keyword)).second;
129  if (inserted) {
130  _keywords.push_back(keyword);
131  _flags |= keyword->get_historical_flag();
132  }
133 
134  return inserted;
135 }
136 
137 /**
138  * Removes all keywords from the field.
139  */
140 void DCKeywordList::
142  _keywords.clear();
143  _keywords_by_name.clear();
144  _flags = 0;
145 }
146 
147 /**
148  *
149  */
150 void DCKeywordList::
151 output_keywords(std::ostream &out) const {
152  Keywords::const_iterator ki;
153  for (ki = _keywords.begin(); ki != _keywords.end(); ++ki) {
154  out << " " << (*ki)->get_name();
155  }
156 }
157 
158 /**
159  * Accumulates the properties of these keywords into the hash.
160  */
161 void DCKeywordList::
162 generate_hash(HashGenerator &hashgen) const {
163  if (_flags != ~0) {
164  // All of the flags are historical flags only, so add just the flags
165  // bitmask to keep the hash code the same as it has historically been.
166  hashgen.add_int(_flags);
167 
168  } else {
169  // There is at least one custom flag, so go ahead and make the hash code
170  // reflect it.
171 
172  hashgen.add_int(_keywords_by_name.size());
173  KeywordsByName::const_iterator ni;
174  for (ni = _keywords_by_name.begin(); ni != _keywords_by_name.end(); ++ni) {
175  (*ni).second->generate_hash(hashgen);
176  }
177  }
178 }
This represents a single keyword declaration in the dc file.
Definition: dcKeyword.h:28
This is a list of keywords (see DCKeyword) that may be set on a particular field.
Definition: dcKeywordList.h:26
const DCKeyword * get_keyword(int n) const
Returns the nth keyword in the list.
void add_int(int num)
Adds another integer to the hash so far.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
const std::string & get_name() const
Returns the name of this keyword.
Definition: dcKeyword.cxx:39
const DCKeyword * get_keyword_by_name(const std::string &name) const
Returns the keyword in the list with the indicated name, or NULL if there is no keyword in the list w...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_num_keywords() const
Returns the number of keywords in the list.
bool has_keyword(const std::string &name) const
Returns true if this list includes the indicated keyword, false otherwise.
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:24
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.
void clear_keywords()
Removes all keywords from the field.
void generate_hash(HashGenerator &hashgen) const
Accumulates the properties of these keywords into the hash.
int get_historical_flag() const
Returns the bitmask associated with this keyword, if any.
Definition: dcKeyword.cxx:50