ad_map_access
Longitude.hpp
Go to the documentation of this file.
1 /*
2  * ----------------- BEGIN LICENSE BLOCK ---------------------------------
3  *
4  * Copyright (C) 2018-2020 Intel Corporation
5  *
6  * SPDX-License-Identifier: MIT
7  *
8  * ----------------- END LICENSE BLOCK -----------------------------------
9  */
10 
18 #pragma once
19 
20 #include <cmath>
21 #include <iostream>
22 #include <limits>
23 #include <sstream>
24 #include <stdexcept>
25 #include "spdlog/fmt/ostr.h"
26 #include "spdlog/spdlog.h"
30 namespace ad {
34 namespace map {
40 namespace point {
41 
45 #define AD_MAP_POINT_LONGITUDE_THROWS_EXCEPTION 1
46 
47 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
48 
51 #define _AD_MAP_POINT_LONGITUDE_EXPLICIT_CONVERSION_ explicit
52 #else
53 
56 #define _AD_MAP_POINT_LONGITUDE_EXPLICIT_CONVERSION_
57 #endif
58 
65 class Longitude
66 {
67 public:
71  static const double cMinValue;
72 
76  static const double cMaxValue;
77 
82  static const double cPrecisionValue;
83 
91  : mLongitude(std::numeric_limits<double>::quiet_NaN())
92  {
93  }
94 
101  : mLongitude(iLongitude)
102  {
103  }
104 
108  Longitude(const Longitude &other) = default;
109 
113  Longitude(Longitude &&other) = default;
114 
122  Longitude &operator=(const Longitude &other) = default;
123 
131  Longitude &operator=(Longitude &&other) = default;
132 
140  bool operator==(const Longitude &other) const
141  {
142  ensureValid();
143  other.ensureValid();
144  return std::fabs(mLongitude - other.mLongitude) < cPrecisionValue;
145  }
146 
154  bool operator!=(const Longitude &other) const
155  {
156  return !operator==(other);
157  }
158 
168  bool operator>(const Longitude &other) const
169  {
170  ensureValid();
171  other.ensureValid();
172  return (mLongitude > other.mLongitude) && operator!=(other);
173  }
174 
184  bool operator<(const Longitude &other) const
185  {
186  ensureValid();
187  other.ensureValid();
188  return (mLongitude < other.mLongitude) && operator!=(other);
189  }
190 
200  bool operator>=(const Longitude &other) const
201  {
202  ensureValid();
203  other.ensureValid();
204  return ((mLongitude > other.mLongitude) || operator==(other));
205  }
206 
216  bool operator<=(const Longitude &other) const
217  {
218  ensureValid();
219  other.ensureValid();
220  return ((mLongitude < other.mLongitude) || operator==(other));
221  }
222 
233  Longitude operator+(const Longitude &other) const
234  {
235  ensureValid();
236  other.ensureValid();
237  Longitude const result(mLongitude + other.mLongitude);
238  result.ensureValid();
239  return result;
240  }
241 
253  {
254  ensureValid();
255  other.ensureValid();
256  mLongitude += other.mLongitude;
257  ensureValid();
258  return *this;
259  }
260 
271  Longitude operator-(const Longitude &other) const
272  {
273  ensureValid();
274  other.ensureValid();
275  Longitude const result(mLongitude - other.mLongitude);
276  result.ensureValid();
277  return result;
278  }
279 
291  {
292  ensureValid();
293  other.ensureValid();
294  mLongitude -= other.mLongitude;
295  ensureValid();
296  return *this;
297  }
298 
309  Longitude operator*(const double &scalar) const
310  {
311  ensureValid();
312  Longitude const result(mLongitude * scalar);
313  result.ensureValid();
314  return result;
315  }
316 
327  Longitude operator/(const double &scalar) const
328  {
329  Longitude const scalarLongitude(scalar);
330  Longitude const result(operator/(scalarLongitude));
331  result.ensureValid();
332  return result;
333  }
334 
346  double operator/(const Longitude &other) const
347  {
348  ensureValid();
349  other.ensureValidNonZero();
350  double const result = mLongitude / other.mLongitude;
351  return result;
352  }
353 
363  {
364  ensureValid();
365  Longitude const result(-mLongitude);
366  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
367  return result;
368  }
369 
377  {
378  return mLongitude;
379  }
380 
388  bool isValid() const
389  {
390  auto const valueClass = std::fpclassify(mLongitude);
391  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mLongitude)
392  && (mLongitude <= cMaxValue);
393  }
394 
401  void ensureValid() const
402  {
403  if (!isValid())
404  {
405  spdlog::info("ensureValid(::ad::map::point::Longitude)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
406 #if (AD_MAP_POINT_LONGITUDE_THROWS_EXCEPTION == 1)
407  throw std::out_of_range("Longitude value out of range"); // LCOV_EXCL_BR_LINE
408 #endif
409  }
410  }
411 
418  void ensureValidNonZero() const
419  {
420  ensureValid();
421  if (operator==(Longitude(0.))) // LCOV_EXCL_BR_LINE
422  {
423  spdlog::info("ensureValid(::ad::map::point::Longitude)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
424 #if (AD_MAP_POINT_LONGITUDE_THROWS_EXCEPTION == 1)
425  throw std::out_of_range("Longitude value is zero"); // LCOV_EXCL_BR_LINE
426 #endif
427  }
428  }
429 
433  static Longitude getMin()
434  {
435  return Longitude(cMinValue);
436  }
437 
441  static Longitude getMax()
442  {
443  return Longitude(cMaxValue);
444  }
445 
450  {
451  return Longitude(cPrecisionValue);
452  }
453 
454 private:
458  double mLongitude;
459 };
460 
461 } // namespace point
462 } // namespace map
463 } // namespace ad
475 inline ::ad::map::point::Longitude operator*(const double &other, ::ad::map::point::Longitude const &value)
476 {
477  return value.operator*(other);
478 }
479 
483 namespace std {
484 
488 inline ::ad::map::point::Longitude fabs(const ::ad::map::point::Longitude other)
489 {
490  ::ad::map::point::Longitude const result(std::fabs(static_cast<double>(other)));
491  return result;
492 }
493 
502 template <> class numeric_limits<::ad::map::point::Longitude> : public numeric_limits<double>
503 {
504 public:
508  static inline ::ad::map::point::Longitude lowest()
509  {
510  return ::ad::map::point::Longitude::getMin();
511  }
515  static inline ::ad::map::point::Longitude max()
516  {
517  return ::ad::map::point::Longitude::getMax();
518  }
519 
523  static inline ::ad::map::point::Longitude epsilon()
524  {
525  return ::ad::map::point::Longitude::getPrecision();
526  }
527 };
528 
529 } // namespace std
530 
534 #ifndef GEN_GUARD_AD_MAP_POINT_LONGITUDE
535 #define GEN_GUARD_AD_MAP_POINT_LONGITUDE
536 
539 namespace ad {
543 namespace map {
549 namespace point {
550 
560 inline std::ostream &operator<<(std::ostream &os, Longitude const &_value)
561 {
562  return os << double(_value);
563 }
564 
565 } // namespace point
566 } // namespace map
567 } // namespace ad
568 
569 namespace std {
573 inline std::string to_string(::ad::map::point::Longitude const &value)
574 {
575  return to_string(static_cast<double>(value));
576 }
577 } // namespace std
578 #endif // GEN_GUARD_AD_MAP_POINT_LONGITUDE
ad
namespace ad
Definition: GeometryStoreItem.hpp:28
ad::map::point::operator<<
std::ostream & operator<<(std::ostream &os, Altitude const &_value)
standard ostream operator
Definition: Altitude.hpp:562
ad::map::point::Longitude::isValid
bool isValid() const
Definition: Longitude.hpp:388
ad::map::point::Longitude::Longitude
_AD_MAP_POINT_LONGITUDE_EXPLICIT_CONVERSION_ Longitude(double const iLongitude)
standard constructor
Definition: Longitude.hpp:100
std::numeric_limits<::ad::map::point::Longitude >::lowest
static inline ::ad::map::point::Longitude lowest()
Definition: Longitude.hpp:508
ad::map::point::Longitude::ensureValidNonZero
void ensureValidNonZero() const
ensure that the Longitude is valid and non zero
Definition: Longitude.hpp:418
ad::map::point::Longitude::operator<=
bool operator<=(const Longitude &other) const
standard comparison operator
Definition: Longitude.hpp:216
std::numeric_limits<::ad::map::point::Longitude >::max
static inline ::ad::map::point::Longitude max()
Definition: Longitude.hpp:515
ad::map::point::Longitude::cMaxValue
static const double cMaxValue
constant defining the maximum valid Longitude value (used in isValid())
Definition: Longitude.hpp:76
ad::map::point::Longitude::operator!=
bool operator!=(const Longitude &other) const
standard comparison operator
Definition: Longitude.hpp:154
ad::map::point::Longitude::operator-
Longitude operator-() const
standard arithmetic operator
Definition: Longitude.hpp:362
operator*
inline ::ad::map::point::Longitude operator*(const double &other, ::ad::map::point::Longitude const &value)
standard arithmetic operator
Definition: Longitude.hpp:475
ad::map::point::Longitude::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed Longitude value accuracy (used in comparison operator==(),...
Definition: Longitude.hpp:82
std::fabs
inline ::ad::map::point::Longitude fabs(const ::ad::map::point::Longitude other)
overload of the std::fabs for Longitude
Definition: Longitude.hpp:488
ad::map::point::Longitude::getPrecision
static Longitude getPrecision()
get assumed accuracy of Longitude (i.e. cPrecisionValue)
Definition: Longitude.hpp:449
std::to_string
std::string to_string(::ad::map::access::GeometryStoreItem const &value)
overload of the std::to_string for GeometryStoreItem
Definition: GeometryStoreItem.hpp:183
ad::map::point::Longitude::Longitude
Longitude()
default constructor
Definition: Longitude.hpp:90
ad::map::point::Longitude::operator>
bool operator>(const Longitude &other) const
standard comparison operator
Definition: Longitude.hpp:168
ad::map::point::Longitude::getMin
static Longitude getMin()
get minimum valid Longitude (i.e. cMinValue)
Definition: Longitude.hpp:433
std::numeric_limits<::ad::map::point::Longitude >::epsilon
static inline ::ad::map::point::Longitude epsilon()
Definition: Longitude.hpp:523
ad::map::point::Longitude::operator*
Longitude operator*(const double &scalar) const
standard arithmetic operator
Definition: Longitude.hpp:309
ad::map::point::Longitude::operator/
double operator/(const Longitude &other) const
standard arithmetic operator
Definition: Longitude.hpp:346
ad::map::point::Longitude::operator+
Longitude operator+(const Longitude &other) const
standard arithmetic operator
Definition: Longitude.hpp:233
ad::map::point::Longitude::ensureValid
void ensureValid() const
ensure that the Longitude is valid
Definition: Longitude.hpp:401
ad::map::point::Longitude::operator<
bool operator<(const Longitude &other) const
standard comparison operator
Definition: Longitude.hpp:184
ad::map::point::Longitude::operator=
Longitude & operator=(const Longitude &other)=default
standard assignment operator
ad::map::point::Longitude::operator/
Longitude operator/(const double &scalar) const
standard arithmetic operator
Definition: Longitude.hpp:327
ad::map::point::Longitude::operator>=
bool operator>=(const Longitude &other) const
standard comparison operator
Definition: Longitude.hpp:200
ad::map::point::Longitude::operator-
Longitude operator-(const Longitude &other) const
standard arithmetic operator
Definition: Longitude.hpp:271
ad::map::point::Longitude::cMinValue
static const double cMinValue
constant defining the minimum valid Longitude value (used in isValid())
Definition: Longitude.hpp:71
ad::map::point::Longitude::getMax
static Longitude getMax()
get maximum valid Longitude (i.e. cMaxValue)
Definition: Longitude.hpp:441
ad::map::point::Longitude::operator+=
Longitude & operator+=(const Longitude &other)
standard arithmetic operator
Definition: Longitude.hpp:252
ad::map::point::Longitude::operator-=
Longitude operator-=(const Longitude &other)
standard arithmetic operator
Definition: Longitude.hpp:290
ad::map::point::Longitude
DataType Longitude.
Definition: Longitude.hpp:65
ad::map::point::Longitude::operator==
bool operator==(const Longitude &other) const
standard comparison operator
Definition: Longitude.hpp:140
_AD_MAP_POINT_LONGITUDE_EXPLICIT_CONVERSION_
#define _AD_MAP_POINT_LONGITUDE_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: Longitude.hpp:56
std::fabs
inline ::ad::map::point::Altitude fabs(const ::ad::map::point::Altitude other)
overload of the std::fabs for Altitude
Definition: Altitude.hpp:490