]> git.mxchange.org Git - simgear.git/commitdiff
hla: Timestamp support down to the DataElements.
authorMathias Froehlich <Mathias.Froehlich@web.de>
Fri, 2 Mar 2012 20:36:08 +0000 (21:36 +0100)
committerMathias Froehlich <Mathias.Froehlich@web.de>
Fri, 2 Mar 2012 20:36:08 +0000 (21:36 +0100)
simgear/hla/HLAArrayDataElement.cxx
simgear/hla/HLAArrayDataElement.hxx
simgear/hla/HLADataElement.cxx
simgear/hla/HLADataElement.hxx
simgear/hla/HLAFixedRecordDataElement.cxx
simgear/hla/HLAFixedRecordDataElement.hxx
simgear/hla/HLAObjectInstance.cxx
simgear/hla/HLAVariantRecordDataElement.cxx
simgear/hla/HLAVariantRecordDataElement.hxx

index 3746cbd88b4fecd875733e36a4e89e74d7df6c64..5fcbbd0a0ecc173ada7b6986d2d65c4b216e5d11 100644 (file)
@@ -99,6 +99,7 @@ HLAArrayDataElement::HLAArrayDataElement(const HLAArrayDataType* dataType) :
 
 HLAArrayDataElement::~HLAArrayDataElement()
 {
+    clearStamp();
 }
 
 bool
@@ -171,7 +172,11 @@ HLAArrayDataElement::setElement(unsigned index, HLADataElement* value)
         for (unsigned j = oldSize; j < index; ++j)
             _elementVector[j] = newElement(j);
     }
+    if (_elementVector[index].valid())
+        _elementVector[index]->clearStamp();
     _elementVector[index] = value;
+    if (value)
+        value->attachStamp(*this);
 }
 
 void
@@ -186,12 +191,27 @@ HLAArrayDataElement::getDataElementFactory()
     return _dataElementFactory.get();
 }
 
+void
+HLAArrayDataElement::_setStamp(Stamp* stamp)
+{
+    HLAAbstractArrayDataElement::_setStamp(stamp);
+    for (ElementVector::iterator i = _elementVector.begin(); i != _elementVector.end(); ++i) {
+        if (!i->valid())
+            continue;
+        (*i)->attachStamp(*this);
+    }
+}
+
 HLADataElement*
 HLAArrayDataElement::newElement(unsigned index)
 {
     if (!_dataElementFactory.valid())
         return 0;
-    return _dataElementFactory->createElement(*this, index);
+    HLADataElement* dataElement = _dataElementFactory->createElement(*this, index);
+    if (!dataElement)
+        return 0;
+    dataElement->attachStamp(*this);
+    return dataElement;
 }
 
 ////////////////////////////////////////////////////////////////////////
@@ -203,6 +223,7 @@ HLAVariantArrayDataElement::HLAVariantArrayDataElement() :
 
 HLAVariantArrayDataElement::~HLAVariantArrayDataElement()
 {
+    clearStamp();
 }
 
 bool
@@ -292,7 +313,11 @@ HLAVariantArrayDataElement::setElement(unsigned index, HLAVariantRecordDataEleme
         for (unsigned j = oldSize; j < index; ++j)
             _elementVector[j] = newElement();
     }
+    if (_elementVector[index].valid())
+        _elementVector[index]->clearStamp();
     _elementVector[index] = value;
+    if (value)
+        value->attachStamp(*this);
 }
 
 void
@@ -307,6 +332,17 @@ HLAVariantArrayDataElement::getAlternativeDataElementFactory()
     return _alternativeDataElementFactory.get();
 }
 
