golib  0.5
goObjectBase Class Reference

Base class for golib objects. More...

#include <goobjectbase.h>

Public Member Functions

 goObjectBase ()
 Constructor.
 
virtual ~goObjectBase ()
 Destructor. More...
 
const char * getClassName () const
 Returns the class name. More...
 
int getClassID () const
 
virtual goSize_t memoryUsage () const
 Returns the size of this object or some measure of its memory consumption. More...
 
void setObjectName (const char *name)
 Set name string for an object. More...
 
void setObjectName (const goString &name)
 Set name string for an object. More...
 
const goStringgetObjectName () const
 Get the object name. More...
 
virtual bool writeObjectFile (FILE *) const
 Write object to a file. More...
 
virtual bool readObjectFile (FILE *)
 Read object from a file. More...
 
void connectObject (goObjectBase *object)
 Connects an object to this object. More...
 
void disconnectObject (const goObjectBase *object)
 Disconnects an object from this object.
 
virtual bool callObjectMethod (int methodID, goObjectMethodParameters *param=NULL)
 Call an object method by identifier. More...
 
virtual bool queueObjectMethod (int methodID, goObjectMethodParameters *param=NULL, bool blocking=false)
 Enqueue a method call to an internal list of methods. More...
 
bool callQueuedMethods ()
 Call all queued methods. More...
 

Protected Member Functions

void setClassID (int id)
 Sets the class name.
 
void printClassMessage (const char *msg)
 Sets the class name. More...
 
void printClassMessage (goString &msg)
 Prints an informational message to the calling console. More...
 
void sendObjectMessage (int messageID, void *data=NULL)
 Sends a message to all connected objects.
 
void sendObjectMessage (goObjectBase *object, int messageID, void *data=NULL)
 Sends a message to a specific object.
 
virtual void receiveObjectMessage (const goObjectMessage &message)
 Receive a message. More...
 

Detailed Description

Base class for golib objects.

This is a base for all objects that we felt needed a common base. It implements class names, class message printing, and inter-object communication.

Note
The communication takes place by "message" functions. Each object can be connected to other objects using connectObject() and disconnectObject() calls. There are problems arising internally which I will describe now. The connected objects are maintained as a list of goObjectBase pointers. Problems arise in cases when sendObjectMessage() traverses the list and calls receiveObjectMessage() for each connected object. When somewhere during the call of receiveObjectMessage(), which can be arbitrarily complicated, the internal list gets changed with disconnectObject(), the initial list traversal will produce undefined results (i.e. segfault). To avoid this, the list is protected with a mutex together with a flag, and if the mutex is locked and an object gets disconnected, its pointer is simply set to NULL. Later, in case the list mutex is unlocked, the list gets cleaned by another internal function. See source code for details. However, if undefined behaviour should still appear, this is a possible source for problems. connectObject() should not be as problematic, because it simply appends to the end of the list.
Author
Christian Gosch
Todo:
The object messaging must be stable against re-entries and changing connections during message sending. There is no way to control what connected classes do during a receive call, so this is the place where everything must be stable. Already hardened against disconnecting while receiving, see sendObjectMessage() in the source code. Mutexes alone are of little help since they will lead to deadlocks here. The pointer in the connection list is therefore first set to NULL and later the list is cleaned up. Test this on multi-processor machines.
Todo:
Do writeObjectFile/readObjectFile as ASCII. Better to port/debug/change. Check if libxml is nice enough to use for this.
Author
Christian Gosch

Constructor & Destructor Documentation

◆ ~goObjectBase()

goObjectBase::~goObjectBase ( )
virtual

Destructor.

The destructor sends a GO_OBJECTMESSAGE_DESTRUCTING message to all connected objects.

Member Function Documentation

◆ callObjectMethod()

bool goObjectBase::callObjectMethod ( int  methodID,
goObjectMethodParameters param = NULL 
)
virtual

Call an object method by identifier.

