-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Refactor explicit operator bool usage for some cases #8601
Conversation
valid()
over explicit operator bool()
valid()
over explicit operator bool()
54ba98f
to
b411b8b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, to add some additional nuance to #8562 (comment), not all our uses of operator bool()
are implementing a validity check (though some of them definitely are). I would distinguish:
- Use of member data sentinel values to create an ad hoc implementation of an "absent" state, e.g. in
PositionedIcon
,Shaping
,Glyph
,GlyphMetrics
. These should typically be replaced with uses ofoptional<T>
and elimination of the "invalid" state from the inner type. - Types which are acting similarly to a pointer or optional type, e.g.
ExtensionFunction
,CFHandle
. These could arguably useoptional
too, but sometimes it's not convenient. Emulating a pointer by providingoperator bool()
(and usuallyoperator*
as well) is reasonable. Result
. This is in its own category because it's a fundamental type, on the level ofoptional
. Providingoperator bool()
is necessary for error-handling ergonomics.- Types which have some other special state, but where that state is considered a valid member of the type's domain. These should typically use a named method specific to the state, e.g.
TransitionOptions::present()
orPropertyValue::defined()
. (Note that the most natural name might name the complement of the special state -- all members of the type's domain other than the special state.)
@@ -34,7 +34,7 @@ class PropertyValue { | |||
const T & asConstant() const { return value.template get< T >(); } | |||
const CameraFunction<T>& asCameraFunction() const { return value.template get<CameraFunction<T>>(); } | |||
|
|||
explicit operator bool() const { return !isUndefined(); }; | |||
bool valid() const { return !isUndefined(); }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defined
or present
would be better; an undefined property value isn't invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually not used, it seems isUndefined()
suits the purpose already.
@@ -18,7 +18,7 @@ class TransitionOptions { | |||
}; | |||
} | |||
|
|||
explicit operator bool() const { | |||
bool valid() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
include/mbgl/util/geo.hpp
Outdated
@@ -48,7 +48,7 @@ class LatLng { | |||
else if (longitude < 0 && end.longitude > 0) longitude += util::DEGREES_MAX; | |||
} | |||
|
|||
explicit operator bool() const { | |||
bool valid() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#3802 tracks removing the invalid state from these classes entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'm going to defer these to #3802.
include/mbgl/util/size.hpp
Outdated
@@ -17,7 +17,7 @@ class Size { | |||
return width * height; | |||
} | |||
|
|||
constexpr explicit operator bool() const { | |||
constexpr bool valid() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverse the sense and call it empty
or zero
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deferring this to #8562.
@@ -17,7 +17,7 @@ class Result : private variant<T, Error> { | |||
public: | |||
using variant<T, Error>::variant; | |||
|
|||
explicit operator bool() const { | |||
bool valid() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a reasonable use of operator bool
and doesn't need to change. Of course, we should probably follow d7227e1 and eliminate this copy of Result
entirely.
platform/default/sqlite3.cpp
Outdated
@@ -158,8 +158,8 @@ Statement &Statement::operator=(Statement &&other) { | |||
Statement::~Statement() { | |||
} | |||
|
|||
Statement::operator bool() const { | |||
return impl.operator bool(); | |||
bool Statement::valid() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
src/mbgl/gl/extension.hpp
Outdated
@@ -18,7 +18,7 @@ class ExtensionFunction<R(Args...)> { | |||
ExtensionFunction(const ProcAddress ptr_) : ptr(ptr_) { | |||
} | |||
|
|||
explicit operator bool() const { | |||
bool valid() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a valid use of operator bool
and doesn't need to change. ExtensionFunction
acts like a function pointer.
src/mbgl/layout/symbol_instance.cpp
Outdated
@@ -23,34 +23,37 @@ SymbolInstance::SymbolInstance(Anchor& anchor, | |||
const std::size_t featureIndex_) : | |||
point(anchor.point), | |||
index(index_), | |||
hasText(shapedTextOrientations.first || shapedTextOrientations.second), | |||
hasIcon(shapedIcon), | |||
hasText(shapedTextOrientations.first.valid() || shapedTextOrientations.second.valid()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better fix here is likely to use an optional
.
src/mbgl/text/glyph.hpp
Outdated
@@ -16,7 +16,7 @@ namespace mbgl { | |||
GlyphRange getGlyphRange(char16_t glyph); | |||
|
|||
struct GlyphMetrics { | |||
explicit operator bool() const { | |||
bool valid() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If "all 0" is being used as a proxy for "no metrics available", a better change is to use optional<GlyphMetrics>
.
src/mbgl/text/glyph.hpp
Outdated
@@ -42,8 +42,8 @@ struct Glyph { | |||
explicit Glyph(Rect<uint16_t> rect_, GlyphMetrics metrics_) | |||
: rect(std::move(rect_)), metrics(std::move(metrics_)) {} | |||
|
|||
explicit operator bool() const { | |||
return metrics || rect.hasArea(); | |||
bool valid() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here -- optional<Glyph>
instead.
@jfirebaugh let's wait for #3802 and #8562 to land, so I can rebase and address the review comments in one step. |
a9c52fd
to
e677563
Compare
e677563
to
e84594d
Compare
e84594d
to
f3f7ec0
Compare
@jfirebaugh I've applied the requested changes based on your comments - thank you for the detailed explaining on these! The only exception is |
src/mbgl/text/glyph_pbf.hpp
Outdated
@@ -21,7 +21,7 @@ class SDFGlyph { | |||
AlphaImage bitmap; | |||
|
|||
// Glyph metrics | |||
GlyphMetrics metrics; | |||
optional<GlyphMetrics> metrics; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once Glyph
is optional, GlyphMetrics::operator bool()
is unused, so we don't need to make this optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have an alternate fix for glyph-related operator bool()
prepared as part of #8747, so I'll omit the last commit here and merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @jfirebaugh 👍
f3f7ec0
to
04fb715
Compare
No description provided.