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