hla: Fixes for the data element index implementation.

This commit is contained in:
Mathias Froehlich 2012-11-11 10:47:09 +01:00
parent 0fef94cfdb
commit 7dc8bf3aa4
6 changed files with 52 additions and 20 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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()
{

View File

@ -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();

View File

@ -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<HLADataElement> 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<HLADataElement>& dataElement)
{
HLADataElementIndex index;
if (!getAttributeIndex(index, path))
if (!getDataElementIndex(index, path))
return;
setAttributeDataElement(index, dataElement);
}

View File

@ -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);