Cppgres
Build Postgres extensions in C++
Loading...
Searching...
No Matches
value.hpp
1#pragma once
2
3#include "datum.hpp"
4#include "type.hpp"
5
6namespace cppgres {
7
8struct value {
9
10 value(nullable_datum &&datum, type &&type) : datum_(datum), type_(type) {}
11
12 const type &get_type() const { return type_; }
13
14 const nullable_datum &get_nullable_datum() const { return datum_; };
15
16private:
17 nullable_datum datum_;
18 type type_;
19};
20
21template <> struct datum_conversion<value> {
22 static value from_datum(const datum &d, oid oid, std::optional<memory_context>) {
23 return {nullable_datum(d), type{.oid = oid}};
24 }
25
26 static datum into_datum(const value &t) { return t.get_nullable_datum(); }
27};
28
29template <> struct type_traits<value> {
30 type_traits() : value_(std::nullopt) {}
31 type_traits(value &value) : value_(std::optional(std::ref(value))) {}
32 bool is(const type &t) { return !value_.has_value() || (*value_).get().get_type() == t; }
33 constexpr type type_for() {
34 if (value_.has_value()) {
35 return (*value_).get().get_type();
36 }
37 throw std::runtime_error("can't determine type for an uninitialized value");
38 }
39
40private:
41 std::optional<std::reference_wrapper<value>> value_;
42};
43
44} // namespace cppgres
A trait to convert from and into a cppgres::datum.
Definition: datum.hpp:115
static T from_datum(const datum &, const oid, std::optional< memory_context > context=std::nullopt)=delete
Convert from a datum.
static datum into_datum(const T &d)=delete
Convert datum into a type.
Definition: datum.hpp:35
Definition: datum.hpp:56
Definition: datum.hpp:17
Definition: type.hpp:41
Postgres type.
Definition: type.hpp:20
Definition: value.hpp:8