tl_boxed.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/basic_types.h"
  9. #include <QtCore/QVector>
  10. namespace tl {
  11. template <typename Accumulator>
  12. struct Writer;
  13. template <typename bare>
  14. class boxed : public bare {
  15. public:
  16. using bare::bare;
  17. boxed() = default;
  18. boxed(const boxed<bare> &v) = default;
  19. boxed<bare> &operator=(const boxed<bare> &v) = default;
  20. boxed(const bare &v) : bare(v) {
  21. }
  22. boxed<bare> &operator=(const bare &v) {
  23. *((bare*)this) = v;
  24. return *this;
  25. }
  26. template <typename Prime>
  27. [[nodiscard]] bool read(const Prime *&from, const Prime *end, uint32 cons = 0) {
  28. if (!Reader<Prime>::Has(1, from, end)) {
  29. return false;
  30. }
  31. cons = Reader<Prime>::Get(from, end);
  32. return bare::read(from, end, cons);
  33. }
  34. template <typename Accumulator>
  35. void write(Accumulator &to) const {
  36. Writer<Accumulator>::Put(to, bare::type());
  37. bare::write(to);
  38. }
  39. using Unboxed = bare;
  40. };
  41. template <typename T>
  42. class boxed<boxed<T> > {
  43. using Unboxed = typename T::CantMakeBoxedBoxedType;
  44. };
  45. template <typename T>
  46. struct is_boxed : std::false_type {
  47. };
  48. template <typename T>
  49. struct is_boxed<boxed<T>> : std::true_type {
  50. };
  51. template <typename T>
  52. inline constexpr bool is_boxed_v = is_boxed<T>::value;
  53. } // namespace tl