~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/include/mozilla_chardet/mfbt/mozilla/Util.h

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * Miscellaneous uncategorized functionality.  Please add new functionality to
 
9
 * new headers, or to other appropriate existing headers, not here.
 
10
 */
 
11
 
 
12
#ifndef mozilla_Util_h
 
13
#define mozilla_Util_h
 
14
 
 
15
#include "mozilla/Assertions.h"
 
16
#include "mozilla/Attributes.h"
 
17
#include "mozilla/Types.h"
 
18
 
 
19
#ifdef __cplusplus
 
20
 
 
21
#include "mozilla/Alignment.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
/*
 
53
 * Compute the address one past the last element of a constant-length array.
 
54
 *
 
55
 * Beware of the implicit trailing '\0' when using this with string constants.
 
56
 */
 
57
template<typename T, size_t N>
 
58
MOZ_CONSTEXPR T*
 
59
ArrayEnd(T (&arr)[N])
 
60
{
 
61
  return arr + ArrayLength(arr);
 
62
}
 
63
 
 
64
} /* namespace mozilla */
 
65
 
 
66
#endif /* __cplusplus */
 
67
 
 
68
/*
 
69
 * MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
 
70
 * that can't use C++ template functions and for static_assert() calls that
 
71
 * can't call ArrayLength() when it is not a C++11 constexpr function.
 
72
 */
 
73
#ifdef MOZ_HAVE_CXX11_CONSTEXPR
 
74
#  define MOZ_ARRAY_LENGTH(array)   mozilla::ArrayLength(array)
 
75
#else
 
76
#  define MOZ_ARRAY_LENGTH(array)   (sizeof(array)/sizeof((array)[0]))
 
77
#endif
 
78
 
 
79
#endif /* mozilla_Util_h */