yaLanTingLibs
struct_pack_doc.hpp
1 /*
2  * Copyright (c) 2023, Alibaba Group Holding Limited;
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #include "ylt/struct_pack/struct_pack_impl.hpp"
18 
19 namespace struct_pack {
20 
107 template <typename T>
108 struct trivial_view {
109  private:
110  const T *ref;
111 
112  public:
118  trivial_view(const T *t);
124  trivial_view(const T &t);
125  trivial_view(const trivial_view &) = default;
130  trivial_view() : ref(nullptr){};
131  trivial_view &operator=(const trivial_view &) = default;
137  using value_type = T;
138 
144  void set(const T &obj);
150  const T &get() const;
156  const T *operator->() const;
157 };
158 
259 template <typename T, uint64_t version>
260 struct compatible : public std::optional<T> {
261  constexpr compatible() = default;
262  constexpr compatible(const compatible &other) = default;
263  constexpr compatible(compatible &&other) = default;
264  constexpr compatible(std::optional<T> &&other)
265  : std::optional<T>(std::move(other)){};
266  constexpr compatible(const std::optional<T> &other)
267  : std::optional<T>(other){};
268  constexpr compatible &operator=(const compatible &other) = default;
269  constexpr compatible &operator=(compatible &&other) = default;
270  using std::optional<T>::optional;
271  friend bool operator==(const compatible<T, version> &self,
272  const compatible<T, version> &other);
273  static constexpr uint64_t version_number = version;
274 };
275 
296 template <typename CharType, std::size_t Size>
298  constexpr string_literal() = default;
303  constexpr string_literal(std::basic_string_view<CharType> str);
308  constexpr string_literal(const CharType (&value)[Size + 1]);
314  constexpr std::size_t size() const;
321  constexpr bool empty() const;
328  constexpr CharType &operator[](std::size_t sz);
335  constexpr const char &operator[](std::size_t sz) const;
336 
342  constexpr const CharType *data() const;
343 
352  template <std::size_t Size2>
353  constexpr bool operator!=(const string_literal<CharType, Size2> &other) const;
354 
363  template <std::size_t Size2>
364  constexpr bool operator==(const string_literal<CharType, Size2> &other) const;
365 
373  template <size_t Size2>
375  string_literal<CharType, Size2> other) const;
376 
377  private:
378  CharType ar[Size + 1];
379 };
380 
388 struct err_code {
389  public:
395  err_code() noexcept;
401  err_code(struct_pack::errc ec) noexcept;
402  err_code &operator=(errc ec);
403  err_code(const err_code &err_code) noexcept;
404  err_code &operator=(const err_code &o) noexcept;
412  bool operator==(const err_code &o) const noexcept { return ec == o.ec; }
420  bool operator!=(const err_code &o);
427  operator bool() const noexcept { return ec != errc::ok; }
433  int val() const noexcept;
439  std::string_view message() const noexcept;
440 };
441 
455 enum class errc {
456  ok = 0,
457  no_buffer_space,
458  invalid_buffer,
459  hash_conflict,
460  invalid_width_of_container_length,
461 };
462 
472 enum sp_config : uint64_t {
473  DEFAULT = 0,
474 
475  DISABLE_TYPE_INFO = 0b1,
476  ENABLE_TYPE_INFO = 0b10,
477  DISABLE_ALL_META_INFO = 0b11,
478 
479  ENCODING_WITH_VARINT = 0b100,
480  USE_FAST_VARINT = 0b1000
481 };
482 
506 template <typename T>
507 constexpr std::size_t members_count;
508 
517 inline std::error_code make_error_code(struct_pack::errc err);
518 
528 inline std::string error_message(struct_pack::errc err);
529 
553 template <typename... Args>
554 constexpr std::uint32_t get_type_code();
555 
571 template <typename... Args>
572 constexpr decltype(auto) get_type_literal();
573 
595 template <typename... Args>
596 [[nodiscard]] constexpr struct_pack::serialize_buffer_size get_needed_size(
597  const Args &...args);
598 
628 template <uint64_t conf = sp_config::DEFAULT, typename Writer, typename... Args>
629 void serialize_to(Writer &writer, const Args &...args);
630 
650 template <uint64_t conf = sp_config::DEFAULT, typename... Args>
651 void serialize_to(char *buffer, serialize_buffer_size len, const Args &...args);
652 
669 template <uint64_t conf = sp_config::DEFAULT, detail::struct_pack_buffer Buffer,
670  typename... Args>
671 void serialize_to_with_offset(Buffer &buffer, std::size_t offset,
672  const Args &...args);
673 
693 template <detail::struct_pack_buffer Buffer = std::vector<char>,
694  typename... Args>
695 [[nodiscard]] Buffer serialize(const Args &...args);
696 
710 template <
711  detail::struct_pack_buffer Buffer = std::vector<char> typename... Args>
712 [[nodiscard]] Buffer serialize_with_offset(std::size_t offset,
713  const Args &...args);
714 
728 template <uint64_t conf, detail::struct_pack_buffer Buffer = std::vector<char>,
729  typename... Args>
730 [[nodiscard]] Buffer serialize(const Args &...args);
731 
747 template <uint64_t conf, detail::struct_pack_buffer Buffer = std::vector<char>,
748  typename... Args>
749 [[nodiscard]] Buffer serialize_with_offset(std::size_t offset,
750  const Args &...args);
751 
779 template <uint64_t conf = sp_config::DEFAULT, typename T, typename... Args,
780  struct_pack::detail::deserialize_view View>
781 [[nodiscard]] struct_pack::err_code deserialize_to(T &t, const View &v,
782  Args &...args);
783 
810 template <uint64_t conf = sp_config::DEFAULT, typename T, typename... Args>
811 [[nodiscard]] struct_pack::err_code deserialize_to(T &t, const char *data,
812  size_t size, Args &...args);
830 template <uint64_t conf = sp_config::DEFAULT, typename T, typename... Args,
831  struct_pack::reader_t Reader>
832 [[nodiscard]] struct_pack::err_code deserialize_to(T &t, Reader &reader,
833  Args &...args);
834 
853 template <uint64_t conf = sp_config::DEFAULT, typename T, typename... Args,
854  struct_pack::detail::deserialize_view View>
855 [[nodiscard]] struct_pack::err_code deserialize_to_with_offset(T &t,
856  const View &v,
857  size_t &offset,
858  Args &...args);
859 
878 template <uint64_t conf = sp_config::DEFAULT, typename T, typename... Args>
879 [[nodiscard]] struct_pack::err_code deserialize_to_with_offset(
880  T &t, const char *data, size_t size, size_t &offset, Args &...args);
881 
903 template <typename... Args, struct_pack::detail::deserialize_view View>
904 [[nodiscard]] auto deserialize(const View &v);
905 
928 template <typename... Args>
929 [[nodiscard]] auto deserialize(const char *data, size_t size);
930 
953 template <typename... Args, struct_pack::reader_t Reader>
954 [[nodiscard]] auto deserialize(Reader &v);
955 
971 template <typename... Args, struct_pack::detail::deserialize_view View>
972 [[nodiscard]] auto deserialize(const View &v, size_t &consume_len);
973 
989 template <typename... Args>
990 [[nodiscard]] auto deserialize(const char *data, size_t size,
991  size_t &consume_len);
992 
1015 template <uint64_t conf, typename... Args,
1016  struct_pack::detail::deserialize_view View>
1017 [[nodiscard]] auto deserialize(const View &v);
1018 
1042 template <uint64_t conf, typename... Args>
1043 [[nodiscard]] auto deserialize(const char *data, size_t size);
1044 
1068 template <uint64_t conf, typename... Args, struct_pack::reader_t Reader>
1069 [[nodiscard]] auto deserialize(Reader &v);
1070 
1088 template <uint64_t conf, typename... Args,
1089  struct_pack::detail::deserialize_view View>
1090 [[nodiscard]] auto deserialize(const View &v, size_t &consume_len);
1091 
1109 template <uint64_t conf, typename... Args>
1110 [[nodiscard]] auto deserialize(const char *data, size_t size,
1111  size_t &consume_len);
1112 
1127 template <typename... Args, struct_pack::detail::deserialize_view View>
1128 [[nodiscard]] auto deserialize_with_offset(const View &v, size_t &offset);
1129 
1160 template <typename... Args>
1161 [[nodiscard]] auto deserialize_with_offset(const char *data, size_t size,
1162  size_t &offset);
1163 
1192 template <typename T, size_t I, uint64_t conf = sp_config::DEFAULT,
1193  typename Field, struct_pack::detail::deserialize_view View>
1194 [[nodiscard]] struct_pack::err_code get_field_to(Field &dst, const View &v);
1221 template <typename T, size_t I, uint64_t conf = sp_config::DEFAULT,
1222  typename Field>
1223 [[nodiscard]] struct_pack::err_code get_field_to(Field &dst, const char *data,
1224  size_t size);
1225 
1242 template <typename T, size_t I, uint64_t conf = sp_config::DEFAULT,
1243  typename Field, struct_pack::reader_t Reader>
1244 [[nodiscard]] struct_pack::err_code get_field_to(Field &dst, Reader &reader);
1245 
1268 template <typename T, size_t I, uint64_t conf = sp_config::DEFAULT,
1269  struct_pack::detail::deserialize_view View>
1270 [[nodiscard]] auto get_field(const View &v);
1271 
1294 template <typename T, size_t I, uint64_t conf = sp_config::DEFAULT>
1295 [[nodiscard]] auto get_field(const char *data, size_t size);
1296 
1309 template <typename T, size_t I, uint64_t conf = sp_config::DEFAULT,
1310  struct_pack::reader_t Reader>
1311 [[nodiscard]] auto get_field(Reader &reader);
1312 
1342 template <typename BaseClass, typename... DerivedClasses,
1343  struct_pack::detail::deserialize_view View>
1344 [[nodiscard]] struct_pack::expected<std::unique_ptr<BaseClass>,
1345  struct_pack::err_code>
1347 
1378 template <typename BaseClass, typename... DerivedClasses>
1379 [[nodiscard]] struct_pack::expected<std::unique_ptr<BaseClass>,
1380  struct_pack::err_code>
1381 deserialize_derived_class(const char *data, size_t size);
1382 
1395 template <typename BaseClass, typename... DerivedClasses,
1396  struct_pack::reader_t Reader>
1397 [[nodiscard]] struct_pack::expected<std::unique_ptr<BaseClass>,
1398  struct_pack::err_code>
1400 
1401 } // namespace struct_pack
struct_pack::err_code deserialize_to(T &t, Reader &reader, Args &...args)
将输入流中的数据反序列化到目的对象
auto deserialize_with_offset(const char *data, size_t size, size_t &offset)
从内存中反序列化目的对象并保存到返回值,反序列化时跳过开头的若干字节
auto deserialize(const char *data, size_t size, size_t &consume_len)
按给定配置反序列化内存中的数据,并按值返回,并在参数中返回消耗的数据长度。
std::error_code make_error_code(struct_pack::errc err)
构造std::error_code
constexpr struct_pack::serialize_buffer_size get_needed_size(const Args &...args)
获取序列化长度
struct_pack::expected< std::unique_ptr< BaseClass >, struct_pack::err_code > deserialize_derived_class(Reader &reader)
从输入流中反序列化派生类到基类的指针
void serialize_to(char *buffer, serialize_buffer_size len, const Args &...args)
将对象序列化到给定的内存地址
constexpr std::uint32_t get_type_code()
获取类型校验码
struct_pack::err_code get_field_to(Field &dst, Reader &reader)
从输入流中反序列化一个字段并保存到目的对象
Buffer serialize_with_offset(std::size_t offset, const Args &...args)
按给定配置将序列化结果保存到容器并返回,同时在序列化结果的头部预留一段字节。
auto get_field(Reader &reader)
从输入流中反序列化一个字段并返回
constexpr decltype(auto) get_type_literal()
struct_pack::err_code deserialize_to_with_offset(T &t, const char *data, size_t size, size_t &offset, Args &...args)
将内存中的数据反序列化到目的对象,反序列化时跳过开头的若干字节
Buffer serialize(const Args &...args)
按给定配置序列化对象并返回结果
void serialize_to_with_offset(Buffer &buffer, std::size_t offset, const Args &...args)
将序列化结果保存到给定缓冲区尾部,并在序列化结果的头部预留一段字节。
std::string error_message(struct_pack::errc err)
获取错误消息
constexpr std::size_t members_count
手动标注结构体成员个数
Definition: struct_pack_doc.hpp:507
errc
struct_pack的错误值枚举
Definition: struct_pack_doc.hpp:455
sp_config
struct_pack的配置
Definition: struct_pack_doc.hpp:472
deserialize函数的返回值类型
兼容字段类型
Definition: struct_pack_doc.hpp:260
struct_pack的错误码,存储了一个枚举值struct_pack::errc,可用于判断序列化是否成功。
Definition: struct_pack_doc.hpp:388
std::string_view message() const noexcept
返回错误码对应的错误消息
int val() const noexcept
将错误码转换为整数
bool operator!=(const err_code &o)
比较错误码是否不同
err_code() noexcept
err_code的默认构造函数,默认情况下无错误
编译期字符串类型
Definition: struct_pack_doc.hpp:297
constexpr CharType & operator[](std::size_t sz)
获取下标对应的字符
constexpr const CharType * data() const
返回一个C-style(以'\0'结尾)的字符串指针
constexpr const char & operator[](std::size_t sz) const
获取下标对应的字符
constexpr std::size_t size() const
返回字符串的长度
constexpr string_literal< CharType, Size+Size2 > operator+(string_literal< CharType, Size2 > other) const
拼接两个字符串
constexpr bool operator==(const string_literal< CharType, Size2 > &other) const
判断两个字符串是否相等
constexpr bool operator!=(const string_literal< CharType, Size2 > &other) const
判断两个字符串是否不相等
constexpr bool empty() const
判断字符串是否为空字符串
constexpr string_literal(std::basic_string_view< CharType > str)
从string_view构造string_literal类型
constexpr string_literal(const CharType(&value)[Size+1])
从数组构造string_literal类型
trivial_view<T> 是一个平凡结构体的视图,在类型系统上等价于T。
Definition: struct_pack_doc.hpp:108
trivial_view(const T &t)
构造一个指向对象t的trivial_view
void set(const T &obj)
设置trivial_view指向的对象
trivial_view(const T *t)
构造一个指向对象t的trivial_view
const T & get() const
返回trivial_view指向的对象T的常引用
const T * operator->() const
允许trivial_view通过运算符->访问指向对象T
trivial_view()
构造一个指向空对象的trivial_view
Definition: struct_pack_doc.hpp:130
T value_type
trivial_view指向的对象的类型,value_type必须是一个平凡的结构体
Definition: struct_pack_doc.hpp:137