void ItemAttributes::setStrAttr(const itemAttrTypes& type, const std::string& value) { if (!validateStrAttrType(type)) { return; } if (value.length() == 0) { return; } //this will create the attribute if it does not exist Attribute* attr = getAttr(type); if (attr) { //if has a previous value delete it if (attr->value) { delete(std::string*)attr->value; } //create the new value as string attr->value = (void*)new std::string(value); } }
void ItemAttributes::removeAttribute(itemAttrTypes type) { //check if we have it if((type & m_attributes) != 0){ //go trough the linked list until find it Attribute* prevAttr = NULL; Attribute* curAttr = m_firstAttr; while(curAttr != NULL){ if(curAttr->type == type){ //found so remove it from the linked list if(prevAttr){ prevAttr->next = curAttr->next; } else{ m_firstAttr = curAttr->next; } //remove it from flags m_attributes = m_attributes & ~type; //delete string if it is string type if(validateStrAttrType(type)){ delete (std::string*)curAttr->value; } //finally delete the attribute and return delete curAttr; return; } //advance in the linked list prevAttr = curAttr; curAttr = curAttr->next; } } }
const std::string& ItemAttributes::getStrAttr(itemAttrTypes type) const { if (!validateStrAttrType(type)) { return emptyString; } Attribute* attr = getAttrConst(type); if (attr) { return *(std::string*)attr->value; } else { return emptyString; } }
const std::string& ItemAttributes::getStrAttr(itemAttrTypes type) const { if(!validateStrAttrType(type)) return emptyString; //this will *NOT* create the attribute if it does not exist Attribute* attr = getAttrConst(type); if(attr){ return *(std::string*)attr->value; } else{ return emptyString; } }
void ItemAttributes::deleteAttrs(Attribute* attr) { if (attr) { if (validateStrAttrType(attr->type)) { delete (std::string*)attr->value; } Attribute* next_attr = attr->next; attr->next = NULL; if (next_attr) { deleteAttrs(next_attr); } delete attr; } }
void ItemAttributes::deleteAttrs(Attribute* attr) { //deletes all attributes recursively if(attr){ //if is string type, delete the allocated string if(validateStrAttrType(attr->type)){ delete (std::string*)attr->value; } Attribute* next_attr = attr->next; attr->next = NULL; //delete next while it was not NULL if(next_attr){ deleteAttrs(next_attr); } delete attr; } }
void ItemAttributes::setStrAttr(itemAttrTypes type, const std::string& value) { if (!validateStrAttrType(type)) { return; } if (value.length() == 0) { return; } Attribute* attr = getAttr(type); if (attr) { if (attr->value) { delete (std::string*)attr->value; } attr->value = (void*)new std::string(value); } }
const std::string& ItemAttributes::getStrAttr(const itemAttrTypes& type) const { static const std::string EMPTY_STRING; if (!validateStrAttrType(type)) { return EMPTY_STRING; } //this will *NOT* create the attribute if it does not exist Attribute* attr = getAttrConst(type); if (attr) { return *(std::string*)attr->value; } else { return EMPTY_STRING; } }