IndexDirectory.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2012-2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_DATASTRUCTURES_INDEXDIRECTORY_H
#define SURGSIM_DATASTRUCTURES_INDEXDIRECTORY_H
#include <string>
#include <unordered_map>
#include <vector>
namespace SurgSim
{
namespace DataStructures
{
class IndexDirectory
{
public:
IndexDirectory();
explicit IndexDirectory(const std::vector<std::string>& names);
int getIndex(const std::string& name) const
{
if (name.length() == 0)
{
return -1;
}
auto entry = m_indices.find(name);
if (entry == m_indices.cend())
{
return -1;
}
else
{
return entry->second;
}
}
std::string getName(int index) const
{
if ((index < 0) || (index >= static_cast<int>(m_names.size())))
{
return "";
}
else
{
return m_names[index];
}
}
const std::vector<std::string>& getAllNames() const;
bool hasEntry(const std::string& name) const
{
return ((name.length() > 0) && (m_indices.count(name) > 0));
}
size_t size() const
{
return m_names.size();
}
int getNumEntries() const
{
return static_cast<int>(m_names.size());
}
protected:
template <typename T>
friend class NamedDataBuilder;
friend class DataGroupBuilder;
IndexDirectory(const IndexDirectory& directory);
IndexDirectory& operator =(const IndexDirectory& directory);
int addEntry(const std::string& name);
private:
std::vector<std::string> m_names;
std::unordered_map<std::string, int> m_indices;
};
}; // namespace Input
}; // namespace SurgSim
#endif // SURGSIM_DATASTRUCTURES_INDEXDIRECTORY_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::DataStructures::IndexDirectory::getAllNames
const std::vector< std::string > & getAllNames() const
Get a list of all the names available from the index directory.
Definition: IndexDirectory.cpp:36
SurgSim::DataStructures::IndexDirectory::IndexDirectory
IndexDirectory()
Create an empty directory object.
Definition: IndexDirectory.cpp:24
SurgSim::DataStructures::IndexDirectory::m_names
std::vector< std::string > m_names
The array of entry names, in index order.
Definition: IndexDirectory.h:131
SurgSim::DataStructures::NamedDataBuilder
A class that allows you to build a NamedData structure.
Definition: NamedDataBuilder.h:36
SurgSim::DataStructures::IndexDirectory::addEntry
int addEntry(const std::string &name)
Create a new entry for the specified name.
Definition: IndexDirectory.cpp:53
SurgSim::DataStructures::IndexDirectory::getIndex
int getIndex(const std::string &name) const
Given a name, return the corresponding index (or -1).
Definition: IndexDirectory.h:45
SurgSim::DataStructures::IndexDirectory
A simple bidirectional mapping between names (strings) and distinct consecutive non-negative indices...
Definition: IndexDirectory.h:32
SurgSim::DataStructures::IndexDirectory::m_indices
std::unordered_map< std::string, int > m_indices
A mapping of entry names to indices.
Definition: IndexDirectory.h:134
SurgSim::DataStructures::IndexDirectory::size
size_t size() const
Check the number of existing entries in the directory.
Definition: IndexDirectory.h:93
SurgSim::DataStructures::IndexDirectory::hasEntry
bool hasEntry(const std::string &name) const
Check whether the specified name exists in the directory.
Definition: IndexDirectory.h:85
SurgSim::DataStructures::IndexDirectory::getName
std::string getName(int index) const
Given an index, return the corresponding name (or "").
Definition: IndexDirectory.h:65
SurgSim::DataStructures::DataGroupBuilder
A class that allows you to build a DataGroup structure.
Definition: DataGroupBuilder.h:38
SurgSim::DataStructures::IndexDirectory::operator=
IndexDirectory & operator=(const IndexDirectory &directory)
Assignment operator.
Definition: IndexDirectory.cpp:46
SurgSim::DataStructures::IndexDirectory::getNumEntries
int getNumEntries() const
Check the number of existing entries in the directory.
Definition: IndexDirectory.h:101