]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/core/scattered_message.hh
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / seastar / include / seastar / core / scattered_message.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2014 Cloudius Systems, Ltd.
20 */
21
22 #pragma once
23
24 #include <seastar/core/deleter.hh>
25 #include <seastar/core/temporary_buffer.hh>
26 #include <seastar/net/packet.hh>
27 #include <seastar/core/sstring.hh>
28 #include <memory>
29 #include <vector>
30 #include <seastar/util/std-compat.hh>
31
32 namespace seastar {
33
34 template <typename CharType>
35 class scattered_message {
36 private:
37 using fragment = net::fragment;
38 using packet = net::packet;
39 using char_type = CharType;
40 packet _p;
41 public:
42 scattered_message() {}
43 scattered_message(scattered_message&&) = default;
44 scattered_message(const scattered_message&) = delete;
45
46 void append_static(const char_type* buf, size_t size) {
47 if (size) {
48 _p = packet(std::move(_p), fragment{(char_type*)buf, size}, deleter());
49 }
50 }
51
52 template <size_t N>
53 void append_static(const char_type(&s)[N]) {
54 append_static(s, N - 1);
55 }
56
57 void append_static(const char_type* s) {
58 append_static(s, strlen(s));
59 }
60
61 template <typename size_type, size_type max_size>
62 void append_static(const basic_sstring<char_type, size_type, max_size>& s) {
63 append_static(s.begin(), s.size());
64 }
65
66 void append_static(const std::string_view& s) {
67 append_static(s.data(), s.size());
68 }
69
70 void append(std::string_view v) {
71 if (v.size()) {
72 _p = packet(std::move(_p), temporary_buffer<char>::copy_of(v));
73 }
74 }
75
76 template <typename size_type, size_type max_size>
77 void append(basic_sstring<char_type, size_type, max_size> s) {
78 if (s.size()) {
79 _p = packet(std::move(_p), std::move(s).release());
80 }
81 }
82
83 template <typename size_type, size_type max_size, typename Callback>
84 void append(const basic_sstring<char_type, size_type, max_size>& s, Callback callback) {
85 if (s.size()) {
86 _p = packet(std::move(_p), fragment{s.begin(), s.size()}, make_deleter(std::move(callback)));
87 }
88 }
89
90 void reserve(int n_frags) {
91 _p.reserve(n_frags);
92 }
93
94 packet release() && {
95 return std::move(_p);
96 }
97
98 template <typename Callback>
99 void on_delete(Callback callback) {
100 _p = packet(std::move(_p), make_deleter(std::move(callback)));
101 }
102
103 operator bool() const {
104 return _p.len();
105 }
106
107 size_t size() {
108 return _p.len();
109 }
110 };
111
112 }