2013-07-24 07:41:39 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-06-04 03:36:43 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 11:12:37 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-04-01 01:46:35 +00:00
|
|
|
|
2011-12-28 16:48:54 +00:00
|
|
|
/*
|
2013-12-09 02:52:54 +00:00
|
|
|
* Implements various helper functions related to arrays.
|
2011-12-28 16:48:54 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#ifndef mozilla_ArrayUtils_h
|
|
|
|
#define mozilla_ArrayUtils_h
|
2011-04-01 01:46:35 +00:00
|
|
|
|
2011-12-19 19:28:35 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2011-12-17 21:45:29 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-12-09 02:52:54 +00:00
|
|
|
|
|
|
|
#include <stddef.h>
|
2011-04-28 22:48:52 +00:00
|
|
|
|
2011-04-20 23:27:30 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2013-11-12 22:31:15 +00:00
|
|
|
#include "mozilla/Array.h"
|
2011-10-05 13:11:17 +00:00
|
|
|
|
2013-08-14 07:00:52 +00:00
|
|
|
namespace mozilla {
|
2011-04-28 22:48:52 +00:00
|
|
|
|
2011-06-06 18:02:34 +00:00
|
|
|
/*
|
|
|
|
* Safely subtract two pointers when it is known that end >= begin. This avoids
|
|
|
|
* the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB
|
|
|
|
* set, the unsigned subtraction followed by right shift will produce -1, or
|
|
|
|
* size_t(-1), instead of the real difference.
|
|
|
|
*/
|
2012-06-04 03:36:43 +00:00
|
|
|
template<class T>
|
2011-06-06 18:02:34 +00:00
|
|
|
MOZ_ALWAYS_INLINE size_t
|
|
|
|
PointerRangeSize(T* begin, T* end)
|
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
MOZ_ASSERT(end >= begin);
|
|
|
|
return (size_t(end) - size_t(begin)) / sizeof(T);
|
2011-06-06 18:02:34 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
/*
|
|
|
|
* Compute the length of an array with constant length. (Use of this method
|
|
|
|
* with a non-array pointer will not compile.)
|
|
|
|
*
|
|
|
|
* Beware of the implicit trailing '\0' when using this with string constants.
|
|
|
|
*/
|
|
|
|
template<typename T, size_t N>
|
2013-01-08 07:29:00 +00:00
|
|
|
MOZ_CONSTEXPR size_t
|
2011-10-11 05:50:08 +00:00
|
|
|
ArrayLength(T (&arr)[N])
|
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
return N;
|
2011-10-11 05:50:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 22:31:15 +00:00
|
|
|
template<typename T, size_t N>
|
|
|
|
MOZ_CONSTEXPR size_t
|
|
|
|
ArrayLength(const Array<T, N>& arr)
|
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
/*
|
|
|
|
* Compute the address one past the last element of a constant-length array.
|
|
|
|
*
|
|
|
|
* Beware of the implicit trailing '\0' when using this with string constants.
|
|
|
|
*/
|
|
|
|
template<typename T, size_t N>
|
2013-01-08 07:29:00 +00:00
|
|
|
MOZ_CONSTEXPR T*
|
2011-10-11 05:50:08 +00:00
|
|
|
ArrayEnd(T (&arr)[N])
|
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
return arr + ArrayLength(arr);
|
2011-10-11 05:50:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 22:31:15 +00:00
|
|
|
template<typename T, size_t N>
|
|
|
|
MOZ_CONSTEXPR T*
|
|
|
|
ArrayEnd(Array<T, N>& arr)
|
|
|
|
{
|
|
|
|
return &arr[0] + ArrayLength(arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N>
|
|
|
|
MOZ_CONSTEXPR const T*
|
|
|
|
ArrayEnd(const Array<T, N>& arr)
|
|
|
|
{
|
|
|
|
return &arr[0] + ArrayLength(arr);
|
|
|
|
}
|
|
|
|
|
2014-02-11 18:31:58 +00:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for the MOZ_ARRAY_LENGTH() macro to make the length a typesafe
|
|
|
|
* compile-time constant even on compilers lacking constexpr support.
|
|
|
|
*/
|
|
|
|
template <typename T, size_t N>
|
|
|
|
char (&ArrayLengthHelper(T (&array)[N]))[N];
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
|
2011-04-20 23:27:30 +00:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* __cplusplus */
|
2011-04-01 01:46:35 +00:00
|
|
|
|
2013-01-06 07:37:25 +00:00
|
|
|
/*
|
|
|
|
* MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
|
2013-07-18 17:59:53 +00:00
|
|
|
* that can't use C++ template functions and for static_assert() calls that
|
2013-01-06 07:37:25 +00:00
|
|
|
* can't call ArrayLength() when it is not a C++11 constexpr function.
|
|
|
|
*/
|
2014-02-11 18:31:58 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) sizeof(mozilla::detail::ArrayLengthHelper(array))
|
2013-01-08 07:29:00 +00:00
|
|
|
#else
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
|
|
|
|
#endif
|
2013-01-06 07:37:25 +00:00
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#endif /* mozilla_ArrayUtils_h */
|