]> git.mxchange.org Git - simgear.git/blobdiff - simgear/hla/HLAObjectInstance.cxx
hla: Remove deprecated methods from HLAObjectClass
[simgear.git] / simgear / hla / HLAObjectInstance.cxx
index 390b70db1ef9b24e0bd9dddbed096ab88532fc86..456da1b6245dfa77c5e4506d49adab0a9226088a 100644 (file)
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
+
+#include <simgear/compiler.h>
+
 #include "HLAObjectInstance.hxx"
 
 #include <algorithm>
@@ -41,9 +47,10 @@ HLAObjectInstance::ReflectCallback::~ReflectCallback()
 }
 
 HLAObjectInstance::HLAObjectInstance(HLAObjectClass* objectClass) :
-    _federate(objectClass->_federate),
     _objectClass(objectClass)
 {
+    if (objectClass)
+        _federate = objectClass->_federate;
 }
 
 HLAObjectInstance::~HLAObjectInstance()
@@ -51,21 +58,45 @@ HLAObjectInstance::~HLAObjectInstance()
     _clearRTIObjectInstance();
 }
 
+const std::string&
+HLAObjectInstance::getName() const
+{
+    return _name;
+}
+
+const SGWeakPtr<HLAFederate>&
+HLAObjectInstance::getFederate() const
+{
+    return _federate;
+}
+
+const SGSharedPtr<HLAObjectClass>&
+HLAObjectInstance::getObjectClass() const
+{
+    return _objectClass;
+}
+
 unsigned
 HLAObjectInstance::getNumAttributes() const
 {
+    if (!_objectClass.valid())
+        return 0;
     return _objectClass->getNumAttributes();
 }
 
 unsigned
 HLAObjectInstance::getAttributeIndex(const std::string& name) const
 {
+    if (!_objectClass.valid())
+        return ~0u;
     return _objectClass->getAttributeIndex(name);
 }
 
 std::string
 HLAObjectInstance::getAttributeName(unsigned index) const
 {
+    if (!_objectClass.valid())
+        return std::string();
     return _objectClass->getAttributeName(index);
 }
 
@@ -80,6 +111,8 @@ HLAObjectInstance::getAttributeOwned(unsigned index) const
 const HLADataType*
 HLAObjectInstance::getAttributeDataType(unsigned index) const
 {
+    if (!_objectClass.valid())
+        return 0;
     return _objectClass->getAttributeDataType(index);
 }
 
