MetaClassManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <map>
19 
20 #include <OpenKarto/MetaClass.h>
22 #include <OpenKarto/SmartPointer.h>
23 
24 namespace karto
25 {
26 
30 
32  {
34  typedef std::map<karto::String, ClassPtr> ClassByNameTable;
35  typedef std::map<karto::String, ClassPtr> ClassByIdTable;
36 
37  ClassByNameTable m_MetaClassByName;
38  ClassByIdTable m_MetaClassById;
39  };
40 
44 
45  MetaClassManager::MetaClassManager()
46  : m_pPrivate(new MetaClassManagerPrivate)
47  {
48  }
49 
50  MetaClassManager::~MetaClassManager()
51  {
52  Clear();
53 
54  delete m_pPrivate;
55  m_pPrivate = NULL;
56  }
57 
58  MetaClassManager& MetaClassManager::GetInstance()
59  {
60  static MetaClassManager manager;
61  return manager;
62  }
63 
64  MetaClass& MetaClassManager::RegisterNew(const karto::String& rName, const karto::String& rId)
65  {
66  if ((m_pPrivate->m_MetaClassByName.find(rName) != m_pPrivate->m_MetaClassByName.end()) || (m_pPrivate->m_MetaClassById.find(rId) != m_pPrivate->m_MetaClassById.end()))
67  {
68  throw karto::Exception("MetaClass already exists for class with name: " + rName);
69  }
70 
71  MetaClassManagerPrivate::ClassPtr newClass = new MetaClass(rName);
72  m_pPrivate->m_MetaClassByName[rName] = newClass;
73  m_pPrivate->m_MetaClassById[rId] = newClass;
74 
75  return *newClass;
76  }
77 
78  const MetaClass& MetaClassManager::GetByName(const karto::String& rName) const
79  {
80  MetaClassManagerPrivate::ClassByNameTable::const_iterator iter = m_pPrivate->m_MetaClassByName.find(rName);
81  if (iter == m_pPrivate->m_MetaClassByName.end())
82  {
83  throw karto::Exception("No MetaClass for class with name: " + rName);
84  }
85 
86  return *iter->second;
87  }
88 
89  const MetaClass& MetaClassManager::GetById(const karto::String& rId) const
90  {
91  MetaClassManagerPrivate::ClassByIdTable::const_iterator iter = m_pPrivate->m_MetaClassById.find(rId);
92  if (iter == m_pPrivate->m_MetaClassById.end())
93  {
94  throw karto::Exception("No MetaClass for class with id: " + rId);
95  }
96 
97  return *iter->second;
98  }
99 
100  kt_bool MetaClassManager::ClassExists(const karto::String& rId) const
101  {
102  return m_pPrivate->m_MetaClassById.find(rId) != m_pPrivate->m_MetaClassById.end();
103  }
104 
105  kt_size_t MetaClassManager::GetSize() const
106  {
107  return m_pPrivate->m_MetaClassByName.size();
108  }
109 
110  const MetaClass& MetaClassManager::operator[](kt_size_t index) const
111  {
112  if (index >= m_pPrivate->m_MetaClassByName.size())
113  {
114  throw karto::Exception("No MetaClass for index: " + index);
115  }
116 
117  MetaClassManagerPrivate::ClassByNameTable::const_iterator iter = m_pPrivate->m_MetaClassByName.begin();
118  std::advance(iter, index);
119 
120  return *iter->second;
121  }
122 
123  void MetaClassManager::Clear()
124  {
125  m_pPrivate->m_MetaClassById.clear();
126  m_pPrivate->m_MetaClassByName.clear();
127  }
128 
129 }
bool kt_bool
Definition: Types.h:145
std::size_t kt_size_t
Definition: Types.h:138
karto::SmartPointer< MetaClass > ClassPtr
std::map< karto::String, ClassPtr > ClassByIdTable
Definition: Any.cpp:20
std::map< karto::String, ClassPtr > ClassByNameTable


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Tue Nov 7 2017 06:02:36