stlab::copy_on_write 1.0.3
Copy-on-write wrapper for any type
Loading...
Searching...
No Matches
stlab::copy_on_write< T > Class Template Reference

#include <stlab/copy_on_write.hpp>

Public Types

using value_type = T
using element_type = T

Public Member Functions

 copy_on_write () noexcept(std::is_nothrow_constructible_v< T >)
template<class U>
 copy_on_write (U &&x, disable_copy< U >=nullptr)
template<class U, class V, class... Args>
 copy_on_write (U &&x, V &&y, Args &&... args)
 copy_on_write (const copy_on_write &x) noexcept
 copy_on_write (copy_on_write &&x) noexcept
auto operator= (const copy_on_write &x) noexcept -> copy_on_write &
auto operator= (copy_on_write &&x) noexcept -> copy_on_write &
template<class U>
auto operator= (U &&x) -> disable_copy_assign< U >
auto write () -> element_type &
template<class Transform, class Inplace>
auto write (Transform transform, Inplace inplace) -> element_type &
auto read () const noexcept -> const element_type &
 operator const element_type & () const noexcept
auto operator* () const noexcept -> const element_type &
auto operator-> () const noexcept -> const element_type *
auto unique () const noexcept -> bool
auto unique_instance () const noexcept -> bool
auto identity (const copy_on_write &x) const noexcept -> bool

Friends

void swap (copy_on_write &x, copy_on_write &y) noexcept
auto operator< (const copy_on_write &x, const copy_on_write &y) noexcept -> bool
auto operator< (const copy_on_write &x, const element_type &y) noexcept -> bool
auto operator< (const element_type &x, const copy_on_write &y) noexcept -> bool
auto operator> (const copy_on_write &x, const copy_on_write &y) noexcept -> bool
auto operator> (const copy_on_write &x, const element_type &y) noexcept -> bool
auto operator> (const element_type &x, const copy_on_write &y) noexcept -> bool
auto operator<= (const copy_on_write &x, const copy_on_write &y) noexcept -> bool
auto operator<= (const copy_on_write &x, const element_type &y) noexcept -> bool
auto operator<= (const element_type &x, const copy_on_write &y) noexcept -> bool
auto operator>= (const copy_on_write &x, const copy_on_write &y) noexcept -> bool
auto operator>= (const copy_on_write &x, const element_type &y) noexcept -> bool
auto operator>= (const element_type &x, const copy_on_write &y) noexcept -> bool
auto operator== (const copy_on_write &x, const copy_on_write &y) noexcept -> bool
auto operator== (const copy_on_write &x, const element_type &y) noexcept -> bool
auto operator== (const element_type &x, const copy_on_write &y) noexcept -> bool
auto operator!= (const copy_on_write &x, const copy_on_write &y) noexcept -> bool
auto operator!= (const copy_on_write &x, const element_type &y) noexcept -> bool
auto operator!= (const element_type &x, const copy_on_write &y) noexcept -> bool

Detailed Description

template<typename T>
class stlab::copy_on_write< T >

A copy-on-write wrapper for any type that models Regular.

Copy-on-write semantics allow for an object to be lazily copied - only creating a copy when the value is modified and there is more than one reference to the value.

This class is thread safe and supports types that model Moveable.

Examples
basic_usage.cpp.

Definition at line 101 of file copy_on_write.hpp.

Member Typedef Documentation

◆ element_type

template<typename T>
using stlab::copy_on_write< T >::element_type = T

The type of value stored.

Definition at line 137 of file copy_on_write.hpp.

◆ value_type

template<typename T>
using stlab::copy_on_write< T >::value_type = T
Deprecated
Use element_type instead. The type of value stored.

Definition at line 132 of file copy_on_write.hpp.

Constructor & Destructor Documentation

◆ copy_on_write() [1/5]

template<typename T>
stlab::copy_on_write< T >::copy_on_write ( )
inlinenoexcept

Default constructs the wrapped value.

Definition at line 142 of file copy_on_write.hpp.

◆ copy_on_write() [2/5]

