Panda3D

dcKeywordList.cxx

00001 // Filename: dcKeywordList.cxx
00002 // Created by:  drose (25Jul05)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "dcKeywordList.h"
00016 #include "dcKeyword.h"
00017 #include "hashGenerator.h"
00018 
00019 ////////////////////////////////////////////////////////////////////
00020 //     Function: DCKeywordList::Constructor
00021 //       Access: Public
00022 //  Description: 
00023 ////////////////////////////////////////////////////////////////////
00024 DCKeywordList::
00025 DCKeywordList() :
00026   _flags(0)
00027 {
00028 }
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: DCKeywordList::Copy Constructor
00032 //       Access: Public
00033 //  Description: 
00034 ////////////////////////////////////////////////////////////////////
00035 DCKeywordList::
00036 DCKeywordList(const DCKeywordList &copy) :
00037   _keywords(copy._keywords),
00038   _keywords_by_name(copy._keywords_by_name),
00039   _flags(copy._flags)
00040 {
00041 }
00042 
00043 ////////////////////////////////////////////////////////////////////
00044 //     Function: DCKeywordList::Copy Assignment Operator
00045 //       Access: Public
00046 //  Description: 
00047 ////////////////////////////////////////////////////////////////////
00048 void DCKeywordList::
00049 operator = (const DCKeywordList &copy) {
00050   _keywords = copy._keywords;
00051   _keywords_by_name = copy._keywords_by_name;
00052   _flags = copy._flags;
00053 }
00054 
00055 ////////////////////////////////////////////////////////////////////
00056 //     Function: DCKeywordList::Destructor
00057 //       Access: Public
00058 //  Description:
00059 ////////////////////////////////////////////////////////////////////
00060 DCKeywordList::
00061 ~DCKeywordList() {
00062   nassertv(_keywords_by_name.size() == _keywords.size());
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: DCKeywordList::has_keyword
00067 //       Access: Published
00068 //  Description: Returns true if this list includes the indicated
00069 //               keyword, false otherwise.
00070 ////////////////////////////////////////////////////////////////////
00071 bool DCKeywordList::
00072 has_keyword(const string &name) const {
00073   return (_keywords_by_name.find(name) != _keywords_by_name.end());
00074 }
00075 
00076 ////////////////////////////////////////////////////////////////////
00077 //     Function: DCKeywordList::has_keyword
00078 //       Access: Published
00079 //  Description: Returns true if this list includes the indicated
00080 //               keyword, false otherwise.
00081 ////////////////////////////////////////////////////////////////////
00082 bool DCKeywordList::
00083 has_keyword(const DCKeyword *keyword) const {
00084   return has_keyword(keyword->get_name());
00085 }
00086 
00087 ////////////////////////////////////////////////////////////////////
00088 //     Function: DCKeywordList::get_num_keywords
00089 //       Access: Published
00090 //  Description: Returns the number of keywords in the list.
00091 ////////////////////////////////////////////////////////////////////
00092 int DCKeywordList::
00093 get_num_keywords() const {
00094   nassertr(_keywords_by_name.size() == _keywords.size(), 0);
00095   return _keywords.size();
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: DCKeywordList::get_keyword
00100 //       Access: Published
00101 //  Description: Returns the nth keyword in the list.
00102 ////////////////////////////////////////////////////////////////////
00103 const DCKeyword *DCKeywordList::
00104 get_keyword(int n) const {
00105   nassertr(n >= 0 && n < (int)_keywords.size(), NULL);
00106   return _keywords[n];
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: DCKeywordList::get_keyword_by_name
00111 //       Access: Published
00112 //  Description: Returns the keyword in the list with the indicated
00113 //               name, or NULL if there is no keyword in the list with
00114 //               that name.
00115 ////////////////////////////////////////////////////////////////////
00116 const DCKeyword *DCKeywordList::
00117 get_keyword_by_name(const string &name) const {
00118   KeywordsByName::const_iterator ni;
00119   ni = _keywords_by_name.find(name);
00120   if (ni != _keywords_by_name.end()) {
00121     return (*ni).second;
00122   }
00123 
00124   return NULL;
00125 }
00126 
00127 ////////////////////////////////////////////////////////////////////
00128 //     Function: DCKeywordList::compare_keywords
00129 //       Access: Published
00130 //  Description: Returns true if this list has the same keywords
00131 //               as the other list, false if some keywords differ.
00132 //               Order is not considered important.
00133 ////////////////////////////////////////////////////////////////////
00134 bool DCKeywordList::
00135 compare_keywords(const DCKeywordList &other) const {
00136   return _keywords_by_name == other._keywords_by_name;
00137 }
00138 
00139 ////////////////////////////////////////////////////////////////////
00140 //     Function: DCKeywordList::copy_keywords
00141 //       Access: Public
00142 //  Description: Replaces this keyword list with those from the other
00143 //               list.
00144 ////////////////////////////////////////////////////////////////////
00145 void DCKeywordList::
00146 copy_keywords(const DCKeywordList &other) {
00147   (*this) = other;
00148 }
00149 
00150 ////////////////////////////////////////////////////////////////////
00151 //     Function: DCKeywordList::add_keyword
00152 //       Access: Public
00153 //  Description: Adds the indicated keyword to the list.  Returns true
00154 //               if it is added, false if it was already there.
00155 ////////////////////////////////////////////////////////////////////
00156 bool DCKeywordList::
00157 add_keyword(const DCKeyword *keyword) {
00158   bool inserted = _keywords_by_name.insert(KeywordsByName::value_type(keyword->get_name(), keyword)).second;
00159   if (inserted) {
00160     _keywords.push_back(keyword);
00161     _flags |= keyword->get_historical_flag();
00162   }
00163 
00164   return inserted;
00165 }
00166 
00167 ////////////////////////////////////////////////////////////////////
00168 //     Function: DCKeywordList::clear_keywords
00169 //       Access: Public
00170 //  Description: Removes all keywords from the field.
00171 ////////////////////////////////////////////////////////////////////
00172 void DCKeywordList::
00173 clear_keywords() {
00174   _keywords.clear();
00175   _keywords_by_name.clear();
00176   _flags = 0;
00177 }
00178 
00179 ////////////////////////////////////////////////////////////////////
00180 //     Function: DCKeywordList::output_keywords
00181 //       Access: Public
00182 //  Description: 
00183 ////////////////////////////////////////////////////////////////////
00184 void DCKeywordList::
00185 output_keywords(ostream &out) const {
00186   Keywords::const_iterator ki;
00187   for (ki = _keywords.begin(); ki != _keywords.end(); ++ki) {
00188     out << " " << (*ki)->get_name();
00189   }
00190 }
00191 
00192 ////////////////////////////////////////////////////////////////////
00193 //     Function: DCKeywordList::generate_hash
00194 //       Access: Public
00195 //  Description: Accumulates the properties of these keywords into the
00196 //               hash.
00197 ////////////////////////////////////////////////////////////////////
00198 void DCKeywordList::
00199 generate_hash(HashGenerator &hashgen) const {
00200   if (_flags != ~0) {
00201     // All of the flags are historical flags only, so add just the
00202     // flags bitmask to keep the hash code the same as it has
00203     // historically been.
00204     hashgen.add_int(_flags);
00205 
00206   } else {
00207     // There is at least one custom flag, so go ahead and make the
00208     // hash code reflect it.
00209 
00210     hashgen.add_int(_keywords_by_name.size());
00211     KeywordsByName::const_iterator ni;
00212     for (ni = _keywords_by_name.begin(); ni != _keywords_by_name.end(); ++ni) {
00213       (*ni).second->generate_hash(hashgen);
00214     }
00215   }
00216 }
 All Classes Functions Variables Enumerations