Device interface library for multiple platforms including Mbed.
Dependents: DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#
Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.
The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface
MaximInterfaceCore/array.hpp@12:7eb41621ba22, 2020-05-29 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Fri May 29 16:19:22 2020 -0500
- Revision:
- 12:7eb41621ba22
- Parent:
- 11:3f3bf6bf5e6c
Updated to version 2.2.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 7:9cd16581b578 | 1 | /******************************************************************************* |
IanBenzMaxim | 8:5ea891c7d1a1 | 2 | * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 7:9cd16581b578 | 3 | * |
IanBenzMaxim | 7:9cd16581b578 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 7:9cd16581b578 | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 7:9cd16581b578 | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 7:9cd16581b578 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 7:9cd16581b578 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 7:9cd16581b578 | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 7:9cd16581b578 | 10 | * |
IanBenzMaxim | 7:9cd16581b578 | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 7:9cd16581b578 | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 7:9cd16581b578 | 13 | * |
IanBenzMaxim | 7:9cd16581b578 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 7:9cd16581b578 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 7:9cd16581b578 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 7:9cd16581b578 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 7:9cd16581b578 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 7:9cd16581b578 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 7:9cd16581b578 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 7:9cd16581b578 | 21 | * |
IanBenzMaxim | 7:9cd16581b578 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 7:9cd16581b578 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 7:9cd16581b578 | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 7:9cd16581b578 | 25 | * |
IanBenzMaxim | 7:9cd16581b578 | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 7:9cd16581b578 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 7:9cd16581b578 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 7:9cd16581b578 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 7:9cd16581b578 | 30 | * ownership rights. |
IanBenzMaxim | 7:9cd16581b578 | 31 | *******************************************************************************/ |
IanBenzMaxim | 7:9cd16581b578 | 32 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 33 | #ifndef MaximInterfaceCore_array_hpp |
IanBenzMaxim | 8:5ea891c7d1a1 | 34 | #define MaximInterfaceCore_array_hpp |
IanBenzMaxim | 7:9cd16581b578 | 35 | |
IanBenzMaxim | 7:9cd16581b578 | 36 | #include <stddef.h> |
IanBenzMaxim | 7:9cd16581b578 | 37 | #include <stdint.h> |
IanBenzMaxim | 7:9cd16581b578 | 38 | #include <algorithm> |
IanBenzMaxim | 7:9cd16581b578 | 39 | #include <iterator> |
IanBenzMaxim | 11:3f3bf6bf5e6c | 40 | #include "type_traits.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 41 | |
IanBenzMaxim | 7:9cd16581b578 | 42 | namespace MaximInterfaceCore { |
IanBenzMaxim | 7:9cd16581b578 | 43 | |
IanBenzMaxim | 7:9cd16581b578 | 44 | /// Generic array class similar to std::array. |
IanBenzMaxim | 7:9cd16581b578 | 45 | template <typename T, size_t N> class array { |
IanBenzMaxim | 7:9cd16581b578 | 46 | public: |
IanBenzMaxim | 7:9cd16581b578 | 47 | typedef T value_type; |
IanBenzMaxim | 7:9cd16581b578 | 48 | typedef size_t size_type; |
IanBenzMaxim | 7:9cd16581b578 | 49 | typedef ptrdiff_t difference_type; |
IanBenzMaxim | 7:9cd16581b578 | 50 | typedef value_type & reference; |
IanBenzMaxim | 7:9cd16581b578 | 51 | typedef const value_type & const_reference; |
IanBenzMaxim | 7:9cd16581b578 | 52 | typedef value_type * pointer; |
IanBenzMaxim | 7:9cd16581b578 | 53 | typedef const value_type * const_pointer; |
IanBenzMaxim | 7:9cd16581b578 | 54 | typedef pointer iterator; |
IanBenzMaxim | 7:9cd16581b578 | 55 | typedef const_pointer const_iterator; |
IanBenzMaxim | 7:9cd16581b578 | 56 | typedef std::reverse_iterator<iterator> reverse_iterator; |
IanBenzMaxim | 7:9cd16581b578 | 57 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
IanBenzMaxim | 7:9cd16581b578 | 58 | |
IanBenzMaxim | 7:9cd16581b578 | 59 | /// @name Element access |
IanBenzMaxim | 7:9cd16581b578 | 60 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 61 | |
IanBenzMaxim | 7:9cd16581b578 | 62 | reference operator[](size_type pos) { |
IanBenzMaxim | 7:9cd16581b578 | 63 | return const_cast<reference>( |
IanBenzMaxim | 7:9cd16581b578 | 64 | static_cast<const array &>(*this).operator[](pos)); |
IanBenzMaxim | 7:9cd16581b578 | 65 | } |
IanBenzMaxim | 7:9cd16581b578 | 66 | |
IanBenzMaxim | 7:9cd16581b578 | 67 | const_reference operator[](size_type pos) const { return data()[pos]; } |
IanBenzMaxim | 7:9cd16581b578 | 68 | |
IanBenzMaxim | 7:9cd16581b578 | 69 | reference front() { |
IanBenzMaxim | 7:9cd16581b578 | 70 | return const_cast<reference>(static_cast<const array &>(*this).front()); |
IanBenzMaxim | 7:9cd16581b578 | 71 | } |
IanBenzMaxim | 7:9cd16581b578 | 72 | |
IanBenzMaxim | 7:9cd16581b578 | 73 | const_reference front() const { return operator[](0); } |
IanBenzMaxim | 7:9cd16581b578 | 74 | |
IanBenzMaxim | 7:9cd16581b578 | 75 | reference back() { |
IanBenzMaxim | 7:9cd16581b578 | 76 | return const_cast<reference>(static_cast<const array &>(*this).back()); |
IanBenzMaxim | 7:9cd16581b578 | 77 | } |
IanBenzMaxim | 7:9cd16581b578 | 78 | |
IanBenzMaxim | 11:3f3bf6bf5e6c | 79 | const_reference back() const { return operator[](N - 1); } |
IanBenzMaxim | 7:9cd16581b578 | 80 | |
IanBenzMaxim | 7:9cd16581b578 | 81 | pointer data() { |
IanBenzMaxim | 7:9cd16581b578 | 82 | return const_cast<pointer>(static_cast<const array &>(*this).data()); |
IanBenzMaxim | 7:9cd16581b578 | 83 | } |
IanBenzMaxim | 7:9cd16581b578 | 84 | |
IanBenzMaxim | 11:3f3bf6bf5e6c | 85 | const_pointer data() const { return reinterpret_cast<const_pointer>(&data_); } |
IanBenzMaxim | 7:9cd16581b578 | 86 | |
IanBenzMaxim | 7:9cd16581b578 | 87 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 88 | |
IanBenzMaxim | 7:9cd16581b578 | 89 | /// @name Iterators |
IanBenzMaxim | 7:9cd16581b578 | 90 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 91 | |
IanBenzMaxim | 7:9cd16581b578 | 92 | iterator begin() { |
IanBenzMaxim | 7:9cd16581b578 | 93 | return const_cast<iterator>(static_cast<const array &>(*this).cbegin()); |
IanBenzMaxim | 7:9cd16581b578 | 94 | } |
IanBenzMaxim | 7:9cd16581b578 | 95 | |
IanBenzMaxim | 7:9cd16581b578 | 96 | const_iterator begin() const { return cbegin(); } |
IanBenzMaxim | 7:9cd16581b578 | 97 | |
IanBenzMaxim | 7:9cd16581b578 | 98 | const_iterator cbegin() const { return data(); } |
IanBenzMaxim | 7:9cd16581b578 | 99 | |
IanBenzMaxim | 7:9cd16581b578 | 100 | iterator end() { |
IanBenzMaxim | 7:9cd16581b578 | 101 | return const_cast<iterator>(static_cast<const array &>(*this).cend()); |
IanBenzMaxim | 7:9cd16581b578 | 102 | } |
IanBenzMaxim | 7:9cd16581b578 | 103 | |
IanBenzMaxim | 7:9cd16581b578 | 104 | const_iterator end() const { return cend(); } |
IanBenzMaxim | 7:9cd16581b578 | 105 | |
IanBenzMaxim | 11:3f3bf6bf5e6c | 106 | const_iterator cend() const { return cbegin() + N; } |
IanBenzMaxim | 7:9cd16581b578 | 107 | |
IanBenzMaxim | 7:9cd16581b578 | 108 | reverse_iterator rbegin() { return reverse_iterator(end()); } |
IanBenzMaxim | 7:9cd16581b578 | 109 | |
IanBenzMaxim | 7:9cd16581b578 | 110 | const_reverse_iterator rbegin() const { |
IanBenzMaxim | 7:9cd16581b578 | 111 | return const_reverse_iterator(end()); |
IanBenzMaxim | 7:9cd16581b578 | 112 | } |
IanBenzMaxim | 7:9cd16581b578 | 113 | |
IanBenzMaxim | 7:9cd16581b578 | 114 | const_reverse_iterator crbegin() const { return rbegin(); } |
IanBenzMaxim | 7:9cd16581b578 | 115 | |
IanBenzMaxim | 7:9cd16581b578 | 116 | reverse_iterator rend() { return reverse_iterator(begin()); } |
IanBenzMaxim | 7:9cd16581b578 | 117 | |
IanBenzMaxim | 7:9cd16581b578 | 118 | const_reverse_iterator rend() const { |
IanBenzMaxim | 7:9cd16581b578 | 119 | return const_reverse_iterator(begin()); |
IanBenzMaxim | 7:9cd16581b578 | 120 | } |
IanBenzMaxim | 7:9cd16581b578 | 121 | |
IanBenzMaxim | 7:9cd16581b578 | 122 | const_reverse_iterator crend() const { return rend(); } |
IanBenzMaxim | 7:9cd16581b578 | 123 | |
IanBenzMaxim | 7:9cd16581b578 | 124 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 125 | |
IanBenzMaxim | 7:9cd16581b578 | 126 | /// @name Capacity |
IanBenzMaxim | 7:9cd16581b578 | 127 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 128 | |
IanBenzMaxim | 11:3f3bf6bf5e6c | 129 | static bool empty() { return N == 0; } |
IanBenzMaxim | 7:9cd16581b578 | 130 | |
IanBenzMaxim | 7:9cd16581b578 | 131 | static size_type size() { return N; } |
IanBenzMaxim | 7:9cd16581b578 | 132 | |
IanBenzMaxim | 11:3f3bf6bf5e6c | 133 | static size_type max_size() { return N; } |
IanBenzMaxim | 7:9cd16581b578 | 134 | |
IanBenzMaxim | 7:9cd16581b578 | 135 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 136 | |
IanBenzMaxim | 7:9cd16581b578 | 137 | /// @name Operations |
IanBenzMaxim | 7:9cd16581b578 | 138 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 139 | |
IanBenzMaxim | 7:9cd16581b578 | 140 | void fill(const_reference value) { std::fill(begin(), end(), value); } |
IanBenzMaxim | 7:9cd16581b578 | 141 | |
IanBenzMaxim | 7:9cd16581b578 | 142 | void swap(array & other) { std::swap_ranges(begin(), end(), other.begin()); } |
IanBenzMaxim | 7:9cd16581b578 | 143 | |
IanBenzMaxim | 7:9cd16581b578 | 144 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 145 | |
IanBenzMaxim | 7:9cd16581b578 | 146 | /// @private |
IanBenzMaxim | 7:9cd16581b578 | 147 | /// @note Implementation detail set public to allow aggregate initialization. |
IanBenzMaxim | 11:3f3bf6bf5e6c | 148 | typename conditional<N == 0, char, T[N]>::type data_; |
IanBenzMaxim | 7:9cd16581b578 | 149 | }; |
IanBenzMaxim | 7:9cd16581b578 | 150 | |
IanBenzMaxim | 7:9cd16581b578 | 151 | template <typename T, size_t N> |
IanBenzMaxim | 11:3f3bf6bf5e6c | 152 | bool operator==(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 153 | return std::equal(lhs.begin(), lhs.end(), rhs.begin()); |
IanBenzMaxim | 7:9cd16581b578 | 154 | } |
IanBenzMaxim | 7:9cd16581b578 | 155 | |
IanBenzMaxim | 7:9cd16581b578 | 156 | template <typename T, size_t N> |
IanBenzMaxim | 11:3f3bf6bf5e6c | 157 | bool operator!=(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 158 | return !operator==(lhs, rhs); |
IanBenzMaxim | 7:9cd16581b578 | 159 | } |
IanBenzMaxim | 7:9cd16581b578 | 160 | |
IanBenzMaxim | 7:9cd16581b578 | 161 | template <typename T, size_t N> |
IanBenzMaxim | 11:3f3bf6bf5e6c | 162 | bool operator<(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 163 | return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), |
IanBenzMaxim | 7:9cd16581b578 | 164 | rhs.end()); |
IanBenzMaxim | 7:9cd16581b578 | 165 | } |
IanBenzMaxim | 7:9cd16581b578 | 166 | |
IanBenzMaxim | 7:9cd16581b578 | 167 | template <typename T, size_t N> |
IanBenzMaxim | 11:3f3bf6bf5e6c | 168 | bool operator>(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 169 | return operator<(rhs, lhs); |
IanBenzMaxim | 7:9cd16581b578 | 170 | } |
IanBenzMaxim | 7:9cd16581b578 | 171 | |
IanBenzMaxim | 7:9cd16581b578 | 172 | template <typename T, size_t N> |
IanBenzMaxim | 11:3f3bf6bf5e6c | 173 | bool operator<=(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 174 | return !operator>(lhs, rhs); |
IanBenzMaxim | 7:9cd16581b578 | 175 | } |
IanBenzMaxim | 7:9cd16581b578 | 176 | |
IanBenzMaxim | 7:9cd16581b578 | 177 | template <typename T, size_t N> |
IanBenzMaxim | 11:3f3bf6bf5e6c | 178 | bool operator>=(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 179 | return !operator<(lhs, rhs); |
IanBenzMaxim | 7:9cd16581b578 | 180 | } |
IanBenzMaxim | 7:9cd16581b578 | 181 | |
IanBenzMaxim | 7:9cd16581b578 | 182 | template <typename T, size_t N> |
IanBenzMaxim | 11:3f3bf6bf5e6c | 183 | void swap(array<T, N> & lhs, array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 184 | lhs.swap(rhs); |
IanBenzMaxim | 7:9cd16581b578 | 185 | } |
IanBenzMaxim | 7:9cd16581b578 | 186 | |
IanBenzMaxim | 7:9cd16581b578 | 187 | } // namespace MaximInterfaceCore |
IanBenzMaxim | 7:9cd16581b578 | 188 | |
IanBenzMaxim | 7:9cd16581b578 | 189 | #endif |