Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers array.hpp Source File

array.hpp

00001 /*******************************************************************************
00002 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #ifndef MaximInterfaceCore_array_hpp
00034 #define MaximInterfaceCore_array_hpp
00035 
00036 #include <stddef.h>
00037 #include <stdint.h>
00038 #include <algorithm>
00039 #include <iterator>
00040 #include "type_traits.hpp"
00041 
00042 namespace MaximInterfaceCore {
00043 
00044 /// Generic array class similar to std::array.
00045 template <typename T, size_t N> class array {
00046 public:
00047   typedef T value_type;
00048   typedef size_t size_type;
00049   typedef ptrdiff_t difference_type;
00050   typedef value_type & reference;
00051   typedef const value_type & const_reference;
00052   typedef value_type * pointer;
00053   typedef const value_type * const_pointer;
00054   typedef pointer iterator;
00055   typedef const_pointer const_iterator;
00056   typedef std::reverse_iterator<iterator> reverse_iterator;
00057   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00058 
00059   /// @name Element access
00060   /// @{
00061 
00062   reference operator[](size_type pos) {
00063     return const_cast<reference>(
00064         static_cast<const array &>(*this).operator[](pos));
00065   }
00066 
00067   const_reference operator[](size_type pos) const { return data()[pos]; }
00068 
00069   reference front() {
00070     return const_cast<reference>(static_cast<const array &>(*this).front());
00071   }
00072 
00073   const_reference front() const { return operator[](0); }
00074 
00075   reference back() {
00076     return const_cast<reference>(static_cast<const array &>(*this).back());
00077   }
00078 
00079   const_reference back() const { return operator[](N - 1); }
00080 
00081   pointer data() {
00082     return const_cast<pointer>(static_cast<const array &>(*this).data());
00083   }
00084 
00085   const_pointer data() const { return reinterpret_cast<const_pointer>(&data_); }
00086 
00087   /// @}
00088 
00089   /// @name Iterators
00090   /// @{
00091 
00092   iterator begin() {
00093     return const_cast<iterator>(static_cast<const array &>(*this).cbegin());
00094   }
00095 
00096   const_iterator begin() const { return cbegin(); }
00097 
00098   const_iterator cbegin() const { return data(); }
00099 
00100   iterator end() {
00101     return const_cast<iterator>(static_cast<const array &>(*this).cend());
00102   }
00103 
00104   const_iterator end() const { return cend(); }
00105 
00106   const_iterator cend() const { return cbegin() + N; }
00107 
00108   reverse_iterator rbegin() { return reverse_iterator(end()); }
00109 
00110   const_reverse_iterator rbegin() const {
00111     return const_reverse_iterator(end());
00112   }
00113 
00114   const_reverse_iterator crbegin() const { return rbegin(); }
00115 
00116   reverse_iterator rend() { return reverse_iterator(begin()); }
00117 
00118   const_reverse_iterator rend() const {
00119     return const_reverse_iterator(begin());
00120   }
00121 
00122   const_reverse_iterator crend() const { return rend(); }
00123 
00124   /// @}
00125 
00126   /// @name Capacity
00127   /// @{
00128 
00129   static bool empty() { return N == 0; }
00130 
00131   static size_type size() { return N; }
00132 
00133   static size_type max_size() { return N; }
00134 
00135   /// @}
00136 
00137   /// @name Operations
00138   /// @{
00139 
00140   void fill(const_reference value) { std::fill(begin(), end(), value); }
00141 
00142   void swap(array & other) { std::swap_ranges(begin(), end(), other.begin()); }
00143 
00144   /// @}
00145 
00146   /// @private
00147   /// @note Implementation detail set public to allow aggregate initialization.
00148   typename conditional<N == 0, char, T[N]>::type data_;
00149 };
00150 
00151 template <typename T, size_t N>
00152 bool operator==(const array<T, N> & lhs, const array<T, N> & rhs) {
00153   return std::equal(lhs.begin(), lhs.end(), rhs.begin());
00154 }
00155 
00156 template <typename T, size_t N>
00157 bool operator!=(const array<T, N> & lhs, const array<T, N> & rhs) {
00158   return !operator==(lhs, rhs);
00159 }
00160 
00161 template <typename T, size_t N>
00162 bool operator<(const array<T, N> & lhs, const array<T, N> & rhs) {
00163   return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(),
00164                                       rhs.end());
00165 }
00166 
00167 template <typename T, size_t N>
00168 bool operator>(const array<T, N> & lhs, const array<T, N> & rhs) {
00169   return operator<(rhs, lhs);
00170 }
00171 
00172 template <typename T, size_t N>
00173 bool operator<=(const array<T, N> & lhs, const array<T, N> & rhs) {
00174   return !operator>(lhs, rhs);
00175 }
00176 
00177 template <typename T, size_t N>
00178 bool operator>=(const array<T, N> & lhs, const array<T, N> & rhs) {
00179   return !operator<(lhs, rhs);
00180 }
00181 
00182 template <typename T, size_t N>
00183 void swap(array<T, N> & lhs, array<T, N> & rhs) {
00184   lhs.swap(rhs);
00185 }
00186 
00187 } // namespace MaximInterfaceCore
00188 
00189 #endif