.TH "FBB::fswap" "3bobcat" "2005\-2023" "libbobcat\-dev_6\&.04\&.00" "Fast swap function" .PP .SH "NAME" FBB::fswap \- generic template fast swap function .PP .SH "SYNOPSIS" \fB#include \fP .br .PP .SH "DESCRIPTION" 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: .nf Class &operator=(Class const &other) { Class tmp(other); swap(tmp); return *this; } .fi The swap functionality merely swaps the content of the current object and another object\&. The standard \fIstd::swap\fP function calls the class\(cq\&s \fIoperator=\fP 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\(cq\&t\&. Classes having reference data members usually don\(cq\&t offer assignment operators but swapping might be a well\-defined operation\&. .PP It is well known that objects can be installed in a block of memory using \fIplacement new\fP, using a block of memory the size of the object to construct the object it\&. This is the foundation of the template function \fIFBB::fswap\fP (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 \fBmemcpy\fP(3) to swap the information contained in the two objects, using the spare block of memory as a placeholder\&. .PP Classes may define data members that do not support fast swapping\&. If such data members can be swapped using either \fIstd::swap\fP or their own \fIswap\fP member, then overloaded versions of \fIfswap\fP 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 \fIstd::swap\fP\&. For these cases overloaded versions of \fIfswap\fP are also available\&. The classes \fIstd::string\fP and \fIstd::unordered_map\fP 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 \fIfswap\fP functions for swapping their objects\&. .PP .SH "NAMESPACE" \fBFBB\fP .br All constructors, members, operators and manipulators, mentioned in this man\-page, are defined in the namespace \fBFBB\fP\&. .PP .SH "INHERITS FROM" \- .PP .SH "ENUMERATION" .PP The enumereation \fISwapMode\fP is used to specify a specific swapping\-method: .IP o \fISWAPMEMBER\fP is selected by default, but it can also explicitly be specified\&. It indicates that selected members are swapped using their own \fIswap\fP member function having prototpype \fIvoid Type::swap(Type &rhs)\fP\&. .IP o \fISTDSWAP\fP can be specified to indicates that selected members should be swapped using \fIstd::swap\fP\&. Specific members not supporting fast swapping can always be swapped using a specific swap\-method\&. E\&.g\&., when \fISWAPMEMBER\fP is selected, but a particular data member does not offer a \fIswap\fP\-member, then \fIstd::swap\fP can be specified for just that member\&. .PP .SH "SWAP FUNCTIONS" .PP .IP o \fBfswap(Type &lhs, Type &rhs)\fP: .br This template function swaps the content of two objects\&. It can be used with classes having const data members, reference members, pointer members or standard value\-typed data members; .IP .IP o \fBfswap(Type &lhs, Type &rhs, member, \&.\&.\&.)\fP: .br This function is provided with a list of member names (i\&.e\&., members of the class \fIType\fP) that do not support fast swapping\&. Those members are swapped using their \fIswap\fP member, while all other members are fast\-swapped\&. When using lists of members, the selected members \fImust\fP be listed in their declaration order or a \fIstd::runtime_error\fP exception is generated when the function is used\&. .IP .IP o \fBfswap(Type &lhs, Type &rhs, member, \&.\&.\&.)\fP: .br This function acts identically as the previous function, but explicitly specifies its default swapping method\&. .IP 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 \fIstdswap(member)\fP can be used, in which case \fIstd::swap\fP is used for swapping \fImember\fP; or \fIswapmember(member)\fP can be used, in which case that \fImember\(cq\&s swap\fP member function is used for swapping \fImember\fP; .IP .IP o \fBfswap(Type &lhs, Type &rhs, member, \&.\&.\&.)\fP: .br This function is also provided with a list of member names that do not support fast swapping\&. Those members are swapped using \fIstd::swap\fP\&. As with the previous function, members can be specified by their names, or \fIstdswap(member)\fP or \fIswapmember(member)\fP can be used; .IP .IP o \fBfswap[swapMode](&firstMember, Type &lhs, Type &rhs [, member, \&.\&.\&.])\fP: .br This function\(cq\&s first argument is the address of \fIType\(cq\&s\fP first data member (usually specified as \fI&d_first\fP inside \fIType\(cq\&s\fP own \fIswap\fP member)\&. It is used when \fIType\fP is derived from a base class that itself does not support fast swapping\&. This function may optionally be provided with a \fISwapMode\fP template non\-type argument (by default SWAPMEMBER is used), and may also be provided with a list of members (optionally using \fIstdswap\fP and \fIswapmember\fP)\&. .PP .SH "EXAMPLE" .nf #include #include \(dq\&\&.\&./fswap\(dq\& 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\(cq\&t work\&.\&.\&. // same, explicitly requesting the // swap\-mode // fswap(*this, rhs, d_v3, d_v4); // explicitly requesting another // swap\-mode // fswap(*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\(cq\&s std::string members // fswap(&d_v1, *this, rhs, d_v3); // same, explicitly requesting the // swap method, swapping all // fswap(&d_v1, *this, rhs, d_v3, d_v4); // explicitly requesting another // swap\-mode // fswap(&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(&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 << \(dq\&\&. \(dq\& << d_v1 << \(dq\&, \(dq\& << d_v2 << \(dq\&, \(dq\& << d_v3 << \(dq\&, \(dq\& << d_v4 << \(dq\&, \(dq\& << d_v5 << \(cq\&\en\(cq\&; } using namespace std; int main() { Demo d1(10); Demo d2(20); d1\&.show(\(dq\&This is d1:\(dq\&); d2\&.show(\(dq\&This is d2:\(dq\&); cout << \(dq\&swapping\&.\&.\&.\en\(dq\&; d1\&.swap(d2); d1\&.show(\(dq\&This is d1:\(dq\&); d2\&.show(\(dq\&This is d2:\(dq\&); } .fi .PP .SH "FILES" \fIbobcat/fswap\fP \- defines the class interface .PP .SH "SEE ALSO" \fBbobcat\fP(7), \fBmemcpy\fP(3) .PP .SH "BUGS" The \fIfswap\fP 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: .nf A \-> B \-> C \-> D .fi fast\-swapping B and C would result in the following corrupted list: .nf +\-\-\-\-\-\-+ | | A \-> C \-+ +\-> B \-+ +\-> D | | +\-\-\-\-\-\-\-\-\-\-\-\-\-+ .fi 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\(cq\& pointers\&. .PP The \fIfswap\fP 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: .nf 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 | +\-\-\-\-\-\-\-\-\-\-\-+ | | +\-\-\-\-\-\-\-\-+ <\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | +\-\-\-\-\-\-\-\-+ <\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ .fi After the swap \fI**Bimp\fP should point to \fIBimp\fP\(cq\&s address (now at A), but in fact it points to \fIAimp\fP\(cq\&s address (now at B)\&. Likewise, the back pointers still point at their original objects rather than at their swapped objects\&. .PP All \fIstream\fP classes define such pointers and can therefore not be swapped using \fIfswap\fP\&. .PP The bottom line being that \fIfswap\fP 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\&. .PP .SH "BOBCAT PROJECT FILES" .PP .IP o \fIhttps://fbb\-git\&.gitlab\&.io/bobcat/\fP: gitlab project page; .IP o \fIbobcat_6\&.04\&.00\-x\&.dsc\fP: detached signature; .IP o \fIbobcat_6\&.04\&.00\-x\&.tar\&.gz\fP: source archive; .IP o \fIbobcat_6\&.04\&.00\-x_i386\&.changes\fP: change log; .IP o \fIlibbobcat1_6\&.04\&.00\-x_*\&.deb\fP: debian package containing the libraries; .IP o \fIlibbobcat1\-dev_6\&.04\&.00\-x_*\&.deb\fP: debian package containing the libraries, headers and manual pages; .PP .SH "BOBCAT" Bobcat is an acronym of `Brokken\(cq\&s Own Base Classes And Templates\(cq\&\&. .PP .SH "COPYRIGHT" This is free software, distributed under the terms of the GNU General Public License (GPL)\&. .PP .SH "AUTHOR" Frank B\&. Brokken (\fBf\&.b\&.brokken@rug\&.nl\fP)\&. .PP