/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. #ifndef js_template_lib_h__
  41. #define js_template_lib_h__
  42. #include "jstypes.h"
  43. /*
  44. * Library of reusable template meta-functions (that is, functions on types and
  45. * compile-time values). Meta-functions are placed inside the 'tl' namespace to
  46. * avoid conflict with non-meta functions that logically have the same name
  47. * (e.g., js::tl::Min vs. js::Min).
  48. */
  49. namespace js {
  50. namespace tl {
  51. /* Compute min/max/clamp. */
  52. template <size_t i, size_t j> struct Min {
  53. static const size_t result = i < j ? i : j;
  54. };
  55. template <size_t i, size_t j> struct Max {
  56. static const size_t result = i > j ? i : j;
  57. };
  58. template <size_t i, size_t min, size_t max> struct Clamp {
  59. static const size_t result = i < min ? min : (i > max ? max : i);
  60. };
  61. /* Compute x^y. */
  62. template <size_t x, size_t y> struct Pow {
  63. static const size_t result = x * Pow<x, y - 1>::result;
  64. };
  65. template <size_t x> struct Pow<x,0> {
  66. static const size_t result = 1;
  67. };
  68. /* Compute floor(log2(i)). */
  69. template <size_t i> struct FloorLog2 {
  70. static const size_t result = 1 + FloorLog2<i / 2>::result;
  71. };
  72. template <> struct FloorLog2<0> { /* Error */ };
  73. template <> struct FloorLog2<1> { static const size_t result = 0; };
  74. /* Compute ceiling(log2(i)). */
  75. template <size_t i> struct CeilingLog2 {
  76. static const size_t result = FloorLog2<2 * i - 1>::result;
  77. };
  78. /* Round up to the nearest power of 2. */
  79. template <size_t i> struct RoundUpPow2 {
  80. static const size_t result = size_t(1) << CeilingLog2<i>::result;
  81. };
  82. template <> struct RoundUpPow2<0> {
  83. static const size_t result = 1;
  84. };
  85. /* Compute the number of bits in the given unsigned type. */
  86. template <class T> struct BitSize {
  87. static const size_t result = sizeof(T) * JS_BITS_PER_BYTE;
  88. };
  89. /* Allow Assertions by only including the 'result' typedef if 'true'. */
  90. template <bool> struct StaticAssert {};
  91. template <> struct StaticAssert<true> { typedef int result; };
  92. /* Boolean test for whether two types are the same. */
  93. template <class T, class U> struct IsSameType {
  94. static const bool result = false;
  95. };
  96. template <class T> struct IsSameType<T,T> {
  97. static const bool result = true;
  98. };
  99. /*
  100. * Produce an N-bit mask, where N <= BitSize<size_t>::result. Handle the
  101. * language-undefined edge case when N = BitSize<size_t>::result.
  102. */
  103. template <size_t N> struct NBitMask {
  104. typedef typename StaticAssert<N < BitSize<size_t>::result>::result _;
  105. static const size_t result = (size_t(1) << N) - 1;
  106. };
  107. template <> struct NBitMask<BitSize<size_t>::result> {
  108. static const size_t result = size_t(-1);
  109. };
  110. /*
  111. * For the unsigned integral type size_t, compute a mask M for N such that
  112. * for all X, !(X & M) implies X * N will not overflow (w.r.t size_t)
  113. */
  114. template <size_t N> struct MulOverflowMask {
  115. static const size_t result =
  116. ~NBitMask<BitSize<size_t>::result - CeilingLog2<N>::result>::result;
  117. };
  118. template <> struct MulOverflowMask<0> { /* Error */ };
  119. template <> struct MulOverflowMask<1> { static const size_t result = 0; };
  120. /*
  121. * Generate a mask for T such that if (X & sUnsafeRangeSizeMask), an X-sized
  122. * array of T's is big enough to cause a ptrdiff_t overflow when subtracting
  123. * a pointer to the end of the array from the beginning.
  124. */
  125. template <class T> struct UnsafeRangeSizeMask {
  126. /*
  127. * The '2' factor means the top bit is clear, sizeof(T) converts from
  128. * units of elements to bytes.
  129. */
  130. static const size_t result = MulOverflowMask<2 * sizeof(T)>::result;
  131. };
  132. /* Return T stripped of any const-ness. */
  133. template <class T> struct StripConst { typedef T result; };
  134. template <class T> struct StripConst<const T> { typedef T result; };
  135. /*
  136. * Traits class for identifying POD types. Until C++0x, there is no automatic
  137. * way to detect PODs, so for the moment it is done manually.
  138. */
  139. template <class T> struct IsPodType { static const bool result = false; };
  140. template <> struct IsPodType<char> { static const bool result = true; };
  141. template <> struct IsPodType<signed char> { static const bool result = true; };
  142. template <> struct IsPodType<unsigned char> { static const bool result = true; };
  143. template <> struct IsPodType<short> { static const bool result = true; };
  144. template <> struct IsPodType<unsigned short> { static const bool result = true; };
  145. template <> struct IsPodType<int> { static const bool result = true; };
  146. template <> struct IsPodType<unsigned int> { static const bool result = true; };
  147. template <> struct IsPodType<long> { static const bool result = true; };
  148. template <> struct IsPodType<unsigned long> { static const bool result = true; };
  149. template <> struct IsPodType<long long> { static const bool result = true; };
  150. template <> struct IsPodType<unsigned long long> { static const bool result = true; };
  151. template <> struct IsPodType<float> { static const bool result = true; };
  152. template <> struct IsPodType<double> { static const bool result = true; };
  153. template <> struct IsPodType<wchar_t> { static const bool result = true; };
  154. template <typename T> struct IsPodType<T *> { static const bool result = true; };
  155. template <bool cond, typename T, T v1, T v2> struct If { static const T result = v1; };
  156. template <typename T, T v1, T v2> struct If<false, T, v1, v2> { static const T result = v2; };
  157. } /* namespace tl */
  158. } /* namespace js */
  159. #endif /* js_template_lib_h__ */