Describe the Skia 'onMethodName' pattern.
This pattern is used frequently throughout the codebase but seems fairly unique to Skia. It can be misleading if you haven't seen it before. (In particular, the `onXxxxx` naming scheme is sometimes used to indicate message-passing or event-handling, but that's not how it is used in Skia.) Change-Id: I73c5f7874bc51f8fde07baa8ef6a0e47c102302a No-Try: true Docs-Preview: https://skia.org/?cl=310159 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/310159 Reviewed-by: Brian Salomon <bsalomon@google.com> Reviewed-by: Mike Klein <mtklein@google.com> Commit-Queue: John Stiles <johnstiles@google.com> Auto-Submit: John Stiles <johnstiles@google.com>
This commit is contained in:
parent
c9c4e2e9ef
commit
dfded1b0a2
@ -32,7 +32,7 @@ We prefer no trailing whitespace but aren't very strict about it.
|
||||
|
||||
We wrap lines at 100 columns unless it is excessively ugly (use your judgement).
|
||||
The soft line length limit was changed from 80 to 100 columns in June 2012. Thus,
|
||||
most files still adhere to the 80 column limit. It is not necessary or worth
|
||||
many files still adhere to the 80 column limit. It is not necessary or worth
|
||||
significant effort to promote 80 column wrapped files to 100 columns. Please
|
||||
don't willy-nilly insert longer lines in 80 column wrapped files. Either be
|
||||
consistent with the surrounding code or, if you really feel the need, promote
|
||||
@ -402,6 +402,52 @@ Method calls within method calls should be prefixed with dereference of the
|
||||
this->method();
|
||||
~~~~
|
||||
|
||||
A common pattern for virtual methods in Skia is to include a public non-virtual
|
||||
(or final) method, paired with a private virtual method named "onMethodName".
|
||||
This ensures that the base-class method is always invoked and gives it control
|
||||
over how the virtual method is used, rather than relying on each subclass to
|
||||
call `INHERITED::onMethodName`. For example:
|
||||
|
||||
<!--?prettify?-->
|
||||
~~~~
|
||||
class SkSandwich {
|
||||
public:
|
||||
void assemble() {
|
||||
// All sandwiches must have bread on the top and bottom.
|
||||
this->addIngredient(kBread_Ingredient);
|
||||
this->onAssemble();
|
||||
this->addIngredient(kBread_Ingredient);
|
||||
}
|
||||
bool cook() {
|
||||
return this->onCook();
|
||||
}
|
||||
|
||||
private:
|
||||
// All sandwiches must implement onAssemble.
|
||||
virtual void onAssemble() = 0;
|
||||
// Sandwiches can remain uncooked by default.
|
||||
virtual bool onCook() { return true; }
|
||||
};
|
||||
|
||||
class SkGrilledCheese : public SkSandwich {
|
||||
private:
|
||||
void onAssemble() override {
|
||||
this->addIngredient(kCheese_Ingredient);
|
||||
}
|
||||
bool onCook() override {
|
||||
return this->toastOnGriddle();
|
||||
}
|
||||
};
|
||||
|
||||
class SkPeanutButterAndJelly : public SkSandwich {
|
||||
private:
|
||||
void onAssemble() override {
|
||||
this->addIngredient(kPeanutButter_Ingredient);
|
||||
this->addIngredient(kGrapeJelly_Ingredient);
|
||||
}
|
||||
};
|
||||
~~~~
|
||||
|
||||
Integer Types
|
||||
-------------
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user