Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Common/TSIndex.h
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415#define DECLARE_TYPESAFE_INDEX(NAME, TYPE) typedef typesafe_index<TYPE, class TAG_##NAME> NAME1617/// This template allows for indexing with compile time check and generating18/// error for using one index variable for another array that is to be indexed19/// with different variable.20/// base_type specifies21///22/// @param base_type Base integral type23///24/// @param tag Tag Name to prevent copy from one type to other25///26template <class base_type, class tag> class typesafe_index27{28public:29explicit typesafe_index(base_type i = base_type()) : p(i) {}3031void operator=(const base_type& val)32{33p = val;34}3536constexpr base_type get_int() const37{38return p;39}4041typesafe_index operator++()42{43return typesafe_index(++p);44}4546typesafe_index operator++(int)47{48return typesafe_index(p++);49}5051typesafe_index operator%(const base_type& val)52{53return typesafe_index(p % val);54}5556bool operator<(const base_type& val) const57{58return (p<val);59}6061bool operator<=(const base_type& val) const62{63return (p<=val);64}6566bool operator>=(const base_type& val) const67{68return (p>=val);69}707172bool operator>(const base_type& val) const73{74return (p>val);75}7677bool operator!=(const base_type& val) const78{79return (p!=val);80}8182bool operator==(const base_type& val) const83{84return (p==val);85}8687explicit constexpr operator base_type() const88{89return p;90}9192typesafe_index operator+(const base_type& val) const93{94return typesafe_index(p+val);95}96private:97base_type p;98};99100/// This template associates the base_type array with accessor_type(index).101/// So the elements can be accessed using [] only using accessor_type index102/// _priv_instance is kept public for use in Parameter declaration.103///104template <class base_type, uint32_t num_instances, typename accessor_type> class RestrictIDTypeArray105{106public:107base_type _priv_instance[num_instances];108base_type& operator[](const accessor_type& index)109{110return _priv_instance[index.get_int()];111}112113constexpr const base_type& operator[](const accessor_type& index) const114{115return _priv_instance[index.get_int()];116}117};118119120