Workspace 6.21.5
Public Member Functions | Protected Member Functions | List of all members
ObjectGroup Class Referenceabstract

Class for relating a group of DataObject instances together. More...

#include <Workspace/DataExecution/DataObjects/objectgroup.h>

Inheritance diagram for ObjectGroup:
[legend]

Public Member Functions

 ~ObjectGroup () override
 
bool add (const QString &name, DataObject &obj)
 
bool canSerialize () const override
 
ObjectGroupclone () const override=0
 
bool empty () const
 
void ensureGroupHasData ()
 
void erase (int index)
 
DataObjectgetChild (const QString &name)
 
const DataObjectgetChild (const QString &name) const
 
DataObjectgetDataObject (int index)
 
const DataObjectgetDataObject (int index) const
 
int getIndex (const QString &name) const
 
const QString & getName (int index) const
 
virtual QString getPreferedWidget (const QString &name) const
 
bool hasData () const
 
bool haveName (const QString &name) const
 
bool insert (const QString &name, DataObject &obj, int index=-1)
 
bool load (const QJsonDocument &doc)
 
bool load (const SerializedItem &item) override
 
bool save (QJsonDocument &doc) const
 
bool save (SerializedItem &item) const override
 
unsigned size () const
 
- Public Member Functions inherited from Clonable
virtual ~Clonable ()=default
 
virtual Clonableclone () const =0
 
- Public Member Functions inherited from Serialize
virtual ~Serialize ()=default
 
virtual bool canSerialize () const =0
 
virtual bool load (const SerializedItem &item)=0
 
virtual bool save (SerializedItem &item) const =0
 

Protected Member Functions

 ObjectGroup ()
 
void clear ()
 
ObjectGroupoperator= (const ObjectGroup &rhs)
 
bool operator== (const ObjectGroup &rhs) const
 
void swap (ObjectGroup &rhs)
 

Detailed Description

The main purpose of the ObjectGroup class is to automate the serialization of a group of objects (possibly a whole hierarchy of objects). It holds no data of its own, but instead it retrieves what it needs on demand from the subclass via the getObjects() virtual function, which is the only function subclasses need to provide.

The most common way to use the class is to make it a base of a class and add objects using push_back in the subclass' constructor. If an empty object group is needed for some purpose, the ObjectGroup class can be instantiated as is.

Constructor & Destructor Documentation

◆ ObjectGroup()

ObjectGroup ( )
protected

Constructs an empty object group.

◆ ~ObjectGroup()

~ObjectGroup ( )
overridedefault

Cleans up the private implementation.

Member Function Documentation

◆ add()

bool add ( const QString &  name,
DataObject obj 
)
Parameters
nameThe name for the object. This must be unique among all the immediate children of this object group and it cannot be changed once the object has been added.
objThe object to append into the group.

This function is entirely equivalent to calling insert() with the optional index parameter omitted.

Returns
True if the object could be added. The only reason that adding should fail is if the specified name is already part of the group.

◆ canSerialize()

bool canSerialize ( ) const
overridevirtual
Returns
True, since it should always be possible to serialize an ObjectGroup.

Implements Serialize.

◆ clear()

void clear ( )
protected

Clear the group. This call is not recursive, so any data objects that the group previously contained will be left unmodified.

◆ clone()

ObjectGroup * clone ( ) const
overridepure virtual

◆ empty()

bool empty ( ) const
Returns
True if the group is empty.

◆ ensureGroupHasData()

void ensureGroupHasData ( )

Recursively ensures that all children of this group have valid data.

See also
DataObject::ensureHasData()

◆ erase()

void erase ( int  index)
Parameters
indexThe index of the child to remove from the group.
Precondition
index must refer to a valid child.

Erases the child at the specified index.

◆ getChild() [1/2]

