|
| 1 | +// -*-C++-*- array.hpp |
| 2 | +// <!!----------------------------------------------------------------------> |
| 3 | +// <!! Copyright (C) 1998 Dietmar Kuehl, Claas Solutions GmbH > |
| 4 | +// <!!> |
| 5 | +// <!! Permission to use, copy, modify, distribute and sell this > |
| 6 | +// <!! software for any purpose is hereby granted without fee, provided > |
| 7 | +// <!! that the above copyright notice appears in all copies and that > |
| 8 | +// <!! both that copyright notice and this permission notice appear in > |
| 9 | +// <!! supporting documentation. Dietmar Kuehl and Claas Solutions make no > |
| 10 | +// <!! representations about the suitability of this software for any > |
| 11 | +// <!! purpose. It is provided "as is" without express or implied warranty. > |
| 12 | +// <!!----------------------------------------------------------------------> |
| 13 | + |
| 14 | +// Author: Dietmar Kuehl [email protected] |
| 15 | +// Title: STL container support, including support for built-in arrays |
| 16 | +// Version: $Id$ |
| 17 | + |
| 18 | +// -------------------------------------------------------------------------- |
| 19 | + |
| 20 | +#if !defined(BOOST_ARRAY_HPP) |
| 21 | +#define BOOST_ARRAY_HPP 1 |
| 22 | + |
| 23 | +// -------------------------------------------------------------------------- |
| 24 | + |
| 25 | +#include <cstddef> |
| 26 | + |
| 27 | +// -------------------------------------------------------------------------- |
| 28 | + |
| 29 | +namespace boost |
| 30 | +{ |
| 31 | + |
| 32 | + // --- a general version of container traits ------------------------------ |
| 33 | + |
| 34 | + template <typename Cont> |
| 35 | + struct array_traits |
| 36 | + { |
| 37 | + typedef typename Cont::iterator iter_type; |
| 38 | + typedef typename Cont::size_type size_type; |
| 39 | + static iter_type begin(Cont &cont) { return cont.begin(); } |
| 40 | + static iter_type end(Cont &cont) { return cont.end(); } |
| 41 | + static size_type size(Cont &cont) { return cont.size(); } |
| 42 | + }; |
| 43 | + |
| 44 | + // --- a version of container traits for constant constainer -------------- |
| 45 | + |
| 46 | + template <typename Cont> |
| 47 | + struct array_traits<Cont const> |
| 48 | + { |
| 49 | + typedef typename Cont::const_iterator iter_type; |
| 50 | + typedef typename Cont::size_type size_type; |
| 51 | + static iter_type begin(Cont const &cont) { return cont.begin(); } |
| 52 | + static iter_type end(Cont const &cont) { return cont.end(); } |
| 53 | + static size_type size(Cont const &cont) { return cont.size(); } |
| 54 | + }; |
| 55 | + |
| 56 | + // --- a special version for non-const built-in arrays -------------------- |
| 57 | + |
| 58 | + template <typename T, size_t sz> |
| 59 | + struct array_traits<T[sz]> |
| 60 | + { |
| 61 | + typedef T* iter_type; |
| 62 | + typedef size_t size_type; |
| 63 | + static iter_type begin(T (&array)[sz]) { return array; } |
| 64 | + static iter_type end(T (&array)[sz]) { return array + sz; } |
| 65 | + static size_type size(T (&)[sz]) { return sz; } |
| 66 | + }; |
| 67 | + |
| 68 | + // --- a special version for const built-in arrays ------------------------ |
| 69 | + |
| 70 | + template <typename T, size_t sz> |
| 71 | + struct array_traits<T const[sz]> |
| 72 | + { |
| 73 | + typedef T const* iter_type; |
| 74 | + typedef size_t size_type; |
| 75 | + static iter_type begin(T const (&array)[sz]) { return array; } |
| 76 | + static iter_type end(T const (&array)[sz]) { return array + sz; } |
| 77 | + static size_type size(T const (&array)[sz]) { return sz; } |
| 78 | + }; |
| 79 | + |
| 80 | + template <typename T, int sz> |
| 81 | + inline char (&sizer(T (&)[sz]))[sz]; |
| 82 | + |
| 83 | + // --- general version of the global accessor functions --------------------- |
| 84 | + |
| 85 | + template <typename Cont> |
| 86 | + inline typename array_traits<Cont>::iter_type |
| 87 | + begin(Cont &cont) { return array_traits<Cont>::begin(cont); } |
| 88 | + |
| 89 | + template <typename Cont> |
| 90 | + inline typename array_traits<Cont>::iter_type |
| 91 | + end(Cont &cont) { return array_traits<Cont>::end(cont); } |
| 92 | + |
| 93 | + template <typename Cont> |
| 94 | + inline typename array_traits<Cont>::size_type |
| 95 | + size(Cont &cont) { return array_traits<Cont>::size(cont); } |
| 96 | + |
| 97 | + // --- Actually the above should be sufficient but compilers seem ----------- |
| 98 | + // --- to welcome some help. So here we go: |
| 99 | + |
| 100 | + template <typename T, size_t sz> |
| 101 | + inline typename array_traits<T[sz]>::iter_type |
| 102 | + begin(T (&a)[sz]) { return array_traits<T[sz]>::begin(a); } |
| 103 | + |
| 104 | + template <typename T, size_t sz> |
| 105 | + inline typename array_traits<T[sz]>::iter_type |
| 106 | + end(T (&a)[sz]) { return array_traits<T[sz]>::end(a); } |
| 107 | + |
| 108 | + template <typename T, size_t sz> |
| 109 | + inline typename array_traits<T[sz]>::size_type |
| 110 | + size(T (&a)[sz]) { return array_traits<T[sz]>::size(a); } |
| 111 | + |
| 112 | + // --- Apparently the compilers also need some specific help, --------------- |
| 113 | + |
| 114 | + // --- EDG-2.39 wants to pass around pointers in some contexts -------------- |
| 115 | +#ifdef __EDG__ |
| 116 | + template <typename T> |
| 117 | + struct array_traits<T*> |
| 118 | + { |
| 119 | + typedef T* iter_type; |
| 120 | + typedef size_t size_type; |
| 121 | + }; |
| 122 | +#endif |
| 123 | + |
| 124 | + // --- egcs-1998-11-22 apparently likes an extra const version: ------------- |
| 125 | +#ifdef __GNUG__ |
| 126 | + template <typename T, size_t sz> |
| 127 | + inline typename array_traits<T const[sz]>::iter_type |
| 128 | + begin(T const(&a)[sz]) { return array_traits<T const[sz]>::begin(a); } |
| 129 | + |
| 130 | + template <typename T, size_t sz> |
| 131 | + inline typename array_traits<T const[sz]>::iter_type |
| 132 | + end(T const(&a)[sz]) { return array_traits<T const[sz]>::end(a); } |
| 133 | + |
| 134 | + template <typename T, size_t sz> |
| 135 | + inline typename array_traits<T const[sz]>::size_type |
| 136 | + size(T const (&a)[sz]) { return array_traits<T const[sz]>::size(a); } |
| 137 | +#endif |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +// ----------------------------------------------------------------------------- |
| 142 | + |
| 143 | +#endif /* BOOST_ARRAY_HPP */ |
0 commit comments