30 std::string_view
name(
bool qualified =
false) {
31 if (!OidIsValid(
oid)) {
32 throw std::runtime_error(
"invalid type");
34 return (qualified ?
ffi_guard{::format_type_be_qualified}
38 bool operator==(
const type &other)
const {
return oid == other.oid; }
44 bool is(
const type &t) {
return false; }
45 type type_for() =
delete;
50 { type_traits<T>().type_for() } -> std::same_as<type>;
51 { type_traits<T>().is(t) } -> std::same_as<bool>;
54template <
typename T>
requires std::is_reference_v<T>
57 type_traits(
const T &) {}
58 constexpr type type_for() {
return type_traits<std::remove_reference_t<T>>().type_for(); }
78 : value_datum(other.value_datum), ctx(other.ctx) {}
80 : value_datum(std::move(other.value_datum)), ctx(std::move(other.ctx)) {}
82 value_datum = std::move(other.value_datum);
83 ctx = std::move(other.ctx);
87 memory_context &get_memory_context() {
return ctx.get_memory_context(); }
89 datum get_datum()
const {
return value_datum; }
94 void *ptr(
bool tracked =
true)
const {
95 if (tracked && ctx.resets() > 0) {
98 return reinterpret_cast<void *
>(value_datum.operator const ::Datum &());
102static_assert(std::copy_constructible<non_by_value_type>);
105 using non_by_value_type::non_by_value_type;
107 operator void *() {
return VARDATA_ANY(detoasted_ptr()); }
109 datum get_datum()
const {
return value_datum; }
111 bool is_detoasted()
const {
return detoasted !=
nullptr; }
114 void *detoasted =
nullptr;
115 void *detoasted_ptr() {
116 if (detoasted !=
nullptr) {
119 detoasted =
ffi_guard{::pg_detoast_datum}(
reinterpret_cast<::
varlena *
>(ptr()));
125 using varlena::varlena;
127 operator std::string_view() {
128 return {
static_cast<char *
>(this->
operator void *()), VARSIZE_ANY_EXHDR(this->detoasted_ptr())};
132using byte_array = std::span<const std::byte>;
135 using varlena::varlena;
139 auto alloc = ctx.alloc<std::byte>(VARHDRSZ + ba.size_bytes());
140 SET_VARSIZE(alloc, VARHDRSZ + ba.size_bytes());
141 auto ptr = VARDATA_ANY(alloc);
142 std::copy(ba.begin(), ba.end(),
reinterpret_cast<std::byte *
>(ptr));
143 return datum(PointerGetDatum(alloc));
147 operator byte_array() {
148 return {
reinterpret_cast<std::byte *
>(this->
operator void *()),
149 VARSIZE_ANY_EXHDR(this->detoasted_ptr())};
155 { T::type() } -> std::same_as<type>;
156 { t.flat_size() } -> std::same_as<std::size_t>;
157 { t.flatten_into(span) };
158 { T::restore_from(span) } -> std::same_as<T>;
162 using flattenable_type = T;
163 using varlena::varlena;
166 :
varlena(allocate_expanded()),
167 detoasted_value(
reinterpret_cast<expanded *
>(DatumGetPointer(value_datum))) {}
169 template <
typename... Args>
172 requires(
sizeof...(Args) > 0 &&
173 !(
sizeof...(Args) == 1 &&
174 std::is_same_v<std::decay_t<std::tuple_element_t<0, std::tuple<Args...>>>,
176 :
varlena(allocate_expanded(args...)),
177 detoasted_value(
reinterpret_cast<expanded *
>(DatumGetPointer(value_datum))) {}
180 if (detoasted_value.has_value()) {
181 return detoasted_value.value()->inner;
183 if (VARATT_IS_EXTERNAL_EXPANDED(ptr())) {
184 detoasted_value =
reinterpret_cast<expanded *
>(DatumGetEOHP(value_datum));
185 return detoasted_value.value()->inner;
187 auto *ptr1 =
reinterpret_cast<std::byte *
>(varlena::operator
void *());
189 auto *
value =
new (ctx.alloc<expanded>())
190 expanded(T::restore_from(std::span(ptr1, VARSIZE_ANY_EXHDR(detoasted_ptr()))));
191 ctx.register_reset_callback(
193 auto v =
reinterpret_cast<expanded *
>(arg);
197 init(&
value->hdr, ctx);
198 detoasted_value =
value;
203 datum get_expanded_datum()
const {
204 if (!detoasted_value.has_value()) {
205 throw std::runtime_error(
"hasn't been expanded yet");
207 return datum(EOHPGetRWDatum(&detoasted_value.value()->hdr));
212 expanded(
auto &&...args) : inner(std::forward<
decltype(args)...>(args)...) {}
213 ::ExpandedObjectHeader hdr;
217 template <
typename... Args>
static auto allocate_expanded(Args &&...args) {
220 auto *e = ctx.alloc<expanded>();
221 std::construct_at(e, args...);
223 ctx.register_reset_callback(
225 auto v =
reinterpret_cast<expanded *
>(arg);
229 return std::make_pair(
datum(PointerGetDatum(e)), ctx);
233 std::optional<expanded *> detoasted_value = std::nullopt;
235 static void init(ExpandedObjectHeader *hdr,
memory_context &ctx) {
236 using header = int32_t;
238 static const ::ExpandedObjectMethods eom = {
240 [](ExpandedObjectHeader *eohptr) {
241 auto *e =
reinterpret_cast<expanded *
>(eohptr);
242 T *inner = &e->inner;
243 return inner->flat_size() +
sizeof(header);
246 [](ExpandedObjectHeader *eohptr,
void *result,
size_t allocated_size) {
247 auto *e =
reinterpret_cast<expanded *
>(eohptr);
248 T *inner = &e->inner;
249 SET_VARSIZE(
reinterpret_cast<header *
>(result), allocated_size);
250 auto bytes =
reinterpret_cast<std::byte *
>(result) +
sizeof(header);
251 std::span buffer(bytes, allocated_size -
sizeof(header));
252 inner->flatten_into(buffer);
255 ffi_guard{::EOH_init_header}(hdr, &eom, ctx);
264 { t.type_for() } -> std::same_as<type>;
Definition: memory.hpp:118
Definition: memory.hpp:85
Definition: memory.hpp:267
Definition: memory.hpp:135
Postgres type.
Definition: type.hpp:20
std::string_view name(bool qualified=false)
Type name as defined in Postgres.
Definition: type.hpp:30