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