Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] operator bool() must always be explicit
Browse files Browse the repository at this point in the history
Otherwise, it can participate in unexpected conversions. Case in point: GlyphSet::insert was comparing the result of GlyphMetrics::operator bool() where it wanted to use operator==.
  • Loading branch information
jfirebaugh committed Oct 29, 2016
1 parent ee32357 commit a4c82b8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/mbgl/util/size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Size {
constexpr Size(const uint32_t width_, const uint32_t height_) : width(width_), height(height_) {
}

constexpr operator bool() const {
constexpr explicit operator bool() const {
return width > 0 && height > 0;
}

Expand Down
14 changes: 11 additions & 3 deletions src/mbgl/text/glyph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace mbgl {
GlyphRange getGlyphRange(char32_t glyph);

struct GlyphMetrics {
operator bool() const {
explicit operator bool() const {
return !(width == 0 && height == 0 && advance == 0);
}

Expand All @@ -27,12 +27,20 @@ struct GlyphMetrics {

};

inline bool operator==(const GlyphMetrics& lhs, const GlyphMetrics& rhs) {
return lhs.width == rhs.width &&
lhs.height == rhs.height &&
lhs.left == rhs.left &&
lhs.top == rhs.top &&
lhs.advance == rhs.advance;
}

struct Glyph {
explicit Glyph() : rect(0, 0, 0, 0), metrics() {}
explicit Glyph(Rect<uint16_t> rect_, GlyphMetrics metrics_)
: rect(std::move(rect_)), metrics(std::move(metrics_)) {}

operator bool() const {
explicit operator bool() const {
return metrics || rect.hasArea();
}

Expand Down Expand Up @@ -64,7 +72,7 @@ class Shaping {
int32_t left;
int32_t right;

operator bool() const { return !positionedGlyphs.empty(); }
explicit operator bool() const { return !positionedGlyphs.empty(); }
};

class SDFGlyph {
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/text/shaping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PositionedIcon {
float left = 0;
float right = 0;

operator bool() const { return image && (*image).pos.hasArea(); }
explicit operator bool() const { return image && (*image).pos.hasArea(); }
};

PositionedIcon shapeIcon(const SpriteAtlasElement& image, const style::SymbolLayoutProperties&);
Expand Down

0 comments on commit a4c82b8

Please sign in to comment.