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@8:5ea891c7d1a1, 2019-09-16 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Sep 16 11:13:37 2019 -0500
- Revision:
- 8:5ea891c7d1a1
- Parent:
- 7:9cd16581b578
- Child:
- 11:3f3bf6bf5e6c
Updated to version 2.0.
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 | 7:9cd16581b578 | 40 | |
IanBenzMaxim | 7:9cd16581b578 | 41 | namespace MaximInterfaceCore { |
IanBenzMaxim | 7:9cd16581b578 | 42 | |
IanBenzMaxim | 7:9cd16581b578 | 43 | /// Generic array class similar to std::array. |
IanBenzMaxim | 7:9cd16581b578 | 44 | template <typename T, size_t N> class array { |
IanBenzMaxim | 7:9cd16581b578 | 45 | public: |
IanBenzMaxim | 7:9cd16581b578 | 46 | typedef T value_type; |
IanBenzMaxim | 7:9cd16581b578 | 47 | typedef size_t size_type; |
IanBenzMaxim | 7:9cd16581b578 | 48 | typedef ptrdiff_t difference_type; |
IanBenzMaxim | 7:9cd16581b578 | 49 | typedef value_type & reference; |
IanBenzMaxim | 7:9cd16581b578 | 50 | typedef const value_type & const_reference; |
IanBenzMaxim | 7:9cd16581b578 | 51 | typedef value_type * pointer; |
IanBenzMaxim | 7:9cd16581b578 | 52 | typedef const value_type * const_pointer; |
IanBenzMaxim | 7:9cd16581b578 | 53 | typedef pointer iterator; |
IanBenzMaxim | 7:9cd16581b578 | 54 | typedef const_pointer const_iterator; |
IanBenzMaxim | 7:9cd16581b578 | 55 | typedef std::reverse_iterator<iterator> reverse_iterator; |
IanBenzMaxim | 7:9cd16581b578 | 56 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
IanBenzMaxim | 7:9cd16581b578 | 57 | |
IanBenzMaxim | 7:9cd16581b578 | 58 | /// @name Element access |
IanBenzMaxim | 7:9cd16581b578 | 59 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 60 | |
IanBenzMaxim | 7:9cd16581b578 | 61 | reference operator[](size_type pos) { |
IanBenzMaxim | 7:9cd16581b578 | 62 | return const_cast<reference>( |
IanBenzMaxim | 7:9cd16581b578 | 63 | static_cast<const array &>(*this).operator[](pos)); |
IanBenzMaxim | 7:9cd16581b578 | 64 | } |
IanBenzMaxim | 7:9cd16581b578 | 65 | |
IanBenzMaxim | 7:9cd16581b578 | 66 | const_reference operator[](size_type pos) const { return data()[pos]; } |
IanBenzMaxim | 7:9cd16581b578 | 67 | |
IanBenzMaxim | 7:9cd16581b578 | 68 | reference front() { |
IanBenzMaxim | 7:9cd16581b578 | 69 | return const_cast<reference>(static_cast<const array &>(*this).front()); |
IanBenzMaxim | 7:9cd16581b578 | 70 | } |
IanBenzMaxim | 7:9cd16581b578 | 71 | |
IanBenzMaxim | 7:9cd16581b578 | 72 | const_reference front() const { return operator[](0); } |
IanBenzMaxim | 7:9cd16581b578 | 73 | |
IanBenzMaxim | 7:9cd16581b578 | 74 | reference back() { |
IanBenzMaxim | 7:9cd16581b578 | 75 | return const_cast<reference>(static_cast<const array &>(*this).back()); |
IanBenzMaxim | 7:9cd16581b578 | 76 | } |
IanBenzMaxim | 7:9cd16581b578 | 77 | |
IanBenzMaxim | 7:9cd16581b578 | 78 | const_reference back() const { return operator[](size() - 1); } |
IanBenzMaxim | 7:9cd16581b578 | 79 | |
IanBenzMaxim | 7:9cd16581b578 | 80 | pointer data() { |
IanBenzMaxim | 7:9cd16581b578 | 81 | return const_cast<pointer>(static_cast<const array &>(*this).data()); |
IanBenzMaxim | 7:9cd16581b578 | 82 | } |
IanBenzMaxim | 7:9cd16581b578 | 83 | |
IanBenzMaxim | 7:9cd16581b578 | 84 | const_pointer data() const { return _buffer; } |
IanBenzMaxim | 7:9cd16581b578 | 85 | |
IanBenzMaxim | 7:9cd16581b578 | 86 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 87 | |
IanBenzMaxim | 7:9cd16581b578 | 88 | /// @name Iterators |
IanBenzMaxim | 7:9cd16581b578 | 89 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 90 | |
IanBenzMaxim | 7:9cd16581b578 | 91 | iterator begin() { |
IanBenzMaxim | 7:9cd16581b578 | 92 | return const_cast<iterator>(static_cast<const array &>(*this).cbegin()); |
IanBenzMaxim | 7:9cd16581b578 | 93 | } |
IanBenzMaxim | 7:9cd16581b578 | 94 | |
IanBenzMaxim | 7:9cd16581b578 | 95 | const_iterator begin() const { return cbegin(); } |
IanBenzMaxim | 7:9cd16581b578 | 96 | |
IanBenzMaxim | 7:9cd16581b578 | 97 | const_iterator cbegin() const { return data(); } |
IanBenzMaxim | 7:9cd16581b578 | 98 | |
IanBenzMaxim | 7:9cd16581b578 | 99 | iterator end() { |
IanBenzMaxim | 7:9cd16581b578 | 100 | return const_cast<iterator>(static_cast<const array &>(*this).cend()); |
IanBenzMaxim | 7:9cd16581b578 | 101 | } |
IanBenzMaxim | 7:9cd16581b578 | 102 | |
IanBenzMaxim | 7:9cd16581b578 | 103 | const_iterator end() const { return cend(); } |
IanBenzMaxim | 7:9cd16581b578 | 104 | |
IanBenzMaxim | 7:9cd16581b578 | 105 | const_iterator cend() const { return cbegin() + size(); } |
IanBenzMaxim | 7:9cd16581b578 | 106 | |
IanBenzMaxim | 7:9cd16581b578 | 107 | reverse_iterator rbegin() { return reverse_iterator(end()); } |
IanBenzMaxim | 7:9cd16581b578 | 108 | |
IanBenzMaxim | 7:9cd16581b578 | 109 | const_reverse_iterator rbegin() const { |
IanBenzMaxim | 7:9cd16581b578 | 110 | return const_reverse_iterator(end()); |
IanBenzMaxim | 7:9cd16581b578 | 111 | } |
IanBenzMaxim | 7:9cd16581b578 | 112 | |
IanBenzMaxim | 7:9cd16581b578 | 113 | const_reverse_iterator crbegin() const { return rbegin(); } |
IanBenzMaxim | 7:9cd16581b578 | 114 | |
IanBenzMaxim | 7:9cd16581b578 | 115 | reverse_iterator rend() { return reverse_iterator(begin()); } |
IanBenzMaxim | 7:9cd16581b578 | 116 | |
IanBenzMaxim | 7:9cd16581b578 | 117 | const_reverse_iterator rend() const { |
IanBenzMaxim | 7:9cd16581b578 | 118 | return const_reverse_iterator(begin()); |
IanBenzMaxim | 7:9cd16581b578 | 119 | } |
IanBenzMaxim | 7:9cd16581b578 | 120 | |
IanBenzMaxim | 7:9cd16581b578 | 121 | const_reverse_iterator crend() const { return rend(); } |
IanBenzMaxim | 7:9cd16581b578 | 122 | |
IanBenzMaxim | 7:9cd16581b578 | 123 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 124 | |
IanBenzMaxim | 7:9cd16581b578 | 125 | /// @name Capacity |
IanBenzMaxim | 7:9cd16581b578 | 126 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 127 | |
IanBenzMaxim | 7:9cd16581b578 | 128 | static bool empty() { return size() == 0; } |
IanBenzMaxim | 7:9cd16581b578 | 129 | |
IanBenzMaxim | 7:9cd16581b578 | 130 | static size_type size() { return N; } |
IanBenzMaxim | 7:9cd16581b578 | 131 | |
IanBenzMaxim | 7:9cd16581b578 | 132 | static size_type max_size() { return size(); } |
IanBenzMaxim | 7:9cd16581b578 | 133 | |
IanBenzMaxim | 7:9cd16581b578 | 134 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 135 | |
IanBenzMaxim | 7:9cd16581b578 | 136 | /// @name Operations |
IanBenzMaxim | 7:9cd16581b578 | 137 | /// @{ |
IanBenzMaxim | 7:9cd16581b578 | 138 | |
IanBenzMaxim | 7:9cd16581b578 | 139 | void fill(const_reference value) { std::fill(begin(), end(), value); } |
IanBenzMaxim | 7:9cd16581b578 | 140 | |
IanBenzMaxim | 7:9cd16581b578 | 141 | void swap(array & other) { std::swap_ranges(begin(), end(), other.begin()); } |
IanBenzMaxim | 7:9cd16581b578 | 142 | |
IanBenzMaxim | 7:9cd16581b578 | 143 | /// @} |
IanBenzMaxim | 7:9cd16581b578 | 144 | |
IanBenzMaxim | 7:9cd16581b578 | 145 | /// @private |
IanBenzMaxim | 7:9cd16581b578 | 146 | /// @note Implementation detail set public to allow aggregate initialization. |
IanBenzMaxim | 7:9cd16581b578 | 147 | T _buffer[N]; |
IanBenzMaxim | 7:9cd16581b578 | 148 | }; |
IanBenzMaxim | 7:9cd16581b578 | 149 | |
IanBenzMaxim | 7:9cd16581b578 | 150 | template <typename T, size_t N> |
IanBenzMaxim | 7:9cd16581b578 | 151 | inline bool operator==(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 152 | return std::equal(lhs.begin(), lhs.end(), rhs.begin()); |
IanBenzMaxim | 7:9cd16581b578 | 153 | } |
IanBenzMaxim | 7:9cd16581b578 | 154 | |
IanBenzMaxim | 7:9cd16581b578 | 155 | template <typename T, size_t N> |
IanBenzMaxim | 7:9cd16581b578 | 156 | inline bool operator!=(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 157 | return !operator==(lhs, rhs); |
IanBenzMaxim | 7:9cd16581b578 | 158 | } |
IanBenzMaxim | 7:9cd16581b578 | 159 | |
IanBenzMaxim | 7:9cd16581b578 | 160 | template <typename T, size_t N> |
IanBenzMaxim | 7:9cd16581b578 | 161 | inline bool operator<(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 162 | return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), |
IanBenzMaxim | 7:9cd16581b578 | 163 | rhs.end()); |
IanBenzMaxim | 7:9cd16581b578 | 164 | } |
IanBenzMaxim | 7:9cd16581b578 | 165 | |
IanBenzMaxim | 7:9cd16581b578 | 166 | template <typename T, size_t N> |
IanBenzMaxim | 7:9cd16581b578 | 167 | inline bool operator>(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 168 | return operator<(rhs, lhs); |
IanBenzMaxim | 7:9cd16581b578 | 169 | } |
IanBenzMaxim | 7:9cd16581b578 | 170 | |
IanBenzMaxim | 7:9cd16581b578 | 171 | template <typename T, size_t N> |
IanBenzMaxim | 7:9cd16581b578 | 172 | inline bool operator<=(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 173 | return !operator>(lhs, rhs); |
IanBenzMaxim | 7:9cd16581b578 | 174 | } |
IanBenzMaxim | 7:9cd16581b578 | 175 | |
IanBenzMaxim | 7:9cd16581b578 | 176 | template <typename T, size_t N> |
IanBenzMaxim | 7:9cd16581b578 | 177 | inline bool operator>=(const array<T, N> & lhs, const array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 178 | return !operator<(lhs, rhs); |
IanBenzMaxim | 7:9cd16581b578 | 179 | } |
IanBenzMaxim | 7:9cd16581b578 | 180 | |
IanBenzMaxim | 7:9cd16581b578 | 181 | template <typename T, size_t N> |
IanBenzMaxim | 7:9cd16581b578 | 182 | inline void swap(array<T, N> & lhs, array<T, N> & rhs) { |
IanBenzMaxim | 7:9cd16581b578 | 183 | lhs.swap(rhs); |
IanBenzMaxim | 7:9cd16581b578 | 184 | } |
IanBenzMaxim | 7:9cd16581b578 | 185 | |
IanBenzMaxim | 7:9cd16581b578 | 186 | } // namespace MaximInterfaceCore |
IanBenzMaxim | 7:9cd16581b578 | 187 | |
IanBenzMaxim | 7:9cd16581b578 | 188 | #endif |