BALL::RTTI Namespace Reference
[Common Definitions]


Functions

template<typename T >
const T & getDefault ()
template<typename T >
void * getNew ()
template<typename T >
const char * getName ()
template<typename T >
void * getClassID ()
template<typename T >
const char * getStreamName ()
template<typename T , typename U >
bool isKindOf (const U &u)
template<typename T , typename U >
T * castTo (const U &u)
template<typename T , typename U >
bool isInstanceOf (const U &u)

Detailed Description

Simplified RunTime Type Identification. ANSI C++ provides support for runtime type identification (RTTI). However, the support is very basic. The template functions of the RTTI namespace provide a more readable support for RTTI. It defines predicates such as isKindOf that simplify tests on the hereditary relationship of different objects.

To use the RTTI template functions, parametrize it with the type you are interested in. For example, to find out whether a given molecule is a protein, the following code can be used:
        Molecule& m =...;
        ...
        if (RTTI::isKindOf<Protein>(m)) 
        {
          // perform some protein specific operations
        } else {
          // this is only a molecule...
        }

Function Documentation

template<typename T , typename U >
T* BALL::RTTI::castTo ( const U &  u  )  [inline]

Cast an object to a specialized class. Example:

          Composite* composite = ...;
          ...
        
          // check whether the composite is also an atom
          if (RTTI::isKindOf<Atom>(composite))
          {
            // perform some atom specific actions
            Atom* atom = RTTI::castTo<Atom>(*composite);
            ...
            atom->setCharge(0);
            ...
          }
template<typename T >
void* BALL::RTTI::getClassID (  )  [inline]

Return a void pointer that is unique for each class.

template<typename T >
const T& BALL::RTTI::getDefault (  )  [inline]

Return a reference to a static default instance of the corresponding class. This method is basically intended to provide a default object for certain operations that require an instance of a certain class without really using this instance. It is mainly used inside the RTTI class.

template<typename T >
const char* BALL::RTTI::getName (  )  [inline]

Return the name of the class. This method returns the name of the class as given by typeid(<class instance>.name()). No additional name demangling and whitespace substitution are performed.

template<typename T >
void* BALL::RTTI::getNew (  )  [inline]

Return a void pointer to a new instance of the class. Use this method to provide an easy factory for objects of a certain class. The main use of this function lies in object persistence. The PersistenceManager needs a function for the dynamic creation of objects.

template<typename T >
const char* BALL::RTTI::getStreamName (  )  [inline]

Return the demangled class name. The class name is demangled (as far as possible) and in the resulting string blanks are substituted by underscores, so the name can be read from a stream as one string. The typical usage is something like

          String class_name = RTTI::getStreamName<Residue>();
          ...

References BALL::streamClassName().

template<typename T , typename U >
bool BALL::RTTI::isInstanceOf ( const U &  u  )  [inline]

Return true if a given object is an instance of a given class. If u is an instance of T, this predicate returns true. If u is an instance of a class that is derived from T or a base class of T, it returns false.

template<typename T , typename U >
bool BALL::RTTI::isKindOf ( const U &  u  )  [inline]

Return true if u is a kind of T. If u is an instance of a class derived from T, this predicate returns true:

          Protein p;

          // return true, since Protein is derived from Molecule
          bool is_molecule = RTTI::isKindOf<Molecule>(p);