Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Issue 2274863002: sk_at_scope_end (Closed)

Created:
4 years, 4 months ago by mtklein_C
Modified:
4 years, 3 months ago
CC:
reviews_skia.org
Base URL:
https://skia.googlesource.com/skia.git@master
Target Ref:
refs/heads/master
Project:
skia
Visibility:
Public.

Description

sk_at_scope_end The scoped WebP cleanup in https://codereview.chromium.org/2269333002/ got me thinking we're missing this handy tool. We've got std::unique_ptr and its variants (SkAutoTCallVProc, etc.) and of course all the SkAutoThisAndThat, but they're all tied to pointers or particular use cases, and none are quite so pithy. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2274863002

Patch Set 1 #

Patch Set 2 : SK_UNUSED #

Patch Set 3 : use it! #

Total comments: 10
Unified diffs Side-by-side diffs Delta from patch set Stats (+70 lines, -2 lines) Patch
M include/core/SkPostConfig.h View 1 1 chunk +5 lines, -1 line 2 comments Download
M include/private/SkTemplates.h View 1 1 chunk +40 lines, -0 lines 4 comments Download
M src/codec/SkWebpCodec.cpp View 1 2 1 chunk +1 line, -1 line 4 comments Download
A tests/ScopedTest.cpp View 1 chunk +24 lines, -0 lines 0 comments Download

Messages

Total messages: 23 (16 generated)
mtklein_C
4 years, 4 months ago (2016-08-24 14:24:18 UTC) #10
msarett
Any interest in trying this out on the webp code now that it has landed?
4 years, 4 months ago (2016-08-24 14:37:12 UTC) #11
mtklein_C
On 2016/08/24 at 14:37:12, msarett wrote: > Any interest in trying this out on the ...
4 years, 4 months ago (2016-08-24 14:52:02 UTC) #14
bungeman-skia
https://codereview.chromium.org/2274863002/diff/40001/include/core/SkPostConfig.h File include/core/SkPostConfig.h (right): https://codereview.chromium.org/2274863002/diff/40001/include/core/SkPostConfig.h#newcode249 include/core/SkPostConfig.h:249: #define SK_UNUSED __pragma(warning(suppress:4189)) Won't this suppress the warning for ...
4 years, 4 months ago (2016-08-24 15:18:17 UTC) #17
mtklein
https://codereview.chromium.org/2274863002/diff/40001/include/core/SkPostConfig.h File include/core/SkPostConfig.h (right): https://codereview.chromium.org/2274863002/diff/40001/include/core/SkPostConfig.h#newcode249 include/core/SkPostConfig.h:249: #define SK_UNUSED __pragma(warning(suppress:4189)) On 2016/08/24 15:18:17, bungeman-skia wrote: > ...
4 years, 4 months ago (2016-08-24 16:52:58 UTC) #21
msarett
https://codereview.chromium.org/2274863002/diff/40001/src/codec/SkWebpCodec.cpp File src/codec/SkWebpCodec.cpp (right): https://codereview.chromium.org/2274863002/diff/40001/src/codec/SkWebpCodec.cpp#newcode113 src/codec/SkWebpCodec.cpp:113: if (demux && WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) { On ...
4 years, 4 months ago (2016-08-24 17:01:25 UTC) #22
bungeman-skia
4 years, 4 months ago (2016-08-24 21:39:49 UTC) #23
https://codereview.chromium.org/2274863002/diff/40001/include/private/SkTempl...
File include/private/SkTemplates.h (right):

https://codereview.chromium.org/2274863002/diff/40001/include/private/SkTempl...
include/private/SkTemplates.h:506: class SkScoped {
As far as naming goes, the current proposal (p0052) seems to lean toward
scope_exit / make_scope_exit. I think the original ScopeGuard class used
AT_SCOPE_EXIT as the name of the define.

https://codereview.chromium.org/2274863002/diff/40001/include/private/SkTempl...
include/private/SkTemplates.h:527: #define SCOPED_NAME(n) SCOPED_NAMEx(n)
These names seem a bit generic for something that will end up in other peoples
translation units. Seems they should be SK_ prefixed, or use
SK_MACRO_APPEND_LINE (or the like) instead.

https://codereview.chromium.org/2274863002/diff/40001/include/private/SkTempl...
include/private/SkTemplates.h:529: #define sk_at_scope_end(stmt) \
You evil person making this define look like a function call.

https://codereview.chromium.org/2274863002/diff/40001/include/private/SkTempl...
include/private/SkTemplates.h:530: SK_UNUSED auto&& SCOPED_NAME(__COUNTER__) =
sk_make_scoped([&] { stmt; })
Looks like we've been using SK_MACRO_APPEND_LINE for this sort of thing.
__COUNTER__ is of course cooler than __LINE__, but I don't think there's any
reason SK_MACRO_APPEND_LINE shouldn't use __COUNTER__.

https://codereview.chromium.org/2274863002/diff/40001/src/codec/SkWebpCodec.cpp
File src/codec/SkWebpCodec.cpp (right):

https://codereview.chromium.org/2274863002/diff/40001/src/codec/SkWebpCodec.c...
src/codec/SkWebpCodec.cpp:111: WebPChunkIterator chunkIterator;
Now that we've established webp has done a silly thing here, this seems more
like it wants unique_resource from p0052 instead of scope_exit. In other words,
it would be nice if the type itself managed the lifetime, instead of some
external guard. Something like

unique_resource<WebPChunkIterator, [](WebPChunkIterator&
r){WebPDemuxReleaseChunkIterator(&r)}> chunkIterator;

Ok, so maybe that doesn't look quite as nice, but that's mostly because we want
a value type instead of a handle type. A generic adapter for that sort of thing
could look like

template <typename T, typename D> using unique_value = unique_resource<T, [](T&
t){D(&t)}>;
unique_value<WebPChunkIterator, WebPDemuxReleaseChunkIterator> chunkIterator;

Note that all these variations here should turn into the same machine code, but
with something like 'unique_value' I can see at a glance from the type that this
is a scoped value of a given type and a specified thing will run on it when it
goes out of scope. Spreading this out over two statements seems like more mental
overhead.

Powered by Google App Engine
This is Rietveld 408576698