/** *Ahelperbuiltontopofstd::optionaltodocopy-on-first-write.Theobjectisinitialized *withaconstpointerbutprovidesanon-constpointeraccessor.Thefirsttimethe *accessoriscalled(ifever)theobjectiscloned. * *InthefollowingexampleatmostonecopyofconstThingismade: * *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.
*/ template <typename T> class SkTCopyOnFirstWrite {
public: explicit SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {}
// Should only be called once, and only if the default constructor was used. void init(const T& initial) {
SkASSERT(!fObj);
SkASSERT(!fLazy.has_value());
fObj = &initial;
}
// If not already initialized, in-place instantiates the writable object template <typename... Args> void initIfNeeded(Args&&... args) { if (!fObj) {
SkASSERT(!fLazy.has_value());
fObj = &fLazy.emplace(std::forward<Args>(args)...);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.