From 56ba6089d03668fc0fe8b7b1e56c04fe1ccaada8 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 27 Aug 2016 07:35:04 +0200 Subject: [PATCH] Add rvalue support for the Array and Dictionary classes refs #12555 --- lib/base/array.cpp | 25 +++++++++++++++++++++++++ lib/base/array.hpp | 2 ++ lib/base/dictionary.cpp | 12 ++++++++++++ lib/base/dictionary.hpp | 1 + lib/base/value.hpp | 7 ++++++- 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/base/array.cpp b/lib/base/array.cpp index 9b7067cd8..581284aa9 100644 --- a/lib/base/array.cpp +++ b/lib/base/array.cpp @@ -56,6 +56,19 @@ void Array::Set(unsigned int index, const Value& value) m_Data.at(index) = value; } +/** + * Sets a value in the array. + * + * @param index The index. + * @param value The value. + */ +void Array::Set(unsigned int index, Value&& value) +{ + ObjectLock olock(this); + + m_Data.at(index).Swap(value); +} + /** * Adds a value to the array. * @@ -68,6 +81,18 @@ void Array::Add(const Value& value) m_Data.push_back(value); } +/** + * Adds a value to the array. + * + * @param value The value. + */ +void Array::Add(Value&& value) +{ + ObjectLock olock(this); + + m_Data.push_back(value); +} + /** * Returns the number of elements in the array. * diff --git a/lib/base/array.hpp b/lib/base/array.hpp index 432ec221f..5016d66b0 100644 --- a/lib/base/array.hpp +++ b/lib/base/array.hpp @@ -55,7 +55,9 @@ public: Value Get(unsigned int index) const; void Set(unsigned int index, const Value& value); + void Set(unsigned int index, Value&& value); void Add(const Value& value); + void Add(Value&& value); /** * Returns an iterator to the beginning of the array. diff --git a/lib/base/dictionary.cpp b/lib/base/dictionary.cpp index b1a5cbafe..5d8da2549 100644 --- a/lib/base/dictionary.cpp +++ b/lib/base/dictionary.cpp @@ -78,6 +78,18 @@ void Dictionary::Set(const String& key, const Value& value) m_Data[key] = value; } +/** + * Sets a value in the dictionary. + * + * @param key The key. + * @param value The value. + */ +void Dictionary::Set(const String& key, Value&& value) +{ + ObjectLock olock(this); + + m_Data[key].Swap(value); +} /** * Returns the number of elements in the dictionary. diff --git a/lib/base/dictionary.hpp b/lib/base/dictionary.hpp index c83f8c0ba..ff2e4b569 100644 --- a/lib/base/dictionary.hpp +++ b/lib/base/dictionary.hpp @@ -58,6 +58,7 @@ public: Value Get(const String& key) const; bool Get(const String& key, Value *result) const; void Set(const String& key, const Value& value); + void Set(const String& key, Value&& value); bool Contains(const String& key) const; /** diff --git a/lib/base/value.hpp b/lib/base/value.hpp index 48c8a63de..1d265a5af 100644 --- a/lib/base/value.hpp +++ b/lib/base/value.hpp @@ -244,11 +244,16 @@ public: * * @returns The type. */ - ValueType GetType(void) const + inline ValueType GetType(void) const { return static_cast(m_Value.which()); } + inline void Swap(Value& other) + { + m_Value.swap(other.m_Value); + } + String GetTypeName(void) const; Type::Ptr GetReflectionType(void) const; -- 2.40.0