+void
+HLAVariantArrayDataElement::_setStamp(Stamp* stamp)
+{
+    HLAAbstractArrayDataElement::_setStamp(stamp);
+    for (ElementVector::iterator i = _elementVector.begin(); i != _elementVector.end(); ++i) {
+        if (!i->valid())
+            continue;
+        (*i)->attachStamp(*this);
+    }
+}
+
 HLAVariantRecordDataElement*
 HLAVariantArrayDataElement::newElement()
 {
@@ -321,6 +357,7 @@ HLAVariantArrayDataElement::newElement()
         return 0;
     HLAVariantRecordDataElement* variantRecordDataElement = new HLAVariantRecordDataElement(variantRecordDataType);
     variantRecordDataElement->setDataElementFactory(_alternativeDataElementFactory.get());
+    variantRecordDataElement->attachStamp(*this);
     return variantRecordDataElement;
 }
 
index 8f510be59c63a959a406050f34d41e5c859aa87c..4557d006afa068e2ef10832c34e02a2bdb10a0bd 100644 (file)
@@ -78,6 +78,9 @@ public:
     void setDataElementFactory(DataElementFactory* dataElementFactory);
     DataElementFactory* getDataElementFactory();
 
+protected:
+    virtual void _setStamp(Stamp* stamp);
+
 private:
     HLADataElement* newElement(unsigned index);
 
@@ -112,6 +115,9 @@ public:
     void setAlternativeDataElementFactory(AlternativeDataElementFactory* alternativeDataElementFactory);
     AlternativeDataElementFactory* getAlternativeDataElementFactory();
 
+protected:
+    virtual void _setStamp(Stamp* stamp);
+
 private:
     HLAVariantRecordDataElement* newElement();
 
index b7da888412e647aaca5eb0d0e5a1051ebd8a3d34..0794f152311f1239d958daa7b751bad16256c5d7 100644 (file)
@@ -131,6 +131,51 @@ HLADataElement::~HLADataElement()
 {
 }
 
+void
+HLADataElement::setTimeStamp(const SGTimeStamp& timeStamp)
+{
+    if (!_stamp.valid())
+        return;
+    _stamp->setTimeStamp(timeStamp);
+}
+
+void
+HLADataElement::setTimeStampValid(bool timeStampValid)
+{
+    if (!_stamp.valid())
+        return;
+    _stamp->setTimeStampValid(timeStampValid);
+}
+
+double
+HLADataElement::getTimeDifference(const SGTimeStamp& timeStamp) const
+{
+    if (!_stamp.valid())
+        return 0;
+    if (!_stamp->getTimeStampValid())
+        return 0;
+    return (timeStamp - _stamp->getTimeStamp()).toSecs();
+}
+
+void
+HLADataElement::createStamp()
+{
+    _setStamp(new Stamp);
+    setDirty(true);
+}
+
+void
+HLADataElement::attachStamp(HLADataElement& dataElement)
+{
+    _setStamp(dataElement._getStamp());
+}
+
+void
+HLADataElement::clearStamp()
+{
+    _setStamp(0);
+}
+
 void
 HLADataElement::accept(HLADataElementVisitor& visitor)
 {
@@ -198,4 +243,10 @@ HLADataElement::toStringPathPair(const std::string& s)
     return StringPathPair(attribute, path);
 }
 
+void
+HLADataElement::_setStamp(HLADataElement::Stamp* stamp)
+{
+    _stamp = stamp;
+}
+
 }
index 35613d069b0784982912b84bb29deb0effbd20fd..0cb14e615991ac785fae8def5dc4f864059d50ff 100644 (file)
@@ -46,34 +46,30 @@ public:
     virtual const HLADataType* getDataType() const = 0;
     virtual bool setDataType(const HLADataType* dataType) = 0;
 
