mfbt/ArrayUtils.h

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:9d2ef9a7e3a5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /*
8 * Implements various helper functions related to arrays.
9 */
10
11 #ifndef mozilla_ArrayUtils_h
12 #define mozilla_ArrayUtils_h
13
14 #include "mozilla/Assertions.h"
15 #include "mozilla/Attributes.h"
16
17 #include <stddef.h>
18
19 #ifdef __cplusplus
20
21 #include "mozilla/Array.h"
22
23 namespace mozilla {
24
25 /*
26 * Safely subtract two pointers when it is known that end >= begin. This avoids
27 * the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB
28 * set, the unsigned subtraction followed by right shift will produce -1, or
29 * size_t(-1), instead of the real difference.
30 */
31 template<class T>
32 MOZ_ALWAYS_INLINE size_t
33 PointerRangeSize(T* begin, T* end)
34 {
35 MOZ_ASSERT(end >= begin);
36 return (size_t(end) - size_t(begin)) / sizeof(T);
37 }
38
39 /*
40 * Compute the length of an array with constant length. (Use of this method
41 * with a non-array pointer will not compile.)
42 *
43 * Beware of the implicit trailing '\0' when using this with string constants.
44 */
45 template<typename T, size_t N>
46 MOZ_CONSTEXPR size_t
47 ArrayLength(T (&arr)[N])
48 {
49 return N;
50 }
51
52 template<typename T, size_t N>
53 MOZ_CONSTEXPR size_t
54 ArrayLength(const Array<T, N>& arr)
55 {
56 return N;
57 }
58
59 /*
60 * Compute the address one past the last element of a constant-length array.
61 *
62 * Beware of the implicit trailing '\0' when using this with string constants.
63 */
64 template<typename T, size_t N>
65 MOZ_CONSTEXPR T*
66 ArrayEnd(T (&arr)[N])
67 {
68 return arr + ArrayLength(arr);
69 }
70
71 template<typename T, size_t N>
72 MOZ_CONSTEXPR T*
73 ArrayEnd(Array<T, N>& arr)
74 {
75 return &arr[0] + ArrayLength(arr);
76 }
77
78 template<typename T, size_t N>
79 MOZ_CONSTEXPR const T*
80 ArrayEnd(const Array<T, N>& arr)
81 {
82 return &arr[0] + ArrayLength(arr);
83 }
84
85 namespace detail {
86
87 /*
88 * Helper for the MOZ_ARRAY_LENGTH() macro to make the length a typesafe
89 * compile-time constant even on compilers lacking constexpr support.
90 */
91 template <typename T, size_t N>
92 char (&ArrayLengthHelper(T (&array)[N]))[N];
93
94 } /* namespace detail */
95
96 } /* namespace mozilla */
97
98 #endif /* __cplusplus */
99
100 /*
101 * MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
102 * that can't use C++ template functions and for static_assert() calls that
103 * can't call ArrayLength() when it is not a C++11 constexpr function.
104 */
105 #ifdef __cplusplus
106 # define MOZ_ARRAY_LENGTH(array) sizeof(mozilla::detail::ArrayLengthHelper(array))
107 #else
108 # define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
109 #endif
110
111 #endif /* mozilla_ArrayUtils_h */

mercurial