GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  FSWAP (3)

NAME

FBB::fswap - generic template fast swap function

CONTENTS

SYNOPSIS

#include <bobcat/fswap>

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:

Class &operator=(Class const &other) { Class tmp(other); swap(tmp); return *this; }

The swap functionality merely swaps the contents of the current object and another object. The standard std::swap function calls the class\(cqs operator= function to swap objects. Newer implementations might use move-operations to increase the speed of the swaping operation, but in both cases some form of the assignment operator must be available. Swapping, however, might be possible when assignemnt isn\(cqt. Classes having reference data members usually don\(cqt offer assignment operators but swapping might be a well-defined operation.

It is well known that objects can be installed in a block of memory using placement new, using a block of memory the size of the object to construct the object it. This is the foundation of the template function FBB::fswap (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 memcpy(3) to swap the information contained in the two objects, using the spare block of memory as a placeholder.

The function uses partial specializations to optimize the swapping operation for objects of sizes 1, 2, 4 or 8 bytes. It uses memcpy(3) for objects of other sizes.

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

-

SWAP FUNCTION

o fswap(Type &lhs, Type &rhs):
This template function swaps the contents of the two objects. It can be used with classes having const data members, reference members, pointer members or standard value-typed data members.

EXAMPLE

#include <iostream>
#include \(dq../fswap\(dq

class Demo { std::ostream &d_out; size_t d_value;

public: Demo(std::ostream &out = std::cerr, size_t value = 0) : d_out(out), d_value(value) {}

void show(char const *msg) { d_out << msg << \(dq. Value: \(dq << d_value << \(cq\n\(cq; } };

using namespace std;

int main() { Demo d1; Demo d2(cout, 12);

FBB::fswap(d1, d2);

d1.show(\(dqThis is d1\(dq); // to cerr: 12 d2.show(\(dqThis is d2\(dq); // to cout: 0 }

FILES

bobcat/fswap - defines the class interface

SEE ALSO

bobcat(7), memcpy(3)

BUGS

The fswap function 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:

A -> B -> C -> D

fast-swapping B and C would result in the following corrupted list:

+------+ | | A -> C -+ +-> B -+ +-> D | | +-------------+

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.

The fswap 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:

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 | +-----------+ | | +--------+ <---------------+ | +--------+ <---------------+ +------------------------------------+

After the swap **Bimp should point to Bimp\(cqs address (now at A), but in fact it points to Aimp\(cqs address (now at B). Likewise, the back pointers still point at their original objects rather than at their swapped objects.

All stream classes define such pointers and can therefore not be swapped using fswap.

The bottom line being that fswap 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.

DISTRIBUTION FILES

o bobcat_3.25.01-x.dsc: detached signature;
o bobcat_3.25.01-x.tar.gz: source archive;
o bobcat_3.25.01-x_i386.changes: change log;
o libbobcat1_3.25.01-x_*.deb: debian package holding the libraries;
o libbobcat1-dev_3.25.01-x_*.deb: debian package holding the libraries, headers and manual pages;
o http://sourceforge.net/projects/bobcat: public archive location;

BOBCAT

Bobcat is an acronym of ‘Brokken\(cqs Own Base Classes And Templates\(cq.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).

Search for    or go to Top of page |  Section 3 |  Main Index


libbobcat-dev_3&.25&.01-x&.tar&.gz FBB::FSWAP (3bobcat) 2005-2015

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.