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

Refactor explicit operator bool usage for some cases #8601

Merged
merged 4 commits into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/mbgl/style/property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ 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(); };

template <typename Evaluator>
auto evaluate(const Evaluator& evaluator) const {
return Value::visit(value, evaluator);
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/style/transition_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TransitionOptions {
};
}

explicit operator bool() const {
bool isDefined() const {
return duration || delay;
}
};
Expand Down
111 changes: 70 additions & 41 deletions include/mbgl/util/geo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,42 @@ class LatLng {
LatLng(const CanonicalTileID& id);
LatLng(const UnwrappedTileID& id);

friend constexpr bool operator==(const LatLng& a, const LatLng& b) {
friend bool operator==(const LatLng& a, const LatLng& b) {
return a.lat == b.lat && a.lon == b.lon;
}

friend constexpr bool operator!=(const LatLng& a, const LatLng& b) {
friend bool operator!=(const LatLng& a, const LatLng& b) {
return !(a == b);
}
};

class ProjectedMeters {
private:
double _northing; // Distance measured northwards.
double _easting; // Distance measured eastwards.

public:
double northing;
double easting;
ProjectedMeters(double n_ = 0, double e_ = 0)
: _northing(n_), _easting(e_) {
if (std::isnan(_northing)) {
throw std::domain_error("northing must not be NaN");
}
if (std::isnan(_easting)) {
throw std::domain_error("easting must not be NaN");
}
}

ProjectedMeters(double n = 0, double e = 0)
: northing(n), easting(e) {}
};
double northing() const { return _northing; }
double easting() const { return _easting; }

constexpr bool operator==(const ProjectedMeters& a, const ProjectedMeters& b) {
return a.northing == b.northing && a.easting == b.easting;
}
friend bool operator==(const ProjectedMeters& a, const ProjectedMeters& b) {
return a._northing == b._northing && a._easting == b._easting;
}

friend bool operator!=(const ProjectedMeters& a, const ProjectedMeters& b) {
return !(a == b);
}
};

class LatLngBounds {
public:
Expand Down Expand Up @@ -188,17 +203,14 @@ class LatLngBounds {
LatLngBounds(LatLng sw_, LatLng ne_)
: sw(std::move(sw_)), ne(std::move(ne_)) {}

friend constexpr bool operator==(const LatLngBounds&, const LatLngBounds&);
friend constexpr bool operator!=(const LatLngBounds&, const LatLngBounds&);
};

constexpr bool operator==(const LatLngBounds& a, const LatLngBounds& b) {
return a.sw == b.sw && a.ne == b.ne;
}
friend bool operator==(const LatLngBounds& a, const LatLngBounds& b) {
return a.sw == b.sw && a.ne == b.ne;
}

constexpr bool operator!=(const LatLngBounds& a, const LatLngBounds& b) {
return !(a == b);
}
friend bool operator!=(const LatLngBounds& a, const LatLngBounds& b) {
return !(a == b);
}
};

// Determines the orientation of the map.
enum class NorthOrientation : uint8_t {
Expand All @@ -210,43 +222,60 @@ enum class NorthOrientation : uint8_t {

/// The distance on each side between a rectangle and a rectangle within.
class EdgeInsets {
public:
double top = 0; // Number of pixels inset from the top edge.
double left = 0; // Number of pixels inset from the left edge.
double bottom = 0; // Number of pixels inset from the bottom edge.
double right = 0; // Number of pixels inset from the right edge.
private:
double _top; // Number of pixels inset from the top edge.
double _left; // Number of pixels inset from the left edge.
double _bottom; // Number of pixels inset from the bottom edge.
double _right; // Number of pixels inset from the right edge.

EdgeInsets() {}
public:
EdgeInsets(double t_ = 0, double l_ = 0, double b_ = 0, double r_ = 0)
: _top(t_), _left(l_), _bottom(b_), _right(r_) {
if (std::isnan(_top)) {
throw std::domain_error("top must not be NaN");
}
if (std::isnan(_left)) {
throw std::domain_error("left must not be NaN");
}
if (std::isnan(_bottom)) {
throw std::domain_error("bottom must not be NaN");
}
if (std::isnan(_right)) {
throw std::domain_error("right must not be NaN");
}
}

EdgeInsets(const double t, const double l, const double b, const double r)
: top(t), left(l), bottom(b), right(r) {}
double top() const { return _top; }
double left() const { return _left; }
double bottom() const { return _bottom; }
double right() const { return _right; }

bool isFlush() const {
return top == 0 && left == 0 && bottom == 0 && right == 0;
return _top == 0 && _left == 0 && _bottom == 0 && _right == 0;
}

void operator+=(const EdgeInsets& o) {
top += o.top;
left += o.left;
bottom += o.bottom;
right += o.right;
_top += o._top;
_left += o._left;
_bottom += o._bottom;
_right += o._right;
}

EdgeInsets operator+(const EdgeInsets& o) const {
return {
top + o.top, left + o.left, bottom + o.bottom, right + o.right,
_top + o._top, _left + o._left, _bottom + o._bottom, _right + o._right,
};
}

ScreenCoordinate getCenter(uint16_t width, uint16_t height) const;
};

constexpr bool operator==(const EdgeInsets& a, const EdgeInsets& b) {
return a.top == b.top && a.left == b.left && a.bottom == b.bottom && a.right == b.right;
}
friend bool operator==(const EdgeInsets& a, const EdgeInsets& b) {
return a._top == b._top && a._left == b._left && a._bottom == b._bottom && a._right == b._right;
}

constexpr bool operator!=(const EdgeInsets& a, const EdgeInsets& b) {
return !(a == b);
}
friend bool operator!=(const EdgeInsets& a, const EdgeInsets& b) {
return !(a == b);
}
};

} // namespace mbgl
4 changes: 2 additions & 2 deletions include/mbgl/util/projection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Projection {
}

static LatLng latLngForProjectedMeters(const ProjectedMeters& projectedMeters) {
double latitude = (2 * std::atan(std::exp(projectedMeters.northing / util::EARTH_RADIUS_M)) - (M_PI / 2.0)) * util::RAD2DEG;
double longitude = projectedMeters.easting * util::RAD2DEG / util::EARTH_RADIUS_M;
double latitude = (2 * std::atan(std::exp(projectedMeters.northing() / util::EARTH_RADIUS_M)) - (M_PI / 2.0)) * util::RAD2DEG;
double longitude = projectedMeters.easting() * util::RAD2DEG / util::EARTH_RADIUS_M;

latitude = util::clamp(latitude, -util::LATITUDE_MAX, util::LATITUDE_MAX);
longitude = util::clamp(longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX);
Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ jni::jdouble NativeMapView::getMetersPerPixelAtLatitude(JNIEnv&, jni::jdouble la

jni::Object<ProjectedMeters> NativeMapView::projectedMetersForLatLng(JNIEnv& env, jni::jdouble latitude, jni::jdouble longitude) {
mbgl::ProjectedMeters projectedMeters = map->projectedMetersForLatLng(mbgl::LatLng(latitude, longitude));
return ProjectedMeters::New(env, projectedMeters.northing, projectedMeters.easting);
return ProjectedMeters::New(env, projectedMeters.northing(), projectedMeters.easting());
}

jni::Object<PointF> NativeMapView::pixelForLatLng(JNIEnv& env, jdouble latitude, jdouble longitude) {
Expand Down
8 changes: 4 additions & 4 deletions platform/darwin/src/MGLMapCamera.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ + (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCo

mbgl::ProjectedMeters centerMeters = mbgl::Projection::projectedMetersForLatLng(centerLatLng);
mbgl::ProjectedMeters eyeMeters = mbgl::Projection::projectedMetersForLatLng(eyeLatLng);
heading = std::atan((centerMeters.northing - eyeMeters.northing) /
(centerMeters.easting - eyeMeters.easting));
heading = std::atan((centerMeters.northing() - eyeMeters.northing()) /
(centerMeters.easting() - eyeMeters.easting()));

double groundDistance = std::hypot(centerMeters.northing - eyeMeters.northing,
centerMeters.easting - eyeMeters.easting);
double groundDistance = std::hypot(centerMeters.northing() - eyeMeters.northing(),
centerMeters.easting() - eyeMeters.easting());
pitch = std::atan(eyeAltitude / groundDistance);
}

Expand Down
8 changes: 0 additions & 8 deletions platform/default/sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ Database &Database::operator=(Database &&other) {
Database::~Database() {
}

Database::operator bool() const {
return impl.operator bool();
}

void Database::setBusyTimeout(std::chrono::milliseconds timeout) {
assert(impl);
const int err = sqlite3_busy_timeout(impl->db,
Expand Down Expand Up @@ -158,10 +154,6 @@ Statement &Statement::operator=(Statement &&other) {
Statement::~Statement() {
}

Statement::operator bool() const {
return impl.operator bool();
}

template <> void Statement::bind(int offset, std::nullptr_t) {
assert(impl);
impl->check(sqlite3_bind_null(impl->stmt, offset));
Expand Down
4 changes: 0 additions & 4 deletions platform/default/sqlite3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class Database {
~Database();
Database &operator=(Database &&);

explicit operator bool() const;

void setBusyTimeout(std::chrono::milliseconds);
void exec(const std::string &sql);
Statement prepare(const char *query);
Expand All @@ -69,8 +67,6 @@ class Statement {
~Statement();
Statement &operator=(Statement &&);

explicit operator bool() const;

template <typename T> void bind(int offset, T value);

// Text
Expand Down
4 changes: 2 additions & 2 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4559,8 +4559,8 @@ - (CLLocationDirection)directionByFollowingWithCourse
mbgl::LatLng targetLatLng = MGLLatLngFromLocationCoordinate2D(self.targetCoordinate);
mbgl::ProjectedMeters userMeters = mbgl::Projection::projectedMetersForLatLng(userLatLng);
mbgl::ProjectedMeters targetMeters = mbgl::Projection::projectedMetersForLatLng(targetLatLng);
double angle = atan2(targetMeters.easting - userMeters.easting,
targetMeters.northing - userMeters.northing);
double angle = atan2(targetMeters.easting() - userMeters.easting(),
targetMeters.northing() - userMeters.northing());
direction = mbgl::util::wrap(MGLDegreesFromRadians(angle), 0., 360.);
}
else
Expand Down
10 changes: 5 additions & 5 deletions platform/qt/src/qmapboxgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ double QMapboxGL::metersPerPixelAtLatitude(double latitude, double zoom) const
QMapbox::ProjectedMeters QMapboxGL::projectedMetersForCoordinate(const QMapbox::Coordinate &coordinate_) const
{
auto projectedMeters = d_ptr->mapObj->projectedMetersForLatLng(mbgl::LatLng { coordinate_.first, coordinate_.second });
return QMapbox::ProjectedMeters(projectedMeters.northing, projectedMeters.easting);
return QMapbox::ProjectedMeters(projectedMeters.northing(), projectedMeters.easting());
}

/*!
Expand Down Expand Up @@ -1229,10 +1229,10 @@ void QMapboxGL::setMargins(const QMargins &margins_)
QMargins QMapboxGL::margins() const
{
return QMargins(
d_ptr->margins.left,
d_ptr->margins.top,
d_ptr->margins.right,
d_ptr->margins.bottom
d_ptr->margins.left(),
d_ptr->margins.top(),
d_ptr->margins.right(),
d_ptr->margins.bottom()
);
}

Expand Down
9 changes: 0 additions & 9 deletions platform/qt/src/sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ Database &Database::operator=(Database &&other) {
Database::~Database() {
}

Database::operator bool() const {
return impl.operator bool();
}

void Database::setBusyTimeout(std::chrono::milliseconds timeout) {
assert(impl);
std::string timeoutStr = mbgl::util::toString(timeout.count());
Expand Down Expand Up @@ -191,11 +187,6 @@ Statement &Statement::operator=(Statement &&other) {
Statement::~Statement() {
}

Statement::operator bool() const {
assert(impl);
return impl.operator bool();
}

template void Statement::bind(int, int64_t);

template <typename T>
Expand Down
9 changes: 2 additions & 7 deletions src/mbgl/layout/symbol_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace style;
SymbolInstance::SymbolInstance(Anchor& anchor,
const GeometryCoordinates& line,
const std::pair<Shaping, Shaping>& shapedTextOrientations,
const PositionedIcon& shapedIcon,
optional<PositionedIcon> shapedIcon,
const SymbolLayoutProperties::Evaluated& layout,
const float layoutTextSize,
const bool addToBuffers,
Expand All @@ -27,18 +27,15 @@ SymbolInstance::SymbolInstance(Anchor& anchor,
hasText(shapedTextOrientations.first || shapedTextOrientations.second),
hasIcon(shapedIcon),


// Create the collision features that will be used to check whether this symbol instance can be placed
textCollisionFeature(line, anchor, shapedTextOrientations.second ?: shapedTextOrientations.first, textBoxScale, textPadding, textPlacement, indexedFeature),
iconCollisionFeature(line, anchor, shapedIcon, iconBoxScale, iconPadding, iconPlacement, indexedFeature),
featureIndex(featureIndex_) {



// Create the quads used for rendering the icon and glyphs.
if (addToBuffers) {
if (shapedIcon) {
iconQuad = getIconQuad(anchor, shapedIcon, line, layout, layoutTextSize, iconPlacement, shapedTextOrientations.first);
iconQuad = getIconQuad(anchor, *shapedIcon, line, layout, layoutTextSize, iconPlacement, shapedTextOrientations.first);
}
if (shapedTextOrientations.first) {
auto quads = getGlyphQuads(anchor, shapedTextOrientations.first, textBoxScale, line, layout, textPlacement, face);
Expand All @@ -59,8 +56,6 @@ SymbolInstance::SymbolInstance(Anchor& anchor,
} else {
writingModes = WritingModeType::None;
}


}

} // namespace mbgl
2 changes: 1 addition & 1 deletion src/mbgl/layout/symbol_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SymbolInstance {
SymbolInstance(Anchor& anchor,
const GeometryCoordinates& line,
const std::pair<Shaping, Shaping>& shapedTextOrientations,
const PositionedIcon& shapedIcon,
optional<PositionedIcon> shapedIcon,
const style::SymbolLayoutProperties::Evaluated&,
const float layoutTextSize,
const bool inside,
Expand Down
10 changes: 5 additions & 5 deletions src/mbgl/layout/symbol_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void SymbolLayout::prepare(const GlyphPositionMap& glyphs, const IconAtlasMap& i
if (feature.geometry.empty()) continue;

std::pair<Shaping, Shaping> shapedTextOrientations;
PositionedIcon shapedIcon;
optional<PositionedIcon> shapedIcon;
GlyphPositions face;

// if feature has text, shape the text
Expand Down Expand Up @@ -262,7 +262,7 @@ void SymbolLayout::prepare(const GlyphPositionMap& glyphs, const IconAtlasMap& i
if (icons != iconMap.end()) {
auto image = icons->second.find(*feature.icon);
if (image != icons->second.end()) {
shapedIcon = shapeIcon(image->second,
shapedIcon = PositionedIcon::shapeIcon(image->second,
layout.evaluate<IconOffset>(zoom, feature),
layout.evaluate<IconRotate>(zoom, feature) * util::DEG2RAD);
if (image->second.sdf) {
Expand Down Expand Up @@ -292,7 +292,7 @@ void SymbolLayout::prepare(const GlyphPositionMap& glyphs, const IconAtlasMap& i
void SymbolLayout::addFeature(const std::size_t index,
const SymbolFeature& feature,
const std::pair<Shaping, Shaping>& shapedTextOrientations,
const PositionedIcon& shapedIcon,
optional<PositionedIcon> shapedIcon,
const GlyphPositions& glyphs) {
const float minScale = 0.5f;
const float glyphSize = 24.0f;
Expand Down Expand Up @@ -363,8 +363,8 @@ void SymbolLayout::addFeature(const std::size_t index,
textMaxAngle,
(shapedTextOrientations.second ?: shapedTextOrientations.first).left,
(shapedTextOrientations.second ?: shapedTextOrientations.first).right,
shapedIcon.left,
shapedIcon.right,
(shapedIcon ? shapedIcon->left() : 0),
(shapedIcon ? shapedIcon->right() : 0),
glyphSize,
textMaxBoxScale,
overscaling);
Expand Down
Loading