Cppgres
Build Postgres extensions in C++
Loading...
Searching...
No Matches
list.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include "imports.h"
7
8#include <iterator>
9#include <type_traits>
10
11namespace cppgres {
12
25template <typename T = void *> struct list {
26 explicit list(::List *l) : list_(l) {}
27
28 struct iterator {
29 using iterator_category = std::forward_iterator_tag;
30 using value_type = T;
31 using difference_type = std::ptrdiff_t;
32
33 ::ListCell *cell = nullptr;
34
35 T operator*() const {
36 if constexpr (std::is_pointer_v<T>) {
37 return static_cast<T>(lfirst(cell));
38 } else if constexpr (std::is_same_v<T, ::Oid>) {
39 return lfirst_oid(cell);
40 } else {
41 static_assert(std::is_same_v<T, int>, "unsupported list element type");
42 return lfirst_int(cell);
43 }
44 }
45
46 iterator &operator++() {
47 ++cell;
48 return *this;
49 }
50 iterator operator++(int) {
51 auto ret = *this;
52 ++cell;
53 return ret;
54 }
55 bool operator==(const iterator &) const = default;
56 };
57
58 iterator begin() const { return {list_ == NIL ? nullptr : &list_->elements[0]}; }
59 iterator end() const { return {list_ == NIL ? nullptr : &list_->elements[list_->length]}; }
60
61 size_t size() const { return list_ == NIL ? 0 : static_cast<size_t>(list_->length); }
62 bool empty() const { return list_ == NIL; }
63
64 operator ::List *() const { return list_; }
65
66private:
67 ::List *list_;
68};
69
70} // namespace cppgres
Definition: list.hpp:28
Typed, range-for-iterable view over a PostgreSQL ::List.
Definition: list.hpp:25