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