NamedDataBuilder.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_NAMEDDATABUILDER_H
#define SURGSIM_DATASTRUCTURES_NAMEDDATABUILDER_H
#include <memory>
#include "SurgSim/DataStructures/NamedData.h"
namespace SurgSim
{
namespace DataStructures
{
template <typename T>
class NamedDataBuilder
{
public:
NamedDataBuilder()
{
}
NamedData<T> createData() const
{
// NB: can't use copy construction in the std::make_shared call, because access is protected.
std::shared_ptr<IndexDirectory> dir = std::make_shared<IndexDirectory>();
*dir = m_directory;
return NamedData<T>(dir);
}
std::shared_ptr<NamedData<T>> createSharedData() const
{
return std::make_shared<NamedData<T>>(createData());
}
int addEntry(const std::string& name)
{
return m_directory.addEntry(name);
}
void addEntriesFrom(const std::vector<std::string>& names)
{
for (auto it = names.cbegin(); it != names.cend(); ++it)
{
addEntry(*it);
}
}
template <typename U>
void addEntriesFrom(const NamedDataBuilder<U>& builder)
{
addEntriesFrom(builder.getAllNames());
}
template <typename U>
void addEntriesFrom(const NamedData<U>& data)
{
addEntriesFrom(data.getDirectory()->getAllNames());
}
void addEntriesFrom(const IndexDirectory& directory)
{
addEntriesFrom(directory.getAllNames());
}
int getIndex(const std::string& name) const
{
return m_directory.getIndex(name);
}
std::string getName(int index) const
{
return m_directory.getName(index);
}
const std::vector<std::string>& getAllNames() const
{
return m_directory.getAllNames();
}
bool hasEntry(const std::string& name) const
{
return m_directory.hasEntry(name);
}
size_t size() const
{
return m_directory.size();
}
int getNumEntries() const
{
return m_directory.getNumEntries();
}
private:
IndexDirectory m_directory;
};
}; // namespace Input
}; // namespace SurgSim
#endif // SURGSIM_DATASTRUCTURES_NAMEDDATABUILDER_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::DataStructures::NamedDataBuilder::createSharedData
std::shared_ptr< NamedData< T > > createSharedData() const
Produces a shared pointer to an empty NamedData object with an immutable set of names and indices...
Definition: NamedDataBuilder.h:58
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::NamedData::getDirectory
std::shared_ptr< const IndexDirectory > getDirectory() const
Return the object&#39;s layout directory, which is its collection of names and indices.
Definition: NamedData-inl.h:127
SurgSim::DataStructures::NamedDataBuilder::hasEntry
bool hasEntry(const std::string &name) const
Check whether the specified name exists in the builder.
Definition: NamedDataBuilder.h:134
SurgSim::DataStructures::NamedData
A templated dictionary in which data can be accessed by name or index, with immutable names & indices...
Definition: NamedData.h:102
SurgSim::DataStructures::NamedDataBuilder::addEntriesFrom
void addEntriesFrom(const std::vector< std::string > &names)
Create new entries from a vector of names.
Definition: NamedDataBuilder.h:74
SurgSim::DataStructures::NamedDataBuilder::addEntriesFrom
void addEntriesFrom(const NamedDataBuilder< U > &builder)
Create new entries from another NamedDataBuilder.
Definition: NamedDataBuilder.h:86
NamedData.h
SurgSim::DataStructures::NamedDataBuilder
A class that allows you to build a NamedData structure.
Definition: NamedDataBuilder.h:36
SurgSim::DataStructures::NamedDataBuilder::getAllNames
const std::vector< std::string > & getAllNames() const
Get a list of all the names available in the builder.
Definition: NamedDataBuilder.h:125
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::NamedDataBuilder::addEntry
int addEntry(const std::string &name)
Creates a new entry for the specified name.
Definition: NamedDataBuilder.h:67
SurgSim::DataStructures::IndexDirectory
A simple bidirectional mapping between names (strings) and distinct consecutive non-negative indices...
Definition: IndexDirectory.h:32
SurgSim::DataStructures::IndexDirectory::size
size_t size() const
Check the number of existing entries in the directory.
Definition: IndexDirectory.h:93
SurgSim::DataStructures::NamedDataBuilder::getIndex
int getIndex(const std::string &name) const
Given a name, return the corresponding index (or -1).
Definition: NamedDataBuilder.h:110
SurgSim::DataStructures::NamedDataBuilder::createData
NamedData< T > createData() const
Produces a NamedData object with an immutable set of names and indices.
Definition: NamedDataBuilder.h:47
SurgSim::DataStructures::NamedDataBuilder::NamedDataBuilder
NamedDataBuilder()
Constructs an empty builder object.
Definition: NamedDataBuilder.h:40
SurgSim::DataStructures::NamedDataBuilder::getNumEntries
int getNumEntries() const
Check the number of existing entries in the builder.
Definition: NamedDataBuilder.h:150
SurgSim::DataStructures::NamedDataBuilder::m_directory
IndexDirectory m_directory
The mapping between names and indices that will be used to create the NamedData instance.
Definition: NamedDataBuilder.h:157
SurgSim::DataStructures::NamedDataBuilder::getName
std::string getName(int index) const
Given an index, return the corresponding name (or "").
Definition: NamedDataBuilder.h:118
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::NamedDataBuilder::size
size_t size() const
Check the number of existing entries in the builder.
Definition: NamedDataBuilder.h:142
SurgSim::DataStructures::IndexDirectory::getName
std::string getName(int index) const
Given an index, return the corresponding name (or "").
Definition: IndexDirectory.h:65
SurgSim::DataStructures::NamedDataBuilder::addEntriesFrom
void addEntriesFrom(const IndexDirectory &directory)
Create new entries from an IndexDirectory.
Definition: NamedDataBuilder.h:102
SurgSim::DataStructures::IndexDirectory::getNumEntries
int getNumEntries() const
Check the number of existing entries in the directory.
Definition: IndexDirectory.h:101
SurgSim::DataStructures::NamedDataBuilder::addEntriesFrom
void addEntriesFrom(const NamedData< U > &data)
Create new entries from an already initialized NamedData.
Definition: NamedDataBuilder.h:95