TemplateTypeInfo Class Template Reference

#include <rtt/TemplateTypeInfo.hpp>

Inheritance diagram for TemplateTypeInfo:

Inheritance graph
[legend]
List of all members.

Detailed Description

template<typename T, bool use_ostream = false>
class RTT::TemplateTypeInfo< T, use_ostream >

This helper class allows user types to be added to Orocos.

It provides 'default' implementations for each conversion type. For user defined types, this is very likely not satisfactory and the user needs to override the methods of this class in a subclass or provide specialised global functions.

Parameters:
T The user class type.
ParamT The type used to pass the value as a parameter. This defaults to T, but if T allocates upon copy (like std::string or std::vector), ParamT tells Orocos how to pass it, probably by const& or &.
use_ostream When set to true, the class will use operator<<(std::ostream&, T) to write out the type to a stream. When set to false, the class will use this function and write '( type name )' to a stream instead. Defaults to false. Set to true if your class T has the above mentioned function.
See also:
TypeInfoRepository.

composeType

decomposeType

operator<<

composeTypeImpl() and decomposeTypeImpl()

The decomposeTypeImpl function is a function which converts user data into property bags of native C++ primitive types. The primitive types are then typically used to write the data to screen or to disk. The inverse operation is done by composeTypeImpl, which reassembles a Property from its composite parts.

Every new user type used as a property will require these two functions if it must be 'written to a file'. The subclass must implement these functions by using composeTypeImpl() and decomposeTypeImpl(). such that class T is decomposed in primitive property types.

Example for type MyClass :

  struct MyClass {
      bool var1;
      double var2;
  };

  bool decomposeTypeImpl(const MyClass &c, PropertyBag& result)
  {
      // Decode c into primitive properties Var1 and Var2 which are in a PropertyBag of the
      // bag-type "MyClass" with the same name as the Property 'c'.
            result.setType("MyClass");

      // Put var1 and var2 in the bag
      result.add( new Property<bool>("Var1","Description", c.var1 );
      result.add( new Property<double("Var2","Description", c.var2 );
            
      return true; // done !
  } 
  bool composeTypeImpl(const PropertyBag& bag, MyClass &result)
  {
      // Read the variables from the bag and write them to result.
            if ( bag.getType() != "MyClass") {
         std::cerr << "Wrong type encountered when composing "<< this->getTypeName()<<std::endl;
         return false;
      }
      if ( bag.find("Var1") == 0 || bag.find("Var2") == 0 ) { 
         std::cerr << "Missing properties when composing "<< this->getTypeName()<<std::endl;
         return false;
      }
      // Read var1 and var2 from the bag
      result.var1 = bag.getProperty<bool>("Var1")->get();
      result.var2 = bag.getProperty<bool>("Var2")->get();
            
      return true; // done !
  } 


Public Types

typedef T UserType
 The given T parameter is the type for reading DataSources.
typedef Property< T >::DataSourceType PropertyType
 When Properties of T are constructed, they are non-const, non-reference.

Public Member Functions

 TemplateTypeInfo (std::string name)
 Setup Type Information for type name.
AttributeBasebuildConstant (std::string name, DataSourceBase::shared_ptr dsb) const
AttributeBasebuildVariable (std::string name) const
AttributeBasebuildAttribute (std::string name, DataSourceBase::shared_ptr in) const
 Build an Attribute of this type.
AttributeBasebuildAlias (std::string name, DataSourceBase::shared_ptr in) const
 build an alias with b as the value.
virtual const std::string & getTypeName () const
 Return unique the type name.
virtual PropertyBasebuildProperty (const std::string &name, const std::string &desc, DataSourceBase::shared_ptr source=0) const
 Build a Property of this type.
virtual DataSourceBase::shared_ptr buildValue () const
 Build a ValueDataSource of this type.
virtual std::ostream & write (std::ostream &os, DataSourceBase::shared_ptr in) const
 Output this datasource as a human readable string.
virtual bool decomposeType (DataSourceBase::shared_ptr source, PropertyBag &targetbag) const
 Decompose a structure as basic components into a PropertyBag.
virtual bool decomposeTypeImpl (typename AssignableDataSource< T >::const_reference_t source, PropertyBag &targetbag) const
 User, implement this function.
virtual bool composeType (DataSourceBase::shared_ptr source, DataSourceBase::shared_ptr result) const
 Compose a structure from a PropertyBase containing its basic components.
virtual bool composeTypeImpl (const PropertyBag &source, typename AssignableDataSource< T >::reference_t result) const
 User, implement this function.
virtual CORBA::Any * createAny (DataSourceBase::shared_ptr source) const
 Create an Any object which contains the value of source.
virtual bool update (const CORBA::Any &any, DataSourceBase::shared_ptr target) const
 Update target with the contents of any.
virtual DataSourceBasebuildCorbaProxy (Corba::Expression *e) const
 Create a DataSource which is a proxy for a Corba Expression object.
virtual AttributeBasebuildConstant (std::string name, DataSourceBase::shared_ptr, int sizehint) const
 Build a non modifyable instance of this type.
virtual AttributeBasebuildVariable (std::string name, int sizehint) const
 Build a modifyable instance of this type.
virtual DataSourceBase::shared_ptr construct (const std::vector< DataSourceBase::shared_ptr > &args) const
 Constructor syntax: construct a DataSource which returns an instance of data depending on the given arguments.
virtual void addConstructor (TypeBuilder *tb)
 Add a constructor.
virtual std::string toString (DataSourceBase::shared_ptr in) const
 Usability function which converts data to a string.

Protected Types

typedef std::vector< TypeBuilder * > Constructors

Protected Attributes

Constructors constructors


Constructor & Destructor Documentation

TemplateTypeInfo ( std::string  name  )  [inline]

Setup Type Information for type name.

This causes a switch from 'unknown' type to basic type information for type T.

Parameters:
name the 'Orocos' type name.


Member Function Documentation

AttributeBase* buildAlias ( std::string  name,
DataSourceBase::shared_ptr  in 
) const [inline, virtual]

build an alias with b as the value.

If b is of the wrong type, 0 will be returned..

Implements TypeInfo.

virtual std::ostream& write ( std::ostream &  os,
DataSourceBase::shared_ptr  in 
) const [inline, virtual]

Output this datasource as a human readable string.

The default just writes the type name in parentheses to os.

Implements TypeInfo.

virtual bool decomposeType ( DataSourceBase::shared_ptr  source,
PropertyBag targetbag 
) const [inline, virtual]

Decompose a structure as basic components into a PropertyBag.

Return values:
true decomposition resulted in new types added to targetbag.
false nothing was added to targetbag.

Implements TypeInfo.

virtual bool decomposeTypeImpl ( typename AssignableDataSource< T >::const_reference_t  source,
PropertyBag targetbag 
) const [inline, virtual]

User, implement this function.

Add the structural elements of source to targetbag.

virtual bool composeType ( DataSourceBase::shared_ptr  source,
DataSourceBase::shared_ptr  result 
) const [inline, virtual]

Compose a structure from a PropertyBase containing its basic components.

The default behavior tries to assign source to target. If this does not work, because source and target have different type, this function returns false.

Implements TypeInfo.

virtual bool composeTypeImpl ( const PropertyBag source,
typename AssignableDataSource< T >::reference_t  result 
) const [inline, virtual]

User, implement this function.

Extract the structural elements in source to result.

virtual AttributeBase* buildConstant ( std::string  name,
DataSourceBase::shared_ptr  ,
int  sizehint 
) const [virtual, inherited]

Build a non modifyable instance of this type.

Parameters:
sizehint For variable size instances, use it to hint the size of the instance.

virtual AttributeBase* buildVariable ( std::string  name,
int  sizehint 
) const [virtual, inherited]

Build a modifyable instance of this type.

Parameters:
sizehint For variable size instances, use it to hint the size of the instance.

Reimplemented in TemplateContainerTypeInfo.

virtual DataSourceBase::shared_ptr construct ( const std::vector< DataSourceBase::shared_ptr > &  args  )  const [virtual, inherited]

Constructor syntax: construct a DataSource which returns an instance of data depending on the given arguments.

When args is empty, the default 'variable' is returned.

Reimplemented in EmptyTypeInfo.


The documentation for this class was generated from the following file:
Generated on Fri May 11 11:19:46 2007 for Orocos by  doxygen 1.5.1