1/*
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.
29 */
30#include "MappingContext.h"
31#include <assert.h>
32#include <string.h>
33#include <stdlib.h>
34
35using std::string;
36
37CMappingContext::CMappingContext(size_t uiNbItemTypes) : _pstItemArray(new CMappingContext::SItem[uiNbItemTypes]), _uiNbItemTypes(uiNbItemTypes)
38{
39    // Clear items
40    memset(_pstItemArray, 0, sizeof(*_pstItemArray) * uiNbItemTypes);
41}
42
43CMappingContext::~CMappingContext()
44{
45    delete [] _pstItemArray;
46}
47
48// Copy constructor
49CMappingContext::CMappingContext(const CMappingContext& from) : _pstItemArray(new CMappingContext::SItem[from._uiNbItemTypes]), _uiNbItemTypes(from._uiNbItemTypes)
50{
51    // Copy content items
52    memcpy(_pstItemArray, from._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
53}
54
55// Affectation
56CMappingContext& CMappingContext::operator=(const CMappingContext& right)
57{
58    if (&right != this) {
59
60        // Size
61        _uiNbItemTypes = right._uiNbItemTypes;
62
63        // Content
64        // Delete previous array
65        delete [] _pstItemArray;
66
67        // Reallocate it
68        _pstItemArray = new CMappingContext::SItem[_uiNbItemTypes];
69
70        // Copy content items
71        memcpy(_pstItemArray, right._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
72    }
73    return *this;
74}
75
76// Item access
77bool CMappingContext::setItem(uint32_t uiItemType, const string* pStrKey, const string* pStrItem)
78{
79    size_t uiIndex;
80
81    // Do some checks
82    for (uiIndex = 0; uiIndex < _uiNbItemTypes; uiIndex++) {
83
84        // Does key already exist ?
85        assert(_pstItemArray[uiIndex].strKey != pStrKey);
86    }
87
88    if (_pstItemArray[uiItemType].bSet) {
89
90        // Already set!
91        return false;
92    }
93
94    // Set item key
95    _pstItemArray[uiItemType].strKey = pStrKey;
96
97    // Set item value
98    _pstItemArray[uiItemType].strItem = pStrItem;
99
100    // Now is set
101    _pstItemArray[uiItemType].bSet = true;
102
103    return true;
104}
105
106const string&  CMappingContext::getItem(uint32_t uiItemType) const
107{
108    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);
119}
120
121const string* CMappingContext::getItem(const string& strKey) const
122{
123    size_t uiItemType;
124
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
137bool CMappingContext::iSet(uint32_t uiItemType) const
138{
139    return _pstItemArray[uiItemType].bSet;
140}
141