blob: 852f89faa97044b56d0e4be9da22d3cf44c2f8c3 [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
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020035CMappingContext::CMappingContext(uint32_t uiNbItemTypes) : _pstItemArray(new CMappingContext::SItem[uiNbItemTypes]), _uiNbItemTypes(uiNbItemTypes)
Patrick Benavoli68a91282011-08-31 11:23:23 +020036{
37 // Clear items
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020038 memset(_pstItemArray, 0, sizeof(*_pstItemArray) * uiNbItemTypes);
Patrick Benavoli68a91282011-08-31 11:23:23 +020039}
40
41CMappingContext::~CMappingContext()
42{
43 delete [] _pstItemArray;
44}
45
46// Copy constructor
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020047CMappingContext::CMappingContext(const CMappingContext& from) : _pstItemArray(new CMappingContext::SItem[from._uiNbItemTypes]), _uiNbItemTypes(from._uiNbItemTypes)
Patrick Benavoli68a91282011-08-31 11:23:23 +020048{
49 // Copy content items
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020050 memcpy(_pstItemArray, from._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
Patrick Benavoli68a91282011-08-31 11:23:23 +020051}
52
53// Affectation
Kevin Rocardb2ffa5a2013-04-04 19:21:46 +020054CMappingContext& CMappingContext::operator=(const CMappingContext& right)
Patrick Benavoli68a91282011-08-31 11:23:23 +020055{
56 if (&right != this) {
57
58 // Size
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020059 _uiNbItemTypes = right._uiNbItemTypes;
Patrick Benavoli68a91282011-08-31 11:23:23 +020060
61 // Content
62 // Delete previous array
63 delete [] _pstItemArray;
64
65 // Reallocate it
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020066 _pstItemArray = new CMappingContext::SItem[_uiNbItemTypes];
Patrick Benavoli68a91282011-08-31 11:23:23 +020067
68 // Copy content items
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020069 memcpy(_pstItemArray, right._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
Patrick Benavoli68a91282011-08-31 11:23:23 +020070 }
71 return *this;
72}
73
74// Item access
Renaud de Chivre46966e02013-09-02 10:48:36 +020075bool CMappingContext::setItem(uint32_t uiItemType, const string* pStrKey, const string* pStrItem)
Patrick Benavoli68a91282011-08-31 11:23:23 +020076{
Renaud de Chivre46966e02013-09-02 10:48:36 +020077 uint32_t uiIndex;
78
Patrick Benavoli68a91282011-08-31 11:23:23 +020079 // Do some checks
Renaud de Chivre46966e02013-09-02 10:48:36 +020080 for (uiIndex = 0; uiIndex < _uiNbItemTypes; uiIndex++) {
81
82 // Does key already exist ?
83 assert(_pstItemArray[uiIndex].strKey != pStrKey);
84 }
85
Patrick Benavoli68a91282011-08-31 11:23:23 +020086 if (_pstItemArray[uiItemType].bSet) {
87
88 // Already set!
89 return false;
90 }
Renaud de Chivre46966e02013-09-02 10:48:36 +020091
92 // Set item key
93 _pstItemArray[uiItemType].strKey = pStrKey;
94
95 // Set item value
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020096 _pstItemArray[uiItemType].strItem = pStrItem;
Patrick Benavoli68a91282011-08-31 11:23:23 +020097
98 // Now is set
99 _pstItemArray[uiItemType].bSet = true;
100
101 return true;
102}
103
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200104const string& CMappingContext::getItem(uint32_t uiItemType) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200105{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200106 return *_pstItemArray[uiItemType].strItem;
107}
108
109uint32_t CMappingContext::getItemAsInteger(uint32_t uiItemType) const
110{
111 if (!_pstItemArray[uiItemType].strItem) {
112
113 return 0;
114 }
115
116 return strtoul(_pstItemArray[uiItemType].strItem->c_str(), NULL, 0);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200117}
118
Renaud de Chivre46966e02013-09-02 10:48:36 +0200119const string* CMappingContext::getItem(const string& strKey) const
120{
121 uint32_t uiItemType;
122
123 for (uiItemType = 0; uiItemType < _uiNbItemTypes; uiItemType++) {
124
125 if (_pstItemArray[uiItemType].strKey != NULL &&
126 strKey == *_pstItemArray[uiItemType].strKey) {
127
128 return _pstItemArray[uiItemType].strItem;
129 }
130 }
131
132 return NULL;
133}
134
Patrick Benavoli68a91282011-08-31 11:23:23 +0200135bool CMappingContext::iSet(uint32_t uiItemType) const
136{
137 return _pstItemArray[uiItemType].bSet;
138}