2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace td {
|
2019-01-19 23:59:37 +01:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SliceStorer final : public Storer {
|
2018-12-31 20:04:05 +01:00
|
|
|
Slice slice;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SliceStorer(Slice slice) : slice(slice) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t size() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return slice.size();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t store(uint8 *ptr) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::memcpy(ptr, slice.ubegin(), slice.size());
|
|
|
|
return slice.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline SliceStorer create_storer(Slice slice) {
|
|
|
|
return SliceStorer(slice);
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ConcatStorer final : public Storer {
|
2018-12-31 20:04:05 +01:00
|
|
|
const Storer &a_;
|
|
|
|
const Storer &b_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ConcatStorer(const Storer &a, const Storer &b) : a_(a), b_(b) {
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t size() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return a_.size() + b_.size();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t store(uint8 *ptr) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
uint8 *ptr_save = ptr;
|
|
|
|
ptr += a_.store(ptr);
|
|
|
|
ptr += b_.store(ptr);
|
|
|
|
return ptr - ptr_save;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline ConcatStorer create_storer(const Storer &a, const Storer &b) {
|
|
|
|
return ConcatStorer(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2021-07-04 04:58:54 +02:00
|
|
|
class DefaultStorer final : public Storer {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit DefaultStorer(const T &object) : object_(object) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t size() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (size_ == std::numeric_limits<size_t>::max()) {
|
|
|
|
size_ = tl_calc_length(object_);
|
|
|
|
}
|
|
|
|
return size_;
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t store(uint8 *ptr) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return tl_store_unsafe(object_, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable size_t size_ = std::numeric_limits<size_t>::max();
|
|
|
|
const T &object_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
DefaultStorer<T> create_default_storer(const T &from) {
|
|
|
|
return DefaultStorer<T>(from);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|