src/engine/KeyNameTable.h

Go to the documentation of this file.
00001 
00010 #ifndef KEYNAMETABLE_H
00011 #define KEYNAMETABLE_H
00012 
00013 #include <vector>
00014 #include <map>
00015 #include <boost/shared_ptr.hpp>
00016 #include "util/Error.h"
00017 
00018 namespace green
00019 {
00020         
00025         typedef size_t Key;
00026 
00030         typedef std::string Name;
00031 
00035         template<class entryType>
00036         class KeyNameTable
00037         {
00038         public:
00041                 virtual ~KeyNameTable() {}
00042                 
00043                 typedef entryType Entry;
00044                 
00046                 size_t GetNumEntries() const {
00047                         return mKeyMap.size();
00048                 }
00049                 
00055                 bool HasEntry(Key key) const {
00056                         return (key < mKeyMap.size() && key >= 0);
00057                 }
00058 
00064                 bool HasEntry(const Name& name) const {
00065                         return (mNameMap.find(name) != mNameMap.end());
00066                 }
00067 
00073                 Key GetKey(const Name& name) const {
00074                         return this->_getEntry(name)->mKey;
00075                 }
00076 
00082                 entryType GetEntry(Key key) const {
00083                         return this->_getEntry(key)->mValue;
00084                 }
00085 
00091                 entryType GetEntry(const Name& name) const {
00092                         return this->_getEntry(name)->mValue;
00093                 }
00094                 
00099                 void SetEntry(Key key, entryType newValue) {
00100                         this->_getEntry(key)->mValue = newValue;
00101                 }
00102                 
00107                 void SetEntry(Name name, entryType newValue) {
00108                         this->_getEntry(name)->mValue = newValue;
00109                 }
00110                 
00117                 virtual Key AddEntry(const Name& name, entryType element)
00118                 {
00119                         if(this->HasEntry(name))
00120                                 THROW_ERROR("element already exists in table");
00121                         if(name == "")
00122                                 THROW_ERROR("name must be at least size 1");
00123                         Key newKey = mKeyMap.size();
00124                         _entryPtr newVar(new _entry(newKey, name, element));
00125                         mKeyMap.push_back(newVar);
00126                         mNameMap[name] = newVar;
00127                         return newKey;
00128                 }
00129                 
00130         protected:
00132                 class _entry 
00133                 {
00134                         public:
00136                                 _entry(Key key, const Name& name, entryType value)
00137                                         : mKey(key), mName(name), mValue(value)
00138                                 {}
00139                                 
00140                                 Key mKey; 
00141                                 Name mName; 
00142                                 entryType mValue; 
00143                                 // TODO: add mValue member
00144                 };
00145                 
00146                 typedef boost::shared_ptr<_entry> _entryPtr;
00147                 
00148         
00150                 typedef std::vector<_entryPtr> _keyMap;
00151                 
00153                 typedef std::map<Name, _entryPtr> _nameMap;
00154                 
00156                 _keyMap mKeyMap;
00157 
00159                 _nameMap mNameMap;
00160                 
00164                 _entryPtr _getEntry(Key key) const
00165                 {
00166                         ERROR_ASSERT(key < mKeyMap.size() && key >= 0, "key does not exist in table");
00167                         return mKeyMap[key];
00168                 }
00169 
00173                 _entryPtr _getEntry(const Name& name) const
00174                 {
00175                         // typename is needed here because _nameMap is a templated
00176                         // typedef which depends on the template parameters, so the
00177                         // keyword typename resolves _nameMap::const_iterator as a type
00178                         // because otherwise it is ambiguous
00179                         typename _nameMap::const_iterator i = mNameMap.find(name);
00180                         if(i == mNameMap.end())
00181                                 THROW_ERROR("name does not exist in table");
00182                         return i->second;
00183                 }
00184         
00185         };
00186          
00187          
00188 }
00189 
00190 #endif

Generated on Fri Apr 27 10:27:37 2007 for Green Engine by  doxygen 1.5.1