bool EC_DynamicComponent::ContainSameAttributes(const EC_DynamicComponent &comp) const { AttributeVector myAttributeVector = NonEmptyAttributes(); AttributeVector attributeVector = comp.NonEmptyAttributes(); if(attributeVector.size() != myAttributeVector.size()) return false; if(attributeVector.empty() && myAttributeVector.empty()) return true; std::sort(myAttributeVector.begin(), myAttributeVector.end(), &CmpAttributeById); std::sort(attributeVector.begin(), attributeVector.end(), &CmpAttributeById); AttributeVector::const_iterator iter1 = myAttributeVector.begin(); AttributeVector::const_iterator iter2 = attributeVector.begin(); while(iter1 != myAttributeVector.end() && iter2 != attributeVector.end()) { // Compare attribute names and type and if they mach continue iteration if not components aren't exactly the same. if (((*iter1)->Id().compare((*iter2)->Id(), Qt::CaseInsensitive) == 0) && (*iter1)->TypeName().compare((*iter2)->TypeName(), Qt::CaseInsensitive) == 0) { if(iter1 != myAttributeVector.end()) ++iter1; if(iter2 != attributeVector.end()) ++iter2; } else { return false; } } return true; /*// Get both attributes and check if they are holding exact number of attributes. AttributeVector myAttributeVector = Attributes(); AttributeVector attributeVector = comp.Attributes(); if(attributeVector.size() != myAttributeVector.size()) return false; // Compare that every attribute is same in both components. QSet<IAttribute*> myAttributeSet; QSet<IAttribute*> attributeSet; for(uint i = 0; i < myAttributeSet.size(); i++) { attributeSet.insert(myAttributeVector[i]); myAttributeSet.insert(attributeVector[i]); } if(attributeSet != myAttributeSet) return false; return true;*/ }
void EC_DynamicComponent::DeserializeCommon(std::vector<DeserializeData>& deserializedAttributes, AttributeChange::Type change) { // Sort both lists in alphabetical order. AttributeVector oldAttributes = NonEmptyAttributes(); std::stable_sort(oldAttributes.begin(), oldAttributes.end(), &CmpAttributeById); std::stable_sort(deserializedAttributes.begin(), deserializedAttributes.end(), &CmpAttributeDataById); std::vector<DeserializeData> addAttributes; std::vector<DeserializeData> remAttributes; AttributeVector::iterator iter1 = oldAttributes.begin(); std::vector<DeserializeData>::iterator iter2 = deserializedAttributes.begin(); // Check what attributes we need to add or remove from the dynamic component (done by comparing two list differences). while(iter1 != oldAttributes.end() || iter2 != deserializedAttributes.end()) { // No point to continue the iteration if other list is empty. We can just push all new attributes into the dynamic component. if(iter1 == oldAttributes.end()) { for(;iter2 != deserializedAttributes.end(); ++iter2) addAttributes.push_back(*iter2); break; } // Only old attributes are left and they can be removed from the dynamic component. else if(iter2 == deserializedAttributes.end()) { for(;iter1 != oldAttributes.end(); ++iter1) remAttributes.push_back(DeserializeData((*iter1)->Id())); break; } // Attribute has already created and we only need to update it's value. if((*iter1)->Id() == (*iter2).id_) { //SetAttribute(QString::fromStdString(iter2->name_), QString::fromStdString(iter2->value_), change); for(AttributeVector::const_iterator attr_iter = attributes.begin(); attr_iter != attributes.end(); ++attr_iter) if((*attr_iter)->Id() == iter2->id_) (*attr_iter)->FromString(iter2->value_.toStdString(), change); ++iter2; ++iter1; } // Found a new attribute that need to be created and added to the component. else if((*iter1)->Id() > (*iter2).id_) { addAttributes.push_back(*iter2); ++iter2; } // Couldn't find the attribute in a new list so it need to be removed from the component. else { remAttributes.push_back(DeserializeData((*iter1)->Id())); ++iter1; } } while(!addAttributes.empty()) { DeserializeData attributeData = addAttributes.back(); IAttribute *attribute = CreateAttribute(attributeData.type_, attributeData.id_); if (attribute) attribute->FromString(attributeData.value_.toStdString(), change); addAttributes.pop_back(); } while(!remAttributes.empty()) { DeserializeData attributeData = remAttributes.back(); RemoveAttribute(attributeData.id_); remAttributes.pop_back(); } }