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

Committer:
IanBenzMaxim
Date:
Mon Nov 06 14:39:18 2017 -0600
Revision:
0:f77ad7f72d04
Child:
5:a8c83a2e6fa4
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 0:f77ad7f72d04 1 /*******************************************************************************
IanBenzMaxim 0:f77ad7f72d04 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 0:f77ad7f72d04 3 *
IanBenzMaxim 0:f77ad7f72d04 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 0:f77ad7f72d04 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 0:f77ad7f72d04 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 0:f77ad7f72d04 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 0:f77ad7f72d04 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 0:f77ad7f72d04 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 0:f77ad7f72d04 10 *
IanBenzMaxim 0:f77ad7f72d04 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 0:f77ad7f72d04 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 0:f77ad7f72d04 13 *
IanBenzMaxim 0:f77ad7f72d04 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 0:f77ad7f72d04 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 0:f77ad7f72d04 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 0:f77ad7f72d04 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 0:f77ad7f72d04 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 0:f77ad7f72d04 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 0:f77ad7f72d04 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 0:f77ad7f72d04 21 *
IanBenzMaxim 0:f77ad7f72d04 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 0:f77ad7f72d04 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 0:f77ad7f72d04 24 * Products, Inc. Branding Policy.
IanBenzMaxim 0:f77ad7f72d04 25 *
IanBenzMaxim 0:f77ad7f72d04 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 0:f77ad7f72d04 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 0:f77ad7f72d04 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 0:f77ad7f72d04 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 0:f77ad7f72d04 30 * ownership rights.
IanBenzMaxim 0:f77ad7f72d04 31 *******************************************************************************/
IanBenzMaxim 0:f77ad7f72d04 32
IanBenzMaxim 0:f77ad7f72d04 33 #ifndef MaximInterface_array
IanBenzMaxim 0:f77ad7f72d04 34 #define MaximInterface_array
IanBenzMaxim 0:f77ad7f72d04 35
IanBenzMaxim 0:f77ad7f72d04 36 #include <stddef.h>
IanBenzMaxim 0:f77ad7f72d04 37 #include <stdint.h>
IanBenzMaxim 0:f77ad7f72d04 38 #include <algorithm>
IanBenzMaxim 0:f77ad7f72d04 39 #include <iterator>
IanBenzMaxim 0:f77ad7f72d04 40
IanBenzMaxim 0:f77ad7f72d04 41 namespace MaximInterface {
IanBenzMaxim 0:f77ad7f72d04 42
IanBenzMaxim 0:f77ad7f72d04 43 /// Generic array class similar to std::array.
IanBenzMaxim 0:f77ad7f72d04 44 template <typename T, size_t N> class array {
IanBenzMaxim 0:f77ad7f72d04 45 public:
IanBenzMaxim 0:f77ad7f72d04 46 typedef T value_type;
IanBenzMaxim 0:f77ad7f72d04 47 typedef size_t size_type;
IanBenzMaxim 0:f77ad7f72d04 48 typedef ptrdiff_t difference_type;
IanBenzMaxim 0:f77ad7f72d04 49 typedef value_type & reference;
IanBenzMaxim 0:f77ad7f72d04 50 typedef const value_type & const_reference;
IanBenzMaxim 0:f77ad7f72d04 51 typedef value_type * pointer;
IanBenzMaxim 0:f77ad7f72d04 52 typedef const value_type * const_pointer;
IanBenzMaxim 0:f77ad7f72d04 53 typedef pointer iterator;
IanBenzMaxim 0:f77ad7f72d04 54 typedef const_pointer const_iterator;
IanBenzMaxim 0:f77ad7f72d04 55 typedef std::reverse_iterator<iterator> reverse_iterator;
IanBenzMaxim 0:f77ad7f72d04 56 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
IanBenzMaxim 0:f77ad7f72d04 57
IanBenzMaxim 0:f77ad7f72d04 58 // Element access
IanBenzMaxim 0:f77ad7f72d04 59 reference operator[](size_type pos) {
IanBenzMaxim 0:f77ad7f72d04 60 return const_cast<reference>(
IanBenzMaxim 0:f77ad7f72d04 61 static_cast<const array &>(*this).operator[](pos));
IanBenzMaxim 0:f77ad7f72d04 62 }
IanBenzMaxim 0:f77ad7f72d04 63 const_reference operator[](size_type pos) const { return _buffer[pos]; }
IanBenzMaxim 0:f77ad7f72d04 64 reference front() {
IanBenzMaxim 0:f77ad7f72d04 65 return const_cast<reference>(static_cast<const array &>(*this).front());
IanBenzMaxim 0:f77ad7f72d04 66 }
IanBenzMaxim 0:f77ad7f72d04 67 const_reference front() const { return _buffer[0]; }
IanBenzMaxim 0:f77ad7f72d04 68 reference back() {
IanBenzMaxim 0:f77ad7f72d04 69 return const_cast<reference>(static_cast<const array &>(*this).back());
IanBenzMaxim 0:f77ad7f72d04 70 }
IanBenzMaxim 0:f77ad7f72d04 71 const_reference back() const { return _buffer[size() - 1]; }
IanBenzMaxim 0:f77ad7f72d04 72 pointer data() {
IanBenzMaxim 0:f77ad7f72d04 73 return const_cast<pointer>(static_cast<const array &>(*this).data());
IanBenzMaxim 0:f77ad7f72d04 74 }
IanBenzMaxim 0:f77ad7f72d04 75 const_pointer data() const { return _buffer; }
IanBenzMaxim 0:f77ad7f72d04 76
IanBenzMaxim 0:f77ad7f72d04 77 // Iterators
IanBenzMaxim 0:f77ad7f72d04 78 iterator begin() {
IanBenzMaxim 0:f77ad7f72d04 79 return const_cast<iterator>(static_cast<const array &>(*this).cbegin());
IanBenzMaxim 0:f77ad7f72d04 80 }
IanBenzMaxim 0:f77ad7f72d04 81 const_iterator begin() const { return cbegin(); }
IanBenzMaxim 0:f77ad7f72d04 82 const_iterator cbegin() const { return _buffer; }
IanBenzMaxim 0:f77ad7f72d04 83 iterator end() {
IanBenzMaxim 0:f77ad7f72d04 84 return const_cast<iterator>(static_cast<const array &>(*this).cend());
IanBenzMaxim 0:f77ad7f72d04 85 }
IanBenzMaxim 0:f77ad7f72d04 86 const_iterator end() const { return cend(); }
IanBenzMaxim 0:f77ad7f72d04 87 const_iterator cend() const { return _buffer + size(); }
IanBenzMaxim 0:f77ad7f72d04 88 reverse_iterator rbegin() { return reverse_iterator(end()); }
IanBenzMaxim 0:f77ad7f72d04 89 const_reverse_iterator rbegin() const {
IanBenzMaxim 0:f77ad7f72d04 90 return const_reverse_iterator(end());
IanBenzMaxim 0:f77ad7f72d04 91 }
IanBenzMaxim 0:f77ad7f72d04 92 const_reverse_iterator crbegin() const { return rbegin(); }
IanBenzMaxim 0:f77ad7f72d04 93 reverse_iterator rend() { return reverse_iterator(begin()); }
IanBenzMaxim 0:f77ad7f72d04 94 const_reverse_iterator rend() const {
IanBenzMaxim 0:f77ad7f72d04 95 return const_reverse_iterator(begin());
IanBenzMaxim 0:f77ad7f72d04 96 }
IanBenzMaxim 0:f77ad7f72d04 97 const_reverse_iterator crend() const { return rend(); }
IanBenzMaxim 0:f77ad7f72d04 98
IanBenzMaxim 0:f77ad7f72d04 99 // Capacity
IanBenzMaxim 0:f77ad7f72d04 100 static bool empty() { return size() == 0; }
IanBenzMaxim 0:f77ad7f72d04 101 static size_type size() { return N; }
IanBenzMaxim 0:f77ad7f72d04 102 static size_type max_size() { return size(); }
IanBenzMaxim 0:f77ad7f72d04 103 static const size_type csize =
IanBenzMaxim 0:f77ad7f72d04 104 N; ///< Alternative to size() when a constant expression is required.
IanBenzMaxim 0:f77ad7f72d04 105
IanBenzMaxim 0:f77ad7f72d04 106 // Operations
IanBenzMaxim 0:f77ad7f72d04 107 void fill(const_reference value) { std::fill(begin(), end(), value); }
IanBenzMaxim 0:f77ad7f72d04 108 void swap(array & other) { std::swap_ranges(begin(), end(), other.begin()); }
IanBenzMaxim 0:f77ad7f72d04 109
IanBenzMaxim 0:f77ad7f72d04 110 T _buffer[N];
IanBenzMaxim 0:f77ad7f72d04 111 };
IanBenzMaxim 0:f77ad7f72d04 112
IanBenzMaxim 0:f77ad7f72d04 113 template <typename T, size_t N>
IanBenzMaxim 0:f77ad7f72d04 114 inline bool operator==(const array<T, N> & lhs, const array<T, N> & rhs) {
IanBenzMaxim 0:f77ad7f72d04 115 return std::equal(lhs.begin(), lhs.end(), rhs.begin());
IanBenzMaxim 0:f77ad7f72d04 116 }
IanBenzMaxim 0:f77ad7f72d04 117
IanBenzMaxim 0:f77ad7f72d04 118 template <typename T, size_t N>
IanBenzMaxim 0:f77ad7f72d04 119 inline bool operator!=(const array<T, N> & lhs, const array<T, N> & rhs) {
IanBenzMaxim 0:f77ad7f72d04 120 return !operator==(lhs, rhs);
IanBenzMaxim 0:f77ad7f72d04 121 }
IanBenzMaxim 0:f77ad7f72d04 122
IanBenzMaxim 0:f77ad7f72d04 123 template <typename T, size_t N>
IanBenzMaxim 0:f77ad7f72d04 124 inline bool operator<(const array<T, N> & lhs, const array<T, N> & rhs) {
IanBenzMaxim 0:f77ad7f72d04 125 return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(),
IanBenzMaxim 0:f77ad7f72d04 126 rhs.end());
IanBenzMaxim 0:f77ad7f72d04 127 }
IanBenzMaxim 0:f77ad7f72d04 128
IanBenzMaxim 0:f77ad7f72d04 129 template <typename T, size_t N>
IanBenzMaxim 0:f77ad7f72d04 130 inline bool operator>(const array<T, N> & lhs, const array<T, N> & rhs) {
IanBenzMaxim 0:f77ad7f72d04 131 return operator<(rhs, lhs);
IanBenzMaxim 0:f77ad7f72d04 132 }
IanBenzMaxim 0:f77ad7f72d04 133
IanBenzMaxim 0:f77ad7f72d04 134 template <typename T, size_t N>
IanBenzMaxim 0:f77ad7f72d04 135 inline bool operator<=(const array<T, N> & lhs, const array<T, N> & rhs) {
IanBenzMaxim 0:f77ad7f72d04 136 return !operator>(lhs, rhs);
IanBenzMaxim 0:f77ad7f72d04 137 }
IanBenzMaxim 0:f77ad7f72d04 138
IanBenzMaxim 0:f77ad7f72d04 139 template <typename T, size_t N>
IanBenzMaxim 0:f77ad7f72d04 140 inline bool operator>=(const array<T, N> & lhs, const array<T, N> & rhs) {
IanBenzMaxim 0:f77ad7f72d04 141 return !operator<(lhs, rhs);
IanBenzMaxim 0:f77ad7f72d04 142 }
IanBenzMaxim 0:f77ad7f72d04 143
IanBenzMaxim 0:f77ad7f72d04 144 template <typename T, size_t N>
IanBenzMaxim 0:f77ad7f72d04 145 inline void swap(array<T, N> & lhs, array<T, N> & rhs) {
IanBenzMaxim 0:f77ad7f72d04 146 lhs.swap(rhs);
IanBenzMaxim 0:f77ad7f72d04 147 }
IanBenzMaxim 0:f77ad7f72d04 148
IanBenzMaxim 0:f77ad7f72d04 149 } // namespace MaximInterface
IanBenzMaxim 0:f77ad7f72d04 150
IanBenzMaxim 0:f77ad7f72d04 151 #endif