Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bslma_sharedptrinplacerep
[Package bslma]

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:
bslma::SharedPtrInplaceRep in-place bslma::SharedPtrRep implementation
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:
  class MySharedDatetimePtr {
      // This class provide a reference counted smart pointer to support
      // shared ownership of a 'bdlt::Datetime' object.

      bdlt::Datetime      *d_ptr_p;  // pointer to the managed object
      bslma::SharedPtrRep *d_rep_p;  // pointer to the representation object

    private:
      // NOT IMPLEMENTED
      MySharedDatetimePtr& operator=(const MySharedDatetimePtr&);

    public:
      // CREATORS
      MySharedDatetimePtr();
          // Create an empty shared datetime.

      MySharedDatetimePtr(bdlt::Datetime* ptr, bslma::SharedPtrRep* rep);
          // Create a shared datetime that adopts ownership of the specified
          // 'ptr' and the specified 'rep.

      MySharedDatetimePtr(const MySharedDatetimePtr& original);
          // Create a shared datetime that refers to the same object managed
          // by the specified 'original'

      ~MySharedDatetimePtr();
          // Destroy this shared datetime and release the reference to the
          // 'bdlt::Datetime' object to which it might be referring.  If this
          // is the last shared reference, deleted the managed object.

      // MANIPULATORS
      void createInplace(bslma::Allocator *basicAllocator,
                         int               year,
                         int               month,
                         int               day);
          // Create a new 'bslma::SharedPtrInplaceRep', using the specified
          // 'basicAllocator' to supply memory, using the specified 'year',
          // 'month' and 'day' to initialize the 'bdlt::Datetime' within the
          // newly created 'bslma::SharedPtrInplaceRep', and make this
          // object refer to the newly created 'bdlt::Datetime' object.

      bdlt::Datetime& operator*() const;
          // Return a reference offering modifiable access to the shared
          // 'bdlt::Datetime' object.

      bdlt::Datetime *operator->() const;
          // Return the address of the modifiable 'bdlt::Datetime' to which
          // this object refers.

      bdlt::Datetime *ptr() const;
          // Return the address of the modifiable 'bdlt::Datetime' to which
          // this object refers.
  };
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;
  }