orocos 的数据表示(模板template): DataObject (还有一条主线为 DataSource)

本文详细介绍了C++中不同类型的数据处理方式,包括基本类型、引用、指针等,并探讨了C++11引入的通用引用和别名模板如何简化代码。通过具体的模板实例,展示了如何获取不同类型的数据信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据有不同的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值