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

Issue 1492413002: ABANDONED CL: Adding a compile-time safe base::IdType<...> into //base. (Closed)

Created:
5 years ago by Łukasz Anforowicz
Modified:
4 years, 10 months ago
Reviewers:
danakj
CC:
chromium-reviews, vmpstr+watch_chromium.org, jam, dcheng, piman
Base URL:
https://chromium.googlesource.com/chromium/src.git@master
Target Ref:
refs/pending/heads/master
Project:
chromium
Visibility:
Public.

Description

ABANDONED CL: This CL is replaced by crrev.com/1548443002 ------------------------ Adding a compile-time safe base::IdType<...> into //base. base::IdType is a generalization of cc::SurfaceId. base::IdType provides a type that for most practical purposes behaves like an integer, but provides extra compile-time safety, by disallowing implicit coercions from raw integers and/or from other, unrelated base::IdTypes. For more information see the comments in base/id_type.h. Implementation of SurfaceId is being converted to reuse base::IdType<...> in crrev.com/1496103002. save_package_id is being converted from a raw integer to base::IdType<...> in crrev.com/1492283004. BUG=565545 .

Patch Set 1 #

Unified diffs Side-by-side diffs Delta from patch set Stats (+290 lines, -0 lines) Patch
M base/BUILD.gn View 1 chunk +1 line, -0 lines 0 comments Download
M base/base.gyp View 1 chunk +1 line, -0 lines 0 comments Download
M base/base.gypi View 1 chunk +1 line, -0 lines 0 comments Download
A base/id_type.h View 1 chunk +144 lines, -0 lines 0 comments Download
A base/id_type_unittest.cc View 1 chunk +143 lines, -0 lines 0 comments Download

Dependent Patchsets:

Messages

Total messages: 8 (5 generated)
Łukasz Anforowicz
danakj@, could you please take a look? (full disclosure - the CL under review has ...
5 years ago (2015-12-04 22:04:41 UTC) #4
danakj
I'm not sold that this is sometihng we need in base. For the vast majority ...
5 years ago (2015-12-07 19:24:49 UTC) #5
Łukasz Anforowicz
5 years ago (2015-12-08 18:07:08 UTC) #6
Let me try to enumerate the possible requirements (I guess all of them are
negotiable):

R1: Passing foo_id as an argument of a function expecting bar_id shouldn't
compile.

R2: Passing foo_id as a key of a map expecting bar_id shouldn't compile.

R3: Parameter declaration should say FooId rather than int.

R4: Map declaration should say that FooId is a key (rather than int).

R5: Low mental overhead when reading / writing id related code.

R3 and R4 are readability improvement.
R1 and R2 improve confidence in code correctness (i.e. when refactoring).
None of the requirements are critical for code health, but I think that they are
definitely desirable
and the main question is how to achieve them with low overhead (readability
overhead / maintenance overhead / perf overhead).

The typedef suggestion addresses R3 and R4 but fails to address R1 and R2.
R5 is ok from simplicity perspective (it is just a simple int), but is not so
good when having
to read/write functions taking multiple ids (i.e. have to be careful to pass
them in the right order).

struct FooId { int id; } addresses R1 and R3, but fails to address R2 and R4:
- If I keep struct definition small, then I have to declare std::map<int, ...>
  (not meeting R2 and R4) and use id.id when interacting with the map (making R5
not so good).
- Defining == and either < or std::hash helps with R2 and R4, but makes the code
more
  complicated (again making R5 not so good).

On 2015/12/07 19:24:49, danakj (behind on reviews) wrote:
> I'm not sold that this is sometihng we need in base.
> 
> For the vast majority of of ids in chromium, a typedef is more than enough.
It's
> nice to have a type name other than int, but the extra type-safety is kinda
> overkill.

Could you help me understand why you think that this is an "overkill"?
Do you think that type safety is not that desirable here?
Or that the cost of getting type safety is too high?

> For some ids (I can think of only one), that we plumb around through a lot of
> layers of code, I've appreciated type-safe semantics. I don't know why it's
not
> enough to just make a struct for those specific Ids, like:
> 
> struct MyId {
>   int id;
> };

I think that once one has to define == and < or std::hash, the cost of
individual MyId structs is higher than the cost of base::IdType<...>.
Also - at the top of my reply I try to explain why the struct doesn't quite meet
the requirements (well, it kind of does, but not quite :-).

I would really like to be able to use a type-safe SavePackageId and SaveItemId
under content/browser/downloads.  I am hesitant to introduce 2 structs and
define
equality and std::hash for both of them - this would feel like quite a bit of
duplicated
code (and would be costly from the perspective of having to maintain the same
code
in multiple places).

Powered by Google App Engine
This is Rietveld 408576698