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