serialize_common.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "storage/serialize_common.h"
  8. namespace Serialize {
  9. ByteArrayWriter::ByteArrayWriter(int expectedSize)
  10. : _stream(&_result, QIODevice::WriteOnly) {
  11. if (expectedSize) {
  12. _result.reserve(expectedSize);
  13. }
  14. _stream.setVersion(QDataStream::Qt_5_1);
  15. }
  16. QByteArray ByteArrayWriter::result() && {
  17. _stream.device()->close();
  18. return std::move(_result);
  19. }
  20. ByteArrayReader::ByteArrayReader(QByteArray data)
  21. : _data(std::move(data))
  22. , _stream(&_data, QIODevice::ReadOnly) {
  23. _stream.setVersion(QDataStream::Qt_5_1);
  24. }
  25. void writeColor(QDataStream &stream, const QColor &color) {
  26. stream << (quint32(uchar(color.red()))
  27. | (quint32(uchar(color.green())) << 8)
  28. | (quint32(uchar(color.blue())) << 16)
  29. | (quint32(uchar(color.alpha())) << 24));
  30. }
  31. QColor readColor(QDataStream &stream) {
  32. auto value = quint32();
  33. stream >> value;
  34. return QColor(
  35. int(value & 0xFFU),
  36. int((value >> 8) & 0xFFU),
  37. int((value >> 16) & 0xFFU),
  38. int((value >> 24) & 0xFFU));
  39. }
  40. } // namespace Serialize