blob: 045fbd76a888493e15d4690736681b4034a3e3fb [file] [log] [blame]
David Wagnerb76c9d62014-02-05 18:30:24 +01001/*
2 * Copyright (c) 2011-2014, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Patrick Benavoli68a91282011-08-31 11:23:23 +020029 */
30#include "MappingContext.h"
Renaud de Chivre46966e02013-09-02 10:48:36 +020031#include <assert.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020032#include <string.h>
33#include <stdlib.h>
34
Sebastien Gonzalved9526492014-02-20 22:28:03 +010035using std::string;
36
Patrick Benavoli911844b2014-07-23 01:27:00 +020037CMappingContext::CMappingContext(size_t uiNbItemTypes) : _pstItemArray(new CMappingContext::SItem[uiNbItemTypes]), _uiNbItemTypes(uiNbItemTypes)
Patrick Benavoli68a91282011-08-31 11:23:23 +020038{
39 // Clear items
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020040 memset(_pstItemArray, 0, sizeof(*_pstItemArray) * uiNbItemTypes);
Patrick Benavoli68a91282011-08-31 11:23:23 +020041}
42
43CMappingContext::~CMappingContext()
44{
45 delete [] _pstItemArray;
46}
47
48// Copy constructor
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020049CMappingContext::CMappingContext(const CMappingContext& from) : _pstItemArray(new CMappingContext::SItem[from._uiNbItemTypes]), _uiNbItemTypes(from._uiNbItemTypes)
Patrick Benavoli68a91282011-08-31 11:23:23 +020050{
51 // Copy content items
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020052 memcpy(_pstItemArray, from._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
Patrick Benavoli68a91282011-08-31 11:23:23 +020053}
54
55// Affectation
Kevin Rocardb2ffa5a2013-04-04 19:21:46 +020056CMappingContext& CMappingContext::operator=(const CMappingContext& right)
Patrick Benavoli68a91282011-08-31 11:23:23 +020057{
58 if (&right != this) {
59
60 // Size
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020061 _uiNbItemTypes = right._uiNbItemTypes;
Patrick Benavoli68a91282011-08-31 11:23:23 +020062
63 // Content
64 // Delete previous array
65 delete [] _pstItemArray;
66
67 // Reallocate it
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020068 _pstItemArray = new CMappingContext::SItem[_uiNbItemTypes];
Patrick Benavoli68a91282011-08-31 11:23:23 +020069
70 // Copy content items
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020071 memcpy(_pstItemArray, right._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
Patrick Benavoli68a91282011-08-31 11:23:23 +020072 }
73 return *this;
74}
75
76// Item access
Renaud de Chivre46966e02013-09-02 10:48:36 +020077bool CMappingContext::setItem(uint32_t uiItemType, const string* pStrKey, const string* pStrItem)
Patrick Benavoli68a91282011-08-31 11:23:23 +020078{
Patrick Benavoli911844b2014-07-23 01:27:00 +020079 size_t uiIndex;
Renaud de Chivre46966e02013-09-02 10:48:36 +020080
Patrick Benavoli68a91282011-08-31 11:23:23 +020081 // Do some checks
Renaud de Chivre46966e02013-09-02 10:48:36 +020082 for (uiIndex = 0; uiIndex < _uiNbItemTypes; uiIndex++) {
83
84 // Does key already exist ?
85 assert(_pstItemArray[uiIndex].strKey != pStrKey);
86 }
87
Patrick Benavoli68a91282011-08-31 11:23:23 +020088 if (_pstItemArray[uiItemType].bSet) {
89
90 // Already set!
91 return false;
92 }
Renaud de Chivre46966e02013-09-02 10:48:36 +020093
94 // Set item key
95 _pstItemArray[uiItemType].strKey = pStrKey;
96
97 // Set item value
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020098 _pstItemArray[uiItemType].strItem = pStrItem;
Patrick Benavoli68a91282011-08-31 11:23:23 +020099
100 // Now is set
101 _pstItemArray[uiItemType].bSet = true;
102
103 return true;
104}
105
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200106const string& CMappingContext::getItem(uint32_t uiItemType) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200107{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200108 return *_pstItemArray[uiItemType].strItem;
109}
110
111uint32_t CMappingContext::getItemAsInteger(uint32_t uiItemType) const
112{
113 if (!_pstItemArray[uiItemType].strItem) {
114
115 return 0;
116 }
117
118 return strtoul(_pstItemArray[uiItemType].strItem->c_str(), NULL, 0);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200119}
120
Renaud de Chivre46966e02013-09-02 10:48:36 +0200121const string* CMappingContext::getItem(const string& strKey) const
122{
Patrick Benavoli911844b2014-07-23 01:27:00 +0200123 size_t uiItemType;
Renaud de Chivre46966e02013-09-02 10:48:36 +0200124
125 for (uiItemType = 0; uiItemType < _uiNbItemTypes; uiItemType++) {
126
127 if (_pstItemArray[uiItemType].strKey != NULL &&
128 strKey == *_pstItemArray[uiItemType].strKey) {
129
130 return _pstItemArray[uiItemType].strItem;
131 }
132 }
133
134 return NULL;
135}
136
Patrick Benavoli68a91282011-08-31 11:23:23 +0200137bool CMappingContext::iSet(uint32_t uiItemType) const
138{
139 return _pstItemArray[uiItemType].bSet;
140}