]> git.mxchange.org Git - simgear.git/commitdiff
hla: Add virtual method for creating data elements.
authorMathias Froehlich <Mathias.Froehlich@web.de>
Sun, 12 Aug 2012 09:45:10 +0000 (11:45 +0200)
committerMathias Froehlich <Mathias.Froehlich@web.de>
Sun, 12 Aug 2012 10:06:42 +0000 (12:06 +0200)
simgear/hla/HLAObjectClass.cxx
simgear/hla/HLAObjectClass.hxx
simgear/hla/HLAObjectInstance.cxx
simgear/hla/HLAObjectInstance.hxx

index 3385f6952ad0c7c9a217377106c81a3b91fa2d7b..aa9e7856f2889fc57dfc3fc7d182f3e8c4d9d4a9 100644 (file)
@@ -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());
 }
 
index ba6ee5c911cf632c10cd3c5202b4614c9c46156a..d80e4f8d5988d9527e39dde7be1f85471f2bbd7b 100644 (file)
@@ -100,6 +100,11 @@ public:
     const SGSharedPtr<InstanceCallback>& 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&);
index f1b7fed07a52085103ae06422646bdf6b9f6c28f..4b32ca084b3af1b3bd6aa730e6c87dff24caf781 100644 (file)
@@ -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<HLADataElement> 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)
 {
index f99be8349803cd20018b57510a5a2c51c1ac709a..bc78ada43f733146fb047118470657de9881efc7 100644 (file)
@@ -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);