PoDoFo  1.0.0-dev
basetypes.h
1 
7 #ifndef PODOFO_BASE_TYPES_H
8 #define PODOFO_BASE_TYPES_H
9 #pragma once
10 
11 #include <string>
12 #include <memory>
13 #include "span.h"
14 
15 namespace PoDoFo
16 {
20 
24 
28 
29  // TODO: Optimize, maintaining string compatibility
30  // Use basic_string::resize_and_overwrite in C++23
31  // https://en.cppreference.com/w/cpp/string/basic_string/resize_and_overwrite
36  template <typename = void>
37  class charbuff_t final : public std::string
38  {
39  public:
40  charbuff_t() noexcept { }
41  charbuff_t(const charbuff_t&) = default;
42  charbuff_t(charbuff_t&&) noexcept = default;
43  charbuff_t(size_t size)
44  {
45  std::string::resize(size);
46  }
47  charbuff_t(std::string&& str)
48  : std::string(std::move(str)) { }
49  charbuff_t(const char* str, size_t size)
50  : std::string(str, size) { }
51  explicit charbuff_t(const bufferview& view)
52  : std::string(view.data(), view.size()) { }
53  explicit charbuff_t(const std::string_view& view)
54  : std::string(view) { }
55  explicit charbuff_t(const std::string& str)
56  : std::string(str) { }
57  public:
58  charbuff_t& operator=(const charbuff_t&) = default;
59  charbuff_t& operator=(charbuff_t&&) noexcept = default;
60  charbuff_t& operator=(const std::string_view & view)
61  {
62  std::string::assign(view.data(), view.size());
63  return *this;
64  }
65  charbuff_t& operator=(const std::string& str)
66  {
67  std::string::assign(str.data(), str.size());
68  return *this;
69  }
70  charbuff_t& operator=(const bufferview& view)
71  {
72  std::string::assign(view.data(), view.size());
73  return *this;
74  }
75  charbuff_t& operator=(std::string&& str) noexcept
76  {
77  std::string::operator=(std::move(str));
78  return *this;
79  }
80  bool operator==(const charbuff_t& rhs) const
81  {
82  return static_cast<const std::string&>(*this) == static_cast<const std::string&>(rhs);
83  }
84  private:
85  size_t length() const = delete;
86  };
87 
88  template <typename = void>
89  bool operator==(const charbuff_t<>& lhs, const char* rhs)
90  {
91  return static_cast<const std::string&>(lhs) == rhs;
92  }
93 
94  template <typename = void>
95  bool operator==(const char* lhs, const charbuff_t<>& rhs)
96  {
97  return lhs == static_cast<const std::string&>(rhs);
98  }
99 
100  template <typename = void>
101  bool operator==(const charbuff_t<>& lhs, const bufferview& rhs)
102  {
103  return static_cast<const std::string&>(lhs) == std::string_view(rhs.data(), rhs.size());
104  }
105 
106  template <typename = void>
107  bool operator==(const bufferview& lhs, const charbuff_t<>& rhs)
108  {
109  return std::string_view(lhs.data(), lhs.size()) == static_cast<const std::string&>(rhs);
110  }
111 
112  template <typename = void>
113  bool operator==(const charbuff_t<>& lhs, const std::string_view& rhs) noexcept
114  {
115  return std::string_view(lhs) == rhs;
116  }
117 
118  template <typename = void>
119  bool operator==(const std::string_view& lhs, const charbuff_t<>& rhs) noexcept
120  {
121  return lhs == std::string_view(rhs);
122  }
123 
124  template <typename = void>
125  bool operator==(const charbuff_t<>& lhs, const std::string& rhs) noexcept
126  {
127  return static_cast<const std::string&>(lhs) == rhs;
128  }
129 
130  template <typename = void>
131  bool operator==(const std::string& lhs, const charbuff_t<>& rhs) noexcept
132  {
133  return lhs == static_cast<const std::string&>(rhs);
134  }
135 
136  using charbuff = charbuff_t<>;
137 
142  template <typename = void>
143  class datahandle_t final
144  {
145  public:
146  datahandle_t() { }
147  datahandle_t(const bufferview& view)
148  : m_view(view) { }
149  datahandle_t(charbuff_t<>&& buff)
150  : m_buff(new charbuff_t<>(std::move(buff))), m_view(*m_buff) { }
151  datahandle_t(std::unique_ptr<const charbuff_t<>>&& buff)
152  : m_buff(std::move(buff)), m_view(*m_buff) { }
153  datahandle_t(std::unique_ptr<charbuff_t<>>&& buff)
154  : m_buff(std::move(buff)), m_view(*m_buff) { }
155  datahandle_t(const std::shared_ptr<const charbuff_t<>>& buff)
156  : m_buff(buff), m_view(*m_buff) { }
157  datahandle_t(const std::shared_ptr<charbuff_t<>>& buff)
158  : m_buff(buff), m_view(*m_buff) { }
159  public:
160  const bufferview& view() const { return m_view; }
161  private:
162  std::shared_ptr<const charbuff_t<>> m_buff;
163  bufferview m_view;
164  };
165 
166  using datahandle = datahandle_t<>;
167 
168  // https://artificial-mind.net/blog/2020/10/03/always-false
169  template <class... T>
170  constexpr bool always_false = false;
171 }
172 
173 #endif // PODOFO_BASE_TYPES_H
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition: basetypes.h:38
A const data provider that can hold a view to a static segments or a shared buffer.
Definition: basetypes.h:144
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16
cspan< char32_t > unicodeview
Unicode code point view.
Definition: basetypes.h:27
tcb::span< const T, Extent > cspan
Constant span.
Definition: span.h:13
mspan< char > bufferspan
Convenient writable char buffer span.
Definition: basetypes.h:23
tcb::span< T, Extent > mspan
Mutable span.
Definition: span.h:18
cspan< char > bufferview
Convenient read-only char buffer span.
Definition: basetypes.h:19