Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Name: gdalmultidim_misc.cpp 4 : * Project: GDAL Core 5 : * Purpose: Implementation of a few utility classes related to multidim 6 : * Author: Even Rouault <even.rouault at spatialys.com> 7 : * 8 : ****************************************************************************** 9 : * Copyright (c) 2019, Even Rouault <even.rouault at spatialys.com> 10 : * 11 : * SPDX-License-Identifier: MIT 12 : ****************************************************************************/ 13 : 14 : #include "gdal_multidim.h" 15 : #include "gdalmultidim_priv.h" 16 : 17 : /************************************************************************/ 18 : /* GDALRawResult() */ 19 : /************************************************************************/ 20 : 21 : //! @cond Doxygen_Suppress 22 258 : GDALRawResult::GDALRawResult(GByte *raw, const GDALExtendedDataType &dt, 23 258 : size_t nEltCount) 24 516 : : m_dt(dt), m_nEltCount(nEltCount), m_nSize(nEltCount * dt.GetSize()), 25 258 : m_raw(raw) 26 : { 27 258 : } 28 : 29 : //! @endcond 30 : 31 : /************************************************************************/ 32 : /* GDALRawResult() */ 33 : /************************************************************************/ 34 : 35 : /** Move constructor. */ 36 0 : GDALRawResult::GDALRawResult(GDALRawResult &&other) 37 0 : : m_dt(std::move(other.m_dt)), m_nEltCount(other.m_nEltCount), 38 0 : m_nSize(other.m_nSize), m_raw(other.m_raw) 39 : { 40 0 : other.m_nEltCount = 0; 41 0 : other.m_nSize = 0; 42 0 : other.m_raw = nullptr; 43 0 : } 44 : 45 : /************************************************************************/ 46 : /* FreeMe() */ 47 : /************************************************************************/ 48 : 49 258 : void GDALRawResult::FreeMe() 50 : { 51 258 : if (m_raw && m_dt.NeedsFreeDynamicMemory()) 52 : { 53 122 : GByte *pabyPtr = m_raw; 54 122 : const auto nDTSize(m_dt.GetSize()); 55 244 : for (size_t i = 0; i < m_nEltCount; ++i) 56 : { 57 122 : m_dt.FreeDynamicMemory(pabyPtr); 58 122 : pabyPtr += nDTSize; 59 : } 60 : } 61 258 : VSIFree(m_raw); 62 258 : } 63 : 64 : /************************************************************************/ 65 : /* operator=() */ 66 : /************************************************************************/ 67 : 68 : /** Move assignment. */ 69 0 : GDALRawResult &GDALRawResult::operator=(GDALRawResult &&other) 70 : { 71 0 : FreeMe(); 72 0 : m_dt = std::move(other.m_dt); 73 0 : m_nEltCount = other.m_nEltCount; 74 0 : m_nSize = other.m_nSize; 75 0 : m_raw = other.m_raw; 76 0 : other.m_nEltCount = 0; 77 0 : other.m_nSize = 0; 78 0 : other.m_raw = nullptr; 79 0 : return *this; 80 : } 81 : 82 : /************************************************************************/ 83 : /* ~GDALRawResult() */ 84 : /************************************************************************/ 85 : 86 : /** Destructor. */ 87 258 : GDALRawResult::~GDALRawResult() 88 : { 89 258 : FreeMe(); 90 258 : } 91 : 92 : /************************************************************************/ 93 : /* StealData() */ 94 : /************************************************************************/ 95 : 96 : //! @cond Doxygen_Suppress 97 : /** Return buffer to caller which becomes owner of it. 98 : * Only to be used by GDALAttributeReadAsRaw(). 99 : */ 100 6 : GByte *GDALRawResult::StealData() 101 : { 102 6 : GByte *ret = m_raw; 103 6 : m_raw = nullptr; 104 6 : m_nEltCount = 0; 105 6 : m_nSize = 0; 106 6 : return ret; 107 : } 108 : 109 : //! @endcond 110 : 111 : //! @cond Doxygen_Suppress 112 : 113 : /************************************************************************/ 114 : /* GDALMDIAsAttribute::GetDimensions() */ 115 : /************************************************************************/ 116 : 117 : const std::vector<std::shared_ptr<GDALDimension>> & 118 29 : GDALMDIAsAttribute::GetDimensions() const 119 : { 120 29 : return m_dims; 121 : } 122 : 123 : /************************************************************************/ 124 : /* GDALMDArrayRawBlockInfo::~GDALMDArrayRawBlockInfo() */ 125 : /************************************************************************/ 126 : 127 64 : GDALMDArrayRawBlockInfo::~GDALMDArrayRawBlockInfo() 128 : { 129 32 : clear(); 130 32 : } 131 : 132 : /************************************************************************/ 133 : /* GDALMDArrayRawBlockInfo::clear() */ 134 : /************************************************************************/ 135 : 136 53 : void GDALMDArrayRawBlockInfo::clear() 137 : { 138 53 : CPLFree(pszFilename); 139 53 : pszFilename = nullptr; 140 53 : CSLDestroy(papszInfo); 141 53 : papszInfo = nullptr; 142 53 : nOffset = 0; 143 53 : nSize = 0; 144 53 : CPLFree(pabyInlineData); 145 53 : pabyInlineData = nullptr; 146 53 : } 147 : 148 : /************************************************************************/ 149 : /* GDALMDArrayRawBlockInfo::GDALMDArrayRawBlockInfo() */ 150 : /************************************************************************/ 151 : 152 4 : GDALMDArrayRawBlockInfo::GDALMDArrayRawBlockInfo( 153 4 : const GDALMDArrayRawBlockInfo &other) 154 4 : : pszFilename(other.pszFilename ? CPLStrdup(other.pszFilename) : nullptr), 155 4 : nOffset(other.nOffset), nSize(other.nSize), 156 4 : papszInfo(CSLDuplicate(other.papszInfo)), pabyInlineData(nullptr) 157 : { 158 4 : if (other.pabyInlineData) 159 : { 160 3 : pabyInlineData = static_cast<GByte *>( 161 3 : VSI_MALLOC_VERBOSE(static_cast<size_t>(other.nSize))); 162 3 : if (pabyInlineData) 163 3 : memcpy(pabyInlineData, other.pabyInlineData, 164 3 : static_cast<size_t>(other.nSize)); 165 : } 166 4 : } 167 : 168 : /************************************************************************/ 169 : /* GDALMDArrayRawBlockInfo::operator=() */ 170 : /************************************************************************/ 171 : 172 : GDALMDArrayRawBlockInfo & 173 7 : GDALMDArrayRawBlockInfo::operator=(const GDALMDArrayRawBlockInfo &other) 174 : { 175 7 : if (this != &other) 176 : { 177 5 : CPLFree(pszFilename); 178 5 : pszFilename = 179 5 : other.pszFilename ? CPLStrdup(other.pszFilename) : nullptr; 180 5 : nOffset = other.nOffset; 181 5 : nSize = other.nSize; 182 5 : CSLDestroy(papszInfo); 183 5 : papszInfo = CSLDuplicate(other.papszInfo); 184 5 : CPLFree(pabyInlineData); 185 5 : pabyInlineData = nullptr; 186 5 : if (other.pabyInlineData) 187 : { 188 4 : pabyInlineData = static_cast<GByte *>( 189 4 : VSI_MALLOC_VERBOSE(static_cast<size_t>(other.nSize))); 190 4 : if (pabyInlineData) 191 4 : memcpy(pabyInlineData, other.pabyInlineData, 192 4 : static_cast<size_t>(other.nSize)); 193 : } 194 : } 195 7 : return *this; 196 : } 197 : 198 : /************************************************************************/ 199 : /* GDALMDArrayRawBlockInfo::GDALMDArrayRawBlockInfo() */ 200 : /************************************************************************/ 201 : 202 2 : GDALMDArrayRawBlockInfo::GDALMDArrayRawBlockInfo( 203 2 : GDALMDArrayRawBlockInfo &&other) 204 2 : : pszFilename(other.pszFilename), nOffset(other.nOffset), 205 2 : nSize(other.nSize), papszInfo(other.papszInfo), 206 2 : pabyInlineData(other.pabyInlineData) 207 : { 208 2 : other.pszFilename = nullptr; 209 2 : other.papszInfo = nullptr; 210 2 : other.pabyInlineData = nullptr; 211 2 : } 212 : 213 : /************************************************************************/ 214 : /* GDALMDArrayRawBlockInfo::operator=() */ 215 : /************************************************************************/ 216 : 217 : GDALMDArrayRawBlockInfo & 218 2 : GDALMDArrayRawBlockInfo::operator=(GDALMDArrayRawBlockInfo &&other) 219 : { 220 2 : if (this != &other) 221 : { 222 2 : std::swap(pszFilename, other.pszFilename); 223 2 : nOffset = other.nOffset; 224 2 : nSize = other.nSize; 225 2 : std::swap(papszInfo, other.papszInfo); 226 2 : std::swap(pabyInlineData, other.pabyInlineData); 227 : } 228 2 : return *this; 229 : } 230 : 231 : //! @endcond