LibreOffice Module o3tl (master) 1
|
Copy-on-write wrapper.
Copy-on-write wrapper. This template provides copy-on-write semantics for the wrapped type: when copying, the operation is performed shallow, i.e. different cow_wrapper objects share the same underlying instance. Only when accessing the underlying object via non-const methods, a unique copy is provided.
The type parameter T
must satisfy the following requirements: it must be default-constructible, copyable (it need not be assignable), and be of non-reference type. Note that, despite the fact that this template provides access to the wrapped type via pointer-like methods (operator->()
and operator*()
), it does not work like e.g. the std smart pointer wrappers (shared_ptr, unique_ptr, etc.). Internally, the cow_wrapper holds a by-value instance of the wrapped object. This is to avoid one additional heap allocation, and providing access via operator->()
/operator*()
is because operator.()
cannot be overridden.
Regarding thread safety: this wrapper is not thread-safe per se, because cow_wrapper has no way of synchronizing the potentially many different cow_wrapper instances, that reference a single shared value_type instance. That said, when passing ThreadSafeRefCountingPolicy
as the MTPolicy
parameter, accessing a thread-safe pointee through multiple cow_wrapper instances might be thread-safe, if the individual pointee methods are thread-safe, including pointee's copy constructor. Any wrapped object that needs external synchronisation (e.g. via an external mutex, which arbitrates access to object methods, and can be held across multiple object method calls) cannot easily be dealt with in a thread-safe way, because, as noted, objects are shared behind the client's back.
class cow_wrapper_client_impl; class cow_wrapper_client { public: cow_wrapper_client(); cow_wrapper_client( const cow_wrapper_client& ); cow_wrapper_client( cow_wrapper_client&& ); ~cow_wrapper_client(); cow_wrapper_client& operator=( const cow_wrapper_client& ); cow_wrapper_client& operator=( cow_wrapper_client&& ); void modify( int nVal ); int queryUnmodified() const; private: o3tl::cow_wrapper< cow_wrapper_client_impl > maImpl; };
and the implementation file would look like this:
class cow_wrapper_client_impl { public: void setValue( int nVal ) { mnValue = nVal; } int getValue() const { return mnValue; } private: int mnValue; } cow_wrapper_client::cow_wrapper_client() : maImpl() { } cow_wrapper_client::cow_wrapper_client( const cow_wrapper_client& rSrc ) : maImpl( rSrc.maImpl ) { } cow_wrapper_client::cow_wrapper_client( cow_wrapper_client& rSrc ) : maImpl( std::move( rSrc.maImpl ) ) { } cow_wrapper_client::~cow_wrapper_client() { } cow_wrapper_client& cow_wrapper_client::operator=( const cow_wrapper_client& rSrc ) { maImpl = rSrc.maImpl; return *this; } cow_wrapper_client& cow_wrapper_client::operator=( cow_wrapper_client&& rSrc ) { maImpl = std::move( rSrc.maImpl ); return *this; } void cow_wrapper_client::modify( int nVal ) { maImpl->setValue( nVal ); } int cow_wrapper_client::queryUnmodified() const { return maImpl->getValue(); }