userver: userver/storages/postgres/io/macaddr.hpp Source File
Loading...
Searching...
No Matches
macaddr.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/io/macaddr.hpp
4/// @brief utils::Macaddr and utils::Macaddr8 I/O support
5/// @ingroup userver_postgres_parse_and_format
6
7#include <userver/storages/postgres/exceptions.hpp>
8#include <userver/storages/postgres/io/buffer_io.hpp>
9#include <userver/storages/postgres/io/buffer_io_base.hpp>
10#include <userver/utils/macaddr.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace storages::postgres {
15
16using Macaddr = USERVER_NAMESPACE::utils::Macaddr;
17using Macaddr8 = USERVER_NAMESPACE::utils::Macaddr8;
18
19namespace io {
20namespace detail {
21
22template <typename T>
23struct MacaddrFormatterBase : BufferFormatterBase<T> {
24 using BaseType = BufferFormatterBase<T>;
25
26 using BaseType::BaseType;
27
28 template <typename Buffer>
29 void operator()(const UserTypes& types, Buffer& buffer) {
30 for (const auto val : this->value.GetOctets()) {
31 io::WriteBuffer(types, buffer, static_cast<char>(val));
32 }
33 }
34};
35
36template <typename T>
37struct MacaddrBufferParser : BufferParserBase<T> {
38 using BaseType = BufferParserBase<T>;
39 using BaseType::BaseType;
40 void operator()(FieldBuffer buffer) {
41 typename T::OctetsType octets;
42 // The buffer is server-supplied; reject a short one before reading the
43 // fixed number of octets straight from it.
44 if (buffer.length < octets.size()) {
45 throw storages::postgres::InvalidInputBufferSize(
46 fmt::format("Buffer size {} is too small for a MAC address of {} bytes", buffer.length, octets.size())
47 );
48 }
49 const uint8_t* byte_cptr = buffer.buffer;
50 for (auto& val : octets) {
51 val = *byte_cptr;
52 ++byte_cptr;
53 }
54 this->value = T(octets);
55 }
56};
57
58} // namespace detail
59
60///@brief Binary formatter for utils::macaddr:Macaddr
61template <>
62struct BufferFormatter<Macaddr> : detail::MacaddrFormatterBase<Macaddr> {
63 using BaseType = detail::MacaddrFormatterBase<Macaddr>;
64
65 using BaseType::BaseType;
66};
67
68///@brief Binary formatter for utils::macaddr:Macaddr8
69template <>
70struct BufferFormatter<Macaddr8> : detail::MacaddrFormatterBase<Macaddr8> {
71 using BaseType = detail::MacaddrFormatterBase<Macaddr8>;
72
73 using BaseType::BaseType;
74};
75
76/// @brief Binary parser for utils::macaddr::Macaddr
77template <>
78struct BufferParser<Macaddr> : detail::MacaddrBufferParser<Macaddr> {
79 using BaseType = detail::MacaddrBufferParser<Macaddr>;
80
81 using BaseType::BaseType;
82};
83
84/// @brief Binary parser for utils::macaddr::Macaddr8
85template <>
86struct BufferParser<Macaddr8> : detail::MacaddrBufferParser<Macaddr8> {
87 using BaseType = detail::MacaddrBufferParser<Macaddr8>;
88
89 using BaseType::BaseType;
90};
91
92template <>
93struct CppToSystemPg<Macaddr> : PredefinedOid<PredefinedOids::kMacaddr> {};
94
95template <>
96struct CppToSystemPg<Macaddr8> : PredefinedOid<PredefinedOids::kMacaddr8> {};
97
98} // namespace io
99} // namespace storages::postgres
100
101USERVER_NAMESPACE_END