1 // -*-C++-*- array_traits.hpp
2 // <!!---------------------------------------------------------------------->
3 // <!! Copyright (C) 1998 Dietmar Kuehl, Claas Solutions GmbH >
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 // <!!---------------------------------------------------------------------->
14 // Author: Dietmar Kuehl dietmar.kuehl@claas-solutions.de
15 // Title: STL container support, including support for built-in arrays
16 // Version: $Id: array_traits.hpp,v 1.3 2002/06/18 15:39:24 larsbj Exp $
18 // Dec 4, 2000 Added some more typedefs to array_traits including
19 // an iterator type to supersede iter_type. -J.Siek
21 // --------------------------------------------------------------------------
23 #if !defined(BOOST_ARRAY_TRAITS_HPP)
24 #define BOOST_ARRAY_TRAITS_HPP 1
26 // --------------------------------------------------------------------------
29 #include <boost/config.hpp>
31 // --------------------------------------------------------------------------
36 // --- a general version of container traits ------------------------------
38 template <typename Cont>
41 typedef typename Cont::iterator iterator;
42 typedef iterator iter_type; // just for backward compatibility
43 typedef typename Cont::size_type size_type;
44 typedef typename Cont::value_type value_type;
45 typedef typename Cont::reference reference;
46 typedef typename Cont::pointer pointer;
47 static iterator begin(Cont &cont) { return cont.begin(); }
48 static iterator end(Cont &cont) { return cont.end(); }
49 static size_type size(Cont &cont) { return cont.size(); }
52 // --- a version of container traits for constant constainer --------------
54 template <typename Cont>
55 struct array_traits<Cont const>
57 typedef typename Cont::const_iterator iterator;
58 typedef iterator iter_type; // just for backward compatibility
59 typedef typename Cont::size_type size_type;
60 typedef typename Cont::value_type value_type;
61 typedef typename Cont::const_reference reference;
62 typedef typename Cont::const_pointer pointer;
63 static iterator begin(Cont const &cont) { return cont.begin(); }
64 static iterator end(Cont const &cont) { return cont.end(); }
65 static size_type size(Cont const &cont) { return cont.size(); }
68 // --- a special version for non-const built-in arrays --------------------
70 template <typename T, std::size_t sz>
71 struct array_traits<T[sz]>
74 typedef iterator iter_type; // just for backward compatibility
76 typedef value_type& reference;
77 typedef std::size_t size_type;
78 static iterator begin(T (&array)[sz]) { return array; }
79 static iterator end(T (&array)[sz]) { return array + sz; }
80 static size_type size(T (&)[sz]) { return sz; }
83 // --- a special version for const built-in arrays ------------------------
85 template <typename T, std::size_t sz>
86 struct array_traits<T const[sz]>
88 typedef T const* iterator;
89 typedef iterator iter_type; // just for backward compatibility
90 typedef std::size_t size_type;
91 typedef T const value_type;
92 typedef value_type& reference;
93 typedef value_type* pointer;
94 static iterator begin(T const (&array)[sz]) { return array; }
95 static iterator end(T const (&array)[sz]) { return array + sz; }
96 static size_type size(T const (&)[sz]) { return sz; }
99 template <typename T, int sz>
100 inline char (&sizer(T (&)[sz]))[sz];
102 // --- general version of the global accessor functions ---------------------
104 template <typename Cont>
105 inline typename array_traits<Cont>::iterator
106 begin(Cont &cont) { return array_traits<Cont>::begin(cont); }
108 template <typename Cont>
109 inline typename array_traits<Cont>::iterator
110 end(Cont &cont) { return array_traits<Cont>::end(cont); }
112 template <typename Cont>
113 inline typename array_traits<Cont>::size_type
114 size(Cont &cont) { return array_traits<Cont>::size(cont); }
116 // --- Actually the above should be sufficient but compilers seem -----------
117 // --- to welcome some help. So here we go:
119 template <typename T, std::size_t sz>
120 inline typename array_traits<T[sz]>::iterator
121 begin(T (&a)[sz]) { return array_traits<T[sz]>::begin(a); }
123 template <typename T, std::size_t sz>
124 inline typename array_traits<T[sz]>::iterator
125 end(T (&a)[sz]) { return array_traits<T[sz]>::end(a); }
127 template <typename T, std::size_t sz>
128 inline typename array_traits<T[sz]>::size_type
129 size(T (&a)[sz]) { return array_traits<T[sz]>::size(a); }
131 // --- Apparently the compilers also need some specific help, ---------------
133 // --- EDG-2.39 wants to pass around pointers in some contexts --------------
135 template <typename T>
136 struct array_traits<T*>
139 typedef iterator iter_type; // just for backward compatibility
140 typedef std::size_t size_type;
144 // --- egcs-1998-11-22 apparently likes an extra const version: -------------
146 template <typename T, std::size_t sz>
147 inline typename array_traits<T const[sz]>::iterator
148 begin(T const(&a)[sz]) { return array_traits<T const[sz]>::begin(a); }
150 template <typename T, std::size_t sz>
151 inline typename array_traits<T const[sz]>::iterator
152 end(T const(&a)[sz]) { return array_traits<T const[sz]>::end(a); }
154 template <typename T, std::size_t sz>
155 inline typename array_traits<T const[sz]>::size_type
156 size(T const (&a)[sz]) { return array_traits<T const[sz]>::size(a); }
161 // -----------------------------------------------------------------------------
163 #endif /* BOOST_ARRAY_TRAITS_HPP */