Line data Source code
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 3558998025 : explicit typesafe_index(base_type i = base_type()) : p(i) {} 31 : 32 4787 : void operator=(const base_type& val) 33 : { 34 4787 : p = val; 35 4787 : } 36 : 37 6452009354 : constexpr base_type get_int() const 38 : { 39 6452009354 : return p; 40 : } 41 : 42 2 : typesafe_index operator++() 43 : { 44 2 : return typesafe_index(++p); 45 : } 46 : 47 966368807 : typesafe_index operator++(int) 48 : { 49 966368807 : return typesafe_index(p++); 50 : } 51 : 52 6706532 : typesafe_index operator%(const base_type& val) 53 : { 54 6706532 : return typesafe_index(p % val); 55 : } 56 : 57 1974709685 : bool operator<(const base_type& val) const 58 : { 59 1974709685 : return (p<val); 60 : } 61 : 62 2 : bool operator<=(const base_type& val) const 63 : { 64 2 : return (p<=val); 65 : } 66 : 67 170038708 : bool operator>=(const base_type& val) const 68 : { 69 170038708 : return (p>=val); 70 : } 71 : 72 : 73 2 : bool operator>(const base_type& val) const 74 : { 75 2 : return (p>val); 76 : } 77 : 78 4787 : bool operator!=(const base_type& val) const 79 : { 80 4787 : return (p!=val); 81 : } 82 : 83 399594 : bool operator==(const base_type& val) const 84 : { 85 399594 : return (p==val); 86 : } 87 : 88 2053523 : explicit constexpr operator base_type() const 89 : { 90 2053523 : return p; 91 : } 92 : 93 6707141 : typesafe_index operator+(const base_type& val) const 94 : { 95 6707141 : 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 287567737 : base_type& operator[](const accessor_type& index) 110 : { 111 287567737 : return _priv_instance[index.get_int()]; 112 : } 113 : 114 6164475620 : constexpr const base_type& operator[](const accessor_type& index) const 115 : { 116 6164475620 : return _priv_instance[index.get_int()]; 117 : } 118 : };