1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 or later of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
#ifndef CC_SERIALIZABLE_OBJECT_HEADER
#define CC_SERIALIZABLE_OBJECT_HEADER
//Local
#include "ccLog.h"
//CCLib
#include <CCPlatform.h>
#include <CCTypes.h>
//System
#include <cassert>
#include <cstdint>
//Qt
#include <QDataStream>
#include <QFile>
//! Serializable object interface
class ccSerializableObject
{
public:
//! Desctructor
virtual ~ccSerializableObject() = default;
//! Returns whether object is serializable of not
virtual bool isSerializable() const { return false; }
//! Saves data to binay stream
/** \param out output file (already opened)
\return success
**/
virtual bool toFile(QFile& out) const { return false; }
//! Deserialization flags (bit-field)
enum DeserializationFlags
{
DF_POINT_COORDS_64_BITS = 1, /**< Point coordinates are stored as 64 bits double (otherwise 32 bits floats) **/
//DGM: inversion is 'historical' ;)
DF_SCALAR_VAL_32_BITS = 2, /**< Scalar values are stored as 32 bits floats (otherwise 64 bits double) **/
};
//! Loads data from binay stream
/** \param in input file (already opened)
\param dataVersion file version
\param flags deserialization flags (see ccSerializableObject::DeserializationFlags)
\return success
**/
virtual bool fromFile(QFile& in, short dataVersion, int flags) { return false; }
//! Sends a custom error message (write error) and returns 'false'
/** Shortcut for returning a standardized error message in the toFile method.
\return always false
**/
static bool WriteError() { ccLog::Error("Write error (disk full or no access right?)"); return false; }
//! Sends a custom error message (read error) and returns 'false'
/** Shortcut for returning a standardized error message in the fromFile method.
\return always false
**/
static bool ReadError() { ccLog::Error("Read error (corrupted file or no access right?)"); return false; }
//! Sends a custom error message (not enough memory) and returns 'false'
/** Shortcut for returning a standardized error message in the fromFile method.
\return always false
**/
static bool MemoryError() { ccLog::Error("Not enough memory"); return false; }
//! Sends a custom error message (corrupted file) and returns 'false'
/** Shortcut for returning a standardized error message in the fromFile method.
\return always false
**/
static bool CorruptError() { ccLog::Error("File seems to be corrupted"); return false; }
};
//! Serialization helpers
class ccSerializationHelper
{
public:
//! Reads one or several 'PointCoordinateType' values from a QDataStream either in float or double format depending on the 'flag' value
static void CoordsFromDataStream(QDataStream& stream, int flags, PointCoordinateType* out, unsigned count = 1)
{
if (flags & ccSerializableObject::DF_POINT_COORDS_64_BITS)
{
for (unsigned i = 0; i < count; ++i, ++out)
{
double val;
stream >> val;
*out = static_cast<PointCoordinateType>(val);
}
}
else
{
for (unsigned i = 0; i < count; ++i, ++out)
{
float val;
stream >> val;
*out = static_cast<PointCoordinateType>(val);
}
}
}
//! Reads one or several 'ScalarType' values from a QDataStream either in float or double format depending on the 'flag' value
static void ScalarsFromDataStream(QDataStream& stream, int flags, ScalarType* out, unsigned count = 1)
{
if (flags & ccSerializableObject::DF_SCALAR_VAL_32_BITS)
{
for (unsigned i = 0; i < count; ++i, ++out)
{
float val;
stream >> val;
*out = static_cast<PointCoordinateType>(val);
}
}
else
{
for (unsigned i = 0; i < count; ++i, ++out)
{
double val;
stream >> val;
*out = static_cast<PointCoordinateType>(val);
}
}
}
//! Helper: saves a vector to file
/** \param data vector to save (must be allocated)
\param out output file (must be already opened)
\return success
**/
template <class Type, int N, class ComponentType> static bool GenericArrayToFile(const std::vector<Type>& data, QFile& out)
{
assert(out.isOpen() && (out.openMode() & QIODevice::WriteOnly));
if (data.empty())
{
return ccSerializableObject::MemoryError();
}
//component count (dataVersion>=20)
::uint8_t componentCount = static_cast<::uint8_t>(N);
if (out.write((const char*)&componentCount, 1) < 0)
return ccSerializableObject::WriteError();
//element count = array size (dataVersion>=20)
::uint32_t elementCount = static_cast<::uint32_t>(data.size());
if (out.write((const char*)&elementCount, 4) < 0)
return ccSerializableObject::WriteError();
//array data (dataVersion>=20)
{
//DGM: do it by chunks, in case it's too big to be processed by the system
const char* _data = (const char*)data.data();
qint64 byteCount = static_cast<qint64>(elementCount);
byteCount *= sizeof(Type);
while (byteCount != 0)
{
static const qint64 s_maxByteSaveCount = (1 << 26); //64 Mb each time
qint64 saveCount = std::min(byteCount, s_maxByteSaveCount);
if (out.write(_data, saveCount) < 0)
return ccSerializableObject::WriteError();
_data += saveCount;
byteCount -= saveCount;
}
}
return true;
}
//! Helper: loads a vector structure from file
/** \param data vector to load
\param in input file (must be already opened)
\param dataVersion version current data version
\return success
**/
template <class Type, int N, class ComponentType> static bool GenericArrayFromFile(std::vector<Type>& data, QFile& in, short dataVersion)
{
::uint8_t componentCount = 0;
::uint32_t elementCount = 0;
if (!ReadArrayHeader(in, dataVersion, componentCount, elementCount))
{
return false;
}
if (componentCount != N)
{
return ccSerializableObject::CorruptError();
}
if (elementCount)
{
//try to allocate memory
try
{
data.resize(elementCount);
}
catch (const std::bad_alloc&)
{
return ccSerializableObject::MemoryError();
}
//array data (dataVersion>=20)
{
//Apparently Qt and/or Windows don't like to read too many bytes in a row...
static const qint64 MaxElementPerChunk = (static_cast<qint64>(1) << 24);
assert(sizeof(ComponentType) * N == sizeof(Type));
qint64 byteCount = static_cast<qint64>(data.size()) * (sizeof(ComponentType) * N);
char* dest = (char*)data.data();
while (byteCount > 0)
{
qint64 chunkSize = std::min(MaxElementPerChunk, byteCount);
if (in.read(dest, chunkSize) < 0)
{
return ccSerializableObject::ReadError();
}
byteCount -= chunkSize;
dest += chunkSize;
}
}
}
return true;
}
//! Helper: loads a vector structure from a file stored with a different type
/** \param data vector to load
\param in input file (must be already opened)
\param dataVersion version current data version
\return success
**/
template <class Type, int N, class ComponentType, class FileComponentType> static bool GenericArrayFromTypedFile(std::vector<Type>& data, QFile& in, short dataVersion)
{
::uint8_t componentCount = 0;
::uint32_t elementCount = 0;
if (!ReadArrayHeader(in, dataVersion, componentCount, elementCount))
{
return false;
}
if (componentCount != N)
{
return ccSerializableObject::CorruptError();
}
if (elementCount)
{
//try to allocate memory
try
{
data.resize(elementCount);
}
catch (const std::bad_alloc&)
{
return ccSerializableObject::MemoryError();
}
//array data (dataVersion>=20)
//--> saldy we can't read it as a block...
//we must convert each element, value by value!
FileComponentType dummyArray[N] = { 0 };
ComponentType* _data = (ComponentType*)data.data();
for (unsigned i = 0; i < elementCount; ++i)
{
if (in.read((char*)dummyArray, sizeof(FileComponentType) * N) >= 0)
{
for (unsigned k = 0; k < N; ++k)
{
*_data++ = static_cast<ComponentType>(dummyArray[k]);
}
}
else
{
return ccSerializableObject::ReadError();
}
}
}
return true;
}
protected:
static bool ReadArrayHeader(QFile& in,
short dataVersion,
::uint8_t &componentCount,
::uint32_t &elementCount)
{
assert(in.isOpen() && (in.openMode() & QIODevice::ReadOnly));
if (dataVersion < 20)
return ccSerializableObject::CorruptError();
//component count (dataVersion>=20)
if (in.read((char*)&componentCount, 1) < 0)
return ccSerializableObject::ReadError();
//element count = array size (dataVersion>=20)
if (in.read((char*)&elementCount, 4) < 0)
return ccSerializableObject::ReadError();
return true;
}
};
#endif //CC_SERIALIZABLE_OBJECT_HEADER
|