ad_map_access
PartitionId.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 <cstdint>
22 #include <iostream>
23 #include <limits>
24 #include <sstream>
25 #include <stdexcept>
26 #include "spdlog/fmt/ostr.h"
27 #include "spdlog/spdlog.h"
31 namespace ad {
35 namespace map {
41 namespace access {
42 
46 #define AD_MAP_ACCESS_PARTITIONID_THROWS_EXCEPTION 1
47 
48 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
49 
52 #define _AD_MAP_ACCESS_PARTITIONID_EXPLICIT_CONVERSION_ explicit
53 #else
54 
57 #define _AD_MAP_ACCESS_PARTITIONID_EXPLICIT_CONVERSION_
58 #endif
59 
67 {
68 public:
72  static const uint64_t cMinValue;
73 
77  static const uint64_t cMaxValue;
78 
86  : mPartitionId(std::numeric_limits<uint64_t>::quiet_NaN())
87  {
88  }
89 
96  : mPartitionId(iPartitionId)
97  {
98  }
99 
103  PartitionId(const PartitionId &other) = default;
104 
108  PartitionId(PartitionId &&other) = default;
109 
117  PartitionId &operator=(const PartitionId &other) = default;
118 
126  PartitionId &operator=(PartitionId &&other) = default;
127 
135  bool operator==(const PartitionId &other) const
136  {
137  ensureValid();
138  other.ensureValid();
139  return mPartitionId == other.mPartitionId;
140  }
141 
149  bool operator!=(const PartitionId &other) const
150  {
151  return !operator==(other);
152  }
153 
163  bool operator>(const PartitionId &other) const
164  {
165  ensureValid();
166  other.ensureValid();
167  return (mPartitionId > other.mPartitionId) && operator!=(other);
168  }
169 
179  bool operator<(const PartitionId &other) const
180  {
181  ensureValid();
182  other.ensureValid();
183  return (mPartitionId < other.mPartitionId) && operator!=(other);
184  }
185 
195  bool operator>=(const PartitionId &other) const
196  {
197  ensureValid();
198  other.ensureValid();
199  return ((mPartitionId > other.mPartitionId) || operator==(other));
200  }
201 
211  bool operator<=(const PartitionId &other) const
212  {
213  ensureValid();
214  other.ensureValid();
215  return ((mPartitionId < other.mPartitionId) || operator==(other));
216  }
217 
228  PartitionId operator+(const PartitionId &other) const
229  {
230  ensureValid();
231  other.ensureValid();
232  PartitionId const result(mPartitionId + other.mPartitionId);
233  result.ensureValid();
234  return result;
235  }
236 
248  {
249  ensureValid();
250  other.ensureValid();
251  mPartitionId += other.mPartitionId;
252  ensureValid();
253  return *this;
254  }
255 
266  PartitionId operator-(const PartitionId &other) const
267  {
268  ensureValid();
269  other.ensureValid();
270  PartitionId const result(mPartitionId - other.mPartitionId);
271  result.ensureValid();
272  return result;
273  }
274 
286  {
287  ensureValid();
288  other.ensureValid();
289  mPartitionId -= other.mPartitionId;
290  ensureValid();
291  return *this;
292  }
293 
301  {
302  return mPartitionId;
303  }
304 
312  bool isValid() const
313  {
314  auto const valueClass = std::fpclassify(mPartitionId);
315  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mPartitionId)
316  && (mPartitionId <= cMaxValue);
317  }
318 
325  void ensureValid() const
326  {
327  if (!isValid())
328  {
329  spdlog::info("ensureValid(::ad::map::access::PartitionId)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
330 #if (AD_MAP_ACCESS_PARTITIONID_THROWS_EXCEPTION == 1)
331  throw std::out_of_range("PartitionId value out of range"); // LCOV_EXCL_BR_LINE
332 #endif
333  }
334  }
335 
342  void ensureValidNonZero() const
343  {
344  ensureValid();
345  if (operator==(PartitionId(0))) // LCOV_EXCL_BR_LINE
346  {
347  spdlog::info("ensureValid(::ad::map::access::PartitionId)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
348 #if (AD_MAP_ACCESS_PARTITIONID_THROWS_EXCEPTION == 1)
349  throw std::out_of_range("PartitionId value is zero"); // LCOV_EXCL_BR_LINE
350 #endif
351  }
352  }
353 
358  {
359  return PartitionId(cMinValue);
360  }
361 
366  {
367  return PartitionId(cMaxValue);
368  }
369 
370 private:
374  uint64_t mPartitionId;
375 };
376 
377 } // namespace access
378 } // namespace map
379 } // namespace ad
383 namespace std {
384 
393 template <> class numeric_limits<::ad::map::access::PartitionId> : public numeric_limits<uint64_t>
394 {
395 public:
399  static inline ::ad::map::access::PartitionId lowest()
400  {
401  return ::ad::map::access::PartitionId::getMin();
402  }
406  static inline ::ad::map::access::PartitionId max()
407  {
408  return ::ad::map::access::PartitionId::getMax();
409  }
410 
414  static inline ::ad::map::access::PartitionId epsilon()
415  {
416  return ::ad::map::access::PartitionId(0);
417  }
418 };
419 
420 } // namespace std
421 
425 #ifndef GEN_GUARD_AD_MAP_ACCESS_PARTITIONID
426 #define GEN_GUARD_AD_MAP_ACCESS_PARTITIONID
427 
430 namespace ad {
434 namespace map {
440 namespace access {
441 
451 inline std::ostream &operator<<(std::ostream &os, PartitionId const &_value)
452 {
453  return os << uint64_t(_value);
454 }
455 
456 } // namespace access
457 } // namespace map
458 } // namespace ad
459 
460 namespace std {
464 inline std::string to_string(::ad::map::access::PartitionId const &value)
465 {
466  return to_string(static_cast<uint64_t>(value));
467 }
468 } // namespace std
469 #endif // GEN_GUARD_AD_MAP_ACCESS_PARTITIONID
ad
namespace ad
Definition: GeometryStoreItem.hpp:28
ad::map::access::PartitionId::operator-=
PartitionId operator-=(const PartitionId &other)
standard arithmetic operator
Definition: PartitionId.hpp:285
ad::map::access::PartitionId::getMin
static PartitionId getMin()
get minimum valid PartitionId (i.e. cMinValue)
Definition: PartitionId.hpp:357
ad::map::access::PartitionId::ensureValid
void ensureValid() const
ensure that the PartitionId is valid
Definition: PartitionId.hpp:325
std::numeric_limits<::ad::map::access::PartitionId >::lowest
static inline ::ad::map::access::PartitionId lowest()
Definition: PartitionId.hpp:399
ad::map::access::PartitionId::operator<
bool operator<(const PartitionId &other) const
standard comparison operator
Definition: PartitionId.hpp:179
ad::map::access::PartitionId::operator>
bool operator>(const PartitionId &other) const
standard comparison operator
Definition: PartitionId.hpp:163
ad::map::access::PartitionId::cMinValue
static const uint64_t cMinValue
constant defining the minimum valid PartitionId value (used in isValid())
Definition: PartitionId.hpp:72
ad::map::access::PartitionId::operator+=
PartitionId & operator+=(const PartitionId &other)
standard arithmetic operator
Definition: PartitionId.hpp:247
ad::map::access::PartitionId::operator-
PartitionId operator-(const PartitionId &other) const
standard arithmetic operator
Definition: PartitionId.hpp:266
ad::map::access::PartitionId::operator!=
bool operator!=(const PartitionId &other) const
standard comparison operator
Definition: PartitionId.hpp:149
_AD_MAP_ACCESS_PARTITIONID_EXPLICIT_CONVERSION_
#define _AD_MAP_ACCESS_PARTITIONID_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: PartitionId.hpp:57
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
std::numeric_limits<::ad::map::access::PartitionId >::epsilon
static inline ::ad::map::access::PartitionId epsilon()
Definition: PartitionId.hpp:414
ad::map::access::PartitionId::cMaxValue
static const uint64_t cMaxValue
constant defining the maximum valid PartitionId value (used in isValid())
Definition: PartitionId.hpp:77
ad::map::access::PartitionId::PartitionId
PartitionId()
default constructor
Definition: PartitionId.hpp:85
ad::map::access::PartitionId::operator>=
bool operator>=(const PartitionId &other) const
standard comparison operator
Definition: PartitionId.hpp:195
ad::map::access::PartitionId
DataType PartitionId.
Definition: PartitionId.hpp:66
ad::map::access::PartitionId::PartitionId
_AD_MAP_ACCESS_PARTITIONID_EXPLICIT_CONVERSION_ PartitionId(uint64_t const iPartitionId)
standard constructor
Definition: PartitionId.hpp:95
ad::map::access::PartitionId::operator==
bool operator==(const PartitionId &other) const
standard comparison operator
Definition: PartitionId.hpp:135
ad::map::access::PartitionId::operator<=
bool operator<=(const PartitionId &other) const
standard comparison operator
Definition: PartitionId.hpp:211
ad::map::access::PartitionId::operator=
PartitionId & operator=(const PartitionId &other)=default
standard assignment operator
ad::map::access::PartitionId::isValid
bool isValid() const
Definition: PartitionId.hpp:312
ad::map::access::PartitionId::getMax
static PartitionId getMax()
get maximum valid PartitionId (i.e. cMaxValue)
Definition: PartitionId.hpp:365
ad::map::access::operator<<
std::ostream & operator<<(std::ostream &os, GeometryStoreItem const &_value)
standard ostream operator
Definition: GeometryStoreItem.hpp:157
ad::map::access::PartitionId::operator+
PartitionId operator+(const PartitionId &other) const
standard arithmetic operator
Definition: PartitionId.hpp:228
std::numeric_limits<::ad::map::access::PartitionId >::max
static inline ::ad::map::access::PartitionId max()
Definition: PartitionId.hpp:406
ad::map::access::PartitionId::ensureValidNonZero
void ensureValidNonZero() const
ensure that the PartitionId is valid and non zero
Definition: PartitionId.hpp:342