Flow-IPC 3.0.0
Flow-IPC project: Full implementation reference.
shared_name_fwd.hpp
Go to the documentation of this file.
1/* Flow-IPC: Core
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
21#include "ipc/util/util_fwd.hpp"
22#include <iosfwd>
23
24namespace ipc::util
25{
26
27// Types.
28
29// Find doc headers near the bodies of these compound types.
30
31class Shared_name;
32
33// Free functions.
34
35/**
36 * Returns new object equal to `Shared_name(src1) /= src2`.
37 *
38 * @relatesalso Shared_name
39 *
40 * @param src1
41 * Object to precede the appended separator and `src2`.
42 * @param src2
43 * Object to append after separator.
44 * @return See above.
45 */
46Shared_name operator/(const Shared_name& src1, const Shared_name& src2);
47
48/**
49 * Returns new object equal to `Shared_name(src1) /= raw_src2`.
50 *
51 * @relatesalso Shared_name
52 *
53 * @tparam Source
54 * See Shared_name::operator/=() similar overload.
55 * @param src1
56 * Object to precede the appended separator and `raw_src2`.
57 * @param raw_src2
58 * String to append after separator.
59 * @return See above.
60 */
61template<typename Source>
62Shared_name operator/(const Shared_name& src1, const Source& raw_src2);
63
64/**
65 * Returns new object equal to `Shared_name(raw_src1) /= src2`.
66 *
67 * @relatesalso Shared_name
68 *
69 * @tparam Source
70 * See Shared_name::operator/=() similar overload.
71 * @param raw_src1
72 * String to precede the appended separator and `src2`.
73 * @param src2
74 * Object to append after separator.
75 * @return See above.
76 */
77template<typename Source>
78Shared_name operator/(const Source& raw_src1, const Shared_name& src2);
79
80/**
81 * Returns new object equal to `Shared_name(src1) += src2`.
82 *
83 * @relatesalso Shared_name
84 *
85 * @param src1
86 * Object to precede the appended `src2`.
87 * @param src2
88 * Object to append.
89 * @return See above.
90 */
91Shared_name operator+(const Shared_name& src1, const Shared_name& src2);
92
93/**
94 * Returns new object equal to `Shared_name(src1) += raw_src2`.
95 *
96 * @relatesalso Shared_name
97 *
98 * @tparam Source
99 * See Shared_name::operator+=() similar overload.
100 * @param src1
101 * Object to precede the appended `raw_src2`.
102 * @param raw_src2
103 * String to append.
104 * @return See above.
105 */
106template<typename Source>
107Shared_name operator+(const Shared_name& src1, const Source& raw_src2);
108
109/**
110 * Returns new object equal to `Shared_name(src2)` with `raw_src1` pre-pended to it.
111 *
112 * @relatesalso Shared_name
113 *
114 * @tparam Source
115 * See Shared_name::operator+=() similar overload.
116 * @param raw_src1
117 * String to precede the appended `src2`.
118 * @param src2
119 * Object to append.
120 * @return See above.
121 */
122template<typename Source>
123Shared_name operator+(const Source& raw_src1, const Shared_name& src2);
124
125/**
126 * Prints embellished string representation of the given Shared_name to the given `ostream`.
127 * @warning This is *not* equivalent to writing Shared_name::str(); as of this writing it includes not just
128 * `str()` but also the number of characters in it as a decimal and a separator, for convenience in
129 * test/debug, to visually detect names approaching certain length limits. (An empty() name prints as
130 * `null`.) If you wish to output `val.str()`, then output... well... `val.str()`.
131 *
132 * @todo Does Shared_name `operator>>` and `operator<<` being asymmetrical get one into trouble when using
133 * Shared_name with boost.program_options (or `flow::cfg` which is built on top of it)? Look into it. It may be
134 * necessary to make `operator<<` equal to that of `ostream << string` after all; though the added niceties of the
135 * current `<<` semantics may still at least be available via some explicit accessor.
136 *
137 * @relatesalso Shared_name
138 *
139 * @param os
140 * Stream to which to write.
141 * @param val
142 * Object to serialize.
143 * @return `os`.
144 */
145std::ostream& operator<<(std::ostream& os, const Shared_name& val);
146
147/**
148 * Reads Shared_name from the given `istream`; equivalent to reading `string` into Shared_name::str().
149 *
150 * @relatesalso Shared_name
151 *
152 * @param is
153 * Stream to read.
154 * @param val
155 * Object to which to deserialize.
156 * @return `is`.
157 */
158std::istream& operator>>(std::istream& is, Shared_name& val);
159
160/**
161 * Returns `true` if and only if `val1.str() == val2.str()`. Caution: this does not execute normalize() or anything
162 * like that.
163 *
164 * @relatesalso Shared_name
165 *
166 * @param val1
167 * Object to compare.
168 * @param val2
169 * Object to compare.
170 * @return See above.
171 */
172bool operator==(const Shared_name& val1, const Shared_name& val2);
173
174/**
175 * Negation of similar `==`.
176 *
177 * @relatesalso Shared_name
178 *
179 * @param val1
180 * See `==`.
181 * @param val2
182 * See `==`.
183 * @return See above.
184 */
185bool operator!=(const Shared_name& val1, const Shared_name& val2);
186
187/**
188 * Returns `true` if and only if `val1.str() == string(val2)`. Caution: this does not execute normalize() or anything
189 * like that.
190 *
191 * @relatesalso Shared_name
192 *
193 * @param val1
194 * Object to compare.
195 * @param val2
196 * String to compare.
197 * @return See above.
198 */
199bool operator==(const Shared_name& val1, util::String_view val2);
200
201/**
202 * Negation of similar `==`.
203 *
204 * @relatesalso Shared_name
205 *
206 * @param val1
207 * See `==`.
208 * @param val2
209 * See `==`.
210 * @return See above.
211 */
212bool operator!=(const Shared_name& val1, util::String_view val2);
213
214/**
215 * Returns `true` if and only if `string(val1) == val2.str()`. Caution: this does not execute normalize() or anything
216 * like that.
217 *
218 * @relatesalso Shared_name
219 *
220 * @param val1
221 * String to compare.
222 * @param val2
223 * Object to compare.
224 * @return See above.
225 */
226bool operator==(util::String_view val1, const Shared_name& val2);
227
228/**
229 * Negation of similar `==`.
230 *
231 * @relatesalso Shared_name
232 *
233 * @param val1
234 * See `==`.
235 * @param val2
236 * See `==`.
237 * @return See above.
238 */
239bool operator!=(util::String_view val1, const Shared_name& val2);
240
241/**
242 * Returns `true` if and only if `val1.str() < val2.str()`. Enables use of Shared_name in an `std::map` without
243 * jumping through any special hoops.
244 *
245 * @relatesalso Shared_name
246 *
247 * @param val1
248 * Object to compare.
249 * @param val2
250 * Object to compare.
251 * @return See above.
252 */
253bool operator<(const Shared_name& val1, const Shared_name& val2);
254
255/**
256 * Hasher of Shared_name for boost.unordered et al.
257 *
258 * @relatesalso Shared_name
259 *
260 * @param val
261 * Object to hash.
262 * @return See above.
263 */
264size_t hash_value(const Shared_name& val);
265
266/**
267 * Swaps two objects. Constant-time. Suitable for standard ADL-swap pattern `using std::swap; swap(val1, val2);`.
268 *
269 * @relatesalso Shared_name
270 *
271 * @param val1
272 * Object.
273 * @param val2
274 * Object.
275 */
276void swap(Shared_name& val1, Shared_name& val2);
277
278/**
279 * Utility that invokes `Persistent_object::for_each_persistent()` and synchronously invokes
280 * `Persistent_object::remove_persistent()` on each resulting item that passes the given filter,
281 * where `Persistent_object` is a type that handles objects -- such as SHM pools or POSIX MQs -- with kernel-persistent
282 * semantics.
283 *
284 * The number of items removed (without any error) is returned.
285 * The nature of any error(s) encountered by individual `remove_persistent()` calls is ignored (not returned
286 * in any way) except for logging.
287 *
288 * This "forgiving" error emission behavior is sufficient in many cases. If you require finer control over
289 * this please use `Persistent_object::for_each_persistent()` and `Persistent_object::remove_persistent()`
290 * plus your own handling of the failure thereof in your custom `handle_name_func()`.
291 *
292 * If `filter_func()` throws, this function does not go through any remaining entries; the exception
293 * is not caught.
294 *
295 * @tparam Persistent_object
296 * See above. It must have `static` methods `for_each_persistent()` and `remove_persistent()`
297 * with semantics identical to, e.g., shm::classic::Pool_arena versions of these methods.
298 * As of this writing this includes: shm::classic::Pool_arena, transport::Persistent_mq_handle (concept),
299 * transport::Posix_mq_handle, transport::Bipc_mq_handle (its impls).
300 * @tparam Filter_func
301 * Function object with signature `bool F(const Shared_name&)`, which should return `true` to delete,
302 * `false` to skip. For example it might simply check that `name` starts with a certain prefix
303 * (remove_each_persistent_with_name_prefix() uses this), or it might check whether the creating process --
304 * whose PID might be encoded in `name` by some convention (e.g., see ipc::session) -- is alive
305 * (util::process_running()).
306 * @param logger_ptr
307 * Logger to use for subsequently logging.
308 * @param filter_func
309 * See `Filter_func` above.
310 * @return The number of successful `Persistent_object::remove_persistent()` calls invoked (might be zero).
311 */
312template<typename Persistent_object, typename Filter_func>
313unsigned int remove_each_persistent_if(flow::log::Logger* logger_ptr, const Filter_func& filter_func);
314
315/**
316 * Utility that invokes remove_each_persistent_if() with the filter that returns `true` (yes, remove)
317 * if and only if the item's name optionally matches a given Shared_name prefix. (Optional in that
318 * supplying an `.empty()` prefix deletes all items.)
319 *
320 * @tparam Persistent_object
321 * See remove_each_persistent_if().
322 * @param logger_ptr
323 * See remove_each_persistent_if().
324 * @param name_prefix_or_empty
325 * An object is removed only if its name starts with this fragment. This filter is skipped if
326 * the value is `.empty()`.
327 * @return See remove_each_persistent_if().
328 */
329template<typename Persistent_object>
330unsigned int remove_each_persistent_with_name_prefix(flow::log::Logger* logger_ptr,
331 const Shared_name& name_prefix_or_empty);
332
333} // namespace ipc::util
util::Shared_name Shared_name
Convenience alias for the commonly used type util::Shared_name.
Flow-IPC module containing miscellaneous general-use facilities that are ubiquitously used by ~all Fl...
Definition: util.cpp:31
unsigned int remove_each_persistent_if(flow::log::Logger *logger_ptr, const Filter_func &filter_func)
Utility that invokes Persistent_object::for_each_persistent() and synchronously invokes Persistent_ob...
Shared_name operator/(const Shared_name &src1, const Shared_name &src2)
Returns new object equal to Shared_name(src1) /= src2.
Definition: shared_name.cpp:84
std::ostream & operator<<(std::ostream &os, const Native_handle &val)
Prints string representation of the given Native_handle to the given ostream.
Shared_name operator+(const Shared_name &src1, const Shared_name &src2)
Returns new object equal to Shared_name(src1) += src2.
Definition: shared_name.cpp:78
constexpr bool operator<(Native_handle val1, Native_handle val2) noexcept
Returns a less-than comparison of two Native_handle objects, with the usual total ordering guarantees...
size_t hash_value(Native_handle val) noexcept
Hasher of Native_handle for boost.unordered et al.
constexpr void swap(Native_handle &val1, Native_handle &val2) noexcept
Standard swap.
constexpr bool operator!=(Native_handle val1, Native_handle val2) noexcept
Negation of similar ==.
unsigned int remove_each_persistent_with_name_prefix(flow::log::Logger *logger_ptr, const Shared_name &name_prefix_or_empty)
Utility that invokes remove_each_persistent_if() with the filter that returns true (yes,...
std::istream & operator>>(std::istream &is, Shared_name &val)
Reads Shared_name from the given istream; equivalent to reading string into Shared_name::str().
flow::util::String_view String_view
Short-hand for Flow's String_view.
Definition: util_fwd.hpp:112
constexpr bool operator==(Native_handle val1, Native_handle val2) noexcept
Returns true if and only if the two Native_handle objects are the same underlying handle.