From 0314a46ee411b97810d49908ab110bbef049e7b7 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 16 Mar 2016 17:45:00 -0700 Subject: [core] Tighten LatLng and other geo.hpp classes * Remove LatLng::null and enforce invariants * Remove unnecessary operator bool() --- include/mbgl/util/geo.hpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'include/mbgl/util/geo.hpp') diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp index 2cc9297aae..d0d0518c2a 100644 --- a/include/mbgl/util/geo.hpp +++ b/include/mbgl/util/geo.hpp @@ -9,6 +9,7 @@ #include #include +#include namespace mbgl { @@ -21,17 +22,29 @@ using ScreenBox = mapbox::geometry::box; class LatLng { public: - struct null {}; - double latitude; double longitude; enum WrapMode : bool { Unwrapped, Wrapped }; - LatLng(null) : latitude(std::numeric_limits::quiet_NaN()), longitude(latitude) {} - LatLng(double lat = 0, double lon = 0, WrapMode mode = Unwrapped) - : latitude(lat), longitude(lon) { if (mode == Wrapped) wrap(); } + : latitude(lat), longitude(lon) { + if (std::isnan(lat)) { + throw std::domain_error("latitude must not be NaN"); + } + if (std::isnan(lon)) { + throw std::domain_error("longitude must not be NaN"); + } + if (std::abs(lat) > 90.0) { + throw std::domain_error("latitude must be between -90 and 90"); + } + if (!std::isfinite(lon)) { + throw std::domain_error("longitude must not be infinite"); + } + if (mode == Wrapped) { + wrap(); + } + } LatLng wrapped() const { return { latitude, longitude, Wrapped }; } @@ -48,10 +61,6 @@ public: else if (longitude < 0 && end.longitude > 0) longitude += util::DEGREES_MAX; } - explicit operator bool() const { - return !(std::isnan(latitude) || std::isnan(longitude)); - } - // Constructs a LatLng object with the top left position of the specified tile. LatLng(const CanonicalTileID& id); LatLng(const UnwrappedTileID& id); @@ -72,10 +81,6 @@ public: ProjectedMeters(double n = 0, double e = 0) : northing(n), easting(e) {} - - explicit operator bool() const { - return !(std::isnan(northing) || std::isnan(easting)); - } }; constexpr bool operator==(const ProjectedMeters& a, const ProjectedMeters& b) { @@ -197,10 +202,6 @@ public: EdgeInsets(const double t, const double l, const double b, const double r) : top(t), left(l), bottom(b), right(r) {} - explicit operator bool() const { - return !(std::isnan(top) || std::isnan(left) || std::isnan(bottom) || std::isnan(right)) - && (top || left || bottom || right); - } void operator+=(const EdgeInsets& o) { top += o.top; -- cgit v1.2.1