-#ifndef __DALI_PROPERTY_MAP_H__
-#define __DALI_PROPERTY_MAP_H__
+#ifndef DALI_PROPERTY_MAP_H
+#define DALI_PROPERTY_MAP_H
/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
// EXTERNAL INCLUDES
+#include <initializer_list>
+#include <sstream>
#include <string>
+#include <string_view>
// INTERNAL INCLUDES
#include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/object/property-key.h>
#include <dali/public-api/object/property-value.h>
#include <dali/public-api/object/property.h>
namespace Dali
{
+/**
+ * @addtogroup dali_core_object
+ * @{
+ */
+
+using KeyValuePair = std::pair<Property::Key, Property::Value>;
+using StringValuePair = std::pair<std::string, Property::Value>;
/**
- * @brief A Map of property values.
+ * @brief A Map of property values, the key type could be String or Property::Index.
+ * @SINCE_1_0.0
*/
-class DALI_IMPORT_API Property::Map
+class DALI_CORE_API Property::Map
{
public:
+ using SizeType = std::size_t;
/**
* @brief Default constructor.
+ * @SINCE_1_0.0
*/
Map();
/**
- * @brief Copy Constructor.
+ * @brief Constructor from initializer_list.
+ *
+ * @SINCE_1_4.17
+ * @param[in] values An initializer_list of pairs of index and value.
+ */
+ Map(const std::initializer_list<KeyValuePair>& values);
+
+ /**
+ * @brief Copy constructor.
+ *
+ * @SINCE_1_0.0
+ * @param[in] other The Map to copy from
+ */
+ Map(const Map& other);
+
+ /**
+ * @brief Move constructor.
*
- * @param[in] other The Map to copy from.
+ * @SINCE_1_4.17
+ * @param[in] other The Map to move from
+ * @note After the @a other array is used, it becomes invalid and is no longer usable.
*/
- Map( const Map& other );
+ Map(Map&& other);
/**
* @brief Non-virtual destructor.
+ * @SINCE_1_0.0
*/
~Map();
/**
- * @brief Retrieve the number of elements in the map.
+ * @brief Retrieves the number of elements in the map.
*
- * @return The number of elements in the map.
+ * @SINCE_1_0.0
+ * @return The number of elements in the map
*/
- unsigned int Count() const;
+ SizeType Count() const;
/**
* @brief Returns whether the map is empty.
*
- * @return true if empty, false otherwise
+ * @SINCE_1_0.0
+ * @return @c true if empty, @c false otherwise
*/
bool Empty() const;
/**
- * @brief Retrieve the value at the specified position.
+ * @brief Inserts the key-value pair in the Map, with the key type as string.
+ *
+ * Does not check for duplicates.
+ * @SINCE_1_0.0
+ * @param[in] key The key to insert
+ * @param[in] value The value to insert
+ */
+ void Insert(std::string key, Value value);
+
+ /**
+ * @brief Inserts the key-value pair in the Map, with the key type as index.
+ *
+ * Does not check for duplicates.
+ * @SINCE_1_1.39
+ * @param[in] key The key to insert
+ * @param[in] value The value to insert
+ */
+ void Insert(Property::Index key, Value value);
+
+ /**
+ * @brief Inserts the key-value pair in the Map, with the key type as string.
+ *
+ * Does not check for duplicates
+ * @SINCE_1_2.5
+ * @param key to insert
+ * @param value to insert
+ * @return a reference to this object
+ */
+ Property::Map& Add(std::string key, Value value)
+ {
+ Insert(std::move(key), std::move(value));
+ return *this;
+ }
+
+ /**
+ * @brief Inserts the key-value pair in the Map, with the key type as index.
+ *
+ * Does not check for duplicates
+ * @SINCE_1_2.5
+ * @param key to insert
+ * @param value to insert
+ * @return a reference to this object
+ */
+ Property::Map& Add(Property::Index key, Value value)
+ {
+ Insert(key, std::move(value));
+ return *this;
+ }
+
+ /**
+ * @brief Retrieves the value at the specified position.
+ *
+ * @SINCE_1_0.0
+ * @param[in] position The specified position
+ * @return A reference to the value at the specified position
+ *
+ * @note Will assert if position >= Count()
+ */
+ Value& GetValue(SizeType position) const;
+
+ /**
+ * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
*
- * @return A reference to the value at the specified position.
+ * @brief Retrieves the key at the specified position.
+ *
+ * @SINCE_1_0.0
+ * @param[in] position The specified position
+ * @return A const reference to the key at the specified position
*
* @note Will assert if position >= Count()
*/
- Value& GetValue( unsigned int position ) const;
+ const std::string& GetKey(SizeType position) const DALI_DEPRECATED_API;
/**
* @brief Retrieve the key at the specified position.
*
- * @return A const reference to the key at the specified position.
+ * @SINCE_1_2.7
+ * @param[in] position The specified position
+ * @return A copy of the key at the specified position.
*
* @note Will assert if position >= Count()
*/
- const std::string& GetKey( unsigned int position ) const;
+ Key GetKeyAt(SizeType position) const;
+
+ /**
+ * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
+ *
+ * @brief Retrieves the key & the value at the specified position.
+ *
+ * @SINCE_1_0.0
+ * @param[in] position The specified position
+ * @return A reference to the pair of key and value at the specified position
+ *
+ * @note Will assert if position >= Count() or key at position is an index key.
+ */
+ StringValuePair& GetPair(SizeType position) const DALI_DEPRECATED_API;
/**
* @brief Retrieve the key & the value at the specified position.
*
- * @return A reference to the pair of key and value at the specified position.
+ * @SINCE_1_2.7
+ * @param[in] position The specified position
+ * @return A copy of the pair of key and value at the specified position.
*
* @note Will assert if position >= Count()
*/
- StringValuePair& GetPair( unsigned int position ) const;
+ KeyValuePair GetKeyValue(SizeType position) const;
+
+ /**
+ * @brief Finds the value for the specified key if it exists.
+ *
+ * @SINCE_1_0.0
+ * @param[in] key The key to find
+ *
+ * @return A const pointer to the value if it exists, NULL otherwise
+ */
+ Value* Find(std::string_view key) const;
/**
* @brief Finds the value for the specified key if it exists.
*
- * @param[in] key The key to find.
+ * @SINCE_1_1.39
+ * @param[in] key The key to find
*
* @return A const pointer to the value if it exists, NULL otherwise
*/
- Value* Find( const std::string& key ) const;
+ Value* Find(Property::Index key) const;
+
+ /**
+ * @brief Finds the value for the specified keys if either exist.
+ *
+ * Will search for the index key first.
+ *
+ * @SINCE_1_1.45
+ * @param[in] indexKey The index key to find
+ * @param[in] stringKey The string key to find
+ *
+ * @return A const pointer to the value if it exists, NULL otherwise
+ */
+ Value* Find(Property::Index indexKey, std::string_view stringKey) const;
+
+ /**
+ * @brief Finds the value for the specified key if it exists and its type is type.
+ *
+ * @SINCE_1_0.0
+ * @param[in] key The key to find
+ * @param[in] type The type to check
+ *
+ * @return A const pointer to the value if it exists, NULL otherwise
+ */
+ Value* Find(std::string_view key, Property::Type type) const;
+
+ /**
+ * @brief Finds the value for the specified key if it exists and its type is type.
+ *
+ * @SINCE_1_1.39
+ * @param[in] key The key to find
+ * @param[in] type The type to check
+ *
+ * @return A const pointer to the value if it exists, NULL otherwise
+ */
+ Value* Find(Property::Index key, Property::Type type) const;
/**
* @brief Clears the map.
+ * @SINCE_1_0.0
*/
void Clear();
*
* Any values in 'from' will overwrite the values in the current map.
*
- * @param[in] from The map to merge from.
+ * @SINCE_1_0.0
+ * @param[in] from The map to merge from
*/
- void Merge( const Map& from );
+ void Merge(const Map& from);
/**
- * @brief Const operator to access element with the specified key.
+ * @brief Const operator to access element with the specified string key.
*
- * @param[in] key The key whose value to access.
+ * @SINCE_1_0.0
+ * @param[in] key The key whose value to access
*
- * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned.
+ * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned
*
* @note Will assert if invalid-key is given.
*/
- const Value& operator[]( const std::string& key ) const;
+ const Value& operator[](std::string_view key) const;
/**
- * @brief Operator to access the element with the specified key.
+ * @brief Operator to access the element with the specified string key.
*
- * @param[in] key The key whose value to access.
+ * @SINCE_1_0.0
+ * @param[in] key The key whose value to access
*
- * @return A reference to the value for the element with the specified key.
+ * @return A reference to the value for the element with the specified key
*
* @note If an element with the key does not exist, then it is created.
*/
- Value& operator[]( const std::string& key );
+ Value& operator[](std::string_view key);
/**
- * @brief Assignment Operator
+ * @brief Const operator to access element with the specified index key.
*
- * @param[in] other The map to copy from.
+ * @SINCE_1_1.39
+ * @param[in] key The key whose value to access
*
- * @return The copied map.
+ * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned
+ *
+ * @note Will assert if invalid-key is given.
*/
- Map& operator=( const Map& other );
+ const Value& operator[](Property::Index key) const;
+
+ /**
+ * @brief Operator to access the element with the specified index key.
+ *
+ * @SINCE_1_1.39
+ * @param[in] key The key whose value to access
+ *
+ * @return A reference to the value for the element with the specified key
+ *
+ * @note If an element with the key does not exist, then it is created.
+ */
+ Value& operator[](Property::Index key);
+
+ /**
+ * @brief Assignment operator.
+ *
+ * @SINCE_1_0.0
+ * @param[in] other The map to copy from
+ *
+ * @return The copied map
+ */
+ Map& operator=(const Map& other);
+
+ /**
+ * @brief Move assignment operator.
+ *
+ * @SINCE_1_4.17
+ * @param[in] other The map to move from
+ *
+ * @return The moved map
+ *
+ * @note The other array is an r-value so becomes invalid and is no longer usable.
+ */
+ Map& operator=(Map&& other);
+
+ /**
+ * @brief Output to stream.
+ * @SINCE_1_1.28
+ */
+ friend DALI_CORE_API std::ostream& operator<<(std::ostream& stream, const Property::Map& map);
private:
- struct DALI_INTERNAL Impl; ///< Private data
- Impl* mImpl; ///< Pointer to private data
+ struct DALI_INTERNAL Impl; ///< Private data
+ Impl* mImpl; ///< Pointer to private data
};
+/**
+ * @brief Converts the key/value pairs of the property map into a string and append to an output stream.
+ *
+ * @SINCE_1_1.28
+ * @param[in] stream The output stream operator
+ * @param[in] map The map to insert
+ * @return The output stream operator
+ */
+DALI_CORE_API std::ostream& operator<<(std::ostream& stream, const Property::Map& map);
+
+/**
+ * @}
+ */
} // namespace Dali
-#endif // __DALI_PROPERTY_MAP_H__
+#endif // DALI_PROPERTY_MAP_H