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>
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;
168 auto *e =
new (ctx.alloc<expanded>()) expanded(T());
169 ctx.register_reset_callback(
171 auto v =
reinterpret_cast<expanded *
>(arg);
176 return std::make_pair(
datum(PointerGetDatum(e)), ctx);
178 detoasted_value(
reinterpret_cast<expanded *
>(DatumGetPointer(value_datum))) {}
181 if (detoasted_value.has_value()) {
182 return detoasted_value.value()->inner;
184 auto *ptr1 =
reinterpret_cast<std::byte *
>(varlena::operator
void *());
186 auto *
value =
new (ctx.alloc<expanded>())
187 expanded(T::restore_from(std::span(ptr1, VARSIZE_ANY_EXHDR(detoasted_ptr()))));
188 ctx.register_reset_callback(
190 auto v =
reinterpret_cast<expanded *
>(arg);
194 init(&
value->hdr, ctx);
195 detoasted_value =
value;
200 datum get_expanded_datum()
const {
201 if (!detoasted_value.has_value()) {
202 throw std::runtime_error(
"hasn't been expanded yet");
204 return datum(EOHPGetRWDatum(&detoasted_value.value()->hdr));
209 expanded(T &&t) : inner(std::move(t)) {}
210 ::ExpandedObjectHeader hdr;
213 std::optional<expanded *> detoasted_value = std::nullopt;
215 static void init(ExpandedObjectHeader *hdr,
memory_context &ctx) {
216 using header = int32_t;
218 static const ::ExpandedObjectMethods eom = {
220 [](ExpandedObjectHeader *eohptr) {
221 auto *e =
reinterpret_cast<expanded *
>(eohptr);
222 T *inner = &e->inner;
223 return inner->flat_size() +
sizeof(header);
226 [](ExpandedObjectHeader *eohptr,
void *result,
size_t allocated_size) {
227 auto *e =
reinterpret_cast<expanded *
>(eohptr);
228 T *inner = &e->inner;
229 SET_VARSIZE(
reinterpret_cast<header *
>(result), allocated_size);
230 auto bytes =
reinterpret_cast<std::byte *
>(result) +
sizeof(header);
231 std::span buffer(bytes, allocated_size -
sizeof(header));
232 inner->flatten_into(buffer);
235 ffi_guard{::EOH_init_header}(hdr, &eom, ctx);
244 { 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