Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::LazyMutableCopy< T > Class Template Reference

#include <lazy_mutable_copy.h>

Public Member Functions

 LazyMutableCopy (const T &obj)
 
 LazyMutableCopy (T &&obj)
 
 LazyMutableCopy (LazyMutableCopy &&)=default
 
 LazyMutableCopy (const LazyMutableCopy &)=delete
 
class LazyMutableCopy< T > & operator= (LazyMutableCopy< T > &&)=default
 
class LazyMutableCopy< T > & operator= (const LazyMutableCopy< T > &)=delete
 
T * get_mutable ()
 This will copy the object if we don't already have ownership.
 
std::unique_ptr< T > copy_or_move_as_unique_ptr () &&
 
bool has_ownership () const
 
const T * get () const
 
const T & operator* () const
 
const T * operator-> () const
 
void dispose () &&
 

Detailed Description

template<class T>
class operations_research::LazyMutableCopy< T >

LazyMutableCopy<T> is a helper class for making an on-demand copy of an object of arbitrary type T. Type T must have a copy constructor.

Sample usage: const Proto& original_input = ...; LazyMutableCopy<Proto> input(original_input); if (input.get().foo() == BAD_VALUE) { input.get_mutable()->set_foo(GOOD_VALUE); ///< Copies the object. } ///< Process "input" here without worrying about BAD_VALUE. A good pattern is to have function taking LazyMutableCopy<> as argument: void ProcessProto(LazyMutableCopy<Proto> input) { ///< pass by copy ... } At the call site: ProcessProto(const_ref_to_my_proto);

In basic usage, a LazyMutableCopy is in one of two states:

  • original: points to the const original. No memory allocated.
  • copy: points to a mutable copy of the original and owns it. Owning the copy means that the destructor will delete it, like std::unique_ptr<>. This is what you get by calling get_mutable() or constructing it with a move.

Definition at line 44 of file lazy_mutable_copy.h.

Constructor & Destructor Documentation

◆ LazyMutableCopy() [1/4]

template<class T >
operations_research::LazyMutableCopy< T >::LazyMutableCopy ( const T & obj)
inline

You can construct a LazyMutableCopy with a const reference to an object, which must outlive this class (unless get_mutable() was called).

Definition at line 48 of file lazy_mutable_copy.h.

◆ LazyMutableCopy() [2/4]

template<class T >
operations_research::LazyMutableCopy< T >::LazyMutableCopy ( T && obj)
inline

The other option is to construct a LazyMutableCopy with a std::move(T). In this case you transfer ownership and you can mutate it for free.

Definition at line 53 of file lazy_mutable_copy.h.

◆ LazyMutableCopy() [3/4]

template<class T >
operations_research::LazyMutableCopy< T >::LazyMutableCopy ( LazyMutableCopy< T > && )
default

You can move a LazyMutableCopy but not copy it, much like a std::unique_ptr<>.

◆ LazyMutableCopy() [4/4]

template<class T >
operations_research::LazyMutableCopy< T >::LazyMutableCopy ( const LazyMutableCopy< T > & )
delete

Member Function Documentation

◆ copy_or_move_as_unique_ptr()

template<class T >
std::unique_ptr< T > operations_research::LazyMutableCopy< T >::copy_or_move_as_unique_ptr ( ) &&
inline

Lazily make a copy if not already done and transfer ownership from this class to the returned std::unique_ptr<T>. Calling this function leaves the class in a state where the only valid operations is to assign it a new value.

We force a call via std::move(lazy_mutable_copy).copy_or_move_as_unique_ptr() to make it clearer that lazy_mutable_copy shouldn't really be used after this.

Definition at line 80 of file lazy_mutable_copy.h.

◆ dispose()

template<class T >
void operations_research::LazyMutableCopy< T >::dispose ( ) &&
inline

Destroys any owned value. Calling this function leaves the class in a state where the only valid operations is to assign it a new value.

We force a call via std::move(lazy_mutable_copy).dispose() to make it clearer that lazy_mutable_copy shouldn't really be used after this.

Definition at line 105 of file lazy_mutable_copy.h.

◆ get()

template<class T >
const T * operations_research::LazyMutableCopy< T >::get ( ) const
inline

Standard smart pointer accessor, but only for const purpose. Undefined if the class contains no object.

Definition at line 96 of file lazy_mutable_copy.h.

◆ get_mutable()

template<class T >
T * operations_research::LazyMutableCopy< T >::get_mutable ( )
inline

This will copy the object if we don't already have ownership.

Definition at line 64 of file lazy_mutable_copy.h.

◆ has_ownership()

template<class T >
bool operations_research::LazyMutableCopy< T >::has_ownership ( ) const
inline

True iff get_mutable() was called at least once (in which case the object was copied) or if we constructed this via std::move().

Definition at line 92 of file lazy_mutable_copy.h.

◆ operator*()

template<class T >
const T & operations_research::LazyMutableCopy< T >::operator* ( ) const
inline

Definition at line 97 of file lazy_mutable_copy.h.

◆ operator->()

template<class T >
const T * operations_research::LazyMutableCopy< T >::operator-> ( ) const
inline

Definition at line 98 of file lazy_mutable_copy.h.

◆ operator=() [1/2]

template<class T >
class LazyMutableCopy< T > & operations_research::LazyMutableCopy< T >::operator= ( const LazyMutableCopy< T > & )
delete

◆ operator=() [2/2]

template<class T >
class LazyMutableCopy< T > & operations_research::LazyMutableCopy< T >::operator= ( LazyMutableCopy< T > && )
default

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