/usr/share/doc/libbobcat4-dev/man/fswap.3.html is in libbobcat-dev 4.08.02-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | <!DOCTYPE html><html><head>
<meta charset="UTF-8">
<title>FBB::fswap(3bobcat)</title>
<style type="text/css">
figure {text-align: center;}
img {vertical-align: center;}
.XXfc {margin-left:auto;margin-right:auto;}
.XXtc {text-align: center;}
.XXtl {text-align: left;}
.XXtr {text-align: right;}
.XXvt {vertical-align: top;}
.XXvb {vertical-align: bottom;}
</style>
<link rev="made" href="mailto:Frank B. Brokken: f.b.brokken@rug.nl">
</head>
<body text="#27408B" bgcolor="#FFFAF0">
<hr/>
<h1 id="title">FBB::fswap(3bobcat)</h1>
<h2 id="author">Fast swap function<br/>(libbobcat-dev_4.08.02-x.tar.gz)</h2>
<h2 id="date">2005-2017</h2>
<p>
<h2 >NAME</h2>FBB::fswap - generic template fast swap function
<p>
<h2 >SYNOPSIS</h2>
<strong >#include <bobcat/fswap></strong><br/>
<p>
<h2 >DESCRIPTION</h2>
The information stored in objects frequently needs to be swapped. A
well-known example is the swapping operation required when implementing an
overloaded assignment operator. For example, the generic form of the operator
assignment operator is:
<pre>
Class &operator=(Class const &other)
{
Class tmp(other);
swap(tmp);
return *this;
}
</pre>
The swap functionality merely swaps the contents of the current object and
another object. The standard <em >std::swap</em> function calls the class's
<em >operator=</em> function to swap objects. Newer implementations might use
move-operations to increase the speed of the swapping operation, but in both
cases some form of the assignment operator must be available. Swapping,
however, might be possible when assignment isn't. Classes having reference
data members usually don't offer assignment operators but swapping might be a
well-defined operation.
<p>
It is well known that objects can be installed in a block of memory using
<em >placement new</em>, using a block of memory the size of the object to construct
the object it. This is the foundation of the template function <em >FBB::fswap</em>
(fast swap). This swap function merely uses the memory occupied by objects to
implement the swapping operation and it may therefore be used with classes
having const data members, reference data members, ponters to allocated memory
etc, etc. The function simply uses a spare block of memory the size of the
object to be swapped. It then uses <strong >memcpy</strong>(3) to swap the information
contained in the two objects, using the spare block of memory as a
placeholder.
<p>
Classes may define data members that do not support fast swapping. If such
data members can be swapped using either <em >std::swap</em> or their own <em >swap</em>
member, then overloaded versions of <em >fswap</em> can be used to fast-swap objects
of such classes. Also, classes may inherit from base classes that do not
support fast-swapping, but that either offer their own swap members or can be
swapped using <em >std::swap</em>. For these cases overloaded versions of <em >fswap</em>
are also available. The classes <em >std::string</em> and <em >std::unordered_map</em> are
examples of classes whose objects might not be swappable using a fast swap
method. Therefore, in practice, classes defining members of such classes
should use one of the overloaded <em >fswap</em> functions for swapping their
objects.
<p>
<h2 >NAMESPACE</h2>
<strong >FBB</strong><br/>
All constructors, members, operators and manipulators, mentioned in this
man-page, are defined in the namespace <strong >FBB</strong>.
<p>
<h2 >INHERITS FROM</h2>
-
<p>
<h2 >ENUMERATION</h2>
<p>
The enumereation <em >SwapMode</em> is used to specify a specific swapping-method:
<ul>
<li> <em >SWAPMEMBER</em> is selected by default, but it can also explicitly be
specified. It indicates that selected members are swapped using their own
<em >swap</em> member function having prototpype <em >void Type::swap(Type &rhs)</em>.
<li> <em >STDSWAP</em> can be specified to indicates that selected members should be
swapped using <em >std::swap</em>.
</ul>
Specific members not supporting fast swapping can always be swapped using
a specific swap-method. E.g., when <em >SWAPMEMBER</em> is selected, but a
particular data member does not offer a <em >swap</em>-member, then <em >std::swap</em>
can be specified for just that member.
<p>
<h2 >SWAP FUNCTIONS</h2>
<p>
<ul>
<li> <strong >fswap(Type &lhs, Type &rhs)</strong>:<br/>
This template function swaps the contents of two objects. It can
be used with classes having const data members, reference members, pointer
members or standard value-typed data members;
<p>
<li> <strong >fswap(Type &lhs, Type &rhs, member, ...)</strong>:<br/>
This function is provided with a list of member names (i.e., members
of the class <em >Type</em>) that do not support fast swapping. Those members are
swapped using their <em >swap</em> member, while all other members are
fast-swapped. When using lists of members, the selected members <em >must</em> be
listed in their declaration order or a <em >std::runtime_error</em> exception is
generated when the function is used.
<p>
<li> <strong >fswap<SwapMode::SWAPMEMBER>(Type &lhs, Type &rhs, member, ...)</strong>:<br/>
This function acts identically as the previous function, but
explicitly specifies its default swapping method.
<p>
Each of the members specified in the list of members can be specified by
their names, in which case the specified swapping method is
used. Alternatively <em >stdswap(member)</em> can be used, in which case
<em >std::swap</em> is used for swapping <em >member</em>; or <em >swapmember(member)</em> can
be used, in which case that <em >member's swap</em> member function is used for
swapping <em >member</em>;
<p>
<li> <strong >fswap<SwapMode::STDSWAP>(Type &lhs, Type &rhs, member, ...)</strong>:<br/>
This function is also provided with a list of member names that do not
support fast swapping. Those members are swapped using <em >std::swap</em>. As with
the previous function, members can be specified by their names, or
<em >stdswap(member)</em> or <em >swapmember(member)</em> can be used;
<p>
<li> <strong >fswap[swapMode](&firstMember, Type &lhs, Type &rhs [, member, ...])</strong>:<br/>
This function's first argument is the address of <em >Type's</em> first data
member (usually specified as <em >&d_first</em>) inside <em >Type's</em> own <em >swap</em>
member</ul>. It is used when <em >Type</em> is derived from a base class that itself
does not support fast swapping. This function may optionally be provided with
a <em >SwapMode</em> template non-type argument (by default SWAPMEMBER) is used),
and may also be provided with a list of members (optionally using <em >stdswap</em>
and <em >swapmember</em>).
)
<p>
<h2 >EXAMPLE</h2>
<pre >
#include <iostream>
#include "../fswap"
using namespace FBB;
// Demo class, members d_v3 and d_v4 cannot be memcpy-fast swapped
//
class Demo
{
size_t d_v1;
size_t d_v2;
std::string d_v3;
std::string d_v4;
size_t d_v5;
public:
Demo(size_t value = 0);
void show(char const *msg);
void swap(Demo &rhs);
};
Demo::Demo(size_t value)
:
d_v1(value),
d_v2(value + 1),
d_v3(std::to_string(value + 2)),
d_v4(std::to_string(value + 3)),
d_v5(value + 4)
{}
// fast-swap 2 objects, except for d_v3 and d_v4, which are
// swapped by either std::swap or their own .swap() members
//
void Demo::swap(Demo &rhs)
{
// This is OK, after commenting out the
// fswap(*this, rhs); // string members
// specifying members that should be swapped
// using std::swap. These members MUST be
// specified in their class declaration order
fswap(*this, rhs, d_v3, d_v4);
// fswap(*this, rhs, d_v4, d_v3); // this won't work...
// same, explicitly requesting the
// swap-mode
// fswap<SwapMode::SWAPMEMBER>(*this, rhs, d_v3, d_v4);
// explicitly requesting another
// swap-mode
// fswap<SwapMode::STDSWAP>(*this, rhs, d_v3, d_v4);
// default, starting at a begin-member
// NOTE: the example does NOT swap d_v1
// fswap(&d_v2, *this, rhs, d_v3, d_v4);
// use fastswap, but start at the
// member d_v1 (use this for derived
// classes whose base class do not
// support fast swapping.
// Before using this example comment
// out the class's std::string members
// fswap(&d_v1, *this, rhs, d_v3);
// same, explicitly requesting the
// swap method, swapping all
// fswap<SwapMode::SWAPMEMBER>(&d_v1, *this, rhs, d_v3, d_v4);
// explicitly requesting another
// swap-mode
// fswap<SwapMode::STDSWAP>(&d_v1, *this, rhs, d_v3, d_v4);
// use stdswap by default, but not
// for d_v4, for which .swap() is
// used
// fswap(&d_v1, *this, rhs, d_v3, swapmember(d_v4));
// same
// fswap<SwapMode::STDSWAP>(&d_v1, *this, rhs, d_v3,
// swapmember(d_v4));
// explicitly requesting the already
// default swap method is OK
// fswap(&d_v1, *this, rhs, swapmember(d_v3), stdswap(d_v4));
}
void Demo::show(char const *msg)
{
std::cout << msg << ". " << d_v1 <<
", " << d_v2 <<
", " << d_v3 <<
", " << d_v4 <<
", " << d_v5 <<
'\n';
}
using namespace std;
int main()
{
Demo d1(10);
Demo d2(20);
d1.show("This is d1:");
d2.show("This is d2:");
cout << "swapping...\n";
d1.swap(d2);
d1.show("This is d1:");
d2.show("This is d2:");
}
</pre>
<p>
<h2 >FILES</h2>
<em >bobcat/fswap</em> - defines the class interface
<p>
<h2 >SEE ALSO</h2>
<strong >bobcat</strong>(7), <strong >memcpy</strong>(3)
<p>
<h2 >BUGS</h2>
The <em >fswap</em> functions should not be applied mechanically to swap objects
of classes having pointer data members defining, e.g., a linked list. Consider
a list of four objects like:
<pre>
A -> B -> C -> D
</pre>
fast-swapping B and C would result in the following corrupted list:
<pre>
+------+
| |
A -> C -+ +-> B -+ +-> D
| |
+-------------+
</pre>
However, classes implementing a data structure like a linked-list might
still benefit from fast swapping operations: by implementing their own swap
member they could first use fast swapping to swap the objects, followed by
another fast swap to unswap their `next' pointers.
<p>
The <em >fswap</em> function should also not be used for objects defining
(back-)pointers to their own data. Consider the following objects using
pointers to data and (back-)pointers to the original objects:
<pre>
Before fswapping:
A B
+--------+ +-----------+ +--------+ +-----------+
| | | | | | | |
+--> *Aimp------> *A (back)--+ +--> *Bimp------> *B (back)--+
| | | | | | | | | | | |
+--**Aimp | +-----------+ | +--**Bimp | +-----------+ |
+--------+ <---------------+ +--------+ <---------------+
After fswapping:
+-------------------------------+
+--|-------------------------------|-+
+-------------|--|-----------------+ | |
| A | v | B | v
| +--------+ | +-----------+ | +--------+ | +-----------+
| | | | | | | | | | | |
+-----> *Bimp---+ | *A (back)--+ +---> *Aimp---+ | *B (back)--+
| | | | | | | | | | | |
| +---**Bimp | +-----------+ | +---**Aimp | +-----------+ |
| +--------+ <---------------+ | +--------+ <---------------+
+------------------------------------+
</pre>
After the swap <em >**Bimp</em> should point to <em >Bimp</em>'s address (now at A),
but in fact it points to <em >Aimp</em>'s address (now at B). Likewise, the back
pointers still point at their original objects rather than at their swapped
objects.
<p>
All <em >stream</em> classes define such pointers and can therefore not be swapped
using <em >fswap</em>.
<p>
The bottom line being that <em >fswap</em> should only be used for self-defined
classes for which it can be proven that fast-swapping does not corrupt the
values of its pointer data.
<p>
<h2 >DISTRIBUTION FILES</h2>
<ul>
<li> <em >bobcat_4.08.02-x.dsc</em>: detached signature;
<li> <em >bobcat_4.08.02-x.tar.gz</em>: source archive;
<li> <em >bobcat_4.08.02-x_i386.changes</em>: change log;
<li> <em >libbobcat1_4.08.02-x_*.deb</em>: debian package holding the
libraries;
<li> <em >libbobcat1-dev_4.08.02-x_*.deb</em>: debian package holding the
libraries, headers and manual pages;
<li> <em >http://sourceforge.net/projects/bobcat</em>: public archive location;
</ul>
<p>
<h2 >BOBCAT</h2>
Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.
<p>
<h2 >COPYRIGHT</h2>
This is free software, distributed under the terms of the
GNU General Public License (GPL).
<p>
<h2 >AUTHOR</h2>
Frank B. Brokken (<strong >f.b.brokken@rug.nl</strong>).
<p>
</body>
</html>
|