Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | List of all members
SkTCopyOnFirstWrite< T > Class Template Reference

#include <SkTLazy.h>

Public Member Functions

 SkTCopyOnFirstWrite (const T &initial)
 
 SkTCopyOnFirstWrite (const T *initial)
 
 SkTCopyOnFirstWrite ()
 
 SkTCopyOnFirstWrite (const SkTCopyOnFirstWrite &that)
 
 SkTCopyOnFirstWrite (SkTCopyOnFirstWrite &&that)
 
SkTCopyOnFirstWriteoperator= (const SkTCopyOnFirstWrite &that)
 
SkTCopyOnFirstWriteoperator= (SkTCopyOnFirstWrite &&that)
 
void init (const T &initial)
 
template<typename... Args>
void initIfNeeded (Args &&... args)
 
Twritable ()
 
const Tget () const
 
const Toperator-> () const
 
 operator const T * () const
 
const Toperator* () const
 

Detailed Description

template<typename T>
class SkTCopyOnFirstWrite< T >

A helper built on top of std::optional to do copy-on-first-write. The object is initialized with a const pointer but provides a non-const pointer accessor. The first time the accessor is called (if ever) the object is cloned.

In the following example at most one copy of constThing is made:

SkTCopyOnFirstWrite<Thing> thing(&constThing); ... function_that_takes_a_const_thing_ptr(thing); // constThing is passed ... if (need_to_modify_thing()) { thing.writable()->modifyMe(); // makes a copy of constThing } ... x = thing->readSomething(); ... if (need_to_modify_thing_now()) { thing.writable()->changeMe(); // makes a copy of constThing if we didn't call modifyMe() }

consume_a_thing(thing); // could be constThing or a modified copy.

Definition at line 139 of file SkTLazy.h.

Constructor & Destructor Documentation

◆ SkTCopyOnFirstWrite() [1/5]

template<typename T >
SkTCopyOnFirstWrite< T >::SkTCopyOnFirstWrite ( const T initial)
inlineexplicit

Definition at line 141 of file SkTLazy.h.

141: fObj(&initial) {}

◆ SkTCopyOnFirstWrite() [2/5]

template<typename T >
SkTCopyOnFirstWrite< T >::SkTCopyOnFirstWrite ( const T initial)
inlineexplicit

Definition at line 143 of file SkTLazy.h.

143: fObj(initial) {}

◆ SkTCopyOnFirstWrite() [3/5]

template<typename T >
SkTCopyOnFirstWrite< T >::SkTCopyOnFirstWrite ( )
inline

Definition at line 146 of file SkTLazy.h.

146: fObj(nullptr) {}

◆ SkTCopyOnFirstWrite() [4/5]

template<typename T >
SkTCopyOnFirstWrite< T >::SkTCopyOnFirstWrite ( const SkTCopyOnFirstWrite< T > &  that)
inline

Definition at line 148 of file SkTLazy.h.

148{ *this = that; }

◆ SkTCopyOnFirstWrite() [5/5]

template<typename T >
SkTCopyOnFirstWrite< T >::SkTCopyOnFirstWrite ( SkTCopyOnFirstWrite< T > &&  that)
inline

Definition at line 149 of file SkTLazy.h.

149{ *this = std::move(that); }

Member Function Documentation

◆ get()

template<typename T >
const T * SkTCopyOnFirstWrite< T >::get ( ) const
inline

Definition at line 191 of file SkTLazy.h.

191{ return fObj; }

◆ init()

template<typename T >
void SkTCopyOnFirstWrite< T >::init ( const T initial)
inline

Definition at line 164 of file SkTLazy.h.

164 {
165 SkASSERT(!fObj);
166 SkASSERT(!fLazy.has_value());
167 fObj = &initial;
168 }
#define SkASSERT(cond)
Definition SkAssert.h:116

◆ initIfNeeded()

template<typename T >
template<typename... Args>
void SkTCopyOnFirstWrite< T >::initIfNeeded ( Args &&...  args)
inline

Definition at line 172 of file SkTLazy.h.

172 {
173 if (!fObj) {
174 SkASSERT(!fLazy.has_value());
175 fObj = &fLazy.emplace(std::forward<Args>(args)...);
176 }
177 }
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args

◆ operator const T *()

template<typename T >
SkTCopyOnFirstWrite< T >::operator const T * ( ) const
inline

Definition at line 199 of file SkTLazy.h.

199{ return fObj; }

◆ operator*()

template<typename T >
const T & SkTCopyOnFirstWrite< T >::operator* ( ) const
inline

Definition at line 201 of file SkTLazy.h.

201{ return *fObj; }

◆ operator->()

template<typename T >
const T * SkTCopyOnFirstWrite< T >::operator-> ( ) const
inline

Operators for treating this as though it were a const pointer.

Definition at line 197 of file SkTLazy.h.

197{ return fObj; }

◆ operator=() [1/2]

template<typename T >
SkTCopyOnFirstWrite & SkTCopyOnFirstWrite< T >::operator= ( const SkTCopyOnFirstWrite< T > &  that)
inline

Definition at line 151 of file SkTLazy.h.

151 {
152 fLazy = that.fLazy;
153 fObj = fLazy.has_value() ? &fLazy.value() : that.fObj;
154 return *this;
155 }

◆ operator=() [2/2]

template<typename T >
SkTCopyOnFirstWrite & SkTCopyOnFirstWrite< T >::operator= ( SkTCopyOnFirstWrite< T > &&  that)
inline

Definition at line 157 of file SkTLazy.h.

157 {
158 fLazy = std::move(that.fLazy);
159 fObj = fLazy.has_value() ? &fLazy.value() : that.fObj;
160 return *this;
161 }

◆ writable()

template<typename T >
T * SkTCopyOnFirstWrite< T >::writable ( )
inline

Returns a writable T*. The first time this is called the initial object is cloned.

Definition at line 182 of file SkTLazy.h.

182 {
183 SkASSERT(fObj);
184 if (!fLazy.has_value()) {
185 fLazy = *fObj;
186 fObj = &fLazy.value();
187 }
188 return &fLazy.value();
189 }

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