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;
48template <
typename T>
requires std::is_reference_v<T>
72 : value_datum(other.value_datum), ctx(other.ctx) {}
74 : value_datum(std::move(other.value_datum)), ctx(std::move(other.ctx)) {}
76 value_datum = std::move(other.value_datum);
77 ctx = std::move(other.ctx);
81 memory_context &get_memory_context() {
return ctx.get_memory_context(); }
83 datum get_datum()
const {
return value_datum; }
88 void *ptr(
bool tracked =
true)
const {
89 if (tracked && ctx.resets() > 0) {
92 return reinterpret_cast<void *
>(value_datum.operator const ::Datum &());
96static_assert(std::copy_constructible<non_by_value_type>);
99 using non_by_value_type::non_by_value_type;
101 operator void *() {
return VARDATA_ANY(detoasted_ptr()); }
103 datum get_datum()
const {
return value_datum; }
105 bool is_detoasted()
const {
return detoasted !=
nullptr; }
108 void *detoasted =
nullptr;
109 void *detoasted_ptr() {
110 if (detoasted !=
nullptr) {
113 detoasted =
ffi_guard{::pg_detoast_datum}(
reinterpret_cast<::
varlena *
>(ptr()));
119 using varlena::varlena;
121 operator std::string_view() {
122 return {
static_cast<char *
>(this->
operator void *()), VARSIZE_ANY_EXHDR(this->detoasted_ptr())};
126using byte_array = std::span<const std::byte>;
129 using varlena::varlena;
133 auto alloc = ctx.alloc<std::byte>(VARHDRSZ + ba.size_bytes());
134 SET_VARSIZE(alloc, VARHDRSZ + ba.size_bytes());
135 auto ptr = VARDATA_ANY(alloc);
136 std::copy(ba.begin(), ba.end(),
reinterpret_cast<std::byte *
>(ptr));
137 return datum(PointerGetDatum(alloc));
141 operator byte_array() {
142 return {
reinterpret_cast<std::byte *
>(this->
operator void *()),
143 VARSIZE_ANY_EXHDR(this->detoasted_ptr())};
149 { T::type() } -> std::same_as<type>;
150 { t.flat_size() } -> std::same_as<std::size_t>;
151 { t.flatten_into(span) };
152 { T::restore_from(span) } -> std::same_as<T>;
156 using flattenable_type = T;
157 using varlena::varlena;
162 auto *e =
new (ctx.alloc<expanded>()) expanded(T());
163 ctx.register_reset_callback(
165 auto v =
reinterpret_cast<expanded *
>(arg);
170 return std::make_pair(
datum(PointerGetDatum(e)), ctx);
172 detoasted_value(
reinterpret_cast<expanded *
>(DatumGetPointer(value_datum))) {}
175 if (detoasted_value.has_value()) {
176 return detoasted_value.value()->inner;
178 auto *ptr1 =
reinterpret_cast<std::byte *
>(varlena::operator
void *());
180 auto *
value =
new (ctx.alloc<expanded>())
181 expanded(T::restore_from(std::span(ptr1, VARSIZE_ANY_EXHDR(detoasted_ptr()))));
182 ctx.register_reset_callback(
184 auto v =
reinterpret_cast<expanded *
>(arg);
188 init(&
value->hdr, ctx);
189 detoasted_value =
value;
194 datum get_expanded_datum()
const {
195 if (!detoasted_value.has_value()) {
196 throw std::runtime_error(
"hasn't been expanded yet");
198 return datum(EOHPGetRWDatum(&detoasted_value.value()->hdr));
203 expanded(T &&t) : inner(std::move(t)) {}
204 ::ExpandedObjectHeader hdr;
207 std::optional<expanded *> detoasted_value = std::nullopt;
209 static void init(ExpandedObjectHeader *hdr,
memory_context &ctx) {
210 using header = int32_t;
212 static const ::ExpandedObjectMethods eom = {
214 [](ExpandedObjectHeader *eohptr) {
215 auto *e =
reinterpret_cast<expanded *
>(eohptr);
216 T *inner = &e->inner;
217 return inner->flat_size() +
sizeof(header);
220 [](ExpandedObjectHeader *eohptr,
void *result,
size_t allocated_size) {
221 auto *e =
reinterpret_cast<expanded *
>(eohptr);
222 T *inner = &e->inner;
223 SET_VARSIZE(
reinterpret_cast<header *
>(result), allocated_size);
224 auto bytes =
reinterpret_cast<std::byte *
>(result) +
sizeof(header);
225 std::span buffer(bytes, allocated_size -
sizeof(header));
226 inner->flatten_into(buffer);
229 ffi_guard{::EOH_init_header}(hdr, &eom, ctx);
238 { t.type_for() } -> std::same_as<type>;
Definition: memory.hpp:94
Definition: memory.hpp:61
Definition: memory.hpp:243
Definition: memory.hpp:111
Postgres type.
Definition: type.hpp:20
std::string_view name(bool qualified=false)
Type name as defined in Postgres.
Definition: type.hpp:30