DataObject

A generic container for passing any kind of data around.  DataObject makes use of the boost::any class and templated constructors to allow for any type of data to be stored in it.  The data is stored in a std::vector of boost::any objects.  When an accessor wants to retrieve the data out of the DataObject, it must use the read<> function and supply an index into the std::vector.  If the requested type does not match the type of the object stored at that location, a boost::bad_any_cast exception is thrown.

DataObject is used to pass information from the UI to an IRule without the type of the information being explicit, which would require a new type of Execute function for every kind of data that could be passed.  The strategy at work here allows the data type to be “hidden” while being passed around, but retrieved later.  It is somewhat slower, but allows for great flexibility.

Project

RulesCore

Include

DataObject.h

Summary
A generic container for passing any kind of data around.
The default DataObject constructor.
The one-parameter DataObject constructor.
The two-parameter DataObject constructror.
The three-parameter DataObject constructror.
Returns how many items are in the DataObject.
Reads data out of the data store.
Reads a DataObject from an input stream.
Writes a DataObject to an output stream.
The data store.

Public

Summary
The default DataObject constructor.
The one-parameter DataObject constructor.
The two-parameter DataObject constructror.
The three-parameter DataObject constructror.
Returns how many items are in the DataObject.
Reads data out of the data store.
Reads a DataObject from an input stream.
Writes a DataObject to an output stream.

Constructors

DataObject

DataObject()

The default DataObject constructor.  Creates an empty DataObject.

DataObject

template <typename T> DataObject(const &data)

The one-parameter DataObject constructor.  Takes the data and appends it to the std::vector.

Parameters

dataThe piece of data to store.

DataObject

template <typename T, typename U> DataObject(const &data1,
const &data2)

The two-parameter DataObject constructror.  Takes the data and appends it to the std::vector.

Parameters

data1The first piece of data to store.
data2The second piece of data to store.

DataObject

template <typename T, typename U, typename V> DataObject(const &data1,
const &data2,
const &data3)

The three-parameter DataObject constructror.  Takes the data and appends it to the std::vector.

Parameters

data1The first piece of data to store.
data2The second piece of data to store.
data3The third piece of data to store.

Functions

numItems

size_t numItems() const

Returns how many items are in the DataObject.

Returns

The number of items in the DataObject.

read

template <typename T> const T &read(const wxUint32 index = 0) const

Reads data out of the data store.  Uses the boost::any_cast function to attempt to cast the data at the given index in the data store to the requested type.  If the any_cast fails, a boost::bad_any_cast exception is thrown, which signals to the developer that the kind of data they were expecting in that DataObject was not what they actually got.  Otherwise, returns a read-only reference to the data object.

Returns

A const reference to the data at the given index.

Throws

A boost::bad_any_cast when the requested data type does not match the data at the given index in the data store.

load

void load(wxDataInputStream &input)

Reads a DataObject from an input stream.  Reads the type information for each boost::any object that has been serialized, looks up the serializer for that type and then uses it to read the boost::any data from the stream.

Parameters

inputThe input stream.

save

void save(wxDataOutputStream &output) const

Writes a DataObject to an output stream.  Uses the IAnySerializer for each boost::any object to handle writing the data.

Parameters

outputThe output stream.

Private

Summary
The data store.

Variables

mData

AnyArray mData

The data store.  Stores boost::any objects in a std::vector.

DataObject()
The default DataObject constructor.
size_t numItems() const
Returns how many items are in the DataObject.
template <typename T> const T &read(const wxUint32 index = 0) const
Reads data out of the data store.
void load(wxDataInputStream &input)
Reads a DataObject from an input stream.
void save(wxDataOutputStream &output) const
Writes a DataObject to an output stream.
AnyArray mData
The data store.
The interface for a single game rule.
RulesCore is the HOSS of Cities3D.
Needs documentation.