]> git.mxchange.org Git - simgear.git/blobdiff - simgear/hla/HLAObjectInstance.cxx
Merge branch 'timoore/optimizations' into next
[simgear.git] / simgear / hla / HLAObjectInstance.cxx
index a163c5953dbb12331ea3069d5736f7e42927c9ce..f1b7fed07a52085103ae06422646bdf6b9f6c28f 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);
 }
 
@@ -411,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;
@@ -447,8 +492,6 @@ HLAObjectInstance::deleteInstance(const RTIData& tag)
 void
 HLAObjectInstance::updateAttributeValues(const RTIData& tag)
 {
-    if (_attributeCallback.valid())
-        _attributeCallback->updateAttributeValues(*this, tag);
     if (_updateCallback.valid()) {
         _updateCallback->updateAttributeValues(*this, tag);
     } else {
@@ -460,8 +503,6 @@ HLAObjectInstance::updateAttributeValues(const RTIData& tag)
 void
 HLAObjectInstance::updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag)
 {
-    if (_attributeCallback.valid())
-        _attributeCallback->updateAttributeValues(*this, tag);
     if (_updateCallback.valid()) {
         _updateCallback->updateAttributeValues(*this, timeStamp, tag);
     } else {
@@ -558,6 +599,9 @@ HLAObjectInstance::reflectAttributeValue(unsigned index, const SGTimeStamp& time
 void
 HLAObjectInstance::_setRTIObjectInstance(RTIObjectInstance* rtiObjectInstance)
 {
+    if (!_objectClass.valid())
+        return;
+
     _rtiObjectInstance = rtiObjectInstance;
     _rtiObjectInstance->setObjectInstance(this);
     _name = _rtiObjectInstance->getName();
@@ -565,7 +609,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);
@@ -580,7 +624,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)
@@ -620,16 +664,6 @@ HLAObjectInstance::_reflectAttributeValues(const HLAIndexList& indexList, const
 {
     if (_reflectCallback.valid()) {
         _reflectCallback->reflectAttributeValues(*this, indexList, tag);
-    } else if (_attributeCallback.valid()) {
-        reflectAttributeValues(indexList, tag);
-
-        RTIIndexDataPairList dataPairList;
-        for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
-            dataPairList.push_back(RTIIndexDataPair());
-            dataPairList.back().first = *i;
-            getAttributeData(*i, dataPairList.back().second);
-        }
-        _attributeCallback->reflectAttributeValues(*this, dataPairList, tag);
     } else {
         reflectAttributeValues(indexList, tag);
     }
@@ -640,16 +674,6 @@ HLAObjectInstance::_reflectAttributeValues(const HLAIndexList& indexList, const
 {
     if (_reflectCallback.valid()) {
         _reflectCallback->reflectAttributeValues(*this, indexList, timeStamp, tag);
-    } else if (_attributeCallback.valid()) {
-        reflectAttributeValues(indexList, timeStamp, tag);
-
-        RTIIndexDataPairList dataPairList;
-        for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
-            dataPairList.push_back(RTIIndexDataPair());
-            dataPairList.back().first = *i;
-            getAttributeData(*i, dataPairList.back().second);
-        }
-        _attributeCallback->reflectAttributeValues(*this, dataPairList, timeStamp, tag);
     } else {
         reflectAttributeValues(indexList, timeStamp, tag);
     }