TAO下IDL结构定义编译文件分析2005-09-21 lwx
每个IDL的定义的结构经过TAOIDL编译器编译将生成一些结构编码的函数的定义。主要分为TypeCode的处理函数和Any的处理函及其CDR编码的处理。每个IDL的定义的结构经过TAOIDL编译器编译将生成一些结构编码的函数的定义。主要分为TypeCode的处理函数和Any的处理函及其CDR编码的处理。由结构到Any的流化和解析,析构,及其结构与TypedCode处理组成。结构到Any类型是通过TAO的内部模板TAO::Any_Dual_Impl_T来实现。结构流编码通过TAO_OutputCDR,TAO_inputCDR.
IDL定义如下
// 告警信息定义
struct AlarmInfo {
GeneralizedTime eventTime;
ObjectInstance source;
ObjectClass sourceClass;
NotificationIdentifier notificationId;
CorrelatedNotifications alarmInfoCorrelatedNotifications;
ProbableCause alarmInfoProbableCause;
SpecificProblems alarmInfoSpecificProblems;
PerceivedSeverity alarmInfoPerceivedSeverity;
boolean backedUpStatus;
ObjectInstance backUpObject;
TrendIndication alarmInfoTrendIndication;
ThresholdInfo alarmInfoThresholdInfo;
AttributeChangeList stateChangeDefinition;
AttributeList monitoredAttributes;
ProposedRepairActions alarmInfoProposedRepairActions;
AdditionalString additionalText;
AdditionalInformation additionalInfo;
};
编译生成的文件*C.h/cpp
struct AlarmInfo
{
typedef AlarmInfo_var _var_type;
static void _tao_any_destructor (void *);
TAO_String_Manager eventTime;
nms::ObjectInstance source;
nms::ObjectClass sourceClass;
nms::NotificationIdentifier notificationId;
nms::CorrelatedNotifications alarmInfoCorrelatedNotifications;
nms::ProbableCause alarmInfoProbableCause;
nms::SpecificProblems alarmInfoSpecificProblems;
nms::PerceivedSeverity alarmInfoPerceivedSeverity;
CORBA::Boolean backedUpStatus;
nms::ObjectInstance backUpObject;
nms::TrendIndication alarmInfoTrendIndication;
nms::ThresholdInfo alarmInfoThresholdInfo;
nms::AttributeChangeList stateChangeDefinition;
nms::AttributeList monitoredAttributes;
nms::ProposedRepairActions alarmInfoProposedRepairActions;
TAO_String_Manager additionalText;
nms::AdditionalInformation additionalInfo;
};
? 1 里面包含一个静态成员函数是析构AlarmInfo指针内存空间
void
nms::AlarmInfo::_tao_any_destructor (
void *_tao_void_pointer
)
{
AlarmInfo *_tao_tmp_pointer =
ACE_static_cast (AlarmInfo *, _tao_void_pointer);
delete _tao_tmp_pointer;
}
在CPP还定义的Typdecode的函数,并设置函数指针
static CORBA::TypeCode _tc_TAO_tc_nms_AlarmInfo (
CORBA::tk_struct,
sizeof (_oc_nms_AlarmInfo),
(char *) &_oc_nms_AlarmInfo,
0,
sizeof (nms::AlarmInfo)
);
namespace nms
{
::CORBA::TypeCode_ptr _tc_AlarmInfo =
&_tc_TAO_tc_nms_AlarmInfo;
}
? 2 定义AlarmInfo 到的Any析构的函数
void
nms::AlarmInfo::_tao_any_destructor (
void *_tao_void_pointer
)
{
AlarmInfo *_tao_tmp_pointer =
ACE_static_cast (AlarmInfo *, _tao_void_pointer);
delete _tao_tmp_pointer;
}
? 3 结构对象AlarmInfo放入Any的打包
void operator<<= (
CORBA::Any &_tao_any,
const nms::AlarmInfo &_tao_elem
)
{
TAO::Any_Dual_Impl_T<nms::AlarmInfo>::insert_copy (
_tao_any,
nms::AlarmInfo::_tao_any_destructor,
nms::_tc_AlarmInfo,
_tao_elem
);
}
? 4 结构对象AlarmInfo从Any的解析出来
// Extraction to non-const pointer (deprecated).
CORBA::Boolean operator>>= (
const CORBA::Any &_tao_any,
nms::AlarmInfo *&_tao_elem
)
{
return _tao_any >>= ACE_const_cast (
const nms::AlarmInfo *&,
_tao_elem
);
}
? 结构与TAO内部的实现类的关系
TAO::Any_Dual_Impl_T<nms::AlarmInfo>::insert_copy (…)
具体的结构到Any的模板实现类,放入insert,吸取extract,结构对象指针value,编码,解码。
template<typename T>
class Any_Basic_Impl_T : public Any_Impl
{
public:
Any_Basic_Impl_T (CORBA::TypeCode_ptr,
const T & val);
virtual ~Any_Basic_Impl_T (void);
static void insert (CORBA::Any &,
CORBA::TypeCode_ptr,
const T &);
static CORBA::Boolean extract (const CORBA::Any &,
CORBA::TypeCode_ptr,
T &);
virtual CORBA::Boolean marshal_value (TAO_OutputCDR &);
CORBA::Boolean demarshal_value (TAO_InputCDR &);
virtual void _tao_decode (TAO_InputCDR &
ACE_ENV_ARG_DECL_WITH_DEFAULTS);
virtual const void *value (void) const;
static Any_Basic_Impl_T<T> *create_empty (CORBA::TypeCode_ptr);
private:
T value_;
};
? 在编译的IDL的文件中产生了一个*C.i的文件,该文件主要是将定义的数据结构类型流化到CDR的对象中,及其从CDR流中解析出来。
? 结构到ANY的操作符
void operator<<= (CORBA::Any &, const nms::AlarmInfo &); // copying version
void operator<<= (CORBA::Any &, nms::AlarmInfo*); // noncopying version
CORBA::Boolean operator>>= (const CORBA::Any &, nms::AlarmInfo *&); // deprecated
CORBA::Boolean operator>>= (const CORBA::Any &, const nms::AlarmInfo *&);
? 结构到CDR的流化和解码处理
CORBA::Boolean operator<< (TAO_OutputCDR &, const nms::AlarmInfo &);
CORBA::Boolean operator>> (TAO_InputCDR &, nms::AlarmInfo &);
流化成CDR格式的流
ACE_INLINE
CORBA::Boolean operator<< (
TAO_OutputCDR &strm,
const nms::AlarmInfo &_tao_aggregate
)
{
return
(strm << _tao_aggregate.eventTime.in ()) &&
(strm << _tao_aggregate.source) &&
(strm << _tao_aggregate.sourceClass) &&
(strm << _tao_aggregate.notificationId) &&
(strm << _tao_aggregate.alarmInfoCorrelatedNotifications) &&
(strm << _tao_aggregate.alarmInfoProbableCause) &&
(strm << _tao_aggregate.alarmInfoSpecificProblems) &&
(strm << _tao_aggregate.alarmInfoPerceivedSeverity) &&
(strm << CORBA::Any::from_boolean (_tao_aggregate.backedUpStatus)) &&
(strm << _tao_aggregate.backUpObject) &&
(strm << _tao_aggregate.alarmInfoTrendIndication) &&
(strm << _tao_aggregate.alarmInfoThresholdInfo) &&
(strm << _tao_aggregate.stateChangeDefinition) &&
(strm << _tao_aggregate.monitoredAttributes) &&
(strm << _tao_aggregate.alarmInfoProposedRepairActions) &&
(strm << _tao_aggregate.additionalText.in ()) &&
(strm << _tao_aggregate.additionalInfo);
}
可以见到IDL的数据结构与流化是比较独立分开的。CORBA的CDR编码是通过IDL中规范定义的基本数据类型来判断类型的流化。经过编译的IDL定义的生成的C++结构是比较干净,基本不带其他信息,或者成员,所以在编写CORBA的程序的时候,我们不要再定义相同的C++数据结构,我们可以直接使用IDL编译后的C++结构。