WvStreams
Classes | Defines | Functions
utils.h File Reference

Various utility functions, macros and templates. More...

#include <stddef.h>
#include <xplc/core.h>
#include <xplc/IWeakRef.h>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  UUID_Info
 Utility structure used for the interface map. More...
struct  IObjectImplInternal
 Helper internal structure. More...
class  WeakRef
 Common implementation of a weak reference. More...

Defines

#define UUID_MAP_BEGIN(component)   const UUID_Info component::xplc_iobject_uuids[] = {
 Start the interface map for "component".
#define UUID_MAP_ENTRY(iface)   { &iface##_IID, reinterpret_cast<ptrdiff_t>(static_cast<iface*>(reinterpret_cast<ThisXPLCComponent*>(1))) - 1 },
 Add an entry to an interface map.
#define UUID_MAP_ENTRY_2(iface, iface2)   { &iface##_IID, reinterpret_cast<ptrdiff_t>(static_cast<iface2*>(reinterpret_cast<ThisXPLCComponent*>(1))) - 1 },
 Add an entry to an interface map for an ambiguous interface.
#define UUID_MAP_END   { 0, 0 } };
 Marks the end of an interface map.
#define xplcdelete   delete
 Internal macro.
#define IMPLEMENT_IOBJECT(component)
 Helper macro to implement the IObject methods automatically.

Functions

template<class Interface >
Interface * get (IObject *aObj)
 XPLC equivalent to dynamic_cast.
template<class Interface >
Interface * mutate (IObject *aObj)
 A version of get() that releases its parameter.

Detailed Description

Various utility functions, macros and templates.

Definition in file utils.h.


Define Documentation

#define UUID_MAP_ENTRY_2 (   iface,
  iface2 
)    { &iface##_IID, reinterpret_cast<ptrdiff_t>(static_cast<iface2*>(reinterpret_cast<ThisXPLCComponent*>(1))) - 1 },

Add an entry to an interface map for an ambiguous interface.

The second parameter is the interface that should be used to disambiguate.

Definition at line 75 of file utils.h.

#define xplcdelete   delete

Internal macro.

This macro is needed to cooperate correctly with the "delete detector".

Definition at line 112 of file utils.h.

#define IMPLEMENT_IOBJECT (   component)
Value:
private: \
  IObjectImplInternal xplc_iobject_internal; \
  static const UUID_Info xplc_iobject_uuids[]; \
  typedef component ThisXPLCComponent; \
public: \
  virtual unsigned int addRef() { \
    return ++xplc_iobject_internal.refcount; \
  } \
  virtual unsigned int release() { \
    if(--xplc_iobject_internal.refcount) \
      return xplc_iobject_internal.refcount; \
    /* protect against re-entering the destructor */ \
    xplc_iobject_internal.refcount = 1; \
    if(xplc_iobject_internal.weakref) { \
      xplc_iobject_internal.weakref->release(); \
      xplc_iobject_internal.weakref->object = 0; \
    } \
    xplcdelete this; \
    return 0; \
  } \
  virtual IObject* getInterface(const UUID& uuid) { \
    return xplc_iobject_internal.getInterface(this, uuid, xplc_iobject_uuids); \
  } \
  virtual IWeakRef* getWeakRef() { \
    if(!xplc_iobject_internal.weakref) \
      xplc_iobject_internal.weakref = new WeakRef(reinterpret_cast<IObject*>(reinterpret_cast<ptrdiff_t>(this) + xplc_iobject_uuids->delta)); \
    xplc_iobject_internal.weakref->addRef(); \
    return xplc_iobject_internal.weakref; \
  }

Helper macro to implement the IObject methods automatically.

Put this at the beginning of your class, specifiying the class name as the parameter, and it will automatically implement all the IObject methods. You also need to define an interface map.

See also:
UUID_MAP_BEGIN, UUID_MAP_ENTRY, UUID_MAP_ENTRY_2, UUID_MAP_END

Definition at line 123 of file utils.h.


Function Documentation

template<class Interface >
Interface* get ( IObject aObj)

XPLC equivalent to dynamic_cast.

This templated function is a typesafe way to call the getInterface method of a component and cast it properly. If the component does not support the interface, a NULL pointer will be returned.

Definition at line 184 of file utils.h.

template<class Interface >
Interface* mutate ( IObject aObj)

A version of get() that releases its parameter.

This templated function is very similar to the "get" one, except that it automatically releases the inbound reference, without regard whether the getInterface actually yielded something.

Definition at line 198 of file utils.h.

References IObject::getInterface(), and IObject::release().