Handle key names in mapping context

BZ: 126002

Mapping context only allows to retrieve a mapping value from its
corresponding key ID. We also want to do the same using its key
as a string.

This patch adds key name handling in context mapping. It also adds
an API to retieve a mapping value from its corresponding key name.

Note: it also removes an unused argument from handleMappingContext
function.

Change-Id: I5a18ad624a69272ea2796090692f2bfa8b373a52
Signed-off-by: Renaud de Chivre <renaud.de.chivre@intel.com>
Reviewed-on: http://android.intel.com:8080/131571
Tested-by: Dixon, CharlesX <charlesx.dixon@intel.com>
Reviewed-by: cactus <cactus@intel.com>
Tested-by: cactus <cactus@intel.com>
diff --git a/parameter/MappingContext.cpp b/parameter/MappingContext.cpp
index dae71ee..727fadb 100644
--- a/parameter/MappingContext.cpp
+++ b/parameter/MappingContext.cpp
@@ -23,6 +23,7 @@
  * UPDATED: 2011-07-27
  */
 #include "MappingContext.h"
+#include <assert.h>
 #include <string.h>
 #include <stdlib.h>
 
@@ -66,15 +67,27 @@
 }
 
 // Item access
-bool CMappingContext::setItem(uint32_t uiItemType, const string* pStrItem)
+bool CMappingContext::setItem(uint32_t uiItemType, const string* pStrKey, const string* pStrItem)
 {
+    uint32_t uiIndex;
+
     // Do some checks
+    for (uiIndex = 0; uiIndex < _uiNbItemTypes; uiIndex++) {
+
+        // Does key already exist ?
+        assert(_pstItemArray[uiIndex].strKey != pStrKey);
+    }
+
     if (_pstItemArray[uiItemType].bSet) {
 
         // Already set!
         return false;
     }
-    // Get item value
+
+    // Set item key
+    _pstItemArray[uiItemType].strKey = pStrKey;
+
+    // Set item value
     _pstItemArray[uiItemType].strItem = pStrItem;
 
     // Now is set
@@ -98,6 +111,22 @@
     return strtoul(_pstItemArray[uiItemType].strItem->c_str(), NULL, 0);
 }
 
+const string* CMappingContext::getItem(const string& strKey) const
+{
+    uint32_t uiItemType;
+
+    for (uiItemType = 0; uiItemType < _uiNbItemTypes; uiItemType++) {
+
+        if (_pstItemArray[uiItemType].strKey != NULL &&
+            strKey == *_pstItemArray[uiItemType].strKey) {
+
+            return _pstItemArray[uiItemType].strItem;
+        }
+    }
+
+    return NULL;
+}
+
 bool CMappingContext::iSet(uint32_t uiItemType) const
 {
     return _pstItemArray[uiItemType].bSet;
diff --git a/parameter/MappingContext.h b/parameter/MappingContext.h
index 47a4ead..385ed9b 100644
--- a/parameter/MappingContext.h
+++ b/parameter/MappingContext.h
@@ -33,6 +33,7 @@
 {
     // Item structure
     struct SItem {
+        const string* strKey;
         const string* strItem;
         bool bSet;
     };
@@ -49,10 +50,28 @@
     CMappingContext& operator=(const CMappingContext& right);
 
     // Item access
-    bool setItem(uint32_t uiItemType, const string* pStrItem);
+    /**
+     * Set context mapping item key and value.
+     *
+     * @param[in] uiItemType Mapping item index.
+     * @param[in] pStrKey Mapping item key name.
+     * @param[in] pStrItem Mapping item value.
+     *
+     * @return False if already set, true else.
+     */
+    bool setItem(uint32_t uiItemType, const string* pStrKey, const string* pStrItem);
     const string& getItem(uint32_t uiItemType) const;
     uint32_t getItemAsInteger(uint32_t uiItemType) const;
+    /**
+     * Get mapping item value from its key name.
+     *
+     * @param[in] strKey Mapping item key name.
+     *
+     * @return Mapping item value pointer if found, NULL else.
+     */
+    const string* getItem(const string& strKey) const;
     bool iSet(uint32_t uiItemType) const;
+
 private:
     // Item array
     SItem* _pstItemArray;
diff --git a/parameter/Subsystem.cpp b/parameter/Subsystem.cpp
index c776065..221b27a 100644
--- a/parameter/Subsystem.cpp
+++ b/parameter/Subsystem.cpp
@@ -335,21 +335,20 @@
 // Mapping generic context handling
 bool CSubsystem::handleMappingContext(
         const CInstanceConfigurableElement* pInstanceConfigurableElement,
-        const vector<string>& contextMappingKeyArray,
         CMappingContext& context,
         string& strError) const
 {
     // Feed context with found mapping data
     uint32_t uiItem;
 
-    for (uiItem = 0; uiItem < contextMappingKeyArray.size(); uiItem++) {
+    for (uiItem = 0; uiItem < _contextMappingKeyArray.size(); uiItem++) {
 
-        string strKey = contextMappingKeyArray[uiItem];
+        const string& strKey = _contextMappingKeyArray[uiItem];
         const string* pStrValue;
 
         if (pInstanceConfigurableElement->getMappingData(strKey, pStrValue)) {
             // Assign item to context
-            if (!context.setItem(uiItem, pStrValue)) {
+            if (!context.setItem(uiItem, &strKey, pStrValue)) {
 
                 strError = getMappingError(strKey, "Already set", pInstanceConfigurableElement);
 
@@ -438,7 +437,7 @@
     CMappingContext context = _contextStack.top();
 
     // Add mapping in context
-    if (!handleMappingContext(pInstanceConfigurableElement, _contextMappingKeyArray, context,
+    if (!handleMappingContext(pInstanceConfigurableElement, context,
                               strError)) {
 
         return false;
diff --git a/parameter/Subsystem.h b/parameter/Subsystem.h
index abd6c39..63546ad 100644
--- a/parameter/Subsystem.h
+++ b/parameter/Subsystem.h
@@ -189,13 +189,12 @@
      * Feed context with mapping data of the current element
      *
      * @param[in] pInstanceConfigurableElement The element containing mapping data
-     * @param[in] contextMappingKeyArray The list of keys of the context mapping
      * @param[out] context The context mapping to update with the current element mapping values
      * @param[out] strError The formated error string
+     *
      * @return true on success
      */
     bool handleMappingContext(const CInstanceConfigurableElement* pInstanceConfigurableElement,
-                              const vector<string>& contextMappingKeyArray,
                               CMappingContext& context,
                               string& strError) const;