Use modern construct 'using' instead of typedef.
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-map.cpp
index 675d910..a45cb3e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 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.
@@ -31,8 +31,8 @@ namespace
 {
 typedef std::vector< StringValuePair > StringValueContainer;
 
-typedef std::pair< Property::Index, Property::Value > IndexValuePair;
-typedef std::vector< IndexValuePair > IndexValueContainer;
+using IndexValuePair      = std::pair<Property::Index, Property::Value>;
+using IndexValueContainer = std::vector<IndexValuePair>;
 
 }; // unnamed namespace
 
@@ -48,6 +48,27 @@ Property::Map::Map()
 {
 }
 
+Property::Map::Map( const std::initializer_list< KeyValuePair >& values ) : Map()
+{
+  for( auto&& value : values )
+  {
+    const auto& key = value.first;
+    switch( key.type )
+    {
+      case Property::Key::INDEX:
+      {
+        Property::Map::Insert( key.indexKey, value.second );
+        break;
+      }
+      case Property::Key::STRING:
+      {
+        Property::Map::Insert( key.stringKey, value.second );
+        break;
+      }
+    }
+  }
+}
+
 Property::Map::Map( const Property::Map& other )
 : mImpl( new Impl )
 {
@@ -55,6 +76,12 @@ Property::Map::Map( const Property::Map& other )
   mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
 }
 
+Property::Map::Map( Property::Map&& other )
+: mImpl( other.mImpl )
+{
+  other.mImpl = nullptr;
+}
+
 Property::Map::~Map()
 {
   delete mImpl;
@@ -62,31 +89,37 @@ Property::Map::~Map()
 
 Property::Map::SizeType Property::Map::Count() const
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
   return mImpl->mStringValueContainer.size() + mImpl->mIndexValueContainer.size();
 }
 
 bool Property::Map::Empty() const
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
   return mImpl->mStringValueContainer.empty() && mImpl->mIndexValueContainer.empty();
 }
 
 void Property::Map::Insert( const char* key, const Value& value )
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
   mImpl->mStringValueContainer.push_back( std::make_pair( key, value ) );
 }
 
 void Property::Map::Insert( const std::string& key, const Value& value )
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
   mImpl->mStringValueContainer.push_back( std::make_pair( key, value ) );
 }
 
 void Property::Map::Insert( Property::Index key, const Value& value )
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
   mImpl->mIndexValueContainer.push_back( std::make_pair( key, value ) );
 }
 
 Property::Value& Property::Map::GetValue( SizeType position ) const
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
   SizeType numStringKeys = mImpl->mStringValueContainer.size();
   SizeType numIndexKeys  = mImpl->mIndexValueContainer.size();
   DALI_ASSERT_ALWAYS( position < ( numStringKeys + numIndexKeys ) && "position out-of-bounds" );
