1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99 ft=cpp:
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef js_template_lib_h__
9 #define js_template_lib_h__
14 * Library of reusable template meta-functions (that is, functions on types and
15 * compile-time values). Meta-functions are placed inside the 'tl' namespace to
16 * avoid conflict with non-meta functions that logically have the same name
17 * (e.g., js::tl::Min vs. js::Min).
23 /* Compute min/max/clamp. */
24 template <size_t i
, size_t j
> struct Min
{
25 static const size_t result
= i
< j
? i
: j
;
27 template <size_t i
, size_t j
> struct Max
{
28 static const size_t result
= i
> j
? i
: j
;
30 template <size_t i
, size_t min
, size_t max
> struct Clamp
{
31 static const size_t result
= i
< min
? min
: (i
> max
? max
: i
);
35 template <size_t x
, size_t y
> struct Pow
{
36 static const size_t result
= x
* Pow
<x
, y
- 1>::result
;
38 template <size_t x
> struct Pow
<x
,0> {
39 static const size_t result
= 1;
42 /* Compute floor(log2(i)). */
43 template <size_t i
> struct FloorLog2
{
44 static const size_t result
= 1 + FloorLog2
<i
/ 2>::result
;
46 template <> struct FloorLog2
<0> { /* Error */ };
47 template <> struct FloorLog2
<1> { static const size_t result
= 0; };
49 /* Compute ceiling(log2(i)). */
50 template <size_t i
> struct CeilingLog2
{
51 static const size_t result
= FloorLog2
<2 * i
- 1>::result
;
54 /* Round up to the nearest power of 2. */
55 template <size_t i
> struct RoundUpPow2
{
56 static const size_t result
= size_t(1) << CeilingLog2
<i
>::result
;
58 template <> struct RoundUpPow2
<0> {
59 static const size_t result
= 1;
62 /* Compute the number of bits in the given unsigned type. */
63 template <class T
> struct BitSize
{
64 static const size_t result
= sizeof(T
) * JS_BITS_PER_BYTE
;
67 /* Allow Assertions by only including the 'result' typedef if 'true'. */
68 template <bool> struct StaticAssert
{};
69 template <> struct StaticAssert
<true> { typedef int result
; };
71 /* Boolean test for whether two types are the same. */
72 template <class T
, class U
> struct IsSameType
{
73 static const bool result
= false;
75 template <class T
> struct IsSameType
<T
,T
> {
76 static const bool result
= true;
80 * Produce an N-bit mask, where N <= BitSize<size_t>::result. Handle the
81 * language-undefined edge case when N = BitSize<size_t>::result.
83 template <size_t N
> struct NBitMask
{
84 typedef typename StaticAssert
<N
< BitSize
<size_t>::result
>::result _
;
85 static const size_t result
= (size_t(1) << N
) - 1;
87 template <> struct NBitMask
<BitSize
<size_t>::result
> {
88 static const size_t result
= size_t(-1);
92 * For the unsigned integral type size_t, compute a mask M for N such that
93 * for all X, !(X & M) implies X * N will not overflow (w.r.t size_t)
95 template <size_t N
> struct MulOverflowMask
{
96 static const size_t result
=
97 ~NBitMask
<BitSize
<size_t>::result
- CeilingLog2
<N
>::result
>::result
;
99 template <> struct MulOverflowMask
<0> { /* Error */ };
100 template <> struct MulOverflowMask
<1> { static const size_t result
= 0; };
103 * Generate a mask for T such that if (X & sUnsafeRangeSizeMask), an X-sized
104 * array of T's is big enough to cause a ptrdiff_t overflow when subtracting
105 * a pointer to the end of the array from the beginning.
107 template <class T
> struct UnsafeRangeSizeMask
{
109 * The '2' factor means the top bit is clear, sizeof(T) converts from
110 * units of elements to bytes.
112 static const size_t result
= MulOverflowMask
<2 * sizeof(T
)>::result
;
115 /* Return T stripped of any const-ness. */
116 template <class T
> struct StripConst
{ typedef T result
; };
117 template <class T
> struct StripConst
<const T
> { typedef T result
; };
120 * Traits class for identifying POD types. Until C++0x, there is no automatic
121 * way to detect PODs, so for the moment it is done manually.
123 template <class T
> struct IsPodType
{ static const bool result
= false; };
124 template <> struct IsPodType
<char> { static const bool result
= true; };
125 template <> struct IsPodType
<signed char> { static const bool result
= true; };
126 template <> struct IsPodType
<unsigned char> { static const bool result
= true; };
127 template <> struct IsPodType
<short> { static const bool result
= true; };
128 template <> struct IsPodType
<unsigned short> { static const bool result
= true; };
129 template <> struct IsPodType
<int> { static const bool result
= true; };
130 template <> struct IsPodType
<unsigned int> { static const bool result
= true; };
131 template <> struct IsPodType
<long> { static const bool result
= true; };
132 template <> struct IsPodType
<unsigned long> { static const bool result
= true; };
133 template <> struct IsPodType
<long long> { static const bool result
= true; };
134 template <> struct IsPodType
<unsigned long long> { static const bool result
= true; };
135 template <> struct IsPodType
<bool> { static const bool result
= true; };
136 template <> struct IsPodType
<float> { static const bool result
= true; };
137 template <> struct IsPodType
<double> { static const bool result
= true; };
138 template <> struct IsPodType
<wchar_t> { static const bool result
= true; };
139 template <typename T
> struct IsPodType
<T
*> { static const bool result
= true; };
141 template <bool cond
, typename T
, T v1
, T v2
> struct If
{ static const T result
= v1
; };
142 template <typename T
, T v1
, T v2
> struct If
<false, T
, v1
, v2
> { static const T result
= v2
; };
144 template <class T
> struct IsPointerType
{ static const bool result
= false; };
145 template <class T
> struct IsPointerType
<T
*> { static const bool result
= true; };
148 * Traits class for identifying types that are implicitly barriered.
150 template <class T
> struct IsRelocatableHeapType
{ static const bool result
= true; };
155 #endif /* js_template_lib_h__ */