blob: 727fadb1203b9cb463bbde6b4672ca3aa5f4628d [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +02001/*
Patrick Benavoli68a91282011-08-31 11:23:23 +02002 * INTEL CONFIDENTIAL
3 * Copyright © 2011 Intel
4 * Corporation All Rights Reserved.
5 *
6 * The source code contained or described herein and all documents related to
7 * the source code ("Material") are owned by Intel Corporation or its suppliers
8 * or licensors. Title to the Material remains with Intel Corporation or its
9 * suppliers and licensors. The Material contains trade secrets and proprietary
10 * and confidential information of Intel or its suppliers and licensors. The
11 * Material is protected by worldwide copyright and trade secret laws and
12 * treaty provisions. No part of the Material may be used, copied, reproduced,
13 * modified, published, uploaded, posted, transmitted, distributed, or
14 * disclosed in any way without Intel’s prior express written permission.
15 *
16 * No license under any patent, copyright, trade secret or other intellectual
17 * property right is granted to or conferred upon you by disclosure or delivery
18 * of the Materials, either expressly, by implication, inducement, estoppel or
19 * otherwise. Any license under such intellectual property rights must be
20 * express and approved by Intel in writing.
21 *
Patrick Benavoli68a91282011-08-31 11:23:23 +020022 * CREATED: 2011-06-01
23 * UPDATED: 2011-07-27
Patrick Benavoli68a91282011-08-31 11:23:23 +020024 */
25#include "MappingContext.h"
Renaud de Chivre46966e02013-09-02 10:48:36 +020026#include <assert.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020027#include <string.h>
28#include <stdlib.h>
29
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020030CMappingContext::CMappingContext(uint32_t uiNbItemTypes) : _pstItemArray(new CMappingContext::SItem[uiNbItemTypes]), _uiNbItemTypes(uiNbItemTypes)
Patrick Benavoli68a91282011-08-31 11:23:23 +020031{
32 // Clear items
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020033 memset(_pstItemArray, 0, sizeof(*_pstItemArray) * uiNbItemTypes);
Patrick Benavoli68a91282011-08-31 11:23:23 +020034}
35
36CMappingContext::~CMappingContext()
37{
38 delete [] _pstItemArray;
39}
40
41// Copy constructor
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020042CMappingContext::CMappingContext(const CMappingContext& from) : _pstItemArray(new CMappingContext::SItem[from._uiNbItemTypes]), _uiNbItemTypes(from._uiNbItemTypes)
Patrick Benavoli68a91282011-08-31 11:23:23 +020043{
44 // Copy content items
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020045 memcpy(_pstItemArray, from._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
Patrick Benavoli68a91282011-08-31 11:23:23 +020046}
47
48// Affectation
Kevin Rocardb2ffa5a2013-04-04 19:21:46 +020049CMappingContext& CMappingContext::operator=(const CMappingContext& right)
Patrick Benavoli68a91282011-08-31 11:23:23 +020050{
51 if (&right != this) {
52
53 // Size
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020054 _uiNbItemTypes = right._uiNbItemTypes;
Patrick Benavoli68a91282011-08-31 11:23:23 +020055
56 // Content
57 // Delete previous array
58 delete [] _pstItemArray;
59
60 // Reallocate it
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020061 _pstItemArray = new CMappingContext::SItem[_uiNbItemTypes];
Patrick Benavoli68a91282011-08-31 11:23:23 +020062
63 // Copy content items
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020064 memcpy(_pstItemArray, right._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
Patrick Benavoli68a91282011-08-31 11:23:23 +020065 }
66 return *this;
67}
68
69// Item access
Renaud de Chivre46966e02013-09-02 10:48:36 +020070bool CMappingContext::setItem(uint32_t uiItemType, const string* pStrKey, const string* pStrItem)
Patrick Benavoli68a91282011-08-31 11:23:23 +020071{
Renaud de Chivre46966e02013-09-02 10:48:36 +020072 uint32_t uiIndex;
73
Patrick Benavoli68a91282011-08-31 11:23:23 +020074 // Do some checks
Renaud de Chivre46966e02013-09-02 10:48:36 +020075 for (uiIndex = 0; uiIndex < _uiNbItemTypes; uiIndex++) {
76
77 // Does key already exist ?
78 assert(_pstItemArray[uiIndex].strKey != pStrKey);
79 }
80
Patrick Benavoli68a91282011-08-31 11:23:23 +020081 if (_pstItemArray[uiItemType].bSet) {
82
83 // Already set!
84 return false;
85 }
Renaud de Chivre46966e02013-09-02 10:48:36 +020086
87 // Set item key
88 _pstItemArray[uiItemType].strKey = pStrKey;
89
90 // Set item value
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020091 _pstItemArray[uiItemType].strItem = pStrItem;
Patrick Benavoli68a91282011-08-31 11:23:23 +020092
93 // Now is set
94 _pstItemArray[uiItemType].bSet = true;
95
96 return true;
97}
98
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020099const string& CMappingContext::getItem(uint32_t uiItemType) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200100{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200101 return *_pstItemArray[uiItemType].strItem;
102}
103
104uint32_t CMappingContext::getItemAsInteger(uint32_t uiItemType) const
105{
106 if (!_pstItemArray[uiItemType].strItem) {
107
108 return 0;
109 }
110
111 return strtoul(_pstItemArray[uiItemType].strItem->c_str(), NULL, 0);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200112}
113
Renaud de Chivre46966e02013-09-02 10:48:36 +0200114const string* CMappingContext::getItem(const string& strKey) const
115{
116 uint32_t uiItemType;
117
118 for (uiItemType = 0; uiItemType < _uiNbItemTypes; uiItemType++) {
119
120 if (_pstItemArray[uiItemType].strKey != NULL &&
121 strKey == *_pstItemArray[uiItemType].strKey) {
122
123 return _pstItemArray[uiItemType].strItem;
124 }
125 }
126
127 return NULL;
128}
129
Patrick Benavoli68a91282011-08-31 11:23:23 +0200130bool CMappingContext::iSet(uint32_t uiItemType) const
131{
132 return _pstItemArray[uiItemType].bSet;
133}