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
24 std::optional<memory_context>) {
25 return {nullable_datum(d), type{.oid = oid}};
26 }
27
28 static value from_datum(const datum &d, oid oid, std::optional<memory_context>) {
29 return {nullable_datum(d), type{.oid = oid}};
30 }
31
32 static datum into_datum(const value &t) { return t.get_nullable_datum(); }
33
34 static nullable_datum into_nullable_datum(const value &t) { return t.get_nullable_datum(); }
35};
36
37template <> struct type_traits<value> {
38 type_traits() : value_(std::nullopt) {}
39 type_traits(value &value) : value_(std::optional(std::ref(value))) {}
40 bool is(const type &t) { return !value_.has_value() || (*value_).get().get_type() == t; }
41 constexpr type type_for() {
42 if (value_.has_value()) {
43 return (*value_).get().get_type();
44 }
45 throw std::runtime_error("can't determine type for an uninitialized value");
46 }
47
48private:
49 std::optional<std::reference_wrapper<value>> value_;
50};
51
52} // namespace cppgres
A trait to convert from and into a cppgres::datum.
Definition: datum.hpp:110
static T from_datum(const datum &, const oid, std::optional< memory_context > context=std::nullopt)=delete
Convert from a datum.
static T from_nullable_datum(const nullable_datum &d, const oid oid, std::optional< memory_context > context=std::nullopt)=delete
Convert from a nullable datum.
static datum into_datum(const T &d)=delete
Convert datum into a type.
static nullable_datum into_nullable_datum(const T &d)=delete
Convert into a nullable datum.
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