-    // Container for the timestamp the originating attribute was last updated for
-    // class TimeStamp : public SGReferenced {
-    // public:
-    //     const SGTimeStamp& getTimeStamp() const
-    //     { return _timeStamp; }
-    //     void setTimeStamp(const SGTimeStamp& timeStamp)
-    //     { _timeStamp = timeStamp; }
-    // private:
-    //     SGTimeStamp _timeStamp;
-    // };
-
-    // const TimeStamp* getTimeStamp() const
-    // { return _timeStamp.get(); }
-    // void setTimeStamp(const TimeStamp* timeStamp)
-    // { _timeStamp = timeStamp; }
-
-    // struct ChangeCount : public SGReferenced {
-    //     ChangeCount() : _value(0) {}
-    //     unsigned _value;
-    // };
-    // SGSharedPtr<ChangeCount> _changeCount;
-    // unsigned getChangeCount() const
-    // {
-    //     // If we don't have return allways the same
-    //     if (!_changeCount.valid())
-    //         return 0;
-    //     return _changeCount->_value;
-    // }
+    /// Returns the time stamp if this data element.
+    /// Do not access this getter if the getTimeStampValid() method returns false.
+    const SGTimeStamp& getTimeStamp() const
+    { return _stamp->getTimeStamp(); }
+    void setTimeStamp(const SGTimeStamp& timeStamp);
+
+    bool getTimeStampValid() const
+    { if (!_stamp.valid()) return false; return _stamp->getTimeStampValid(); }
+    void setTimeStampValid(bool timeStampValid);
+
+    /// Convenience function that gives the time difference in seconds to a given timestamp
+    /// This function returns 0 if the timestamp is not valid.
+    double getTimeDifference(const SGTimeStamp& timeStamp) const;
+
+    /// Dirty tracking of the attribute/parameter that this data element belongs to
+    bool getDirty() const
+    { if (!_stamp.valid()) return true; return _stamp->getDirty(); }
+    void setDirty(bool dirty)
+    { if (!_stamp.valid()) return; _stamp->setDirty(dirty); }
+
+    /// Stamp handling
+    void createStamp();
+    void attachStamp(HLADataElement& dataElement);
+    void clearStamp();
 
     /// HLADataElements could be identified by path
     /// These paths are composed of structure field names and array indices in the
@@ -163,8 +159,42 @@ public:
     static Path toPath(const std::string& s)
     { return toStringPathPair(s).second; }
 
+protected:
+    // Container for the timestamp the originating attribute was last updated for
+    class Stamp : public SGReferenced {
+    public:
+        Stamp() : _timeStampValid(false), _dirty(true)
+        { }
+
+        const SGTimeStamp& getTimeStamp() const
+        { return _timeStamp; }
+        void setTimeStamp(const SGTimeStamp& timeStamp)
+        { _timeStamp = timeStamp; }
+
+        bool getTimeStampValid() const
+        { return _timeStampValid; }
+        void setTimeStampValid(bool timeStampValid)
+        { _timeStampValid = timeStampValid; }
+
+        bool getDirty() const
+        { return _dirty; }
+        void setDirty(bool dirty)
+        { _dirty = dirty; }
+
+    private:
+        SGTimeStamp _timeStamp;
+        bool _timeStampValid;
+        bool _dirty;
+    };
+
+    /// get the stamp
+    Stamp* _getStamp() const
+    { return _stamp.get(); }
+    /// Set the stamp
+    virtual void _setStamp(Stamp* stamp);
+
 private:
