MetaEnumManager.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2011, SRI International (R)
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as published by
00006  * the Free Software Foundation, either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <map>
00019 
00020 #include <OpenKarto/MetaEnum.h>
00021 #include <OpenKarto/MetaEnumManager.h>
00022 #include <OpenKarto/SmartPointer.h>
00023 
00024 namespace karto
00025 {
00026 
00030 
00031   struct MetaEnumManagerPrivate
00032   {
00033     typedef karto::SmartPointer<MetaEnum> MetaEnumPtr;
00034     typedef std::map<karto::String, MetaEnumPtr> EnumByNameTable;
00035     typedef std::map<karto::String, MetaEnumPtr> EnumByIdTable;
00036 
00037     EnumByNameTable m_MetaEnumByName;
00038     EnumByIdTable m_MetaEnumById;
00039   };
00040 
00044 
00045   MetaEnumManager::MetaEnumManager()
00046     : m_pPrivate(new MetaEnumManagerPrivate)
00047   {
00048   }
00049 
00050   MetaEnumManager::~MetaEnumManager()
00051   {
00052     Clear();
00053 
00054     delete m_pPrivate;
00055     m_pPrivate = NULL;
00056   }
00057 
00058   MetaEnumManager& MetaEnumManager::GetInstance()
00059   {
00060     static MetaEnumManager manager;
00061     return manager;
00062   }
00063 
00064   MetaEnum& MetaEnumManager::RegisterNew(const karto::String& rName, const karto::String& rId)
00065   {
00066     if ((m_pPrivate->m_MetaEnumByName.find(rName) != m_pPrivate->m_MetaEnumByName.end()) || (m_pPrivate->m_MetaEnumById.find(rId) != m_pPrivate->m_MetaEnumById.end()))
00067     {
00068       assert(false);
00069     }
00070 
00071     MetaEnumManagerPrivate::MetaEnumPtr newEnum = new MetaEnum(rName);
00072     m_pPrivate->m_MetaEnumByName[rName] = newEnum;
00073     m_pPrivate->m_MetaEnumById[rId] = newEnum;
00074 
00075     return *newEnum;
00076   }
00077 
00078   const MetaEnum& MetaEnumManager::GetByName(const karto::String& rName) const
00079   {
00080     MetaEnumManagerPrivate::EnumByNameTable::const_iterator iter = m_pPrivate->m_MetaEnumByName.find(rName);
00081     if (iter == m_pPrivate->m_MetaEnumByName.end())
00082     {
00083       throw karto::Exception("No MetaEnum for enum with name: " + rName);
00084     }
00085 
00086     return *iter->second;
00087   }
00088 
00089   const MetaEnum& MetaEnumManager::GetById(const karto::String& rId) const
00090   {
00091     MetaEnumManagerPrivate::EnumByIdTable::const_iterator iter = m_pPrivate->m_MetaEnumById.find(rId);
00092     if (iter == m_pPrivate->m_MetaEnumById.end())
00093     {
00094       throw karto::Exception("No MetaEnum for enum with id: " + rId);
00095     }
00096 
00097     return *iter->second;
00098   }
00099 
00100   kt_bool MetaEnumManager::EnumExists(const karto::String& rId) const
00101   {
00102     return m_pPrivate->m_MetaEnumById.find(rId) != m_pPrivate->m_MetaEnumById.end();
00103   }
00104 
00105   kt_size_t MetaEnumManager::GetSize() const
00106   {
00107     return m_pPrivate->m_MetaEnumByName.size();
00108   }
00109 
00110   const MetaEnum& MetaEnumManager::operator[]( kt_size_t index ) const
00111   {
00112     if (index >= m_pPrivate->m_MetaEnumByName.size())
00113     {
00114       assert(false);
00115     }
00116 
00117     MetaEnumManagerPrivate::EnumByNameTable::const_iterator iter = m_pPrivate->m_MetaEnumByName.begin();
00118     std::advance(iter, index);
00119 
00120     return *iter->second;
00121   }
00122 
00123   void MetaEnumManager::Clear()
00124   {
00125     m_pPrivate->m_MetaEnumById.clear();
00126     m_pPrivate->m_MetaEnumByName.clear();
00127   }
00128 
00129 }


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Sun Apr 2 2017 04:05:23