Skip to content

Commit

Permalink
libcore|DictionaryValue: Removing values by key
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed May 18, 2014
1 parent ace5559 commit 2e15744
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doomsday/libcore/include/de/data/dictionaryvalue.h
Expand Up @@ -59,6 +59,9 @@ class DENG2_PUBLIC DictionaryValue : public Value
/// Returns a direct reference to the elements map.
Elements const &elements() const { return _elements; }

/// Returns a direct reference to the elements map.
Elements &elements() { return _elements; }

/**
* Clears the dictionary of all values.
*/
Expand All @@ -73,6 +76,15 @@ class DENG2_PUBLIC DictionaryValue : public Value
*/
void add(Value *key, Value *value);

/**
* Removes a key-value pair from the dictionary.
*
* @param key Key that will be removed.
*/
void remove(Value const &key);

void remove(Elements::iterator const &pos);

// Implementations of pure virtual methods.
Value *duplicate() const;
Text asText() const;
Expand Down
18 changes: 18 additions & 0 deletions doomsday/libcore/src/data/dictionaryvalue.cpp
Expand Up @@ -19,6 +19,7 @@

#include "de/DictionaryValue"
#include "de/ArrayValue"
#include "de/RecordValue"
#include "de/Writer"
#include "de/Reader"

Expand Down Expand Up @@ -74,6 +75,22 @@ void DictionaryValue::add(Value *key, Value *value)
}
}

void DictionaryValue::remove(Value const &key)
{
Elements::iterator i = _elements.find(ValueRef(&key));
if(i != _elements.end())
{
remove(i);
}
}

void DictionaryValue::remove(Elements::iterator const &pos)
{
delete pos->first.value;
delete pos->second;
_elements.erase(pos);
}

Value *DictionaryValue::duplicate() const
{
return new DictionaryValue(*this);
Expand All @@ -94,6 +111,7 @@ Value::Text DictionaryValue::asText() const
{
s << ",";
}
if(i->second->is<RecordValue>()) s << "\n"; // Records have multiple lines.
s << " " << i->first.value->asText() << ": " << i->second->asText();
isFirst = false;
}
Expand Down

0 comments on commit 2e15744

Please sign in to comment.