diff options
Diffstat (limited to 'raul')
-rw-r--r-- | raul/Table.hpp | 2 | ||||
-rw-r--r-- | raul/TableImpl.hpp | 22 |
2 files changed, 24 insertions, 0 deletions
diff --git a/raul/Table.hpp b/raul/Table.hpp index db5216f..0e78f2f 100644 --- a/raul/Table.hpp +++ b/raul/Table.hpp @@ -81,6 +81,8 @@ public: const_iterator find(const K& key) const; iterator find(const K& key); + T& operator[](const K& key); + const_iterator begin() const { return const_iterator(*this, 0); } const_iterator end() const { return const_iterator(*this, size()); } iterator begin() { return iterator(*this, 0); } diff --git a/raul/TableImpl.hpp b/raul/TableImpl.hpp index 35bf2f6..8fcba0b 100644 --- a/raul/TableImpl.hpp +++ b/raul/TableImpl.hpp @@ -140,6 +140,28 @@ Table<K,T>::insert(const std::pair<K, T>& entry) return make_pair(iterator(*this, i), true); } + + +/** Insert an item, and return a reference to it's value. + * + * This may be used to insert values with pretty syntax: + * + * table["gorilla"] = "killa"; + * + * T must have a default constructor for this to be possible. + */ +template <typename K, typename T> +T& +Table<K, T>::operator[](const K& key) +{ + iterator i = find(key); + if (i != end()) { + return i->second; + } else { + std::pair<iterator,bool> ret = insert(make_pair(key, T())); + return ret.first->second; + } +} template <typename K, typename T> |