@@ -116,9 +149,11 @@ HLAObjectInstance::setAttributeDataElement(unsigned index, const SGSharedPtr<HLA
     if (numAttributes <= index)
         return;
     _attributeVector.resize(numAttributes);
+    if (_attributeVector[index]._dataElement.valid())
+        _attributeVector[index]._dataElement->clearStamp();
     _attributeVector[index]._dataElement = dataElement;
-    if (getAttributeOwned(index))
-        encodeAttributeValue(index);
+    if (_attributeVector[index]._dataElement.valid())
+        _attributeVector[index]._dataElement->createStamp();
 }
 
 class HLAObjectInstance::DataElementFactoryVisitor : public HLADataElementFactoryVisitor {
@@ -409,15 +444,27 @@ HLAObjectInstance::setAttributes(const HLAAttributePathElementMap& attributePath
 
 void
 HLAObjectInstance::registerInstance()
+{
+    registerInstance(_objectClass.get());
+}
+
+void
+HLAObjectInstance::registerInstance(HLAObjectClass* objectClass)
 {
     if (_rtiObjectInstance.valid()) {
         SG_LOG(SG_IO, SG_ALERT, "Trying to register object " << getName() << " already known to the RTI!");
         return;
     }
-    if (!_objectClass.valid()) {
+    if (!objectClass) {
         SG_LOG(SG_IO, SG_ALERT, "Could not register object with unknown object class!");
         return;
     }
+    if (_objectClass.valid() && objectClass != _objectClass.get()) {
+        SG_LOG(SG_IO, SG_ALERT, "Could not change object class while registering!");
+        return;
+    }
+    _objectClass = objectClass;
+    _federate = _objectClass->_federate;
     // This error must have been flagged before
     if (!_objectClass->_rtiObjectClass.valid())
         return;
@@ -473,9 +520,13 @@ HLAObjectInstance::encodeAttributeValues()
 {
     unsigned numAttributes = _attributeVector.size();
     for (unsigned i = 0; i < numAttributes;++i) {
-        if (!_attributeVector[i]._unconditionalUpdate)
-            continue;
-        encodeAttributeValue(i);
+        if (_attributeVector[i]._unconditionalUpdate) {
+            encodeAttributeValue(i);
+        } else if (_attributeVector[i]._enabledUpdate) {
+            const HLADataElement* dataElement = getAttributeDataElement(i);
+            if (dataElement && dataElement->getDirty())
+                encodeAttributeValue(i);
+        }
     }
 }
 
@@ -486,10 +537,11 @@ HLAObjectInstance::encodeAttributeValue(unsigned index)
         SG_LOG(SG_IO, SG_INFO, "Not updating inactive object!");
         return;
     }
-    const HLADataElement* dataElement = getAttributeDataElement(index);
+    HLADataElement* dataElement = getAttributeDataElement(index);
     if (!dataElement)
         return;
     _rtiObjectInstance->encodeAttributeData(index, *dataElement);
+    dataElement->setDirty(false);
 }
 
 void
@@ -533,6 +585,7 @@ HLAObjectInstance::reflectAttributeValue(unsigned index, const RTIData& tag)
     HLADataElement* dataElement = getAttributeDataElement(index);
     if (!dataElement)
         return;
+    dataElement->setTimeStampValid(false);
     _rtiObjectInstance->decodeAttributeData(index, *dataElement);
 }
 
@@ -542,13 +595,17 @@ HLAObjectInstance::reflectAttributeValue(unsigned index, const SGTimeStamp& time
     HLADataElement* dataElement = getAttributeDataElement(index);
     if (!dataElement)
         return;
-    // dataElement->setTimeStamp(timeStamp);
+    dataElement->setTimeStamp(timeStamp);
+    dataElement->setTimeStampValid(true);
     _rtiObjectInstance->decodeAttributeData(index, *dataElement);
 }
 
 void
 HLAObjectInstance::_setRTIObjectInstance(RTIObjectInstance* rtiObjectInstance)
 {
+    if (!_objectClass.valid())
+        return;
+
     _rtiObjectInstance = rtiObjectInstance;
     _rtiObjectInstance->setObjectInstance(this);
     _name = _rtiObjectInstance->getName();
@@ -556,7 +613,7 @@ HLAObjectInstance::_setRTIObjectInstance(RTIObjectInstance* rtiObjectInstance)
     unsigned numAttributes = getNumAttributes();
     _attributeVector.resize(numAttributes);
     for (unsigned i = 0; i < numAttributes; ++i) {
-        HLAUpdateType updateType = getObjectClass()->getAttributeUpdateType(i);
+        HLAUpdateType updateType = _objectClass->getAttributeUpdateType(i);
         if (getAttributeOwned(i) && updateType != HLAUndefinedUpdate) {
             _attributeVector[i]._enabledUpdate = true;
             _attributeVector[i]._unconditionalUpdate = (updateType == HLAPeriodicUpdate);
@@ -571,7 +628,7 @@ HLAObjectInstance::_setRTIObjectInstance(RTIObjectInstance* rtiObjectInstance)
     // This makes sense with any new object. Even if we registered one, there might be unpublished attributes.
     HLAIndexList indexList;
     for (unsigned i = 0; i < numAttributes; ++i) {
-        HLAUpdateType updateType = getObjectClass()->getAttributeUpdateType(i);
+        HLAUpdateType updateType = _objectClass->getAttributeUpdateType(i);
         if (getAttributeOwned(i))
             continue;
         if (updateType == HLAUndefinedUpdate)