From: Mathias Froehlich Date: Sun, 12 Aug 2012 09:45:10 +0000 (+0200) Subject: hla: Add virtual method for creating data elements. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b18c92f5156b871268dcc7e8d7b82879d25baf31;p=simgear.git hla: Add virtual method for creating data elements. --- diff --git a/simgear/hla/HLAObjectClass.cxx b/simgear/hla/HLAObjectClass.cxx index 3385f695..aa9e7856 100644 --- a/simgear/hla/HLAObjectClass.cxx +++ b/simgear/hla/HLAObjectClass.cxx @@ -28,6 +28,7 @@ #include "RTIObjectClass.hxx" #include "RTIObjectInstance.hxx" #include "HLADataType.hxx" +#include "HLADataTypeVisitor.hxx" #include "HLAFederate.hxx" #include "HLAObjectInstance.hxx" @@ -286,6 +287,34 @@ HLAObjectClass::unpublish() return _rtiObjectClass->unpublish(); } +void +HLAObjectClass::discoverInstance(HLAObjectInstance& objectInstance, const RTIData& tag) +{ + if (_instanceCallback.valid()) + _instanceCallback->discoverInstance(*this, objectInstance, tag); +} + +void +HLAObjectClass::removeInstance(HLAObjectInstance& objectInstance, const RTIData& tag) +{ + if (_instanceCallback.valid()) + _instanceCallback->removeInstance(*this, objectInstance, tag); +} + +void +HLAObjectClass::registerInstance(HLAObjectInstance& objectInstance) +{ + if (_instanceCallback.valid()) + _instanceCallback->registerInstance(*this, objectInstance); +} + +void +HLAObjectClass::deleteInstance(HLAObjectInstance& objectInstance) +{ + if (_instanceCallback.valid()) + _instanceCallback->deleteInstance(*this, objectInstance); +} + void HLAObjectClass::startRegistration() const { @@ -305,6 +334,26 @@ HLAObjectClass::createObjectInstance(const std::string& name) return federate->createObjectInstance(this, name); } +void +HLAObjectClass::createAttributeDataElements(HLAObjectInstance& objectInstance) +{ + unsigned numAttributes = getNumAttributes(); + for (unsigned i = 0; i < numAttributes; ++i) + objectInstance.createAndSetAttributeDataElement(i); +} + +HLADataElement* +HLAObjectClass::createAttributeDataElement(HLAObjectInstance& objectInstance, unsigned index) +{ + // FIXME here we want to have a vector of factories and if this fails do the following + const HLADataType* dataType = getAttributeDataType(index); + if (!dataType) + return 0; + HLADataElementFactoryVisitor dataElementFactoryVisitor; + dataType->accept(dataElementFactoryVisitor); + return dataElementFactoryVisitor.getDataElement(); +} + void HLAObjectClass::_setRTIObjectClass(RTIObjectClass* objectClass) { @@ -363,8 +412,8 @@ HLAObjectClass::_discoverInstance(RTIObjectInstance* rtiObjectInstance, const RT << rtiObjectInstance->getName() << "\" object"); return; } - if (_instanceCallback.valid()) - _instanceCallback->discoverInstance(*this, *objectInstance, tag); + objectInstance->discoverInstance(tag); + objectInstance->createAttributeDataElements(); } void @@ -376,8 +425,7 @@ HLAObjectClass::_removeInstance(HLAObjectInstance& objectInstance, const RTIData return; } SG_LOG(SG_NETWORK, SG_INFO, "RTI: remove object instance \"" << objectInstance.getName() << "\""); - if (_instanceCallback.valid()) - _instanceCallback->removeInstance(*this, objectInstance, tag); + objectInstance.removeInstance(tag); federate->_eraseObjectInstance(objectInstance.getName()); } @@ -398,8 +446,8 @@ HLAObjectClass::_registerInstance(HLAObjectInstance* objectInstance) << objectInstance->getName() << "\" object"); return; } - if (_instanceCallback.valid()) - _instanceCallback->registerInstance(*this, *objectInstance); + registerInstance(*objectInstance); + objectInstance->createAttributeDataElements(); } void @@ -410,8 +458,7 @@ HLAObjectClass::_deleteInstance(HLAObjectInstance& objectInstance) SG_LOG(SG_NETWORK, SG_ALERT, "RTI: could not find parent federate while deleting object instance"); return; } - if (_instanceCallback.valid()) - _instanceCallback->deleteInstance(*this, objectInstance); + deleteInstance(objectInstance); federate->_eraseObjectInstance(objectInstance.getName()); } diff --git a/simgear/hla/HLAObjectClass.hxx b/simgear/hla/HLAObjectClass.hxx index ba6ee5c9..d80e4f8d 100644 --- a/simgear/hla/HLAObjectClass.hxx +++ b/simgear/hla/HLAObjectClass.hxx @@ -100,6 +100,11 @@ public: const SGSharedPtr& getInstanceCallback() const { return _instanceCallback; } + virtual void discoverInstance(HLAObjectInstance& objectInstance, const RTIData& tag); + virtual void removeInstance(HLAObjectInstance& objectInstance, const RTIData& tag); + virtual void registerInstance(HLAObjectInstance& objectInstance); + virtual void deleteInstance(HLAObjectInstance& objectInstance); + // Is called by the default registration callback if installed // Should register the already known object instances of this class. virtual void startRegistration() const; @@ -121,6 +126,9 @@ public: /// Create a new instance of this class. virtual HLAObjectInstance* createObjectInstance(const std::string& name); + virtual void createAttributeDataElements(HLAObjectInstance& objectInstance); + virtual HLADataElement* createAttributeDataElement(HLAObjectInstance& objectInstance, unsigned index); + private: HLAObjectClass(const HLAObjectClass&); HLAObjectClass& operator=(const HLAObjectClass&); diff --git a/simgear/hla/HLAObjectInstance.cxx b/simgear/hla/HLAObjectInstance.cxx index f1b7fed0..4b32ca08 100644 --- a/simgear/hla/HLAObjectInstance.cxx +++ b/simgear/hla/HLAObjectInstance.cxx @@ -442,6 +442,28 @@ HLAObjectInstance::setAttributes(const HLAAttributePathElementMap& attributePath } } +void +HLAObjectInstance::discoverInstance(const RTIData& tag) +{ + HLAObjectClass* objectClass = getObjectClass().get(); + if (!objectClass) { + SG_LOG(SG_IO, SG_ALERT, "Could not discover instance of unknown object class!"); + return; + } + objectClass->discoverInstance(*this, tag); +} + +void +HLAObjectInstance::removeInstance(const RTIData& tag) +{ + HLAObjectClass* objectClass = getObjectClass().get(); + if (!objectClass) { + SG_LOG(SG_IO, SG_ALERT, "Could not remove instance of unknown object class!"); + return; + } + objectClass->removeInstance(*this, tag); +} + void HLAObjectInstance::registerInstance() { @@ -489,6 +511,40 @@ HLAObjectInstance::deleteInstance(const RTIData& tag) _rtiObjectInstance->deleteObjectInstance(tag); } +void +HLAObjectInstance::createAttributeDataElements() +{ + HLAObjectClass* objectClass = getObjectClass().get(); + if (!objectClass) { + SG_LOG(SG_IO, SG_ALERT, "Could not create data elements for instance of unknown object class!"); + return; + } + objectClass->createAttributeDataElements(*this); +} + +void +HLAObjectInstance::createAndSetAttributeDataElement(unsigned index) +{ + if (getAttributeDataElement(index)) { + SG_LOG(SG_IO, SG_DEBUG, "Attribute data element for attribute \"" + << getAttributeName(index) << "\" is already set."); + return; + } + SGSharedPtr dataElement = createAttributeDataElement(index); + setAttributeDataElement(index, dataElement); +} + +HLADataElement* +HLAObjectInstance::createAttributeDataElement(unsigned index) +{ + HLAObjectClass* objectClass = getObjectClass().get(); + if (!objectClass) { + SG_LOG(SG_IO, SG_ALERT, "Could not create data element for instance of unknown object class!"); + return 0; + } + return objectClass->createAttributeDataElement(*this, index); +} + void HLAObjectInstance::updateAttributeValues(const RTIData& tag) { diff --git a/simgear/hla/HLAObjectInstance.hxx b/simgear/hla/HLAObjectInstance.hxx index f99be834..bc78ada4 100644 --- a/simgear/hla/HLAObjectInstance.hxx +++ b/simgear/hla/HLAObjectInstance.hxx @@ -77,9 +77,26 @@ public: void setAttribute(unsigned index, const HLAPathElementMap& pathElementMap); void setAttributes(const HLAAttributePathElementMap& attributePathElementMap); + /// Gets called on discovering this object instance. + virtual void discoverInstance(const RTIData& tag); + /// Gets called on remove this object instance. + virtual void removeInstance(const RTIData& tag); + + /// Call this to register the object instance at the rti and assign the object class to it. void registerInstance(); - void registerInstance(HLAObjectClass* objectClass); - void deleteInstance(const RTIData& tag); + virtual void registerInstance(HLAObjectClass* objectClass); + /// Call this to delete the object instance from the rti. + virtual void deleteInstance(const RTIData& tag); + + /// Is called when the instance is either registered or discovered. + /// It creates data elements for each element that is not yet set and that has a data type attached. + /// the default calls back into the object class createAttributeDataElements method. + virtual void createAttributeDataElements(); + /// Create and set the data element with index. Called somewhere in the above callchain. + void createAndSetAttributeDataElement(unsigned index); + /// Create an individual data element, the default calls back into the object class + /// createAttributeDataElement method. + virtual HLADataElement* createAttributeDataElement(unsigned index); // Push the current values into the RTI virtual void updateAttributeValues(const RTIData& tag);