opensurgsim
|
A templated dictionary in which data can be accessed by name or index, with immutable names & indices. More...
#include <NamedData.h>
Public Member Functions | |
NamedData () | |
Create an empty object, with no associated names and indices yet. | |
NamedData (std::shared_ptr< const IndexDirectory > directory) | |
Create an object containing items from an index directory. More... | |
NamedData (const std::vector< std::string > &names) | |
Construct an object, using the names from a vector of strings. More... | |
NamedData (const NamedData &namedData) | |
Construct an object as a copy of the data from another object. More... | |
NamedData & | operator= (const NamedData &namedData) |
Copy the data from another object. More... | |
NamedData (NamedData &&namedData) | |
Create an object and move the data from another object. More... | |
NamedData & | operator= (NamedData &&namedData) |
Move the data from another object. More... | |
bool | isValid () const |
Check if the object has been initialized, which means it has a set of entries (i.e., names, indices, and the map between them). More... | |
std::shared_ptr< const IndexDirectory > | getDirectory () const |
Return the object's layout directory, which is its collection of names and indices. More... | |
int | getIndex (const std::string &name) const |
Given a name, return the corresponding index (or -1). More... | |
std::string | getName (int index) const |
Given an index, return the corresponding name (or ""). More... | |
bool | hasEntry (int index) const |
Check whether the object contains an entry with the specified index. More... | |
bool | hasEntry (const std::string &name) const |
Check whether the object contains an entry with the specified name. More... | |
bool | hasData (int index) const |
Check whether the entry with the specified index contains valid data. More... | |
bool | hasData (const std::string &name) const |
Check whether the entry with the specified name contains valid data. More... | |
bool | get (int index, T *value) const |
Given an index, get the corresponding value. More... | |
bool | get (const std::string &name, T *value) const |
Given a name, get the corresponding value. More... | |
bool | set (int index, const T &value) |
Record the data for an entry specified by an index. More... | |
bool | set (int index, T &&value) |
Record the data for an entry specified by an index. More... | |
bool | set (const std::string &name, const T &value) |
Record the data for an entry specified by a name. More... | |
bool | set (const std::string &name, T &&value) |
Record the data for an entry specified by a name. More... | |
bool | reset (int index) |
Invalidate an entry— mark it as not containing any valid data. More... | |
bool | reset (const std::string &name) |
Invalidate an entry— mark it as not containing any valid data. More... | |
void | resetAll () |
Invalidate all entries— mark everything as not containing any valid data. | |
size_t | size () const |
Check the number of existing entries. More... | |
int | getNumEntries () const |
Check the number of existing entries. More... | |
template<typename N > | |
void | copy (const NamedData< N > &source, const NamedDataCopyMap &map) |
Copy the data from another NamedData, based on a map of indices. More... | |
void | cacheIndex (const std::string &name, int *index) const |
Caches an entry's index if it is not already cached. More... | |
A templated dictionary in which data can be accessed by name or index, with immutable names & indices.
A NamedData object consists of a collection of entries of type T. The data value for each entry can be accessed by either the entry's unique name (a std::string) or the entry's unique index (a non-negative integer). Access by name is more convenient, but less efficient.
A NamedData object constructed by the default constructor has no entries, meaning it has not been associated with a set of names and indices, and is called invalid or empty.
An non-empty object contains an immutable collection of entries. For a non-empty object: entries cannot be added or removed, and the entries' names and indices cannot be changed. Further, a non-empty object cannot become empty. These properties ensure that a stable data layout is available to the code using this class so that it can, for example, record entry indices and use them to retrieve the same entries later on.
The data associated with an entry (e.g., the true or false associated with a particular name and index in a NamedData<bool>) can be changed, and each entry can be reset to a "missing" state. A reset entry remains in the collection, but has no associated data.
The entries (i.e., names & indices) in a NamedData object can be set by passing a vector of names to the constructor, or by using the NamedDataBuilder class. Given one non-empty object, other objects with the same entries can be created via copy construction or assignment of the non-empty object to an empty (default-constructed) object.
T | the data type used for values contained in this collection. |
|
inlineexplicit |
Create an object containing items from an index directory.
You should probably use NamedDataBuilder or copy construction/assignment instead.
directory | The IndexDirectory object describing the names and indices to be used. |
|
inlineexplicit |
Construct an object, using the names from a vector of strings.
The indices corresponding to each name's entry will be the same as that name's index in the vector.
names | The names, which should be unique. |
|
inline |
Construct an object as a copy of the data from another object.
This is used in the NamedVariantData copy constructor.
namedData | The object to copy from. |
|
inline |
Create an object and move the data from another object.
[in,out] | namedData | The object to copy from, which will be left in an unusable state. |
void SurgSim::DataStructures::NamedData< T >::cacheIndex | ( | const std::string & | name, |
int * | index | ||
) | const |
Caches an entry's index if it is not already cached.
An index is considered already cached if it is >= 0.
name | The name of the entry. | |
[in,out] | index | The cached index. |
|
inline |
|
inline |
Given an index, get the corresponding value.
It's only possible to get the value if the data was set using set(int, const T&) or set(const std::string&, const T&), without being subsequently invalidated by reset(int) or reset(const std::string&). In other words, get returns the same value as hasData would return.
index | The index of the entry. | |
[out] | value | The location for the retrieved value. Must not be null. |
|
inline |
Given a name, get the corresponding value.
It's only possible to get the value if the data was set using set(int, const T&) or set(const std::string&, const T&), without being subsequently invalidated by reset(int) or reset(const std::string&). In other words, get returns the same value as hasData would return.
name | The name of the entry. | |
[out] | value | The location for the retrieved value. Must not be null. |
|
inline |
Return the object's layout directory, which is its collection of names and indices.
In most cases, you should use direct assignment instead of doing things via the directory.
|
inline |
Given a name, return the corresponding index (or -1).
name | The name. |
|
inline |
Given an index, return the corresponding name (or "").
index | The index. |
|
inline |
|
inline |
Check whether the entry with the specified index contains valid data.
The check verifies that the entry's data was set using set(int, const T&) or set(const std::string&, const T&), without being subsequently invalidated by reset(int) or reset(const std::string&).
index | The index of the entry. |
|
inline |
Check whether the entry with the specified name contains valid data.
The check verifies that the entry's data was set using set(int, const T&) or set(const std::string&, const T&), without being subsequently invalidated by reset(int) or reset(const std::string&).
name | The name of the entry. |
|
inline |
Check whether the object contains an entry with the specified index.
Logically equivalent to getName(index) != ""
.
index | The index corresponding to the entry. |
|
inline |
Check whether the object contains an entry with the specified name.
Logically equivalent to getIndex(name) != -1
.
name | The name corresponding to the entry. |
|
inline |
Check if the object has been initialized, which means it has a set of entries (i.e., names, indices, and the map between them).
If the object has not been initialized, it can become initialized on assignment from an initialized object.
|
inline |
Copy the data from another object.
The object being assigned into must either be empty (not yet associated with a set of names and indices), or the two objects must share the same data layout, resulting from earlier copy construction or assignment.
Note that the data layout must be the same, i.e. related to one another by object assignment or copy construction. Objects that merely contain entries with the same names and indices are not acceptable! (Otherwise, we'd need to inefficiently compare layout contents each time we assign.)
namedData | The object to copy from. |
|
inline |
Move the data from another object.
The same restrictions on object compatibility apply as in the case of the copy assignment operator=(const NamedData&).
[in,out] | namedData | The object to copy from, which will be left in an unusable state. |
|
inline |
Invalidate an entry— mark it as not containing any valid data.
index | The index of the entry. |
|
inline |
Invalidate an entry— mark it as not containing any valid data.
name | The name of the entry. |
|
inline |
Record the data for an entry specified by an index.
The entry will also be marked as containing valid data.
index | The index of the entry. |
value | The value to be set. |
|
inline |
Record the data for an entry specified by an index.
This version accepts rvalues, and the data will be moved The entry will also be marked as containing valid data.
index | The index of the entry. |
value | The value to be set. |
|
inline |
Record the data for an entry specified by a name.
The entry will also be marked as containing valid data.
name | The name of the entry. |
value | The value to be set. |
|
inline |
Record the data for an entry specified by a name.
This version accepts rvalues, and the data will be moved The entry will also be marked as containing valid data.
name | The name of the entry. |
value | The value to be set. |
|
inline |
Check the number of existing entries.