opensurgsim
NamedDataBuilder.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_NAMEDDATABUILDER_H
17 #define SURGSIM_DATASTRUCTURES_NAMEDDATABUILDER_H
18 
19 #include <memory>
20 
21 #include "SurgSim/DataStructures/NamedData.h"
22 
23 namespace SurgSim
24 {
25 namespace DataStructures
26 {
27 
35 template <typename T>
37 {
38 public:
41  {
42  }
43 
48  {
49  // NB: can't use copy construction in the std::make_shared call, because access is protected.
50  std::shared_ptr<IndexDirectory> dir = std::make_shared<IndexDirectory>();
51  *dir = m_directory;
52  return NamedData<T>(dir);
53  }
54 
58  std::shared_ptr<NamedData<T>> createSharedData() const
59  {
60  return std::make_shared<NamedData<T>>(createData());
61  }
62 
67  int addEntry(const std::string& name)
68  {
69  return m_directory.addEntry(name);
70  }
71 
74  void addEntriesFrom(const std::vector<std::string>& names)
75  {
76  for (auto it = names.cbegin(); it != names.cend(); ++it)
77  {
78  addEntry(*it);
79  }
80  }
81 
85  template <typename U>
86  void addEntriesFrom(const NamedDataBuilder<U>& builder)
87  {
88  addEntriesFrom(builder.getAllNames());
89  }
90 
94  template <typename U>
95  void addEntriesFrom(const NamedData<U>& data)
96  {
97  addEntriesFrom(data.getDirectory()->getAllNames());
98  }
99 
102  void addEntriesFrom(const IndexDirectory& directory)
103  {
104  addEntriesFrom(directory.getAllNames());
105  }
106 
110  int getIndex(const std::string& name) const
111  {
112  return m_directory.getIndex(name);
113  }
114 
118  std::string getName(int index) const
119  {
120  return m_directory.getName(index);
121  }
122 
125  const std::vector<std::string>& getAllNames() const
126  {
127  return m_directory.getAllNames();
128  }
129 
134  bool hasEntry(const std::string& name) const
135  {
136  return m_directory.hasEntry(name);
137  }
138 
142  size_t size() const
143  {
144  return m_directory.size();
145  }
146 
150  int getNumEntries() const
151  {
152  return m_directory.getNumEntries();
153  }
154 
155 private:
157  IndexDirectory m_directory;
158 };
159 
160 }; // namespace Input
161 }; // namespace SurgSim
162 
163 #endif // SURGSIM_DATASTRUCTURES_NAMEDDATABUILDER_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
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
const std::vector< std::string > & getAllNames() const
Get a list of all the names available from the index directory.
Definition: IndexDirectory.cpp:36
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
bool hasEntry(const std::string &name) const
Check whether the specified name exists in the builder.
Definition: NamedDataBuilder.h:134
A templated dictionary in which data can be accessed by name or index, with immutable names & indices...
Definition: NamedData.h:102
void addEntriesFrom(const std::vector< std::string > &names)
Create new entries from a vector of names.
Definition: NamedDataBuilder.h:74
void addEntriesFrom(const NamedDataBuilder< U > &builder)
Create new entries from another NamedDataBuilder.
Definition: NamedDataBuilder.h:86
A class that allows you to build a NamedData structure.
Definition: NamedDataBuilder.h:36
const std::vector< std::string > & getAllNames() const
Get a list of all the names available in the builder.
Definition: NamedDataBuilder.h:125
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
int addEntry(const std::string &name)
Creates a new entry for the specified name.
Definition: NamedDataBuilder.h:67
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
int getIndex(const std::string &name) const
Given a name, return the corresponding index (or -1).
Definition: NamedDataBuilder.h:110
NamedData< T > createData() const
Produces a NamedData object with an immutable set of names and indices.
Definition: NamedDataBuilder.h:47
NamedDataBuilder()
Constructs an empty builder object.
Definition: NamedDataBuilder.h:40
int getNumEntries() const
Check the number of existing entries in the builder.
Definition: NamedDataBuilder.h:150
std::string getName(int index) const
Given an index, return the corresponding name (or "").
Definition: NamedDataBuilder.h:118
bool hasEntry(const std::string &name) const
Check whether the specified name exists in the directory.
Definition: IndexDirectory.h:85
size_t size() const
Check the number of existing entries in the builder.
Definition: NamedDataBuilder.h:142
std::string getName(int index) const
Given an index, return the corresponding name (or "").
Definition: IndexDirectory.h:65
void addEntriesFrom(const IndexDirectory &directory)
Create new entries from an IndexDirectory.
Definition: NamedDataBuilder.h:102
int getNumEntries() const
Check the number of existing entries in the directory.
Definition: IndexDirectory.h:101
void addEntriesFrom(const NamedData< U > &data)
Create new entries from an already initialized NamedData.
Definition: NamedDataBuilder.h:95