opensurgsim
IndexDirectory.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2012-2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_INDEXDIRECTORY_H
17 #define SURGSIM_DATASTRUCTURES_INDEXDIRECTORY_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 namespace SurgSim
24 {
25 namespace DataStructures
26 {
27 
33 {
34 public:
37 
40  explicit IndexDirectory(const std::vector<std::string>& names);
41 
45  int getIndex(const std::string& name) const
46  {
47  if (name.length() == 0)
48  {
49  return -1;
50  }
51  auto entry = m_indices.find(name);
52  if (entry == m_indices.cend())
53  {
54  return -1;
55  }
56  else
57  {
58  return entry->second;
59  }
60  }
61 
65  std::string getName(int index) const
66  {
67  if ((index < 0) || (index >= static_cast<int>(m_names.size())))
68  {
69  return "";
70  }
71  else
72  {
73  return m_names[index];
74  }
75  }
76 
79  const std::vector<std::string>& getAllNames() const;
80 
85  bool hasEntry(const std::string& name) const
86  {
87  return ((name.length() > 0) && (m_indices.count(name) > 0));
88  }
89 
93  size_t size() const
94  {
95  return m_names.size();
96  }
97 
101  int getNumEntries() const
102  {
103  return static_cast<int>(m_names.size());
104  }
105 
106 protected:
107  template <typename T>
108  friend class NamedDataBuilder;
109  friend class DataGroupBuilder;
110 
114  IndexDirectory(const IndexDirectory& directory);
115 
119  IndexDirectory& operator =(const IndexDirectory& directory);
120 
127  int addEntry(const std::string& name);
128 
129 private:
131  std::vector<std::string> m_names;
132 
134  std::unordered_map<std::string, int> m_indices;
135 };
136 
137 }; // namespace Input
138 }; // namespace SurgSim
139 
140 #endif // SURGSIM_DATASTRUCTURES_INDEXDIRECTORY_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
const std::vector< std::string > & getAllNames() const
Get a list of all the names available from the index directory.
Definition: IndexDirectory.cpp:36
IndexDirectory()
Create an empty directory object.
Definition: IndexDirectory.cpp:24
A class that allows you to build a NamedData structure.
Definition: NamedDataBuilder.h:36
int addEntry(const std::string &name)
Create a new entry for the specified name.
Definition: IndexDirectory.cpp:53
int getIndex(const std::string &name) const
Given a name, return the corresponding index (or -1).
Definition: IndexDirectory.h:45
A simple bidirectional mapping between names (strings) and distinct consecutive non-negative indices...
Definition: IndexDirectory.h:32
size_t size() const
Check the number of existing entries in the directory.
Definition: IndexDirectory.h:93
bool hasEntry(const std::string &name) const
Check whether the specified name exists in the directory.
Definition: IndexDirectory.h:85
std::string getName(int index) const
Given an index, return the corresponding name (or "").
Definition: IndexDirectory.h:65
A class that allows you to build a DataGroup structure.
Definition: DataGroupBuilder.h:38
IndexDirectory & operator=(const IndexDirectory &directory)
Assignment operator.
Definition: IndexDirectory.cpp:46
int getNumEntries() const
Check the number of existing entries in the directory.
Definition: IndexDirectory.h:101