See the file goobjectmessage.h for messages and add messages there if needed. External applications building on this class should use identifiers starting with GO_OBJECTMETHOD_USER.

Note
Reimplement this in sub-classes as needed. The method should return true if the method call was successful. Values can also be returned through the goObjectMethodParameters* param.
Parameters
methodIDID of the method. See goobjectmethod.h
paramPointer to parameters for the method, if any.
Returns
True if successful, false otherwise.

Reimplemented in goMatlab, goPointCloud< T >, and goCurve< T >.

◆ callQueuedMethods()

bool goObjectBase::callQueuedMethods ( )

Call all queued methods.

Calls all methods queued with queueObjectMethod().

Note
This method is thread-safe.
Returns
True if successful, false otherwise. If one of the methods failed, this method returns false.

◆ connectObject()

void goObjectBase::connectObject ( goObjectBase object)

Connects an object to this object.

Connected objects will receive messages sent by this object. So when you say
object1.connectObject (anotherobject)
then anotherobject will receive messages from object1.

Todo:
It may be necessary to only allow bi-directional connections.

◆ getClassName()

const char * goObjectBase::getClassName ( ) const

Returns the class name.

Returns
Name string for this class.

◆ getObjectName()

const goString & goObjectBase::getObjectName ( ) const

Get the object name.

Returns
Name of this object.
See also
setObjectName()

◆ memoryUsage()

goSize_t goObjectBase::memoryUsage ( ) const
virtual

Returns the size of this object or some measure of its memory consumption.

Overload this function as you please and as it makes sense.

Returns
Size in bytes of this object.

Reimplemented in goSignal2D< T >, goSignal3DBase< T >, goSignal2D< goFloat >, goSignal3DBase< void >, goSignal3D< T >, and goSignal3D< void >.

◆ printClassMessage() [1/2]

void goObjectBase::printClassMessage ( const char *  msg)
protected

Sets the class name.

Prints an informational message to the calling console.

Prints messages of the form "<classname> message: <your message>".

◆ printClassMessage() [2/2]

void goObjectBase::printClassMessage ( goString msg)
protected

Prints an informational message to the calling console.

Prints messages of the form "<classname> message: <your message>".

◆ queueObjectMethod()

bool goObjectBase::queueObjectMethod ( int  methodID,
goObjectMethodParameters param = NULL,
bool  blocking = false 
)
virtual

Enqueue a method call to an internal list of methods.

The queued methods can be called in one go by a call to callQueuedMethods().

Note
This method is thread-safe.
Parameters
methodIDID of the method. See goobjectmethod.h
paramPointer to a parameter structure, if any. The parameters will be deep-copied, so there is no need to worry about keeping them after the method returned.
Returns
True if successful, false otherwise.

◆ readObjectFile()

bool goObjectBase::readObjectFile ( FILE *  f)
virtual

Read object from a file.

Reimplement this in sub-classes.

Parameters
fValid C file pointer.
Returns
True if successful, false otherwise.

Reimplemented in goCurve< T >, and goPointCloud< T >.

◆ receiveObjectMessage()

void goObjectBase::receiveObjectMessage ( const goObjectMessage message)
protectedvirtual

Receive a message.

This function gets called each time another object "sends" a message to this object. Reimplement this in order to allow derived classes to react to messages.

Reimplemented in goPointCloud< T >, and goCurve< T >.

◆ setObjectName() [1/2]

void goObjectBase::setObjectName ( const char *  name)

Set name string for an object.

Parameters
nameName of the object.

◆ setObjectName() [2/2]

void goObjectBase::setObjectName ( const goString name)

Set name string for an object.

Parameters
nameName of the object.

◆ writeObjectFile()

bool goObjectBase::writeObjectFile ( FILE *  f) const
virtual

Write object to a file.

Reimplement this in sub-classes.

Parameters
fValid C file pointer.
Returns
True if successful, false otherwise.

Reimplemented in goCurve< T >, and goPointCloud< T >.


The documentation for this class was generated from the following files: