OZO 「お象」
Boost.Asio and libpq based asynchronous PostgreSQL unofficial header-only C++17 client library.
send.h
1 #pragma once
2 
3 #include <ozo/core/concept.h>
4 #include <ozo/type_traits.h>
5 #include <ozo/io/size_of.h>
6 #include <ozo/io/ostream.h>
7 #include <ozo/io/type_traits.h>
8 #include <libpq-fe.h>
9 #include <type_traits>
10 
11 namespace ozo {
12 
36 template <typename In, typename = std::void_t<>>
37 struct send_impl{
38  static_assert(HasDefinition<In>, "type In must be defined as PostgreSQL type");
39 
40  static_assert(
41  Readable<In&>,
42  "In object type can't be sent. Probably it is not an arithmetic or doens't have data and size methods"
43  " or it is a struct with field which can't be sent."
44  );
45 
54  template <typename OidMap>
55  static ostream& apply(ostream& out, const OidMap&, const In& in) {
56  return write(out, in);
57  }
58 };
59 
60 namespace detail {
61 
62 template <typename T, typename = std::void_t<>>
63 struct send_impl_dispatcher { using type = send_impl<std::decay_t<T>>; };
64 
65 template <typename T, typename Tag>
66 struct send_impl_dispatcher<strong_typedef_wrapper<T, Tag>> { using type = send_impl<std::decay_t<T>>; };
67 
68 template <typename T>
69 using get_send_impl = typename send_impl_dispatcher<unwrap_type<T>>::type;
70 
71 } // namespace detail
72 
92 template <class OidMap, class In>
93 inline ostream& send(ostream& out, const OidMap& oid_map, const In& in) {
94  return ozo::is_null(in) ? out : detail::get_send_impl<In>::apply(out, oid_map, ozo::unwrap(in));
95 }
96 
110 template <class OidMap, class In>
111 inline ostream& send_data_frame(ostream& out, const OidMap& oid_map, const In& in) {
112  write(out, size_of(in));
113  return send(out, oid_map, in);
114 }
115 
130 template <class OidMap, class In>
131 inline ostream& send_frame(ostream& out, const OidMap& oid_map, const In& in) {
132  write(out, type_oid(oid_map, in));
133  return send_data_frame(out, oid_map, in);
134 }
135 
136 } // namespace ozo
ozo::unwrap
constexpr decltype(auto) unwrap(T &&v) noexcept(noexcept(unwrap_impl< std::decay_t< T >>::apply(std::forward< T >(v))))
Unwraps argument underlying value or forwards the argument.
Definition: unwrap.h:57
HasDefinition
Condition indicates if type has corresponding type traits for PostgreSQL.
ozo::size_of
constexpr size_type size_of(const T &v)
Returns size of object binary representation in bytes.
Definition: size_of.h:105
ozo::send
ostream & send(ostream &out, const OidMap &oid_map, const In &in)
Send object to an output stream.
Definition: send.h:93
ozo::send_impl::apply
static ostream & apply(ostream &out, const OidMap &, const In &in)
Implementation of serialization object into stream.
Definition: send.h:55
ozo::send_data_frame
ostream & send_data_frame(ostream &out, const OidMap &oid_map, const In &in)
Send data frame of an object to an output stream.
Definition: send.h:111
ozo::OidMap
constexpr auto OidMap
Map of C++ types to corresponding PostgreSQL types OIDs.
Definition: type_traits.h:534
ozo::type_oid
oid_t type_oid(const OidMap &map) noexcept
Function returns oid for a type from #OidMap.
ozo::send_impl
Defines how to send an object to an output stream.
Definition: send.h:37
ozo::is_null
constexpr decltype(auto) is_null(const T &v) noexcept(noexcept(is_null_impl< T >::apply(v)))
Indicates if value is in null state.
Definition: nullable.h:85
ozo::send_frame
ostream & send_frame(ostream &out, const OidMap &oid_map, const In &in)
Send full frame of an object to an output stream.
Definition: send.h:131