Erase a value in a subtree. ! \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. \return Whether the resolved value is found and erased. \note Erasing with an empty pointer \c Pointer(""), i.e. the root, always fail and return false. */
| 709 | \note Erasing with an empty pointer \c Pointer(""), i.e. the root, always fail and return false. |
| 710 | */ |
| 711 | bool Erase(ValueType& root) const { |
| 712 | RAPIDJSON_ASSERT(IsValid()); |
| 713 | if (tokenCount_ == 0) // Cannot erase the root |
| 714 | return false; |
| 715 | |
| 716 | ValueType* v = &root; |
| 717 | const Token* last = tokens_ + (tokenCount_ - 1); |
| 718 | for (const Token *t = tokens_; t != last; ++t) { |
| 719 | switch (v->GetType()) { |
| 720 | case kObjectType: |
| 721 | { |
| 722 | typename ValueType::MemberIterator m = v->FindMember(GenericStringRef<Ch>(t->name, t->length)); |
| 723 | if (m == v->MemberEnd()) |
| 724 | return false; |
| 725 | v = &m->value; |
| 726 | } |
| 727 | break; |
| 728 | case kArrayType: |
| 729 | if (t->index == kPointerInvalidIndex || t->index >= v->Size()) |
| 730 | return false; |
| 731 | v = &((*v)[t->index]); |
| 732 | break; |
| 733 | default: |
| 734 | return false; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | switch (v->GetType()) { |
| 739 | case kObjectType: |
| 740 | return v->EraseMember(GenericStringRef<Ch>(last->name, last->length)); |
| 741 | case kArrayType: |
| 742 | if (last->index == kPointerInvalidIndex || last->index >= v->Size()) |
| 743 | return false; |
| 744 | v->Erase(v->Begin() + last->index); |
| 745 | return true; |
| 746 | default: |
| 747 | return false; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | private: |
| 752 | //! Clone the content from rhs to this. |
no test coverage detected