⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sub_person.cpp

📁 一个用HLA编写的聊天程序
💻 CPP
字号:
//SubPerson.cpp: the implementation of the CSubPerson class
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#if KDRTI_VERSION < 7
#include <RTI.h>
#else
#include <RTI.hh>
#include <fedtime.hh>
#endif

#include "Sub_Person.h"
#include "Global.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//}}FW_PUBLIC_STATIC_ATTRIBUTE_DEFINE_END <<----- Don't delete it!>>
RTI::ObjectClassHandle	CSub_Person::ms_PersonId = 0;
char*					CSub_Person::ms_PersonStr = "Person";
RTI::AttributeHandle	CSub_Person::ms_ageId = 0;
char*					CSub_Person::ms_ageStr = "age";
RTI::AttributeHandle	CSub_Person::ms_real_nameId = 0;
char*					CSub_Person::ms_real_nameStr = "real_name";
RTI::AttributeHandle	CSub_Person::ms_user_nameId = 0;
char*					CSub_Person::ms_user_nameStr = "user_name";
CStringArray			CSub_Person::m_AttrNamesArray;
//}}FW_PUBLIC_STATIC_ATTRIBUTE_DEFINE_END <<----- Don't delete it!>>

CSub_Person::CSub_Person()
{
	m_ObjClassName = "Person";
	ms_IsReflectedage = RTI::RTI_FALSE;
	ms_IsReflectedreal_name = RTI::RTI_FALSE;
	ms_IsReflecteduser_name = RTI::RTI_FALSE;
}

RTI::AttributeHandle CSub_Person::FindAttributeHandle(CString theAttrName)
{
	RTI::AttributeHandle theFindValue = ATTRNOHANDLE;

	if(theAttrName == ms_ageStr) {
		theFindValue = ms_ageId;
	} else if(theAttrName == ms_real_nameStr) {
		theFindValue = ms_real_nameId;
	} else if(theAttrName == ms_user_nameStr) {
		theFindValue = ms_user_nameId;
	} else {
		theFindValue = CFWBaseClass::FindAttributeHandle(theAttrName);
	}

	return theFindValue;
}

RTI::AttributeHandleSet* CSub_Person::GetAttributeHandleSet(CStringArray& theAttrNames)
{
	if(theAttrNames.GetSize() == 0) {
		return NULL;
	}
	RTI::AttributeHandleSet *theAttrHandles = 
		RTI::AttributeHandleSetFactory::create(theAttrNames.GetSize());

	RTI::AttributeHandle theHandle = ATTRNOHANDLE;
	for(int i = 0; i < theAttrNames.GetSize(); i++) {
		theHandle = FindAttributeHandle(theAttrNames.GetAt(i));
		if(ATTRNOHANDLE != theHandle) {
			theAttrHandles->add(theHandle);			
		}
		theHandle = ATTRNOHANDLE;
	}

	return theAttrHandles;
}

RTI::Boolean CSub_Person::GetReflectedStatusOfAttr(CString theAttrName)
{
	RTI::Boolean theStatus = RTI::RTI_FALSE;

	if(theAttrName == ms_ageStr) {
		theStatus = ms_IsReflectedage;
	} else if(theAttrName == ms_real_nameStr) {
		theStatus = ms_IsReflectedreal_name;
	} else if(theAttrName == ms_user_nameStr) {
		theStatus = ms_IsReflecteduser_name;
	} else {
		theStatus = CFWBaseClass::GetReflectedStatusOfAttr(theAttrName);
	}

	return theStatus;
}

void CSub_Person::SetReflectedStatusOfAttrs(CStringArray &theAttrNames, RTI::Boolean theStatus)
{
	for(int i = 0; i < theAttrNames.GetSize(); i++) {
		CString theAttrName = theAttrNames.GetAt(i);
		if(theAttrName == ms_ageStr) {
			ms_IsReflectedage = theStatus;
		} else if(theAttrName == ms_real_nameStr) {
			ms_IsReflectedreal_name = theStatus;
		} else if(theAttrName == ms_user_nameStr) {
			ms_IsReflecteduser_name = theStatus;
		}
	}
	CFWBaseClass::SetReflectedStatusOfAttrs(theAttrNames, theStatus);
}

void CSub_Person::ReflectAttributeValues(const RTI::AttributeHandleValuePairSet &theAttributes, const RTI::FedTime &theTime)
{
	try{
		BEGIN_UNPACKING_ATTRIBUTES_DATA(&theAttributes)
			UNPACKING_BASIC_ATTRIBUTEDATA(m_age, ms_ageStr)
			UNPACKING_STRING_ATTRIBUTEDATA(m_real_name, ms_real_nameStr)
			UNPACKING_STRING_ATTRIBUTEDATA(m_user_name, ms_user_nameStr)
		END_UNPACKING_ATTRIBUTES_DATA()

		SetReflectedStatusOfAttrs(m_AttrNamesArray, RTI::RTI_TRUE);
		CFWBaseClass::ReflectAttributeValues(theAttributes);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CSub_Person::ReflectAttributeValues(const RTI::AttributeHandleValuePairSet&, const RTI::FedTime&)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void CSub_Person::ReflectAttributeValues(const RTI::AttributeHandleValuePairSet &theAttributes)
{
	try{
		BEGIN_UNPACKING_ATTRIBUTES_DATA(&theAttributes)
			UNPACKING_BASIC_ATTRIBUTEDATA(m_age, ms_ageStr)
			UNPACKING_STRING_ATTRIBUTEDATA(m_real_name, ms_real_nameStr)
			UNPACKING_STRING_ATTRIBUTEDATA(m_user_name, ms_user_nameStr)
		END_UNPACKING_ATTRIBUTES_DATA()

		CFWBaseClass::ReflectAttributeValues(theAttributes);
		SetReflectedStatusOfAttrs(m_AttrNamesArray, RTI::RTI_TRUE);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CSub_Person::ReflectAttributeValues(const RTI::AttributeHandleValuePairSet&, const RTI::FedTime&)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

unsigned long CSub_Person::GetAttributeCount()
{
	return m_AttrNamesArray.GetSize();
}

RTI::ObjectClassHandle CSub_Person::GetClassId()
{
	return ms_PersonId;
}

void CSub_Person::Init(RTI::ObjectClassHandle theObject)
{
	try{
		ms_PersonId = m_RtiAmb->getObjectClassHandle(ms_PersonStr);
		ms_ageId = m_RtiAmb->getAttributeHandle(ms_ageStr,ms_PersonId);
		ms_real_nameId = m_RtiAmb->getAttributeHandle(ms_real_nameStr,ms_PersonId);
		ms_user_nameId = m_RtiAmb->getAttributeHandle(ms_user_nameStr,ms_PersonId);

		CFWBaseClass::Init(ms_PersonId);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CSub_Person::Init(RTI::ObjectClassHandle)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void CSub_Person::Subscribing()
{
	try{
		RTI::AttributeHandleSet* theAttrHandles = NULL;
		
		//the subscribed attributes of this student
		m_AttrNamesArray.RemoveAll();
		m_AttrNamesArray.Add("age");
		m_AttrNamesArray.Add("real_name");
		m_AttrNamesArray.Add("user_name");
		
		theAttrHandles = GetAttributeHandleSet(m_AttrNamesArray);
		m_RtiAmb->subscribeObjectClassAttributes(ms_PersonId, *theAttrHandles);
		delete theAttrHandles;
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CSub_Person::Subscribing()\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void CSub_Person::Unsubscribed()
{
	try{
		m_RtiAmb->unsubscribeObjectClass(ms_PersonId);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CSub_Person::Unsubscribed()\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -