MIP_SDK  v3.0.0
MicroStrain Communications Library for embedded systems
index.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <stddef.h>
5 #include <climits>
6 
7 namespace microstrain
8 {
9  class Id;
10 
28  class Index
29  {
30  private:
31  unsigned int INVALID = UINT_MAX;
32 
33  public:
34  constexpr explicit Index(unsigned int index) : m_index(index) {}
35  constexpr Index() : m_index(INVALID) {}
36 
37  constexpr Index& setFromIndex(unsigned int index) { m_index = index; return *this; }
38  constexpr Index& setFromId(unsigned int id) { m_index = id-1; return *this; }
39 
40  constexpr unsigned int index() const { return m_index; }
41  constexpr unsigned int id() const { return m_index+1; }
42 
43  constexpr bool operator==(const Index& other) const { return m_index == other.m_index; }
44  constexpr bool operator!=(const Index& other) const { return m_index != other.m_index; }
45 
46  constexpr bool isAssigned() const { return m_index != INVALID; }
47  constexpr bool isValid(size_t max_count) const { return m_index < max_count; }
48 
49  constexpr void clear() { m_index = INVALID; }
50 
51  Index& operator++() { m_index++; return *this; }
52  Index& operator--() { m_index--; return *this; }
53  Index operator++(int) { Index tmp(*this); m_index++; return tmp; }
54  Index operator--(int) { Index tmp(*this); m_index--; return tmp; }
55 
56  constexpr Index(const Id& other);
57  constexpr Index& operator=(const Id& other);
58 
59  bool operator<(Index other) const { return m_index < other.m_index; }
60  bool operator>(Index other) const { return m_index > other.m_index; }
61  bool operator<=(Index other) const { return m_index <= other.m_index; }
62  bool operator>=(Index other) const { return m_index >= other.m_index; }
63  bool operator==(Index other) const { return m_index == other.m_index; }
64  bool operator!=(Index other) const { return m_index != other.m_index; }
65 
66  private:
67  unsigned int m_index;
68  };
69 
75  class Id
76  {
77  private:
78  unsigned int INVALID = 0;
79 
80  public:
81  constexpr explicit Id(unsigned int id) : m_id(id) {}
82  constexpr Id() : m_id(INVALID) {}
83 
84  constexpr Id& setFromIndex(unsigned int index) { m_id = index+1; return *this; }
85  constexpr Id& setFromId(unsigned int id) { m_id = id; return *this; }
86 
87  constexpr unsigned int index() const { return m_id-1; }
88  constexpr unsigned int id() const { return m_id; }
89 
90  constexpr bool operator==(const Id& other) const { return m_id == other.m_id; }
91  constexpr bool operator!=(const Id& other) const { return m_id != other.m_id; }
92 
93  constexpr bool isAssigned() const { return m_id != INVALID; }
94  constexpr bool isValid(size_t max_count) const { return index() < max_count; }
95 
96  constexpr void clear() { m_id = INVALID; }
97 
98  Id& operator++() { m_id++; return *this; }
99  Id& operator--() { m_id--; return *this; }
100  Id operator++(int) { Id tmp(*this); m_id++; return tmp; }
101  Id operator--(int) { Id tmp(*this); m_id--; return tmp; }
102 
103  constexpr Id(const Index& other) : m_id(other.id()) {}
104  constexpr Id& operator=(const Index& other) { return setFromIndex(other.index()); }
105 
106  private:
107  unsigned int m_id;
108  };
109 
110  inline constexpr Index::Index(const Id& other) : m_index(other.index()) {}
111  inline constexpr Index& Index::operator=(const Id& other) { return setFromIndex(other.index()); }
112 
113  inline constexpr bool operator==(Id l, Index r) { return l.index() == r.index(); }
114  inline constexpr bool operator==(Index l, Id r) { return l.index() == r.index(); }
115 
116  // Get a pointer to a value in the container at index, or NULL if index out of range.
117  template<class Container>
118  auto* indexOrNull(Container& container, Index index) { return index.isValid(container.size()) ? &container[index.index()] : nullptr; }
119 
120  // Get the value in the container at index, or a default value if index out of range.
121  template<class Container, typename Value>
122  auto indexOrDefault(Container& container, Index index, Value default_) { return index.isValid(container.size()) ? container[index.index()] : default_; }
123 
124 } // namespace microstrain
microstrain::Index::setFromId
constexpr Index & setFromId(unsigned int id)
Definition: index.hpp:38
microstrain::Id::isAssigned
constexpr bool isAssigned() const
Definition: index.hpp:93
microstrain::Id::operator--
Id operator--(int)
Definition: index.hpp:101
microstrain::Index::clear
constexpr void clear()
Definition: index.hpp:49
microstrain::Id::Id
constexpr Id(const Index &other)
Definition: index.hpp:103
microstrain::indexOrDefault
auto indexOrDefault(Container &container, Index index, Value default_)
Definition: index.hpp:122
microstrain::Index::isValid
constexpr bool isValid(size_t max_count) const
Definition: index.hpp:47
microstrain::Id::Id
constexpr Id(unsigned int id)
Definition: index.hpp:81
microstrain::Id::operator==
constexpr bool operator==(const Id &other) const
Definition: index.hpp:90
microstrain::Index::operator=
constexpr Index & operator=(const Id &other)
Definition: index.hpp:111
microstrain::Id::index
constexpr unsigned int index() const
Definition: index.hpp:87
microstrain::Index::operator!=
bool operator!=(Index other) const
Definition: index.hpp:64
microstrain::Id::operator!=
constexpr bool operator!=(const Id &other) const
Definition: index.hpp:91
microstrain::Index::operator>=
bool operator>=(Index other) const
Definition: index.hpp:62
microstrain::operator==
constexpr bool operator==(Id l, Index r)
Definition: index.hpp:113
microstrain::Index::operator++
Index & operator++()
Definition: index.hpp:51
microstrain::Index::operator--
Index operator--(int)
Definition: index.hpp:54
microstrain::Id::operator++
Id & operator++()
Definition: index.hpp:98
microstrain::Index::operator--
Index & operator--()
Definition: index.hpp:52
microstrain::Index::operator++
Index operator++(int)
Definition: index.hpp:53
microstrain::Index::isAssigned
constexpr bool isAssigned() const
Definition: index.hpp:46
microstrain::Index::operator==
bool operator==(Index other) const
Definition: index.hpp:63
microstrain::Index::operator==
constexpr bool operator==(const Index &other) const
Definition: index.hpp:43
microstrain::indexOrNull
auto * indexOrNull(Container &container, Index index)
Definition: index.hpp:118
microstrain::Id::Id
constexpr Id()
Definition: index.hpp:82
microstrain::Index::index
constexpr unsigned int index() const
Definition: index.hpp:40
microstrain::Index::operator<=
bool operator<=(Index other) const
Definition: index.hpp:61
microstrain::Index::operator<
bool operator<(Index other) const
Definition: index.hpp:59
microstrain::Id::clear
constexpr void clear()
Definition: index.hpp:96
microstrain::Index
Represents an index ranging from 0..N excluding N.
Definition: index.hpp:28
microstrain::Id::id
constexpr unsigned int id() const
Definition: index.hpp:88
microstrain::Index::operator>
bool operator>(Index other) const
Definition: index.hpp:60
microstrain::Id::operator--
Id & operator--()
Definition: index.hpp:99
microstrain::Index::Index
constexpr Index()
Definition: index.hpp:35
microstrain::Id::operator=
constexpr Id & operator=(const Index &other)
Definition: index.hpp:104
microstrain::Id
Representes an ID number ranging from 1..N including N.
Definition: index.hpp:75
microstrain::Index::setFromIndex
constexpr Index & setFromIndex(unsigned int index)
Definition: index.hpp:37
microstrain::Id::operator++
Id operator++(int)
Definition: index.hpp:100
microstrain::Id::isValid
constexpr bool isValid(size_t max_count) const
Definition: index.hpp:94
microstrain::Index::id
constexpr unsigned int id() const
Definition: index.hpp:41
microstrain::Index::operator!=
constexpr bool operator!=(const Index &other) const
Definition: index.hpp:44
microstrain
Definition: embedded_time.h:8
microstrain::Id::setFromId
constexpr Id & setFromId(unsigned int id)
Definition: index.hpp:85
microstrain::Index::Index
constexpr Index(unsigned int index)
Definition: index.hpp:34
microstrain::Id::setFromIndex
constexpr Id & setFromIndex(unsigned int index)
Definition: index.hpp:84