Skia
2DGraphicsLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkTCopyOnFirstWrite< T > Class Template Reference

A helper built on top of SkTLazy to do copy-on-first-write. More...

#include <SkTLazy.h>

Public Member Functions

 SkTCopyOnFirstWrite (const T &initial)
 
 SkTCopyOnFirstWrite (const T *initial)
 
void init (const T &initial)
 
T * writable ()
 Returns a writable T*. More...
 
const T * operator-> () const
 Operators for treating this as though it were a const pointer.
 
 operator const T * () const
 
const T & operator* () const
 

Private Attributes

const T * fObj
 
SkTLazy< T > fLazy
 

Detailed Description

template<typename T>
class SkTCopyOnFirstWrite< T >

A helper built on top of SkTLazy 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.

Member Function Documentation

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

Returns a writable T*.

The first time this is called the initial object is cloned.


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