BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_ownerequal

Detailed Description

Outline

Purpose

Provide an ownership comparison for shared and weak pointers.

Classes

Canonical header: bsl_memory.h

See also
bslstl_sharedptr

Description

This component provides the C++26 standard binary comparison functor, bsl::owner_equal, that determines the equality of two smart pointer objects by the address of their bslma::SharedPtrRep data. Note that this class is an empty POD type.

Usage

This section illustrates intended use of this component.

Example 1: Basic Use of @ref owner_equal

Suppose we need an unordered map accepting shared pointers as keys. We also expect that this container will be accessible from multiple threads and some of them will store weak versions of smart pointers to break reference cycles.

First, we create a container and populate it:

int,
Map container;
bsl::shared_ptr<int> sharedPtr1 = bsl::make_shared<int>(1);
bsl::shared_ptr<int> sharedPtr2 = bsl::make_shared<int>(2);
bsl::weak_ptr<int> weakPtr1(sharedPtr1);
container[sharedPtr1] = 1;
container[sharedPtr2] = 2;
Definition bslstl_sharedptr.h:1830
Definition bslstl_unorderedmap.h:1089
Definition bslstl_sharedptr.h:3705
Definition bslstl_ownerequal.h:122
Definition bslstl_ownerhash.h:172

Then we make sure that shared pointers can be used to perform lookup, and verify that the results are correct.

Map::const_iterator iter = container.find(sharedPtr1);
assert(container.end() != iter );
assert(1 == iter->second);
iter = container.find(sharedPtr2);
assert(container.end() != iter);
assert(2 == iter->second);

Finally, we simulate the accessing the container from another thread and perform lookup using weak pointers:

iter = container.find(weakPtr1);
assert(container.end() != iter );
assert(1 == iter->second);
bsl::weak_ptr<int> weakPtr3(bsl::make_shared<int>(3));
iter = container.find(weakPtr3);
assert(container.end() == iter);