数据有不同的type(int,double,struct…),还有 c-v qualifier, 还可以是 reference、pointer、array等等。因此,在处理数据的时候要正确区分每一种数据类型的处理方法。
例如:不能对引用类型取引用,不能对右值取引用等等:
typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type value_t;
typedef typename boost::call_traits<value_t>::param_type param_t;
typedef typename boost::call_traits<value_t>::reference reference_t;
typedef typename boost::call_traits<value_t>::const_reference const_reference_t;
see Effective Modern C++ Deducing Types
c++11引入 universal reference( T&&, std::forward) 和 alias(using), 可以简化上述代码:
The forward template exists in C++11, in the
<utility>
header, as std::forward
see link universal-references: http://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/
orocos数据TypeInfo:
struct RTT_API UnknownType {};
template< class T>
struct DataSourceTypeInfo;
template<>
struct RTT_API DataSourceTypeInfo<UnknownType> {
typedef UnknownType value_type;
typedef DataSourceTypeInfo<UnknownType> value_type_info;
static types::TypeInfo* TypeInfoObject;
static const std::string noqual; // void
static const std::string cqual; // const
static const std::string refqual; // reference
static const std::string crefqual; // const reference
static const std::string ptrqual; // pointer
static const std::string cptrqual; // const pointer
static const std::string& getType(); // 都是 static 函数,包括下列各个特化模板,因此其他类只需要直接使用该模板即可得到其他类的数据类型信息
static const std::string& getTypeName();
static const std::string& getQualifier();
// we drop the const qualifier in this specialisation, since it is
// not registered in the type repository (which returns a non const in type() )
static types::TypeInfo* getTypeInfo(); // type_name、 qualifier、std::type_info * etc. compiler dependent info
};
template< class T>
struct DataSourceTypeInfo<const T&>
{
typedef T value_type;
typedef DataSourceTypeInfo<T> value_type_info;
static std::string getType() { return getTypeName() + getQualifier(); }
static const std::string& getTypeName() { return DataSourceTypeInfo< T >::getTypeName(); } // value_type 的类型
static const std::string& getQualifier() { return DataSourceTypeInfo<UnknownType>::crefqual; } //qualifier: 其他类型就换成别的字符串("const"、"&" 等等)
static const types::TypeInfo* getTypeInfo() { return DataSourceTypeInfo< T >::getTypeInfo(); }
};
// 以下类似
template< class T>
struct DataSourceTypeInfo<T&> {...}
template< class T>
struct DataSourceTypeInfo<const T> {...}
template< class T>
struct DataSourceTypeInfo<T*> {...}
template< class T>
struct DataSourceTypeInfo<const T*> {...}
template<>
struct RTT_API DataSourceTypeInfo<void> {...}
/**
* Specialisation for a types::carray<T> type info object.
* All RTT internal primitives should use carray references
* to (boost) arrays such that run-time size checks can be done.
* For example, Property<carray<double> > holds a
* ValueDataSource<carray<double> >( carray<double>( data.c_array(), 6 ) )
* where data is a boost::array<double,6> or equivalent
*/
template< class T>
struct DataSourceTypeInfo<types::carray<T> > {...}
Orocos DataObject类的结构:
DataObjectInterface
|
/ \
/ \
DataObjectLocked DataObjectLockFree
\ /
\ /
DataOjbect
DataObjectLocked 和 DataObjectLockFree 分别继承自DataObjectInterface; 然后 DataOject再继承 DataObjectLocked 和 DataObjectLockFree