#include <AbstractValue.h> [code]
Inheritance diagram for Utils::AbstractValue< AbstractType, NullObjectType, HierarchyRoot >:

Requirements for the template parameter AbstractType:
Template parameters:
AbstractValue can be specialized with any abstract data type that fulfills the above requirements. With its help, data objects, for which only the abstract base is known, can be treated as Value Objects - they can be copied and compared as if their concrete type was known. Thus, passing or returning values of unknow dynamic type is possible:
class DataBase {}; // Abstract class. class NullData : public DataBase {}; // Null Data Object. class ConcreteDataA : public DataBase {}; // Concrete Data implementation. class ConcreteDataB : public DataBase {}; // Concrete Data implementation. ... typedef AbstractValue <DataBase, NullData> Data; ... Data makeDataFromRawBytes (const QByteArray &bytes) { if (...) return Data (ConcreteDataA()); else return Data (ConcreteDataB()); }
Complex data hierarchies containing multiple leve of abstraction (and AbstractType classes) can also be modelled:
class DataBase {}; // Abstract class (hierarchy root). class NullData : DataBase {}; // Null Data Object. class PacketBase : public DataBase {}; // Abstract class. class NullPacket : public PacketBase {}; // Null Packet Object. class ConcretePacket : public PacketBase {}; // Concrete Packet impelementation. ... typedef AbstractValue <DataBase, NullData> Data; typedef AbstractValue <PacketBase, NullPacket, DataBase> Packet; ... void main() { Data data; // Creates a default Data object that // encapsulates a NullData object. Packet packet; // Creates a default Packet object that // encapsulates a NullPacket object. packet = ConcretePacket()); // Assigns the new ConcretePacket object to // packet, which will release the NullPacket // object and will encapsulate a copy of the // new ConcretePacket object. data = packet; // Assigning Packet to Data works! // Both packet and data encapsulate copies // of the ConcretePacket object. packet = data; // This assignment would work too! // If data encapsulates an object that // derives from DataBase but not from // PacketBase an std::bad_cast exception // will be thrown at runtime. If the types // match, the assignment will be done. }
The template parameter HierarchyRoot is needed because the virtual copy ctor returns a auto_ptr instead of a raw pointer. If Base::copy() returned a Base *, then Derived::copy() could return a Derived * and all would have been fine. But because copy() returns an auto_ptr <Base>, we are not allowed to return auto_ptr <Derived> in the derived class.
Definition at line 126 of file AbstractValue.h.
Public Member Functions | |
| AbstractValue () | |
| Create a Null AbstractValue object. | |
| AbstractValue (const AbstractType &object) | |
| Create an AbstractValue object encapsulating a copy of object. | |
| AbstractValue (const AbstractValue &other) | |
| AbstractValue copy ctor. | |
| AbstractValue & | operator= (const AbstractValue &other) |
| Assignment operator. | |
| bool | operator== (const AbstractValue &other) const |
| Compares the objects encapsulated by two AbstractValue objects. | |
| bool | operator!= (const AbstractValue &other) const |
| Compares the objects encapsulated by two AbstractValue objects. | |
| AbstractType & | operator * () |
| Indirection operator - returns a reference to the encapsulated object. | |
| const AbstractType & | operator * () const |
| Indirection operator - returns a reference to the encapsulated object. | |
| AbstractType * | operator-> () |
| Arrow operator - returns a pointer to the encapsulated object. | |
| const AbstractType * | operator-> () const |
| Arrow operator - returns a pointer to the encapsulated object. | |
| template<typename RelatedAbstractType, typename RelatedNullType> | |
| operator AbstractValue () const | |
| Cast operator (casts to other AbstractValue instantiations). | |
Private Attributes | |
| auto_ptr< HierarchyRoot > | object_ |
| An auto_ptr to the encapsulated object. | |
|
|||||||||
|
Create a Null AbstractValue object. The new object will encapsulate a NullObjectType object. Definition at line 133 of file AbstractValue.h. |
|
||||||||||
|
Create an AbstractValue object encapsulating a copy of object.
Definition at line 142 of file AbstractValue.h. |
|
||||||||||
|
AbstractValue copy ctor.
Definition at line 151 of file AbstractValue.h. |
|
|||||||||
|
Indirection operator - returns a reference to the encapsulated object.
Definition at line 206 of file AbstractValue.h. |
|
|||||||||
|
Indirection operator - returns a reference to the encapsulated object.
Definition at line 199 of file AbstractValue.h. |
|
|||||||||||||
|
Cast operator (casts to other AbstractValue instantiations). Code using this cast operator will always compile as long as AbstractType and RelatedAbstractType are in a is-a relationship. A compiler error will be produced otherwise. "Up-casts" from AbstractValue <DerivedType, ...> to AbstractValue <BaseType, ...> will always work. "Down-casts" from AbstractValue <BaseType, ...> to AbstractValue <DerivedType, ...> will throw an std::bad_cast exception at runtime if the convertion is invalid.
Definition at line 245 of file AbstractValue.h. |
|
||||||||||
|
Compares the objects encapsulated by two AbstractValue objects.
Definition at line 193 of file AbstractValue.h. |
|
|||||||||
|
Arrow operator - returns a pointer to the encapsulated object.
Definition at line 220 of file AbstractValue.h. |
|
|||||||||
|
Arrow operator - returns a pointer to the encapsulated object.
Definition at line 213 of file AbstractValue.h. |
|
||||||||||
|
Assignment operator.
Definition at line 163 of file AbstractValue.h. |
|
||||||||||
|
Compares the objects encapsulated by two AbstractValue objects.
Definition at line 178 of file AbstractValue.h. |
|
|||||
|
An auto_ptr to the encapsulated object.
Definition at line 257 of file AbstractValue.h. |