template<typename T>
template<class U>
stlab::copy_on_write< T >::copy_on_write ( U && x,
disable_copy< U > = nullptr )
inline

Constructs a new instance by forwarding arguments to the wrapped value constructor.

Definition at line 153 of file copy_on_write.hpp.

◆ copy_on_write() [3/5]

template<typename T>
template<class U, class V, class... Args>
stlab::copy_on_write< T >::copy_on_write ( U && x,
V && y,
Args &&... args )
inline

Constructs a new instance by forwarding multiple arguments to the wrapped value constructor.

Definition at line 159 of file copy_on_write.hpp.

◆ copy_on_write() [4/5]

template<typename T>
stlab::copy_on_write< T >::copy_on_write ( const copy_on_write< T > & x)
inlinenoexcept

Copy constructor that shares the underlying data with the source object.

Definition at line 165 of file copy_on_write.hpp.

◆ copy_on_write() [5/5]

template<typename T>
stlab::copy_on_write< T >::copy_on_write ( copy_on_write< T > && x)
inlinenoexcept

Move constructor that takes ownership of the source object's data.

Definition at line 175 of file copy_on_write.hpp.

◆ ~copy_on_write()

template<typename T>
stlab::copy_on_write< T >::~copy_on_write ( )
inline

Definition at line 179 of file copy_on_write.hpp.

Member Function Documentation

◆ identity()

template<typename T>
auto stlab::copy_on_write< T >::identity ( const copy_on_write< T > & x) const -> bool
inlinenodiscardnoexcept

Returns true if this object and the given object share the same underlying data.

Examples
basic_usage.cpp.

Definition at line 305 of file copy_on_write.hpp.

◆ operator const element_type &()

template<typename T>
stlab::copy_on_write< T >::operator const element_type & ( ) const
inlinenoexcept

Implicit conversion to const reference of the underlying value.

Definition at line 273 of file copy_on_write.hpp.

◆ operator*()

template<typename T>
auto stlab::copy_on_write< T >::operator* ( ) const -> const element_type&
inlinenoexcept

Dereference operator that returns a const reference to the underlying value.

Definition at line 278 of file copy_on_write.hpp.

◆ operator->()

template<typename T>
auto stlab::copy_on_write< T >::operator-> ( ) const -> const element_type*
inlinenoexcept

Arrow operator that returns a const pointer to the underlying value.

Definition at line 283 of file copy_on_write.hpp.

◆ operator=() [1/3]

template<typename T>
auto stlab::copy_on_write< T >::operator= ( const copy_on_write< T > & x) -> copy_on_write&
inlinenoexcept

Copy assignment operator that shares the underlying data with the source object.

Definition at line 193 of file copy_on_write.hpp.

◆ operator=() [2/3]

template<typename T>
auto stlab::copy_on_write< T >::operator= ( copy_on_write< T > && x) -> copy_on_write&
inlinenoexcept

Move assignment operator that takes ownership of the source object's data.

Definition at line 202 of file copy_on_write.hpp.

◆ operator=() [3/3]

template<typename T>
template<class U>
auto stlab::copy_on_write< T >::operator= ( U && x) -> disable_copy_assign<U>
inline

Assigns a new value to the wrapped object, optimizing for in-place assignment when unique.

Definition at line 212 of file copy_on_write.hpp.

◆ read()

template<typename T>
auto stlab::copy_on_write< T >::read ( ) const -> const element_type&
inlinenodiscardnoexcept

Returns a const reference to the underlying value for read-only access.

Examples
basic_usage.cpp.

Definition at line 264 of file copy_on_write.hpp.

◆ unique()

template<typename T>
auto stlab::copy_on_write< T >::unique ( ) const -> bool
inlinenodiscardnoexcept

Returns true if this is the only reference to the underlying object.

This is useful to determine if calling write() will cause a copy.

Definition at line 290 of file copy_on_write.hpp.

◆ unique_instance()

template<typename T>
auto stlab::copy_on_write< T >::unique_instance ( ) const -> bool
inlinenodiscardnoexcept
Deprecated
Use unique() instead. Returns true if this is the only reference to the underlying object.