DataObject * getChild ( const QString &  name)
Parameters
nameThe name to look for among the immediate children of the group.
Returns
The data object for the immediate child with the specified name, or a null pointer if no child could be found with that name. The names of further descended children are not checked.

◆ getChild() [2/2]

const DataObject * getChild ( const QString &  name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ getDataObject() [1/2]

DataObject & getDataObject ( int  index)
Parameters
indexThe index of the object to retrieve.
Returns
The object of the group at index. No check is made to ensure that index is valid.
See also
getName()

◆ getDataObject() [2/2]

const DataObject & getDataObject ( int  index) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ getIndex()

int getIndex ( const QString &  name) const
Parameters
nameThe name to look for among the immediate children of the group.
Returns
The index of the child among its children. -1 if the child does not exist as an immediate child of the object gorup.

◆ getName()

const QString & getName ( int  index) const
Parameters
indexThe index of the name to retrieve.
Returns
The name at index. No check is made to ensure that index is valid.
See also
getDataObject()

◆ getPreferedWidget()

QString getPreferedWidget ( const QString &  name) const
virtual
Returns
The full namespaced name of the preferred widget for this object or empty string for default

◆ hasData()

bool hasData ( ) const

return True if all children of this group have valid data.

See also
DataObject::hasData()

◆ haveName()

bool haveName ( const QString &  name) const
Parameters
nameThe name to look for among the immediate children of the group.
Returns
True if one of the immediate children of the group already has the specified name. The names of further descended children are not checked.

◆ insert()

bool insert ( const QString &  name,
DataObject obj,
int  index = -1 
)
Parameters
nameThe name for the object. This must be unique among all the immediate children of this object group and it cannot be changed once the object has been added.
objThe object to insert into the group.
indexThe index before which to insert the object. If this is negative, then obj will be appended to the group.

Object groups can themselves be part of a group, so it is possible to define a whole hierarchy of data object relationships with the ObjectGroup class by passing an ObjectGroup as the obj parameter.

Returns
True if the object could be inserted. The only reason insertion should fail is if the specified name is already part of the group.

◆ load() [1/2]

bool load ( const QJsonDocument doc)

◆ load() [2/2]

bool load ( const SerializedItem item)
overridevirtual
Parameters
itemSupplies the state to be loaded into this object group.

This function will load the state of this object group from item. It will usually be called by an implementation of Serialize::load.

Precondition
The immediate children of item must all have unique tag names. Data saved by the save function is guaranteed to satisfy this.

Implements Serialize.

◆ operator=()

ObjectGroup & operator= ( const ObjectGroup rhs)
protected
Parameters
rhsThe other object group being assigned to this one.
Returns
The object group itself.

Assigns the matching members of one object group to this object group. Does not add new members to the object group. Note that this operator is only accessible by derived classes who must explicitly invoke it. If your derived class contains members that are not assignable, you will probably want to define your own assignment operator implementation rather than using this one.

Note
Only ObjectGroup subclasses that contain a pointer to an implementation (pImpl) will need to invoke this method, as the compiler generated assignment operator will work just fine for object groups where the members are defined directly in the public section of the class definition.

◆ operator==()

bool operator== ( const ObjectGroup rhs) const
protected
Parameters
rhsThe second operand in the comparison
Returns
true if all members of the object group are equal, false otherwise.

◆ save() [1/2]

bool save ( QJsonDocument doc) const

◆ save() [2/2]

bool save ( SerializedItem item) const
overridevirtual
Parameters
itemWhere to save the state of this object group.

This function will serialise the object group by saving each object it contains to item.

This function is the reason why the group's children must all have unique tag names. If they didn't, there would be no way to re-load the data we save here into the right children again.

See also
Serialize::save

Implements Serialize.

◆ size()

unsigned size ( ) const
Returns
The number of objects in the group. This count is only for objects immediately contained in this group. It does not count any sub-objects below them.

◆ swap()

void swap ( ObjectGroup rhs)
protected

Swaps one object group with another one.