BDE 4.14.0 Production release
|
Provide a manager of reusable, non-negative integer indices.
This component implements an efficient, value-semantic manager class for reusable, non-negative integer indices. Each new instance of a bdlc::IndexClerk
will issue consecutive integers on request, beginning with 0, 1, 2, ...
. Indices that are no longer needed may be returned for reuse. Existing decommissioned indices are reissued before any new ones are created. Value-semantic operations such as copy construction and assignment, equality comparison, and streaming are also provided. Finally, a bdlc::IndexClerkIter
is provided to enable sequential, read-only access to the currently decommissioned indices. Note that the order of iteration is not defined.
The following characterizes the performance of representative operations using "big-oh" notation, O[f(N,M)], where the names N
and M
also refer to the number of respective elements in the sequence of decommissioned indices.
This section illustrates intended use of this component.
A bdlc::IndexClerk
is commonly used in conjunction with an array to enable machine-address-independent referencing. Rather than dynamically allocating an object and holding its address, the object is stored in the array at the next position dispensed by its associated bdlc::IndexClerk
, and that index becomes an identifier (Id) for the new object. Instead of destroying an unneeded object, its Id is merely returned to the clerk.
Care must be taken to ensure that objects "created" at reused indices (i.e., indices below the current length of the array) replace (the value of) an existing object in the array while objects created at new indices (i.e., indices at the current length) are appended to the array.
For example, suppose we have a security class object. To add and remove security values from a security array/clerk pair, you might write the following two functions: