pars 0.2.1
Loading...
Searching...
No Matches
msg_body.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "nngxx/concept.h"
33#include "nngxx/err.h"
34#include "nngxx/msg.h"
35
36namespace nngxx
37{
38
40{
41 msg_body(nng_msg* m) noexcept
42 : m{m}
43 {
44 }
45
46 [[nodiscard]] inline std::size_t size() const noexcept
47 {
48 return nng_msg_len(m);
49 }
50
51 template<typename char_t>
52 [[nodiscard]] inline char_t* data() noexcept
53 {
54 return static_cast<char_t*>(nng_msg_body(m));
55 }
56
57 template<typename char_t>
58 [[nodiscard]] inline char_t* const data() const noexcept
59 {
60 return static_cast<char_t* const>(nng_msg_body(m));
61 }
62
63 template<uint_c uint_t>
64 [[nodiscard]] inline clev::expected<uint_t> append() noexcept
65 {
66 uint_t x;
67
68 int (*append)(nng_msg*, uint_t*);
69
70 if constexpr (std::is_same_v<uint_t, uint16_t>)
71 {
72 append = nng_msg_append;
73 }
74 else if constexpr (std::is_same_v<uint_t, uint32_t>)
75 {
76 append = nng_msg_trim_u32;
77 }
78 else if constexpr (std::is_same_v<uint_t, uint64_t>)
79 {
80 append = nng_msg_trim_u64;
81 }
82
83 return nngxx::invoke(append, m, &x);
84 }
85
86 template<uint_c uint_t>
87 [[nodiscard]] inline clev::expected<uint_t> trim() noexcept
88 {
89 uint_t x;
90
91 int (*trim)(nng_msg*, uint_t*);
92
93 if constexpr (std::is_same_v<uint_t, uint16_t>)
94 {
95 trim = nng_msg_trim_u16;
96 }
97 else if constexpr (std::is_same_v<uint_t, uint32_t>)
98 {
99 trim = nng_msg_trim_u32;
100 }
101 else if constexpr (std::is_same_v<uint_t, uint64_t>)
102 {
103 trim = nng_msg_trim_u64;
104 }
105
106 return nngxx::invoke(trim, m, &x);
107 }
108
109 [[nodiscard]] inline clev::expected<void> trim(std::size_t sz) noexcept
110 {
111 return nngxx::invoke(nng_msg_trim, m, sz);
112 }
113
114 template<uint_c uint_t>
115 [[nodiscard]] inline clev::expected<uint_t> chop() noexcept
116 {
117 uint_t x;
118
119 int (*chop)(nng_msg*, uint_t*);
120
121 if constexpr (std::is_same_v<uint_t, uint16_t>)
122 {
123 chop = nng_msg_chop;
124 }
125 else if constexpr (std::is_same_v<uint_t, uint32_t>)
126 {
127 chop = nng_msg_chop_u32;
128 }
129 else if constexpr (std::is_same_v<uint_t, uint64_t>)
130 {
131 chop = nng_msg_chop_u64;
132 }
133
134 return nngxx::invoke(chop, m, &x);
135 }
136
137 [[nodiscard]] inline clev::expected<void> chop(std::size_t sz) noexcept
138 {
139 return nngxx::invoke(nng_msg_chop, m, sz);
140 }
141
142private:
143 nng_msg* m;
144};
145
146} // namespace nngxx
147
148nngxx::msg_body nngxx::msg_view::body() noexcept
149{
150 return nngxx::msg_body{v};
151}
152
153const nngxx::msg_body nngxx::msg_view::body() const noexcept
154{
155 return nngxx::msg_body{v};
156}
157
158static_assert(std::copyable<nngxx::msg_body>);
Definition aio.h:35
clev::expected< void > invoke(ret_t(*f)(args_t...), args_t... args) noexcept
Definition err.h:123
clev::expected< uint_t > append() noexcept
Definition msg_body.h:64
clev::expected< void > trim(std::size_t sz) noexcept
Definition msg_body.h:109
clev::expected< void > chop(std::size_t sz) noexcept
Definition msg_body.h:137
char_t *const data() const noexcept
Definition msg_body.h:58
msg_body(nng_msg *m) noexcept
Definition msg_body.h:41
std::size_t size() const noexcept
Definition msg_body.h:46
clev::expected< uint_t > trim() noexcept
Definition msg_body.h:87
char_t * data() noexcept
Definition msg_body.h:52
clev::expected< uint_t > chop() noexcept
Definition msg_body.h:115