-    // SGSharedPtr<const TimeStamp> _timeStamp;
+    SGSharedPtr<Stamp> _stamp;
 };
 
 class HLADataElementProvider {
index c4c0c8b2d6cd4ae9baf0b8038edc9b60cbf713cb..d04c273233ad843b703d7a2f3ea78883725f88bc 100644 (file)
@@ -120,6 +120,7 @@ HLAFixedRecordDataElement::HLAFixedRecordDataElement(const HLAFixedRecordDataTyp
 
 HLAFixedRecordDataElement::~HLAFixedRecordDataElement()
 {
+    clearStamp();
 }
 
 bool
@@ -181,7 +182,11 @@ HLAFixedRecordDataElement::setField(unsigned index, HLADataElement* value)
 {
     if (getNumFields() <= index)
         return;
+    if (_fieldVector[index].valid())
+        _fieldVector[index]->clearStamp();
     _fieldVector[index] = value;
+    if (value)
+        value->attachStamp(*this);
 }
 
 void
@@ -190,4 +195,15 @@ HLAFixedRecordDataElement::setField(const std::string& name, HLADataElement* val
     setField(getFieldNumber(name), value);
 }
 
+void
+HLAFixedRecordDataElement::_setStamp(Stamp* stamp)
+{
+    HLAAbstractFixedRecordDataElement::_setStamp(stamp);
+    for (FieldVector::iterator i = _fieldVector.begin(); i != _fieldVector.end(); ++i) {
+        if (!i->valid())
+            continue;
+        (*i)->attachStamp(*this);
+    }
+}
+
 }
index ca8ed99782ca61a4c29cf7f9bdeb846a53887c0e..7a471e8dfcf8d2003bdaf51202553b71e64e6c47 100644 (file)
@@ -70,6 +70,9 @@ public:
     void setField(unsigned index, HLADataElement* value);
     void setField(const std::string& name, HLADataElement* value);
 
+protected:
+    virtual void _setStamp(Stamp* stamp);
+
 private:
     typedef std::vector<SGSharedPtr<HLADataElement> > FieldVector;
     FieldVector _fieldVector;
index 390b70db1ef9b24e0bd9dddbed096ab88532fc86..f54ca8bb4598dd823884149e9abb62dd045051ca 100644 (file)
@@ -116,7 +116,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 (_attributeVector[index]._dataElement.valid())
+        _attributeVector[index]._dataElement->createStamp();
     if (getAttributeOwned(index))
         encodeAttributeValue(index);
 }
@@ -533,6 +537,7 @@ HLAObjectInstance::reflectAttributeValue(unsigned index, const RTIData& tag)
     HLADataElement* dataElement = getAttributeDataElement(index);
     if (!dataElement)
         return;
+    dataElement->setTimeStampValid(false);
     _rtiObjectInstance->decodeAttributeData(index, *dataElement);
 }
 
@@ -542,7 +547,8 @@ 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);
 }
 
index db8f1be0aba231d12cc4205350e1f7cdd5438118..e3ef35a1368e9a13b42a0d91a74e37247ceae5ba 100644 (file)
@@ -113,6 +113,7 @@ HLAVariantRecordDataElement::HLAVariantRecordDataElement(const HLAVariantRecordD
 
 HLAVariantRecordDataElement::~HLAVariantRecordDataElement()
 {
+    clearStamp();
 }
 
 bool
@@ -158,12 +159,25 @@ HLAVariantRecordDataElement::getDataElementFactory()
     return _dataElementFactory;
 }
 
+void
+HLAVariantRecordDataElement::_setStamp(Stamp* stamp)
+{
+    HLAAbstractVariantRecordDataElement::_setStamp(stamp);
+    if (!_dataElement.valid())
+        return;
+    _dataElement->attachStamp(*this);
+}
+
 HLADataElement*
 HLAVariantRecordDataElement::newElement(unsigned index)
 {
     if (!_dataElementFactory.valid())
         return 0;
-    return _dataElementFactory->createElement(*this, index);
+    HLADataElement* dataElement = _dataElementFactory->createElement(*this, index);
+    if (!dataElement)
+        return 0;
+    dataElement->attachStamp(*this);
+    return dataElement;
 }
 
 }
index 0f4f364f8ef189f4be24754bc9328477321e726d..cc76efbb46bf7d926278c1731acdcd4719b14a70 100644 (file)
@@ -71,6 +71,9 @@ public:
     void setDataElementFactory(DataElementFactory* dataElementFactory);
     DataElementFactory* getDataElementFactory();
 
+protected:
+    virtual void _setStamp(Stamp* stamp);
+
 private:
     HLADataElement* newElement(unsigned index);