MetaClassManager.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/MetaClass.h>
00021 #include <OpenKarto/MetaClassManager.h>
00022 #include <OpenKarto/SmartPointer.h>
00023 
00024 namespace karto
00025 {
00026 
00030 
00031   struct MetaClassManagerPrivate
00032   {
00033     typedef karto::SmartPointer<MetaClass> ClassPtr;
00034     typedef std::map<karto::String, ClassPtr> ClassByNameTable;
00035     typedef std::map<karto::String, ClassPtr> ClassByIdTable;
00036 
00037     ClassByNameTable m_MetaClassByName; 
00038     ClassByIdTable m_MetaClassById; 
00039   };
00040 
00044 
00045   MetaClassManager::MetaClassManager()
00046     : m_pPrivate(new MetaClassManagerPrivate)
00047   {
00048   }
00049 
00050   MetaClassManager::~MetaClassManager()
00051   {
00052     Clear();
00053 
00054     delete m_pPrivate;
00055     m_pPrivate = NULL;
00056   }
00057 
00058   MetaClassManager& MetaClassManager::GetInstance()
00059   {
00060     static MetaClassManager manager;
00061     return manager;
00062   }
00063 
00064   MetaClass& MetaClassManager::RegisterNew(const karto::String& rName, const karto::String& rId)
00065   {
00066     if ((m_pPrivate->m_MetaClassByName.find(rName) != m_pPrivate->m_MetaClassByName.end()) || (m_pPrivate->m_MetaClassById.find(rId) != m_pPrivate->m_MetaClassById.end()))
00067     {
00068       throw karto::Exception("MetaClass already exists for class with name: " + rName);
00069     }
00070 
00071     MetaClassManagerPrivate::ClassPtr newClass = new MetaClass(rName);
00072     m_pPrivate->m_MetaClassByName[rName] = newClass;
00073     m_pPrivate->m_MetaClassById[rId] = newClass;
00074 
00075     return *newClass;
00076   }
00077 
00078   const MetaClass& MetaClassManager::GetByName(const karto::String& rName) const
00079   {
00080     MetaClassManagerPrivate::ClassByNameTable::const_iterator iter = m_pPrivate->m_MetaClassByName.find(rName);
00081     if (iter == m_pPrivate->m_MetaClassByName.end())
00082     {
00083       throw karto::Exception("No MetaClass for class with name: " + rName);
00084     }
00085 
00086     return *iter->second;
00087   }
00088 
00089   const MetaClass& MetaClassManager::GetById(const karto::String& rId) const
00090   {
00091     MetaClassManagerPrivate::ClassByIdTable::const_iterator iter = m_pPrivate->m_MetaClassById.find(rId);
00092     if (iter == m_pPrivate->m_MetaClassById.end())
00093     {
00094       throw karto::Exception("No MetaClass for class with id: " + rId);
00095     }
00096 
00097     return *iter->second;
00098   }
00099 
00100   kt_bool MetaClassManager::ClassExists(const karto::String& rId) const
00101   {
00102     return m_pPrivate->m_MetaClassById.find(rId) != m_pPrivate->m_MetaClassById.end();
00103   }
00104 
00105   kt_size_t MetaClassManager::GetSize() const
00106   {
00107     return m_pPrivate->m_MetaClassByName.size();
00108   }
00109 
00110   const MetaClass& MetaClassManager::operator[](kt_size_t index) const
00111   {
00112     if (index >= m_pPrivate->m_MetaClassByName.size())
00113     {
00114       throw karto::Exception("No MetaClass for index: " + index);
00115     }
00116 
00117     MetaClassManagerPrivate::ClassByNameTable::const_iterator iter = m_pPrivate->m_MetaClassByName.begin();
00118     std::advance(iter, index);
00119 
00120     return *iter->second;
00121   }
00122 
00123   void MetaClassManager::Clear()
00124   {
00125     m_pPrivate->m_MetaClassById.clear();
00126     m_pPrivate->m_MetaClassByName.clear();
00127   }
00128 
00129 }


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