@@ -105,6 +138,7 @@ const std::string& Property::Map::GetKey( SizeType position ) const
 {
   DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: GetKey() is deprecated and will be removed from next release.\n" );
 
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
   SizeType numStringKeys = mImpl->mStringValueContainer.size();
   DALI_ASSERT_ALWAYS( position < numStringKeys && "position out-of-bounds" );
 
@@ -113,6 +147,8 @@ const std::string& Property::Map::GetKey( SizeType position ) const
 
 Property::Key Property::Map::GetKeyAt( SizeType position ) const
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
   SizeType numStringKeys = mImpl->mStringValueContainer.size();
   SizeType numIndexKeys  = mImpl->mIndexValueContainer.size();
   DALI_ASSERT_ALWAYS( position < ( numStringKeys + numIndexKeys ) && "position out-of-bounds" );
@@ -133,6 +169,8 @@ StringValuePair& Property::Map::GetPair( SizeType position ) const
 {
   DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: GetPair() is deprecated and will be removed from next release.\n" );
 
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
   SizeType numStringKeys = mImpl->mStringValueContainer.size();
 
   DALI_ASSERT_ALWAYS( position < ( numStringKeys ) && "position out-of-bounds" );
@@ -142,6 +180,8 @@ StringValuePair& Property::Map::GetPair( SizeType position ) const
 
 KeyValuePair Property::Map::GetKeyValue( SizeType position ) const
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
   SizeType numStringKeys = mImpl->mStringValueContainer.size();
   SizeType numIndexKeys  = mImpl->mIndexValueContainer.size();
 
@@ -163,11 +203,13 @@ KeyValuePair Property::Map::GetKeyValue( SizeType position ) const
 
 Property::Value* Property::Map::Find( const char* key ) const
 {
-  for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
+  for( auto&& iter : mImpl->mStringValueContainer )
   {
-    if ( iter->first == key )
+    if ( iter.first == key )
     {
-      return &iter->second;
+      return &iter.second;
     }
   }
   return NULL; // Not found
@@ -180,11 +222,13 @@ Property::Value* Property::Map::Find( const std::string& key ) const
 
 Property::Value* Property::Map::Find( Property::Index key ) const
 {
-  for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
+  for( auto&& iter : mImpl->mIndexValueContainer )
   {
-    if ( iter->first == key )
+    if ( iter.first == key )
     {
-      return &iter->second;
+      return &iter.second;
     }
   }
   return NULL; // Not found
@@ -202,11 +246,13 @@ Property::Value* Property::Map::Find( Property::Index indexKey, const std::strin
 
 Property::Value* Property::Map::Find( const std::string& key, Property::Type type ) const
 {
-  for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
+  for( auto&& iter : mImpl->mStringValueContainer )
   {
-    if( (iter->second.GetType() == type) && (iter->first == key) )
+    if( (iter.second.GetType() == type) && (iter.first == key) )
     {
-      return &iter->second;
+      return &iter.second;
     }
   }
   return NULL; // Not found
@@ -214,11 +260,13 @@ Property::Value* Property::Map::Find( const std::string& key, Property::Type typ
 
 Property::Value* Property::Map::Find( Property::Index key, Property::Type type ) const
 {
-  for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
+  for( auto&& iter : mImpl->mIndexValueContainer )
   {
-    if( (iter->second.GetType() == type) && (iter->first == key) )
+    if( (iter.second.GetType() == type) && (iter.first == key) )
     {
-      return &iter->second;
+      return &iter.second;
     }
   }
   return NULL; // Not found
@@ -226,25 +274,29 @@ Property::Value* Property::Map::Find( Property::Index key, Property::Type type )
 
 void Property::Map::Clear()
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
   mImpl->mStringValueContainer.clear();
   mImpl->mIndexValueContainer.clear();
 }
 
 void Property::Map::Merge( const Property::Map& from )
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
   // Ensure we're not attempting to merge with ourself
   if ( this != &from )
   {
     if ( Count() )
     {
-      for ( StringValueContainer::const_iterator iter = from.mImpl->mStringValueContainer.begin(), endIter = from.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
+      for( auto&& iter : from.mImpl->mStringValueContainer )
       {
-        (*this)[iter->first] = iter->second;
+        (*this)[iter.first] = iter.second;
       }
 
-      for ( IndexValueContainer::const_iterator iter = from.mImpl->mIndexValueContainer.begin(), endIter = from.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
+      for( auto&& iter : from.mImpl->mIndexValueContainer )
       {
-        (*this)[iter->first] = iter->second;
+        (*this)[iter.first] = iter.second;
       }
     }
     else
@@ -257,11 +309,13 @@ void Property::Map::Merge( const Property::Map& from )
 
 const Property::Value& Property::Map::operator[]( const std::string& key ) const
 {
-  for ( StringValueContainer::const_iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
+  for( auto&& iter : mImpl->mStringValueContainer )
   {
-    if ( iter->first == key )
+    if ( iter.first == key )
     {
-      return iter->second;
+      return iter.second;
     }
   }
 
@@ -270,11 +324,13 @@ const Property::Value& Property::Map::operator[]( const std::string& key ) const
 
 Property::Value& Property::Map::operator[]( const std::string& key )
 {
-  for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
+  for( auto&& iter : mImpl->mStringValueContainer )
   {
-    if ( iter->first == key )
+    if ( iter.first == key )
     {
-      return iter->second;
+      return iter.second;
     }
   }
 
@@ -285,11 +341,13 @@ Property::Value& Property::Map::operator[]( const std::string& key )
 
 const Property::Value& Property::Map::operator[]( Property::Index key ) const
 {
-  for ( IndexValueContainer::const_iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
+  for( auto&& iter : mImpl->mIndexValueContainer )
   {
-    if ( iter->first == key )
+    if ( iter.first == key )
     {
-      return iter->second;
+      return iter.second;
     }
   }
 
@@ -298,11 +356,13 @@ const Property::Value& Property::Map::operator[]( Property::Index key ) const
 
 Property::Value& Property::Map::operator[]( Property::Index key )
 {
-  for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
+  for( auto&& iter : mImpl->mIndexValueContainer )
   {
-    if ( iter->first == key )
+    if ( iter.first == key )
     {
-      return iter->second;
+      return iter.second;
     }
   }
 
@@ -313,39 +373,53 @@ Property::Value& Property::Map::operator[]( Property::Index key )
 
 Property::Map& Property::Map::operator=( const Property::Map& other )
 {
+  DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
+
   if( this != &other )
   {
-    delete mImpl;
-    mImpl = new Impl;
     mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
     mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
   }
   return *this;
 }
 
+Property::Map& Property::Map::operator=( Property::Map&& other )
+{
+  if( this != &other )
+  {
+    delete mImpl;
+    mImpl = other.mImpl;
+    other.mImpl = nullptr;
+  }
+  return *this;
+}
+
 std::ostream& operator<<( std::ostream& stream, const Property::Map& map )
 {
   stream << "Map(" << map.Count() << ") = {";
 
-  int32_t count = 0;
-  // Output the String-Value pairs
-  for ( StringValueContainer::iterator iter = map.mImpl->mStringValueContainer.begin(), endIter = map.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
+  if ( map.mImpl )
   {
-    if( count++ > 0 )
+    int32_t count = 0;
+    // Output the String-Value pairs
+    for( auto&& iter : map.mImpl->mStringValueContainer )
     {
-      stream<<", ";
+      if( count++ > 0 )
+      {
+        stream<<", ";
+      }
+      stream<< iter.first << ":" << iter.second;
     }
-    stream<< iter->first << ":"<<iter->second;
-  }
 
-  // Output the Index-Value pairs
-  for ( IndexValueContainer::iterator iter = map.mImpl->mIndexValueContainer.begin(), endIter = map.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
-  {
-    if( count++ > 0 )
+    // Output the Index-Value pairs
+    for( auto&& iter : map.mImpl->mIndexValueContainer )
     {
-      stream<<", ";
+      if( count++ > 0 )
+      {
+        stream<<", ";
+      }
+      stream<< iter.first << ":" << iter.second;
     }
-    stream<< iter->first << ":"<<iter->second;
   }
 
   stream << "}";