Definition at line 300 of file copy_on_write.hpp.

◆ write() [1/2]

template<typename T>
auto stlab::copy_on_write< T >::write ( ) -> element_type&
inline

Obtains a non-const reference to the underlying value.

This will copy the underlying value if necessary so changes to the value do not affect other copy_on_write objects sharing the same data.

Examples
basic_usage.cpp.

Definition at line 227 of file copy_on_write.hpp.

◆ write() [2/2]

template<typename T>
template<class Transform, class Inplace>
auto stlab::copy_on_write< T >::write ( Transform transform,
Inplace inplace ) -> element_type&
inline

If the object is not unique, the transform is applied to the underlying value to copy it and a reference to the new value is returned. If the object is unique, the inplace function is called with a reference to the underlying value and a reference to the value is returned.

Parameters
transformA function object that takes a const reference to the underlying value and returns a new value.
inplaceA function object that takes a reference to the underlying value and modifies it in place.
Returns
A reference to the underlying value.

Definition at line 246 of file copy_on_write.hpp.

◆ operator!= [1/3]

template<typename T>
auto operator!= ( const copy_on_write< T > & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 382 of file copy_on_write.hpp.

◆ operator!= [2/3]

template<typename T>
auto operator!= ( const copy_on_write< T > & x,
const element_type & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 386 of file copy_on_write.hpp.

◆ operator!= [3/3]

template<typename T>
auto operator!= ( const element_type & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 390 of file copy_on_write.hpp.

◆ operator< [1/3]

template<typename T>
auto operator< ( const copy_on_write< T > & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 322 of file copy_on_write.hpp.

◆ operator< [2/3]

template<typename T>
auto operator< ( const copy_on_write< T > & x,
const element_type & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 326 of file copy_on_write.hpp.

◆ operator< [3/3]

template<typename T>
auto operator< ( const element_type & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 330 of file copy_on_write.hpp.

◆ operator<= [1/3]

template<typename T>
auto operator<= ( const copy_on_write< T > & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 346 of file copy_on_write.hpp.

◆ operator<= [2/3]

template<typename T>
auto operator<= ( const copy_on_write< T > & x,
const element_type & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 350 of file copy_on_write.hpp.

◆ operator<= [3/3]

template<typename T>
auto operator<= ( const element_type & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 354 of file copy_on_write.hpp.

◆ operator== [1/3]

template<typename T>
auto operator== ( const copy_on_write< T > & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 370 of file copy_on_write.hpp.

◆ operator== [2/3]

template<typename T>
auto operator== ( const copy_on_write< T > & x,
const element_type & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 374 of file copy_on_write.hpp.

◆ operator== [3/3]

template<typename T>
auto operator== ( const element_type & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 378 of file copy_on_write.hpp.

◆ operator> [1/3]

template<typename T>
auto operator> ( const copy_on_write< T > & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 334 of file copy_on_write.hpp.

◆ operator> [2/3]

template<typename T>
auto operator> ( const copy_on_write< T > & x,
const element_type & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 338 of file copy_on_write.hpp.

◆ operator> [3/3]

template<typename T>
auto operator> ( const element_type & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 342 of file copy_on_write.hpp.

◆ operator>= [1/3]

template<typename T>
auto operator>= ( const copy_on_write< T > & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 358 of file copy_on_write.hpp.

◆ operator>= [2/3]

template<typename T>
auto operator>= ( const copy_on_write< T > & x,
const element_type & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 362 of file copy_on_write.hpp.

◆ operator>= [3/3]

template<typename T>
auto operator>= ( const element_type & x,
const copy_on_write< T > & y ) -> bool
friend

Comparisons can be done with the underlying value or the copy_on_write object.

Definition at line 366 of file copy_on_write.hpp.

◆ swap

template<typename T>
void swap ( copy_on_write< T > & x,
copy_on_write< T > & y )
friend

Efficiently swaps the contents of two copy_on_write objects.

Definition at line 314 of file copy_on_write.hpp.


The documentation for this class was generated from the following file: