MappingContext.cpp revision 46966e050fdd1ae48c7692ae9818762ba262e781
1/*
2 * 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 *
22 * CREATED: 2011-06-01
23 * UPDATED: 2011-07-27
24 */
25#include "MappingContext.h"
26#include <assert.h>
27#include <string.h>
28#include <stdlib.h>
29
30CMappingContext::CMappingContext(uint32_t uiNbItemTypes) : _pstItemArray(new CMappingContext::SItem[uiNbItemTypes]), _uiNbItemTypes(uiNbItemTypes)
31{
32    // Clear items
33    memset(_pstItemArray, 0, sizeof(*_pstItemArray) * uiNbItemTypes);
34}
35
36CMappingContext::~CMappingContext()
37{
38    delete [] _pstItemArray;
39}
40
41// Copy constructor
42CMappingContext::CMappingContext(const CMappingContext& from) : _pstItemArray(new CMappingContext::SItem[from._uiNbItemTypes]), _uiNbItemTypes(from._uiNbItemTypes)
43{
44    // Copy content items
45    memcpy(_pstItemArray, from._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
46}
47
48// Affectation
49CMappingContext& CMappingContext::operator=(const CMappingContext& right)
50{
51    if (&right != this) {
52
53        // Size
54        _uiNbItemTypes = right._uiNbItemTypes;
55
56        // Content
57        // Delete previous array
58        delete [] _pstItemArray;
59
60        // Reallocate it
61        _pstItemArray = new CMappingContext::SItem[_uiNbItemTypes];
62
63        // Copy content items
64        memcpy(_pstItemArray, right._pstItemArray, sizeof(*_pstItemArray) * _uiNbItemTypes);
65    }
66    return *this;
67}
68
69// Item access
70bool CMappingContext::setItem(uint32_t uiItemType, const string* pStrKey, const string* pStrItem)
71{
72    uint32_t uiIndex;
73
74    // Do some checks
75    for (uiIndex = 0; uiIndex < _uiNbItemTypes; uiIndex++) {
76
77        // Does key already exist ?
78        assert(_pstItemArray[uiIndex].strKey != pStrKey);
79    }
80
81    if (_pstItemArray[uiItemType].bSet) {
82
83        // Already set!
84        return false;
85    }
86
87    // Set item key
88    _pstItemArray[uiItemType].strKey = pStrKey;
89
90    // Set item value
91    _pstItemArray[uiItemType].strItem = pStrItem;
92
93    // Now is set
94    _pstItemArray[uiItemType].bSet = true;
95
96    return true;
97}
98
99const string&  CMappingContext::getItem(uint32_t uiItemType) const
100{
101    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);
112}
113
114const 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
130bool CMappingContext::iSet(uint32_t uiItemType) const
131{
132    return _pstItemArray[uiItemType].bSet;
133}
134