Provide an in-place implementation of bslma::SharedPtrRep
.
More...
Namespaces |
namespace | bslma |
Detailed Description
- Outline
-
-
- Purpose:
- Provide an in-place implementation of
bslma::SharedPtrRep
.
-
- Classes:
-
- See also:
- bslma_sharedptr, bslma_sharedptr_rep, Component bslma_sharedptroutofplacerep
-
- Description:
- This component provides a concrete implementation of
bslma::SharedPtrRep
for managing objects of the parameterized TYPE
that are stored in-place in the representation . Thus, only one memory allocation is required to create both the representation and the managed object. When all references to the in-place object are released (using releaseRef
), the destructor of TYPE
is invoked.
-
- Thread Safety:
bslma::SharedPtrInplaceRep
is thread-safe provided that disposeObject
and disposeRep
are not called explicitly, meaning that all non-creator operations other than disposeObject
and disposeRep
on a given instance can be safely invoked simultaneously from multiple threads (disposeObject
and disposeRep
are meant to be invoked only by releaseRef
and releaseWeakRef
). Note that there is no thread safety guarantees for operations on the managed object contained in bslma::SharedPtrInplaceRep
.
-
- Usage:
- The following example demonstrates how to implement a shared
bdlt::Datetime
using bslma::SharedPtrInplaceRep
: Finally, we define the implementation. MySharedDatetimePtr::MySharedDatetimePtr()
: d_ptr_p(0)
, d_rep_p(0)
{
}
MySharedDatetimePtr::MySharedDatetimePtr(bdlt::Datetime *ptr,
bslma::SharedPtrRep *rep)
: d_ptr_p(ptr)
, d_rep_p(rep)
{
}
MySharedDatetimePtr::MySharedDatetimePtr(
const MySharedDatetimePtr& original)
: d_ptr_p(original.d_ptr_p)
, d_rep_p(original.d_rep_p)
{
if (d_ptr_p) {
d_rep_p->acquireRef();
} else {
d_rep_p = 0;
}
}
MySharedDatetimePtr::~MySharedDatetimePtr()
{
if (d_rep_p) {
d_rep_p->releaseRef();
}
}
void MySharedDatetimePtr::createInplace(bslma::Allocator *basicAllocator,
int year,
int month,
int day)
{
basicAllocator = bslma::Default::allocator(basicAllocator);
bslma::SharedPtrInplaceRep<bdlt::Datetime> *rep = new (*basicAllocator)
bslma::SharedPtrInplaceRep<bdlt::Datetime>(basicAllocator,
year,
month,
day);
MySharedDatetimePtr temp(rep->ptr(), rep);
bsl::swap(d_ptr_p, temp.d_ptr_p);
bsl::swap(d_rep_p, temp.d_rep_p);
}
bdlt::Datetime& MySharedDatetimePtr::operator*() const {
return *d_ptr_p;
}
bdlt::Datetime *MySharedDatetimePtr::operator->() const {
return d_ptr_p;
}
bdlt::Datetime *MySharedDatetimePtr::ptr() const {
return d_ptr_p;
}