diff --git a/simgear/hla/HLADataType.cxx b/simgear/hla/HLADataType.cxx index ff9b2f19..1eaf0456 100644 --- a/simgear/hla/HLADataType.cxx +++ b/simgear/hla/HLADataType.cxx @@ -90,7 +90,7 @@ HLADataType::releaseDataTypeReferences() class HLADataType::_DataElementIndexVisitor : public HLADataTypeVisitor { public: - _DataElementIndexVisitor(HLADataElementIndex& index, const std::string& path, size_t offset) : + _DataElementIndexVisitor(HLADataElementIndex& index, const std::string& path, std::string::size_type offset) : _index(index), _path(path), _offset(offset), @@ -106,7 +106,11 @@ public: } virtual void apply(const HLAArrayDataType& dataType) { - if (_path.size() <= _offset) { + if (_path.size() == _offset) { + _success = true; + return; + } + if (_path.size() < _offset) { SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n" << "Expected array subscript at the end of the path!"); return; @@ -157,7 +161,11 @@ public: virtual void apply(const HLAFixedRecordDataType& dataType) { - if (_path.size() <= _offset) { + if (_path.size() == _offset) { + _success = true; + return; + } + if (_path.size() < _offset) { SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n" << "Expected field name at the end of the path!"); return; @@ -169,7 +177,7 @@ public: << "Expected field name at the end of the path!"); return; } - size_t len = _path.find_first_of("[.", _offset) - _offset; + std::string::size_type len = std::min(_path.find_first_of("[.", _offset), _path.size()) - _offset; unsigned index = 0; while (index < dataType.getNumFields()) { if (_path.compare(_offset, len, dataType.getFieldName(index)) == 0) @@ -194,7 +202,11 @@ public: virtual void apply(const HLAVariantRecordDataType& dataType) { - if (_path.size() <= _offset) { + if (_path.size() == _offset) { + _success = true; + return; + } + if (_path.size() < _offset) { SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n" << "Expected alternative name at the end of the path!"); return; @@ -206,7 +218,7 @@ public: << "Expected alternative name at the end of the path!"); return; } - size_t len = _path.find_first_of("[.", _offset) - _offset; + std::string::size_type len = std::min(_path.find_first_of("[.", _offset), _path.size()) - _offset; unsigned index = 0; while (index < dataType.getNumAlternatives()) { if (_path.compare(_offset, len, dataType.getAlternativeName(index)) == 0) @@ -231,12 +243,12 @@ public: HLADataElementIndex& _index; const std::string& _path; - size_t _offset; + std::string::size_type _offset; bool _success; }; bool -HLADataType::getDataElementIndex(HLADataElementIndex& index, const std::string& path, size_t offset) const +HLADataType::getDataElementIndex(HLADataElementIndex& index, const std::string& path, std::string::size_type offset) const { _DataElementIndexVisitor visitor(index, path, offset); accept(visitor); diff --git a/simgear/hla/HLADataType.hxx b/simgear/hla/HLADataType.hxx index 7d045d9d..ad52958b 100644 --- a/simgear/hla/HLADataType.hxx +++ b/simgear/hla/HLADataType.hxx @@ -64,7 +64,7 @@ public: /// required for propper feeing of memory. virtual void releaseDataTypeReferences(); - bool getDataElementIndex(HLADataElementIndex& index, const std::string& path, size_t offset) const; + bool getDataElementIndex(HLADataElementIndex& index, const std::string& path, std::string::size_type offset) const; protected: HLADataType(const std::string& name, unsigned alignment = 1); diff --git a/simgear/hla/HLAObjectClass.cxx b/simgear/hla/HLAObjectClass.cxx index 06287c38..c9282688 100644 --- a/simgear/hla/HLAObjectClass.cxx +++ b/simgear/hla/HLAObjectClass.cxx @@ -211,13 +211,13 @@ HLAObjectClass::getIndexPathPair(const std::string& path) const } bool -HLAObjectClass::getAttributeIndex(HLADataElementIndex& dataElementIndex, const std::string& path) const +HLAObjectClass::getDataElementIndex(HLADataElementIndex& dataElementIndex, const std::string& path) const { if (path.empty()) { SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: failed to parse empty element path!"); return false; } - size_t len = path.find_first_of("[."); + std::string::size_type len = std::min(path.find_first_of("[."), path.size()); unsigned index = 0; while (index < getNumAttributes()) { if (path.compare(0, len, getAttributeName(index)) == 0) @@ -240,6 +240,14 @@ HLAObjectClass::getAttributeIndex(HLADataElementIndex& dataElementIndex, const s return getAttributeDataType(index)->getDataElementIndex(dataElementIndex, path, len); } +HLADataElementIndex +HLAObjectClass::getDataElementIndex(const std::string& path) const +{ + HLADataElementIndex dataElementIndex; + getDataElementIndex(dataElementIndex, path); + return dataElementIndex; +} + bool HLAObjectClass::subscribe() { diff --git a/simgear/hla/HLAObjectClass.hxx b/simgear/hla/HLAObjectClass.hxx index 10767cf9..97596d40 100644 --- a/simgear/hla/HLAObjectClass.hxx +++ b/simgear/hla/HLAObjectClass.hxx @@ -78,7 +78,8 @@ public: HLADataElement::IndexPathPair getIndexPathPair(const std::string& path) const; /// Get the attribute data element index for the given path, return true if successful - bool getAttributeIndex(HLADataElementIndex& dataElementIndex, const std::string& path) const; + bool getDataElementIndex(HLADataElementIndex& dataElementIndex, const std::string& path) const; + HLADataElementIndex getDataElementIndex(const std::string& path) const; virtual bool subscribe(); virtual bool unsubscribe(); diff --git a/simgear/hla/HLAObjectInstance.cxx b/simgear/hla/HLAObjectInstance.cxx index ea8114a2..39ff74ca 100644 --- a/simgear/hla/HLAObjectInstance.cxx +++ b/simgear/hla/HLAObjectInstance.cxx @@ -443,14 +443,22 @@ HLAObjectInstance::setAttributes(const HLAAttributePathElementMap& attributePath } bool -HLAObjectInstance::getAttributeIndex(HLADataElementIndex& index, const std::string& path) const +HLAObjectInstance::getDataElementIndex(HLADataElementIndex& index, const std::string& path) const { HLAObjectClass* objectClass = getObjectClass().get(); if (!objectClass) { SG_LOG(SG_IO, SG_ALERT, "Could not get the data element index of an object instance with unknown class!"); return false; } - return objectClass->getAttributeIndex(index, path); + return objectClass->getDataElementIndex(index, path); +} + +HLADataElementIndex +HLAObjectInstance::getDataElementIndex(const std::string& path) const +{ + HLADataElementIndex dataElementIndex; + getDataElementIndex(dataElementIndex, path); + return dataElementIndex; } HLADataElement* @@ -488,8 +496,10 @@ HLAObjectInstance::setAttributeDataElement(const HLADataElementIndex& index, con setAttributeDataElement(index[0], dataElement); } else { SGSharedPtr attributeDataElement = getAttributeDataElement(index[0]); - if (!attributeDataElement.valid()) - attributeDataElement = createAttributeDataElement(index[0]); + if (!attributeDataElement.valid()) { + createAndSetAttributeDataElement(index[0]); + attributeDataElement = getAttributeDataElement(index[0]); + } if (!attributeDataElement.valid()) return; attributeDataElement->setDataElement(index.begin() + 1, index.end(), dataElement.get()); @@ -500,7 +510,7 @@ HLADataElement* HLAObjectInstance::getAttributeDataElement(const std::string& path) { HLADataElementIndex index; - if (!getAttributeIndex(index, path)) + if (!getDataElementIndex(index, path)) return 0; return getAttributeDataElement(index); } @@ -509,7 +519,7 @@ const HLADataElement* HLAObjectInstance::getAttributeDataElement(const std::string& path) const { HLADataElementIndex index; - if (!getAttributeIndex(index, path)) + if (!getDataElementIndex(index, path)) return 0; return getAttributeDataElement(index); } @@ -518,7 +528,7 @@ void HLAObjectInstance::setAttributeDataElement(const std::string& path, const SGSharedPtr& dataElement) { HLADataElementIndex index; - if (!getAttributeIndex(index, path)) + if (!getDataElementIndex(index, path)) return; setAttributeDataElement(index, dataElement); } diff --git a/simgear/hla/HLAObjectInstance.hxx b/simgear/hla/HLAObjectInstance.hxx index cd18d06f..a7c802d4 100644 --- a/simgear/hla/HLAObjectInstance.hxx +++ b/simgear/hla/HLAObjectInstance.hxx @@ -78,7 +78,8 @@ public: void setAttributes(const HLAAttributePathElementMap& attributePathElementMap); /// Retrieve the data element index for the given path. - bool getAttributeIndex(HLADataElementIndex& index, const std::string& path) const; + bool getDataElementIndex(HLADataElementIndex& index, const std::string& path) const; + HLADataElementIndex getDataElementIndex(const std::string& path) const; /// Return the data element of the attribute with the given index HLADataElement* getAttributeDataElement(const HLADataElementIndex& index);