The CompositeData class allows arbitrary data structures to be composed together, copied, and moved with type erasure. More...
#include <CompositeData.hh>
Classes | |
struct | DataStatus |
Struct that describes the status of data. More... | |
struct | InsertResult |
This struct is the return type of the various Insert...<T>() functions. It returns a reference to the data entry for the Data type, and it indicates whether the insertion operation actually occurred. More... | |
Public Types | |
using | MapOfData = std::map< std::string, DataEntry > |
enum class | QueryMode : int { NORMAL = 0 , SILENT } |
Use these flags in Query(), Has(), and StatusOf() to change their effects on the meta info of the data being queried. More... | |
Public Member Functions | |
CompositeData () | |
Default constructor. Creates an empty CompositeData object. | |
CompositeData (CompositeData &&_other) | |
Move constructor. Same as Copy(_other). | |
CompositeData (const CompositeData &_other) | |
Copy constructor. Same as Copy(_other). | |
virtual | ~CompositeData ()=default |
Virtual destructor. | |
std::set< std::string > | AllEntries () const |
Get an ordered set of all data entries in this CompositeData. Runs with O(N) complexity. | |
CompositeData & | Copy (CompositeData &&_other, const bool _mergeRequirements=false) |
An alternative to Copy(const CompositeData &, bool) that takes advantage of move semantics. | |
CompositeData & | Copy (const CompositeData &_other, const bool _mergeRequirements=false) |
Make this CompositeData a copy of _other. However, any data entries in this CompositeData which are marked as required will not be removed. | |
std::size_t | EntryCount () const |
Check how many data entries are in this CompositeData. Runs with O(1) complexity. | |
template<typename Data > | |
Data & | Get () |
Get a reference to a Data object. If an object of the Data type does not already exist in this CompositeData, then create one using its default constructor. This function will fail to compile if a default constructor is not available for the Data type. | |
template<typename Data > | |
bool | Has () const |
Returns true if this CompositeData has an object of type Data, otherwise returns false. This is literally equivalent to (nullptr != Query<Data>(QueryMode::SILENT)). | |
template<typename Data , typename... Args> | |
InsertResult< Data > | Insert (Args &&..._args) |
This will attempt to insert a new Data entry into the CompositeData object, forwarding _args to the constructor of the entry. If an entry already exists for this Data type, then nothing will be inserted. | |
template<typename Data , typename... Args> | |
InsertResult< Data > | InsertOrAssign (Args &&... _args) |
Attempt to insert a Data-type entry. If a Data-type entry did not already exist, it will be constructed by copying (or moving) the given arguments. If a Data-type entry already existed, the existing entry will be assigned the value of Data(_args...). | |
template<typename Data , typename... Args> | |
Data & | MakeRequired (Args &&..._args) |
Marks the specified type of Data as required, creates one with the given arguments if it did not exist, and returns a reference to it. | |
CompositeData & | Merge (CompositeData &&_other, const bool _mergeRequirements=false) |
An alternative to Merge(const CompositeData &, bool) that takes advantage of move semantics. | |
CompositeData & | Merge (const CompositeData &_other, const bool _mergeRequirements=false) |
Merge the data from _other into this CompositeData. If there are any conflicting data entries, the entry from _other will take precedence. | |
CompositeData & | operator= (CompositeData &&_other) |
Move operator. Same as Copy(_other). | |
CompositeData & | operator= (const CompositeData &_other) |
Copy operator. Same as Copy(_other). | |
template<typename Data > | |
Data * | Query (const QueryMode _mode=QueryMode::NORMAL) |
Query this CompositeData for a Data-type entry. If it contains a Data-type object, it gets returned as a Data*. Otherwise, a nullptr is returned. | |
template<typename Data > | |
const Data * | Query (const QueryMode mode=QueryMode::NORMAL) const |
Const-qualified version of Query. This can be used to retrieve data from a const CompositeData . | |
template<typename Data > | |
bool | Remove () |
This will remove a Data-type object from this CompositeData and delete it if one is present. Otherwise, it will do nothing. Data-types that are marked as required will not (and cannot) be removed. | |
template<typename Data > | |
bool | Requires () const |
Returns true if the specified Data type is required by this CompositeData object. Otherwise, returns false. | |
void | ResetQueries () const |
Reset the query flags on all data entries. This will make it appear as though no entries have ever been queried. See UnqueriedEntries() for more information about the "queried" flag. | |
template<typename Data > | |
DataStatus | StatusOf () const |
Returns a DataStatus object that describes the status of the requested data type. | |
std::set< std::string > | UnqueriedEntries () const |
Get an ordered (alphabetical) set of the data entries in this CompositeData which have not been queried (Get, Insert, InsertOrAssign, Query, and MakeRequired all perform querying) since the data was implicitly created (e.g. by a copy or move operation) or since the last call to ResetQueries(), whichever is more recent. Runs with O(N) complexity. | |
std::size_t | UnqueriedEntryCount () const |
Check how many data entries in this CompositeData have not been queried. See UnqueriedEntries() for more information about the "queried" flag. Runs with O(1) complexity. | |
template<typename Data > | |
bool | Unquery () const |
Returns true if this CompositeData has a Data-type object which was marked as queried, and that object is now marked as unqueried. If an object of that type does not exist or it was already unqueried, this returns false. | |
Static Public Member Functions | |
template<typename Data > | |
static constexpr bool | AlwaysRequires () |
When called from a generic CompositeData type, this always returns false. Static (Always) requirements are determined at compile-time and cannot be changed at runtime. Using the RequireData class can make this return true for more highly specified CompositeData types. | |
template<typename Data > | |
static constexpr bool | Expects () |
When called from a generic CompositeData type, this always returns false. More highly specified CompositeData types that use ExpectData or RequireData may return true if the type of Data is expected. | |
Protected Attributes | |
MapOfData | dataMap |
Map from the label of a data object type to its entry. | |
std::size_t | numEntries |
Total number of data entries currently in this CompositeData. Note that this may differ from the size of dataMap, because some entries in dataMap will be referring to nullptrs. | |
std::size_t | numQueries |
Total number of unique queries which have been performed since either construction or the last call to ResetQueries(). | |
Detailed Description
The CompositeData class allows arbitrary data structures to be composed together, copied, and moved with type erasure.
Member Typedef Documentation
◆ MapOfData
using MapOfData = std::map<std::string, DataEntry> |
Member Enumeration Documentation
◆ QueryMode
|
strong |
Use these flags in Query(), Has(), and StatusOf() to change their effects on the meta info of the data being queried.
See UnqueriedEntries() for more on the "queried" flag.
Constructor & Destructor Documentation
◆ CompositeData() [1/3]
CompositeData | ( | ) |
Default constructor. Creates an empty CompositeData object.
◆ ~CompositeData()
|
virtualdefault |
Virtual destructor.
◆ CompositeData() [2/3]
CompositeData | ( | const CompositeData & | _other | ) |
Copy constructor. Same as Copy(_other).
◆ CompositeData() [3/3]
CompositeData | ( | CompositeData && | _other | ) |
Move constructor. Same as Copy(_other).
Member Function Documentation
◆ AllEntries()
std::set< std::string > AllEntries | ( | ) | const |
Get an ordered set of all data entries in this CompositeData. Runs with O(N) complexity.
Example usage:
- Returns
- an ordered (alphabetical) set of all data entries in this CompositeData.
◆ AlwaysRequires()
|
staticconstexpr |
When called from a generic CompositeData type, this always returns false. Static (Always) requirements are determined at compile-time and cannot be changed at runtime. Using the RequireData class can make this return true for more highly specified CompositeData types.
- Warning
- This should never be used to check whether Data is required on a specific instance, because the requirements that are placed on an instance can be changed at runtime. This should only be used to check whether a certain data type is always required for a certain specification of CompositeData.
- Template Parameters
-
Data The type of data whose requirement we are checking at compile time
- Returns
- When called on a basic CompositeData object, this is always false.
◆ Copy() [1/2]
CompositeData & Copy | ( | CompositeData && | _other, |
const bool | _mergeRequirements = false |
||
) |
An alternative to Copy(const CompositeData &, bool) that takes advantage of move semantics.
◆ Copy() [2/2]
CompositeData & Copy | ( | const CompositeData & | _other, |
const bool | _mergeRequirements = false |
||
) |
Make this CompositeData a copy of _other. However, any data entries in this CompositeData which are marked as required will not be removed.
- Parameters
-
[in] _other Another CompositeData [in] _mergeRequirements If true, this object will also take on the requirements specified by _other. Any objects that are already marked as required in this CompositeData will remain required. If false, the requirements of this CompositeData object are unaffected.
- Returns
- A reference to this object
◆ EntryCount()
std::size_t EntryCount | ( | ) | const |
Check how many data entries are in this CompositeData. Runs with O(1) complexity.
- Returns
- the number of data entries currently contained in this CompositeData.
◆ Expects()
|
staticconstexpr |
When called from a generic CompositeData type, this always returns false. More highly specified CompositeData types that use ExpectData or RequireData may return true if the type of Data is expected.
- Template Parameters
-
Data The type of data whose expectation is being checked
- Returns
- When called on a basic CompositeData object, this is always false.
◆ Get()
Data & Get | ( | ) |
Get a reference to a Data object. If an object of the Data type does not already exist in this CompositeData, then create one using its default constructor. This function will fail to compile if a default constructor is not available for the Data type.
When you have a const CompositeData
object, you must use the function CompositeData::query<Data>() in order to retrieve objects.
Example usage:
- Template Parameters
-
Data The type of data entry to get
- Returns
- a reference to to a Data-type object that is stored within this CompositeData.
◆ Has()
bool Has | ( | ) | const |
Returns true if this CompositeData has an object of type Data, otherwise returns false. This is literally equivalent to (nullptr != Query<Data>(QueryMode::SILENT)).
- Template Parameters
-
Data The type of data entry to check for
- Returns
- true if this CompositeData contains a Data-type entry.
◆ Insert()
InsertResult< Data > Insert | ( | Args &&... | _args | ) |
This will attempt to insert a new Data entry into the CompositeData object, forwarding _args to the constructor of the entry. If an entry already exists for this Data type, then nothing will be inserted.
Example usage:
- Template Parameters
-
Data The type name for the data entry Args This will be inferred from _args; you should not typically set this explicitly.
- Parameters
-
[in] _args The arguments to use for construction. These will get wrapped in a Data(...) constructor. If _args is left blank, the default constructor will be used.
- Returns
- An InsertResult<Data> which contains a reference to the data entry (either the one that is newly inserted or the one that already existed). InsertResult::inserted will be true if the entry was inserted by this function, or false if the entry already existed.
- See also
- InsertOrAssign
◆ InsertOrAssign()
InsertResult< Data > InsertOrAssign | ( | Args &&... | _args | ) |
Attempt to insert a Data-type entry. If a Data-type entry did not already exist, it will be constructed by copying (or moving) the given arguments. If a Data-type entry already existed, the existing entry will be assigned the value of Data(_args...).
Any previously existing valid references to the Data entry will remain valid, even after this function is called.
Example usage:
- Template Parameters
-
Data The type name for the data entry Args This will be inferred from _args; you should not typically set this explicitly.
- Parameters
-
[in] _args The arguments to use for construction or assignment. These will get wrapped in a Data(...) constructor. If _args is left blank, the default constructor will be used.
- Returns
- an InsertResult<Data> which contains a reference to the Data-type entry of this CompositeData. InsertResult<Data>::inserted will be true iff a Data-type entry did not already exist in this CompositeData. If the value was instead assigned, InsertResult<Data>::inserted will be false.
- See also
- Insert
◆ MakeRequired()
Data & MakeRequired | ( | Args &&... | _args | ) |
Marks the specified type of Data as required, creates one with the given arguments if it did not exist, and returns a reference to it.
Warning: This cannot be undone. Once a Data type is marked as required, it will continue to be required for this object throughout the rest of the CompositeData object's lifespan.
Example usage:
- Template Parameters
-
Data The type of data entry that should be marked as required Args This will be inferred from _args; you should not typically set this explicitly.
- Parameters
-
[in] _args The arguments to use for construction, if a Data-type entry did not already exist within this CompositeData.
- Returns
- a reference to the Data-type entry
◆ Merge() [1/2]
CompositeData & Merge | ( | CompositeData && | _other, |
const bool | _mergeRequirements = false |
||
) |
An alternative to Merge(const CompositeData &, bool) that takes advantage of move semantics.
◆ Merge() [2/2]
CompositeData & Merge | ( | const CompositeData & | _other, |
const bool | _mergeRequirements = false |
||
) |
Merge the data from _other into this CompositeData. If there are any conflicting data entries, the entry from _other will take precedence.
- Parameters
-
[in] _other Another CompositeData [in] _mergeRequirements If true, this object will also take on the requirements specified by _other. Any objects that are already marked as required in this CompositeData will remain required. If false, the requirements of this CompositeData object are unaffected.
- Returns
- A reference to this object
- See also
- Merge(CompositeData &&)
- Copy()
◆ operator=() [1/2]
CompositeData & operator= | ( | CompositeData && | _other | ) |
Move operator. Same as Copy(_other).
◆ operator=() [2/2]
CompositeData & operator= | ( | const CompositeData & | _other | ) |
Copy operator. Same as Copy(_other).
◆ Query() [1/2]
Data * Query | ( | const QueryMode | _mode = QueryMode::NORMAL | ) |
Query this CompositeData for a Data-type entry. If it contains a Data-type object, it gets returned as a Data*. Otherwise, a nullptr is returned.
If _mode is set to QueryMode::SILENT, then calling this function will not cause the "queried" flag to change (see UnqueriedEntries() for more on the "queried" flag).
Example usage:
- Template Parameters
-
Data The type of data entry to query for
- Parameters
-
[in] _mode Specify how this call should affect the query flag of the entry. The default behavior is strongly recommended, unless you know what you are doing. See QueryMode for more info.
- Returns
- a pointer to the Data entry if this CompositeData has one. Otherwise, this returns a nullptr.
◆ Query() [2/2]
const Data * Query | ( | const QueryMode | mode = QueryMode::NORMAL | ) | const |
Const-qualified version of Query. This can be used to retrieve data from a const CompositeData
.
If "mode" is set to QueryMode::SILENT, then calling this function will not cause the "queried" flag to change (see UnqueriedEntries() for more on the "queried" flag).
Example usage:
- Template Parameters
-
Data The type of data entry to query for
- Parameters
-
[in] _mode Specify how this call should affect the query flag of the entry. The default behavior is strongly recommended, unless you know what you are doing. See QueryMode for more info.
- Returns
- a const-qualified pointer to the Data entry if this CompositeData has one. Otherwise, this returns a nullptr.
◆ Remove()
bool Remove | ( | ) |
This will remove a Data-type object from this CompositeData and delete it if one is present. Otherwise, it will do nothing. Data-types that are marked as required will not (and cannot) be removed.
If the data was successfully removed or did not exist to begin with, this returns true. If the data was marked as required and therefore not removed, this returns false.
- Warning
- Calling this function will permanently invalidate all existing references to the Data-type entry of this CompositeData, i.e. the references that get returned by Get<Data>(), Insert<Data>(~), InsertOrAssign<Data>(~), or Query<Data>().
Example usage:
- Template Parameters
-
Data The type of data entry to remove
- Returns
- true iff the CompositeData no longer contains this Data type.
◆ Requires()
bool Requires | ( | ) | const |
Returns true if the specified Data type is required by this CompositeData object. Otherwise, returns false.
For more on required data, see MakeRequired<Data>().
- Template Parameters
-
Data The type of data entry whose requirements are being checked
- Returns
- true iff the specified Data type is required by this CompositeData
◆ ResetQueries()
void ResetQueries | ( | ) | const |
Reset the query flags on all data entries. This will make it appear as though no entries have ever been queried. See UnqueriedEntries() for more information about the "queried" flag.
- Attention
- It is good practice to call this function before returning a CompositeData from a function and handing it off to another segment of a pipeline, because sometimes the compiler inappropriately elides the copy/move constructor/operators and passes along the state of the queries, even though it should not.
◆ StatusOf()
DataStatus StatusOf | ( | ) | const |
Returns a DataStatus object that describes the status of the requested data type.
- Template Parameters
-
Data The type of data entry to check the status of
- Returns
- a DataStatus for the requested entry.
◆ UnqueriedEntries()
std::set< std::string > UnqueriedEntries | ( | ) | const |
Get an ordered (alphabetical) set of the data entries in this CompositeData which have not been queried (Get, Insert, InsertOrAssign, Query, and MakeRequired all perform querying) since the data was implicitly created (e.g. by a copy or move operation) or since the last call to ResetQueries(), whichever is more recent. Runs with O(N) complexity.
Unqueried data entries might be created by copy/move construction, copy/move assignment operation, or the Copy(~) function. Using the copy/move operator or the Copy(~) function will reset the query flag on any data that gets copied or moved over.
- Note
- This function can be useful for reporting runtime warnings about any unsupported data types that have been given to you.
Example usage:
- Returns
- an ordered set of the names of unqueried data entries in this CompositeData.
◆ UnqueriedEntryCount()
std::size_t UnqueriedEntryCount | ( | ) | const |
Check how many data entries in this CompositeData have not been queried. See UnqueriedEntries() for more information about the "queried" flag. Runs with O(1) complexity.
- Returns
- the number of entries in this CompositeData which have not been queried.
◆ Unquery()
bool Unquery | ( | ) | const |
Returns true if this CompositeData has a Data-type object which was marked as queried, and that object is now marked as unqueried. If an object of that type does not exist or it was already unqueried, this returns false.
This function is const-qualified because the query flags are declared as mutable.
Example usage:
- Template Parameters
-
Data The type of data entry whose query flag should be cleared
- Returns
- true if the query flag is changing from queried to unqueried, otherwise false.
Member Data Documentation
◆ dataMap
|
protected |
Map from the label of a data object type to its entry.
◆ numEntries
|
protected |
Total number of data entries currently in this CompositeData. Note that this may differ from the size of dataMap, because some entries in dataMap will be referring to nullptrs.
◆ numQueries
|
mutableprotected |
Total number of unique queries which have been performed since either construction or the last call to ResetQueries().
The documentation for this class was generated from the following file: