PageRenderTime 31ms CodeModel.GetById 10ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/js/public/TemplateLib.h

http://github.com/zpao/v8monkey
C Header | 179 lines | 84 code | 20 blank | 75 comment | 0 complexity | 108172a2c47e21ab2cd0fc7b38bccc80 MD5 | raw file
  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:
  3 *
  4 * ***** BEGIN LICENSE BLOCK *****
  5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6 *
  7 * The contents of this file are subject to the Mozilla Public License Version
  8 * 1.1 (the "License"); you may not use this file except in compliance with
  9 * the License. You may obtain a copy of the License at
 10 * http://www.mozilla.org/MPL/
 11 *
 12 * Software distributed under the License is distributed on an "AS IS" basis,
 13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 14 * for the specific language governing rights and limitations under the
 15 * License.
 16 *
 17 * The Original Code is Mozilla SpiderMonkey JavaScript code.
 18 *
 19 * The Initial Developer of the Original Code is
 20 * the Mozilla Foundation.
 21 * Portions created by the Initial Developer are Copyright (C) 2011
 22 * the Initial Developer. All Rights Reserved.
 23 *
 24 * Contributor(s):
 25 *  Luke Wagner <luke@mozilla.com>
 26 *
 27 * Alternatively, the contents of this file may be used under the terms of
 28 * either the GNU General Public License Version 2 or later (the "GPL"), or
 29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 30 * in which case the provisions of the GPL or the LGPL are applicable instead
 31 * of those above. If you wish to allow use of your version of this file only
 32 * under the terms of either the GPL or the LGPL, and not to allow others to
 33 * use your version of this file under the terms of the MPL, indicate your
 34 * decision by deleting the provisions above and replace them with the notice
 35 * and other provisions required by the GPL or the LGPL. If you do not delete
 36 * the provisions above, a recipient may use your version of this file under
 37 * the terms of any one of the MPL, the GPL or the LGPL.
 38 *
 39 * ***** END LICENSE BLOCK ***** */
 40
 41#ifndef js_template_lib_h__
 42#define js_template_lib_h__
 43
 44#include "jstypes.h"
 45
 46/*
 47 * Library of reusable template meta-functions (that is, functions on types and
 48 * compile-time values). Meta-functions are placed inside the 'tl' namespace to
 49 * avoid conflict with non-meta functions that logically have the same name
 50 * (e.g., js::tl::Min vs. js::Min).
 51 */
 52
 53namespace js {
 54namespace tl {
 55
 56/* Compute min/max/clamp. */
 57template <size_t i, size_t j> struct Min {
 58    static const size_t result = i < j ? i : j;
 59};
 60template <size_t i, size_t j> struct Max {
 61    static const size_t result = i > j ? i : j;
 62};
 63template <size_t i, size_t min, size_t max> struct Clamp {
 64    static const size_t result = i < min ? min : (i > max ? max : i);
 65};
 66
 67/* Compute x^y. */
 68template <size_t x, size_t y> struct Pow {
 69    static const size_t result = x * Pow<x, y - 1>::result;
 70};
 71template <size_t x> struct Pow<x,0> {
 72    static const size_t result = 1;
 73};
 74
 75/* Compute floor(log2(i)). */
 76template <size_t i> struct FloorLog2 {
 77    static const size_t result = 1 + FloorLog2<i / 2>::result;
 78};
 79template <> struct FloorLog2<0> { /* Error */ };
 80template <> struct FloorLog2<1> { static const size_t result = 0; };
 81
 82/* Compute ceiling(log2(i)). */
 83template <size_t i> struct CeilingLog2 {
 84    static const size_t result = FloorLog2<2 * i - 1>::result;
 85};
 86
 87/* Round up to the nearest power of 2. */
 88template <size_t i> struct RoundUpPow2 {
 89    static const size_t result = size_t(1) << CeilingLog2<i>::result;
 90};
 91template <> struct RoundUpPow2<0> {
 92    static const size_t result = 1;
 93};
 94
 95/* Compute the number of bits in the given unsigned type. */
 96template <class T> struct BitSize {
 97    static const size_t result = sizeof(T) * JS_BITS_PER_BYTE;
 98};
 99
100/* Allow Assertions by only including the 'result' typedef if 'true'. */
101template <bool> struct StaticAssert {};
102template <> struct StaticAssert<true> { typedef int result; };
103
104/* Boolean test for whether two types are the same. */
105template <class T, class U> struct IsSameType {
106    static const bool result = false;
107};
108template <class T> struct IsSameType<T,T> {
109    static const bool result = true;
110};
111
112/*
113 * Produce an N-bit mask, where N <= BitSize<size_t>::result.  Handle the
114 * language-undefined edge case when N = BitSize<size_t>::result.
115 */
116template <size_t N> struct NBitMask {
117    typedef typename StaticAssert<N < BitSize<size_t>::result>::result _;
118    static const size_t result = (size_t(1) << N) - 1;
119};
120template <> struct NBitMask<BitSize<size_t>::result> {
121    static const size_t result = size_t(-1);
122};
123
124/*
125 * For the unsigned integral type size_t, compute a mask M for N such that
126 * for all X, !(X & M) implies X * N will not overflow (w.r.t size_t)
127 */
128template <size_t N> struct MulOverflowMask {
129    static const size_t result =
130        ~NBitMask<BitSize<size_t>::result - CeilingLog2<N>::result>::result;
131};
132template <> struct MulOverflowMask<0> { /* Error */ };
133template <> struct MulOverflowMask<1> { static const size_t result = 0; };
134
135/*
136 * Generate a mask for T such that if (X & sUnsafeRangeSizeMask), an X-sized
137 * array of T's is big enough to cause a ptrdiff_t overflow when subtracting
138 * a pointer to the end of the array from the beginning.
139 */
140template <class T> struct UnsafeRangeSizeMask {
141    /*
142     * The '2' factor means the top bit is clear, sizeof(T) converts from
143     * units of elements to bytes.
144     */
145    static const size_t result = MulOverflowMask<2 * sizeof(T)>::result;
146};
147
148/* Return T stripped of any const-ness. */
149template <class T> struct StripConst          { typedef T result; };
150template <class T> struct StripConst<const T> { typedef T result; };
151
152/*
153 * Traits class for identifying POD types. Until C++0x, there is no automatic
154 * way to detect PODs, so for the moment it is done manually.
155 */
156template <class T> struct IsPodType                 { static const bool result = false; };
157template <> struct IsPodType<char>                  { static const bool result = true; };
158template <> struct IsPodType<signed char>           { static const bool result = true; };
159template <> struct IsPodType<unsigned char>         { static const bool result = true; };
160template <> struct IsPodType<short>                 { static const bool result = true; };
161template <> struct IsPodType<unsigned short>        { static const bool result = true; };
162template <> struct IsPodType<int>                   { static const bool result = true; };
163template <> struct IsPodType<unsigned int>          { static const bool result = true; };
164template <> struct IsPodType<long>                  { static const bool result = true; };
165template <> struct IsPodType<unsigned long>         { static const bool result = true; };
166template <> struct IsPodType<long long>             { static const bool result = true; };
167template <> struct IsPodType<unsigned long long>    { static const bool result = true; };
168template <> struct IsPodType<float>                 { static const bool result = true; };
169template <> struct IsPodType<double>                { static const bool result = true; };
170template <> struct IsPodType<wchar_t>               { static const bool result = true; };
171template <typename T> struct IsPodType<T *>         { static const bool result = true; };
172
173template <bool cond, typename T, T v1, T v2> struct If        { static const T result = v1; };
174template <typename T, T v1, T v2> struct If<false, T, v1, v2> { static const T result = v2; };
175
176} /* namespace tl */
177} /* namespace js */
178
179#endif  /* js_template_lib_h__ */