MetaEnumManager.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/MetaEnum.h>
22 #include <OpenKarto/SmartPointer.h>
23 
24 namespace karto
25 {
26 
30 
32  {
34  typedef std::map<karto::String, MetaEnumPtr> EnumByNameTable;
35  typedef std::map<karto::String, MetaEnumPtr> EnumByIdTable;
36 
37  EnumByNameTable m_MetaEnumByName;
38  EnumByIdTable m_MetaEnumById;
39  };
40 
44 
45  MetaEnumManager::MetaEnumManager()
46  : m_pPrivate(new MetaEnumManagerPrivate)
47  {
48  }
49 
50  MetaEnumManager::~MetaEnumManager()
51  {
52  Clear();
53 
54  delete m_pPrivate;
55  m_pPrivate = NULL;
56  }
57 
58  MetaEnumManager& MetaEnumManager::GetInstance()
59  {
60  static MetaEnumManager manager;
61  return manager;
62  }
63 
64  MetaEnum& MetaEnumManager::RegisterNew(const karto::String& rName, const karto::String& rId)
65  {
66  if ((m_pPrivate->m_MetaEnumByName.find(rName) != m_pPrivate->m_MetaEnumByName.end()) || (m_pPrivate->m_MetaEnumById.find(rId) != m_pPrivate->m_MetaEnumById.end()))
67  {
68  assert(false);
69  }
70 
71  MetaEnumManagerPrivate::MetaEnumPtr newEnum = new MetaEnum(rName);
72  m_pPrivate->m_MetaEnumByName[rName] = newEnum;
73  m_pPrivate->m_MetaEnumById[rId] = newEnum;
74 
75  return *newEnum;
76  }
77 
78  const MetaEnum& MetaEnumManager::GetByName(const karto::String& rName) const
79  {
80  MetaEnumManagerPrivate::EnumByNameTable::const_iterator iter = m_pPrivate->m_MetaEnumByName.find(rName);
81  if (iter == m_pPrivate->m_MetaEnumByName.end())
82  {
83  throw karto::Exception("No MetaEnum for enum with name: " + rName);
84  }
85 
86  return *iter->second;
87  }
88 
89  const MetaEnum& MetaEnumManager::GetById(const karto::String& rId) const
90  {
91  MetaEnumManagerPrivate::EnumByIdTable::const_iterator iter = m_pPrivate->m_MetaEnumById.find(rId);
92  if (iter == m_pPrivate->m_MetaEnumById.end())
93  {
94  throw karto::Exception("No MetaEnum for enum with id: " + rId);
95  }
96 
97  return *iter->second;
98  }
99 
100  kt_bool MetaEnumManager::EnumExists(const karto::String& rId) const
101  {
102  return m_pPrivate->m_MetaEnumById.find(rId) != m_pPrivate->m_MetaEnumById.end();
103  }
104 
105  kt_size_t MetaEnumManager::GetSize() const
106  {
107  return m_pPrivate->m_MetaEnumByName.size();
108  }
109 
110  const MetaEnum& MetaEnumManager::operator[]( kt_size_t index ) const
111  {
112  if (index >= m_pPrivate->m_MetaEnumByName.size())
113  {
114  assert(false);
115  }
116 
117  MetaEnumManagerPrivate::EnumByNameTable::const_iterator iter = m_pPrivate->m_MetaEnumByName.begin();
118  std::advance(iter, index);
119 
120  return *iter->second;
121  }
122 
123  void MetaEnumManager::Clear()
124  {
125  m_pPrivate->m_MetaEnumById.clear();
126  m_pPrivate->m_MetaEnumByName.clear();
127  }
128 
129 }
bool kt_bool
Definition: Types.h:145
std::size_t kt_size_t
Definition: Types.h:138
karto::SmartPointer< MetaEnum > MetaEnumPtr
std::map< karto::String, MetaEnumPtr > EnumByIdTable
Definition: Any.cpp:20
std::map< karto::String, MetaEnumPtr > EnumByNameTable


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Thu Jun 6 2019 19:20:24