/usr/share/doc/libbobcat2-dev/man/refcount.3.html is in libbobcat-dev 2.20.01-1.
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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | <html><head>
<title>FBB::RefCount</title>
<link rev="made" href="mailto:Frank B. Brokken: f.b.brokken@rug.nl">
</head>
<body text="#27408B" bgcolor="#FFFAF0">
<hr>
<h1>FBB::RefCount</h1>
<h2>libbobcat1-dev_2.20.01-x.tar.gz</h2>
<h2>2005-2011</h2>
<html><head>
<link rev="made" href="mailto:Frank B. Brokken: f.b.brokken@rug.nl">
</head>
<body text="#27408B" bgcolor="#FFFAF0">
<hr>
<h1></h1>
<html><head>
<title>FBB::RefCount(3bobcat)</title>
<link rev="made" href="mailto:Frank B. Brokken: f.b.brokken@rug.nl">
</head>
<body text="#27408B" bgcolor="#FFFAF0">
<hr>
<h1>FBB::RefCount(3bobcat)</h1>
<h2>libbobcat1-dev_2.20.01-x.tar.gz Reference Counting</h2>
<h2>2005-2011</h2>
<p>
<h2>NAME</h2>FBB::RefCount - Base class implementing reference counting.
<p>
<h2>SYNOPSIS</h2>
<strong>#include <bobcat/refcount></strong><br>
Linking option: <em>-lbobcat</em>
<p>
<h2>DESCRIPTION</h2>
<strong>RefCount</strong> implements a virtual base class implementing reference
counting. When reference counting is used, objects share the memory of a
(usually big or complex) data structure, until objects need to modify their
data, in which case they obtain a copy of the data of their own. This approach
is most useful with classes that seldomly alter their data, but consult their
data most of the time.
<p>
As an example, consider hidden structures as found in the <strong>regcomp</strong>(3)
function: there is no documented way to copy an existing compiled regular
expression, so if multiple objects must refer to the same regular expression,
the expression should be compiled once. Thereafter, a reference count monitors
the number of objects using the compiled expression. Only when the last object
is destroyed the compiled expression is freed.
<p>
In general, objects using reference counting should obtain their own data
if they need to alter their data. This situation is also called
`copy-on-write'. Copy-on-write is implemented by obtaining a copy of the data
before any modification of the data takes place. So, each non-const member
function should first copy the data, and should only then modify its own
data. Constant members may simply refer to the data, without the need to copy
them first.
<p>
The class <strong>RefCount</strong> should be embedded in programs as follows:
<ul>
<li> <strong>RefCount</strong> defines a <em>virtual base class</em>, defining all members
that are required to implement reference counting. The class is a virtual base
class since it defines a member <strong>virtual RefCount *clone() const = 0</strong>, which
must return a real copy of the derived class object (cf. Gamma et al.'s (1995)
<em>proxy</em> Design Pattern).
<li> From <strong>RefCount</strong> a class (named <strong>Data</strong> in this man page) is derived
defining the functionality that is required to manipulate the data:
this class may contain constructors, overloaded operators, accessors,
modifiers, etc.
<li> A third class (called <strong>Client</strong> in this man page) defines the actual
interface to the program in which reference counting is required. In its
purest form, <strong>Client</strong> only has a <strong>Data *</strong> data member referring to the
(possibly shared) data. The functionality offered to the users of <strong>Client</strong>
objects is normally defined by shadowing the functionality defined by
<strong>Data</strong>, using simple one-line in-line functions.
</ul>
<p>
Except for <strong>clone()</strong>, there are several issues to bear in mind when
using reference counting as defined by <strong>RefCount</strong>:
<ul>
<li> When <strong>Client</strong> share data (e.g., when using a copy constructor or an
overloaded assignment operator), <strong>RefCount::share()</strong> should be called rather
than <strong>operator new</strong>. Since at this point the data are shared with other
objects, no copy is required, and the use of <strong>operator new</strong> should be
avoided.
<li> When <strong>Client</strong> objects go out of scope, the destructor should call
<strong>RefCount::release()</strong> rather than <strong>operator delete</strong> to disassociate itself
from the object's data. The <strong>operator delete</strong> should not be called directly,
since this might prematurely destroy data shared with other objects.
<li> All members modifying data (i.e., all non-const member functions)
should call <strong>RefCount::modifying()</strong> prior to performing the
modification. This ensures that the object operates on its own data, rather
than modifying shared data.
</ul>
Except for the abovementioned items, all members of <strong>Client</strong> should be
implemented as usual: constructors use <strong>new Data(argument list)</strong>,
<strong>clone()</strong> returns a pointer to a clone of itself, etc.. Refer to the code
example for an actual implementation.
<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>PROTECTED CONSTRUCTORS</h2>
<ul>
<li> <strong>RefCount()</strong>:<br>
This constructor implements the default constructor.
<li> <strong>RefCount(RefCount const &other)</strong>:<br>
This constructor implements the copy constructor, which is a
convenience function for <strong>Data</strong>'s copy constructor, but otherwise acting
identically as <strong>RefCount()</strong> itself.
</ul>
<p>
<h2>PROTECTED DESTRUCTOR</h2>
<ul>
<li> <strong>virtual ~RefCount()</strong>:<br>
The destructor is an empty virtual member, thus allowing <strong>RefCount *</strong>
variables to destroy any derived class objects they point to.
</ul>
<p>
<h2>PUBLIC MEMBER FUNCTIONS</h2>
<ul>
<li> <strong>size_t refcount() const</strong>:<br>
This member returns the current number of objects sharing the data.
<li> <strong>void release()</strong>:<br>
This member must be called by <strong>Client</strong> objects that must
disassociate themselves from the (possibly shared) data. In practice it is
called by the <strong>Client</strong>'s destructor and overloaded assignment operator. It
will actually call <strong>Data</strong>'s destructor when the object was the only object
still referring to the data.
</ul>
<p>
<h2>PUBLIC STATIC MEMBER FUNCTIONS</h2>
<ul>
<li> <strong>Data *RefCount::share(Data const *ptr)</strong>:<br>
This member should be called by the constructor of <strong>Client</strong> objects
sharing another client's data. In practice it is called by the client's copy
constructor and overloaded assignment operator. It receives the actual pointer
to the data as its argument, and returns the new value of the pointer.
<p>
Note that <strong>Data</strong> is not a hard-coded class: the function is implemented
as a template member, and so it can be used by every class derived from
<strong>RefCount</strong>.
<p>
<li> <strong>Data &RefCount::modifying(Data **ptr)</strong>:<br>
This member should be called by <strong>Client</strong> objects' non-const members,
just before modifying data. The function may alter the value of the client's
<strong>Data *</strong> data member. It returns a <em>reference</em> to the data, allowing the
client's member function to call the required <strong>Data</strong> modifier in one single
statement, using the member selection operator (dot).
<p>
Note that <strong>Data</strong> is not a hard-coded class: the function is implemented
as a template member, and so it can be used by every class derived from
<strong>RefCount</strong>.
<p>
This function performs a <strong>dynamic_cast</strong>, which will always succeed if
<strong>Data</strong> was indeed derived from <strong>RefCount</strong>. A <strong>bad_cast</strong> is thrown if the
cast fails.
</ul>
<p>
<h2>EXAMPLE</h2>
The following example illustrates the use the class <strong>RefCount</strong>. A class
<strong>Data</strong> is derived from <strong>RefCount</strong>, defining <strong>clone()</strong>, several standard
members (copy constructor, overloaded assignment operator) as <em>private</em>
members, and a default constructor, destructor, accessor and modifier member
as public members.
<p>
The class that is used directly by the program is <strong>Client</strong>, given
next. It defines all standard constructors and members, and it shadows the
accessor and modifier members of <strong>Data</strong>:
<p>
Finally, a small program using <strong>Client</strong> is shown.
<p>
<pre>
/*
driver.cc
*/
#include <iostream>
#include <string>
#include <sstream>
#include "../refcount"
using namespace std;
using namespace FBB;
// The class Data uses reference counting. Data are shared until they are
// modified.
class Data: public RefCount
{
static size_t s_n; // count the number of objects in use
public:
Data() // all other constructors are built like this:
{ // using RefCount's default constructor.
s_n++;
cout << "Data(), " << s_n << " Data objects created\n";
}
virtual ~Data()
{
s_n--;
cout << "~Data(), " << s_n << " Data objects left\n";
}
string accessor() const
{
ostringstream ostr;
ostr << "calling Data::accessor(). Data at " << this;
return ostr.str();
}
void modifier() // a plain modifier
{
cout << "calling Data::modifier(). Data at " << this << endl;
}
// support function for operator>>()
istream &extract(istream &istr)
{
cout << "extraction from istream. " <<
"Enter a non-empty line of text" << endl;
string s;
getline(istr, s);
cout << "Read: " << s << endl;
return istr;
}
// another modifier: operator+=()
Data &operator+=(Data const &rvalue)
{
cout << "calling Data::operator+=(): @`" <<
this << "' += @`" << &rvalue << "'\n";
return *this;
}
private:
Data &operator=(Data const &other); // NI
Data(Data const &other) // The copy constructor MUST call RefCount's
: // CC. Data(Data) is a convenience function
// for clone() and should not be available to
RefCount(other) // clients, thus enforcing the use of
{ // clone() / share() / release()
s_n++;
cout << "Data(Data const &), " << s_n << " Data objects created\n";
}
virtual RefCount *clone() const
{
cout << "Data::clone()\n";
return new Data(*this);
}
};
// Client: uses a pointer to a Data object. It is implemented in an
// almost standard way. Deviations are:
// *. Copy(): should call share() rather than new Data(*d_dataPtr)
// *. Destroy(): should call release() rather than delete d_dataPtr;
// *. non-const members modifying d_dataPtr's data"
// should call Data:modifying() first.
class Client
{
// modifying friend
// see below at modifier()
friend istream &operator>>(istream &istr, Client &c)
{
return Data::modifying(&c.d_dataPtr).extract(istr);
}
Data *d_dataPtr;
public:
// Constructors, destructor, overloaded assignment operator: all
// follow my standard copy() / destroy() approach.
Client() // new object, creates its own data
: // use new Data(...) to initialize.
d_dataPtr(new Data())
{}
~Client()
{
destroy();
}
Client(Client const &other)
{
copy(other);
}
Client &operator=(Client const &other)
{
if (this != &other)
{
destroy();
copy(other);
}
return *this;
}
string accessor() const // accessors shadow Data's members
{
return d_dataPtr->accessor();
}
// modifiers call modifying first
void modifier() // simple modifier
{
Data::modifying(&d_dataPtr).modifier();
}
// arithmetic assignment modifier
Client &operator+=(Client const &other)
{
Data::modifying(&d_dataPtr).operator+=(*other.d_dataPtr);
return *this;
}
private:
void copy(Client const &other) // copying is sharing: call share()
{
d_dataPtr = Data::share(other.d_dataPtr);
}
void destroy() // destroying is disassociation: call
{ // release
d_dataPtr->release();
}
};
size_t Data::s_n = 0;
Client const operator+(Client const &lvalue, Client const &rvalue)
{
return Client(lvalue) += rvalue;
}
int main()
{
cout << "Construction:\n";
Client c;
cout << "Extraction c from cin:\n";
cin >> c;
cout << "c's Modifier called:\n";
c.modifier();
cout << "operator += :\n";
c += c;
cout << "operator + :\n";
c + c;
cout << "Copy construction:\n";
Client c2(c);
cout << "Assignment:\n";
c = c2;
cout << "Accessors:\n";
cout << "access c: " << c.accessor() << endl;
cout << "access c2: " << c2.accessor() << endl;
cout << "operator += :\n";
c += c;
cout << "c's Modifier called:\n";
c.modifier();
cout << "Accessors:\n";
cout << "access c: " << c.accessor() << endl;
cout << "access c2: " << c2.accessor() << endl;
cout << "c2's Modifier called:\n";
c2.modifier();
cout << "resetting refcount to 2:\n";
c = c2;
cout << "Extraction c from cin:\n";
cin >> c;
cout << "End of program:\n";
}
</pre>
<p>
<h2>FILES</h2>
<em>bobcat/refcount</em> - defines the class interface
<p>
<h2>SEE ALSO</h2>
<strong>bobcat</strong>(7)
<p>
<h2>BUGS</h2>
None Reported.
<p>
<h2>DISTRIBUTION FILES</h2>
<ul>
<li> <em>bobcat_2.20.01-x.dsc</em>: detached signature;
<li> <em>bobcat_2.20.01-x.tar.gz</em>: source archive;
<li> <em>bobcat_2.20.01-x_i386.changes</em>: change log;
<li> <em>libbobcat1_2.20.01-x_*.deb</em>: debian package holding the
libraries;
<li> <em>libbobcat1-dev_2.20.01-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>
|