dillo  3.0.5
About: dillo is a small, fast, extensible Web browser particularly suitable for older or smaller computers and embedded systems (but only limited or no support for frames, CSS, JavaScript, Java).
  Fossies Dox: dillo-3.0.5.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

lout::identity::IdentifiableObject Class Reference

Instances of classes, which are sub classes of this class, may be identified at run-time. More...

#include <identity.hh>

Inheritance diagram for lout::identity::IdentifiableObject:
[legend]
Collaboration diagram for lout::identity::IdentifiableObject:
[legend]

Classes

class  Class
 

Public Member Functions

 IdentifiableObject ()
 
virtual void intoStringBuffer (misc::StringBuffer *sb)
 Store a textual representation of the object in a misc::StringBuffer. More...
 
int getClassId ()
 Returns the class identifier. More...
 
const char * getClassName ()
 Return the name, under which the class of this object was registered. More...
 
bool instanceOf (int otherClassId)
 Returns, whether this class is an instance of the class, given by otherClassId, or of a sub class of this class. More...
 
- Public Member Functions inherited from lout::object::Object
virtual ~Object ()
 The destructor is defined as virtual (but not abstract), so that destruction of Object's works properly. More...
 
virtual bool equals (Object *other)
 Returns, whether two objects are equal. More...
 
virtual int hashValue ()
 Return a hash value for the object. More...
 
virtual Objectclone ()
 Return an exact copy of the object. More...
 
const char * toString ()
 Use object::Object::intoStringBuffer to return a textual representation of the object. More...
 
virtual size_t sizeOf ()
 Return the number of bytes, this object totally uses. More...
 

Protected Member Functions

void registerName (const char *className, int *classId)
 This method must be called in the constructor for the sub class. See class comment for details. More...
 

Private Attributes

int classId
 

Static Private Attributes

static container::typed::HashTable< object::ConstString, Class > * classesByName
 
static container::typed::Vector< Class > * classesById
 
static ClasscurrentlyConstructedClass
 

Detailed Description

Instances of classes, which are sub classes of this class, may be identified at run-time.

Testing the class

Since e.g. dw::Textblock is a sub class of IdentifiableObject, and implemented in the correct way (as described below), for any given IdentifiableObject the following test can be done:

identity::IdentifiableObject *o;
// ...
bool isATextblock = o->instanceOf(dw::Textblock::CLASS_ID);

isATextblock is true, when o is an instance of dw::Textblock, or of a sub class of dw::Textblock. Otherwise, isATextblock is false.

It is also possible to get the class identifier of an identity::IdentifiableObject, e.g.

bool isOnlyATextblock = o->getClassId() == dw::Textblock::CLASS_ID;

would result in true, if o is an instance of dw::Textblock, but not an instance of a sub class of dw::Textblock.

Defining Sub Classes

Each direct or indirect sub class of IdentifiableObject must

  • add a static int CLASS_ID with -1 as initial value, and
  • call registerName (name, &CLASS_ID) in the constructor, where name should be unique, e.g. the fully qualified class name.

After this, class::CLASS_ID refers to a number, which denotes the class. (If this is still -1, since the class has not yet been instantiated, any test will fail, which is correct.)

Notes on implementation

If there are some classes like this:

[legend]

and first, an instance of A, and then an instance of C is created, there will be the following calls of functions and constructors:

  1. IdentifiableObject ();
  2. registerName ("A", &A::CLASS_ID);
  3. IdentifiableObject ();
  4. registerName ("B", &B::CLASS_ID);
  5. registerName ("C", &C::CLASS_ID);

From this, the class hierarchy above can easily constructed, and stored in identity::IdentifiableObject::classesByName and in identity::IdentifiableObject::classesById. See the code for details.

N.b. Multiple inheritance is not supported, the construction of the tree would become confused.

Definition at line 98 of file identity.hh.

Constructor & Destructor Documentation

◆ IdentifiableObject()

lout::identity::IdentifiableObject::IdentifiableObject ( )

Definition at line 51 of file identity.cc.

Member Function Documentation

◆ getClassId()

int lout::identity::IdentifiableObject::getClassId ( )
inline

Returns the class identifier.

This is rarely used, rather, tests with identity::IdentifiableObject::instanceOf are done.

Definition at line 132 of file identity.hh.

References classId.

◆ getClassName()

const char* lout::identity::IdentifiableObject::getClassName ( )
inline

Return the name, under which the class of this object was registered.

Definition at line 138 of file identity.hh.

References classesById, and classId.

Referenced by dw::Textblock::printWordShort(), and dw::Textblock::wordWrap().

◆ instanceOf()

bool lout::identity::IdentifiableObject::instanceOf ( int  otherClassId)

Returns, whether this class is an instance of the class, given by otherClassId, or of a sub class of this class.

Definition at line 88 of file identity.cc.

References lout::identity::IdentifiableObject::Class::parent.

Referenced by dw::Table::getCellRef().

◆ intoStringBuffer()

void lout::identity::IdentifiableObject::intoStringBuffer ( misc::StringBuffer sb)
virtual

Store a textual representation of the object in a misc::StringBuffer.

This is used by object::Object::toString.

Reimplemented from lout::object::Object.

Definition at line 56 of file identity.cc.

References lout::misc::StringBuffer::append().

◆ registerName()

void lout::identity::IdentifiableObject::registerName ( const char *  className,
int *  classId 
)
protected

This method must be called in the constructor for the sub class. See class comment for details.

Definition at line 67 of file identity.cc.

References lout::identity::IdentifiableObject::Class::id.

Referenced by dw::AlignedTextblock::AlignedTextblock(), dw::ListItem::ListItem(), and dw::TableCell::TableCell().

Member Data Documentation

◆ classesById

Vector< IdentifiableObject::Class > * lout::identity::IdentifiableObject::classesById
staticprivate
Initial value:
=
new Vector <IdentifiableObject::Class> (16, false)

Definition at line 113 of file identity.hh.

Referenced by getClassName().

◆ classesByName

HashTable< ConstString, IdentifiableObject::Class > * lout::identity::IdentifiableObject::classesByName
staticprivate
Initial value:
=
new HashTable<ConstString, IdentifiableObject::Class> (true, true)

Definition at line 112 of file identity.hh.

◆ classId

int lout::identity::IdentifiableObject::classId
private

Definition at line 116 of file identity.hh.

Referenced by getClassId(), and getClassName().

◆ currentlyConstructedClass

IdentifiableObject::Class * lout::identity::IdentifiableObject::currentlyConstructedClass
staticprivate

Definition at line 114 of file identity.hh.


The documentation for this class was generated from the following files:
dw::Textblock::CLASS_ID
static int CLASS_ID
Definition: textblock.hh:669