CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Common/TSIndex.h
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
#define DECLARE_TYPESAFE_INDEX(NAME, TYPE) typedef typesafe_index<TYPE, class TAG_##NAME> NAME
17
18
/// This template allows for indexing with compile time check and generating
19
/// error for using one index variable for another array that is to be indexed
20
/// with different variable.
21
/// base_type specifies
22
///
23
/// @param base_type Base integral type
24
///
25
/// @param tag Tag Name to prevent copy from one type to other
26
///
27
template <class base_type, class tag> class typesafe_index
28
{
29
public:
30
explicit typesafe_index(base_type i = base_type()) : p(i) {}
31
32
void operator=(const base_type& val)
33
{
34
p = val;
35
}
36
37
constexpr base_type get_int() const
38
{
39
return p;
40
}
41
42
typesafe_index operator++()
43
{
44
return typesafe_index(++p);
45
}
46
47
typesafe_index operator++(int)
48
{
49
return typesafe_index(p++);
50
}
51
52
typesafe_index operator%(const base_type& val)
53
{
54
return typesafe_index(p % val);
55
}
56
57
bool operator<(const base_type& val) const
58
{
59
return (p<val);
60
}
61
62
bool operator<=(const base_type& val) const
63
{
64
return (p<=val);
65
}
66
67
bool operator>=(const base_type& val) const
68
{
69
return (p>=val);
70
}
71
72
73
bool operator>(const base_type& val) const
74
{
75
return (p>val);
76
}
77
78
bool operator!=(const base_type& val) const
79
{
80
return (p!=val);
81
}
82
83
bool operator==(const base_type& val) const
84
{
85
return (p==val);
86
}
87
88
explicit constexpr operator base_type() const
89
{
90
return p;
91
}
92
93
typesafe_index operator+(const base_type& val) const
94
{
95
return typesafe_index(p+val);
96
}
97
private:
98
base_type p;
99
};
100
101
/// This template associates the base_type array with accessor_type(index).
102
/// So the elements can be accessed using [] only using accessor_type index
103
/// _priv_instance is kept public for use in Parameter declaration.
104
///
105
template <class base_type, uint32_t num_instances, typename accessor_type> class RestrictIDTypeArray
106
{
107
public:
108
base_type _priv_instance[num_instances];
109
base_type& operator[](const accessor_type& index)
110
{
111
return _priv_instance[index.get_int()];
112
}
113
114
constexpr const base_type& operator[](const accessor_type& index) const
115
{
116
return _priv_instance[index.get_int()];
117
}
118
};
119
120