Ticket #1077 (closed enhancement: fixed)

Opened 5 months ago

Last modified 4 months ago

[PATCH] Add bool operator conversion to CmpPtr

Reported by: jdm Owned by: leper
Priority: Nice to Have Milestone: Alpha 9
Component: Core engine Keywords: patch
Cc:

Description

Consider the elegance of |if (cmpVisual)| instead of |if (!cmpVisual.null())|.

Attachments

#1077-2012-01-29.patch (79.5 KB) - added by leper 4 months ago.
version with updated copyright (year)
#1077-2012-02-01.patch (80.2 KB) - added by leper 4 months ago.
Using safe bool
#1077-2012-02-04.patch (82.0 KB) - added by leper 4 months ago.
added safe bool to AtlasObject.h

Change History

comment:1 follow-up: ↓ 2 Changed 4 months ago by leper

  • Keywords patch review added
  • Summary changed from Add bool operator conversion to CmpPtr to [PATCH] Add bool operator conversion to CmpPtr

I implemented this but as I am unsure if this approach is safe (as in those this apply here http://www.artima.com/cppsource/safebool.html ?) can someone more knowledgable in C++ review this?

I removed null() completely in this patch. Maybe it would be good to leave it in but to make it cause a compiler warning.

Changed 4 months ago by leper

version with updated copyright (year)

comment:2 in reply to: ↑ 1 ; follow-up: ↓ 4 Changed 4 months ago by historic_bruno

Another argument in favor of this change: it would match the syntax we use in JS, which doesn't require checking null().

Replying to leper:

I implemented this but as I am unsure if this approach is safe (as in those this apply here http://www.artima.com/cppsource/safebool.html ?)

Yes, the concerns in that article would apply if we implemented operator bool and someone tried to do the following: if (cmpFoo == cmpBar) ... which would compile but give unintended results. If it's not too difficult, maybe we should implement safe bool? On the one hand we could say we'll review code changes and know better than to try an unsafe bool operation, but then some patches are quite long and one small bug like that could slip through and be difficult to track down.

comment:3 Changed 4 months ago by leper

  • Keywords review removed
  • Owner set to leper
  • Status changed from new to assigned

I'm working on implementing safe bool (resolved the problem of expected type specifier before bool_type by moving the private: above the public declarations).

Changed 4 months ago by leper

Using safe bool

comment:4 in reply to: ↑ 2 Changed 4 months ago by leper

Replying to historic_bruno:

If it's not too difficult, maybe we should implement safe bool? On the one hand we could say we'll review code changes and know better than to try an unsafe bool operation, but then some patches are quite long and one small bug like that could slip through and be difficult to track down.

Implemented safe bool. == and != comparisions result in a compiler error and a warning that rhs is unused, so maybe replace it with UNUSED(rhs).

comment:5 Changed 4 months ago by leper

  • Keywords patch, review added; patch removed

Just stumbled across source/tools/atlas/AtlasObject/AtlasObject.h and noticed that it overloads operator bool but isn't using safe bool. Is this worth fixing (maybe a ticket of its own)?

comment:6 Changed 4 months ago by historic_bruno

Thanks for responding so quickly :) It looks great to me, and I agree about AtlasObject, you could even fix it in this patch if you want, to avoid the wait of having another one reviewed.

Changed 4 months ago by leper

added safe bool to AtlasObject.h

comment:7 Changed 4 months ago by leper

No problem ;) Fixed AtlasObject. I also did a quick grep over the source tree and couldn't find any matches for operator bool or operator! in the sources (except some in the libraries but I assume they are of no concern).

Last edited 4 months ago by leper (previous) (diff)

comment:8 Changed 4 months ago by ben

  • Status changed from assigned to closed
  • Resolution set to fixed

In [11036]:

Adds safe bool operator to ICmpPtr, replacing the null() method, based on patch by leper. Also changes bool operator in AtSmartPtr? to safe bool. Fixes #1077.
Changes some CmpPtr? variable names for consistency.

comment:9 Changed 4 months ago by historic_bruno

  • Keywords patch added; patch, review removed
  • Milestone changed from Backlog to Alpha 9

Patch committed, thanks!

Note: See TracTickets for help on using tickets.