/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * vim: set ts=8 sw=4 et tw=99 ft=cpp: * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released * July 16, 2009. * * The Initial Developer of the Original Code is * the Mozilla Corporation. * * Contributor(s): * Luke Wagner * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ #ifndef jstl_h_ #define jstl_h_ #include "jspubtd.h" #include "jsbit.h" #include "jsstaticcheck.h" #include "jsstdint.h" #include #include namespace js { /* JavaScript Template Library. */ namespace tl { /* Compute min/max/clamp. */ template struct Min { static const size_t result = i < j ? i : j; }; template struct Max { static const size_t result = i > j ? i : j; }; template struct Clamp { static const size_t result = i < min ? min : (i > max ? max : i); }; /* Compute x^y. */ template struct Pow { static const size_t result = x * Pow::result; }; template struct Pow { static const size_t result = 1; }; /* Compute floor(log2(i)). */ template struct FloorLog2 { static const size_t result = 1 + FloorLog2::result; }; template <> struct FloorLog2<0> { /* Error */ }; template <> struct FloorLog2<1> { static const size_t result = 0; }; /* Compute ceiling(log2(i)). */ template struct CeilingLog2 { static const size_t result = FloorLog2<2 * i - 1>::result; }; /* Round up to the nearest power of 2. */ template struct RoundUpPow2 { static const size_t result = 1u << CeilingLog2::result; }; template <> struct RoundUpPow2<0> { static const size_t result = 1; }; /* Compute the number of bits in the given unsigned type. */ template struct BitSize { static const size_t result = sizeof(T) * JS_BITS_PER_BYTE; }; /* Allow Assertions by only including the 'result' typedef if 'true'. */ template struct StaticAssert {}; template <> struct StaticAssert { typedef int result; }; /* Boolean test for whether two types are the same. */ template struct IsSameType { static const bool result = false; }; template struct IsSameType { static const bool result = true; }; /* * Produce an N-bit mask, where N <= BitSize::result. Handle the * language-undefined edge case when N = BitSize::result. */ template struct NBitMask { typedef typename StaticAssert::result>::result _; static const size_t result = (size_t(1) << N) - 1; }; template <> struct NBitMask::result> { static const size_t result = size_t(-1); }; /* * For the unsigned integral type size_t, compute a mask M for N such that * for all X, !(X & M) implies X * N will not overflow (w.r.t size_t) */ template struct MulOverflowMask { static const size_t result = ~NBitMask::result - CeilingLog2::result>::result; }; template <> struct MulOverflowMask<0> { /* Error */ }; template <> struct MulOverflowMask<1> { static const size_t result = 0; }; /* * Generate a mask for T such that if (X & sUnsafeRangeSizeMask), an X-sized * array of T's is big enough to cause a ptrdiff_t overflow when subtracting * a pointer to the end of the array from the beginning. */ template struct UnsafeRangeSizeMask { /* * The '2' factor means the top bit is clear, sizeof(T) converts from * units of elements to bytes. */ static const size_t result = MulOverflowMask<2 * sizeof(T)>::result; }; /* Return T stripped of any const-ness. */ template struct StripConst { typedef T result; }; template struct StripConst { typedef T result; }; /* * Traits class for identifying POD types. Until C++0x, there is no automatic * way to detect PODs, so for the moment it is done manually. */ template struct IsPodType { static const bool result = false; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template <> struct IsPodType { static const bool result = true; }; template struct IsPodType { static const bool result = true; }; /* Return the size/end of an array without using macros. */ template inline T *ArraySize(T (&)[N]) { return N; } template inline T *ArrayEnd(T (&arr)[N]) { return arr + N; } template struct If { static const T result = v1; }; template struct If { static const T result = v2; }; } /* namespace tl */ /* Useful for implementing containers that assert non-reentrancy */ class ReentrancyGuard { /* ReentrancyGuard is not copyable. */ ReentrancyGuard(const ReentrancyGuard &); void operator=(const ReentrancyGuard &); #ifdef DEBUG bool &entered; #endif public: template #ifdef DEBUG ReentrancyGuard(T &obj) : entered(obj.entered) #else ReentrancyGuard(T &/*obj*/) #endif { #ifdef DEBUG JS_ASSERT(!entered); entered = true; #endif } ~ReentrancyGuard() { #ifdef DEBUG entered = false; #endif } }; /* * Round x up to the nearest power of 2. This function assumes that the most * significant bit of x is not set, which would lead to overflow. */ STATIC_POSTCONDITION_ASSUME(return >= x) JS_ALWAYS_INLINE size_t RoundUpPow2(size_t x) { size_t log2 = JS_CEILING_LOG2W(x); JS_ASSERT(log2 < tl::BitSize::result); size_t result = size_t(1) << log2; return result; } template class AlignedPtrAndFlag { uintptr_t bits; public: AlignedPtrAndFlag(T *t, bool flag) { JS_ASSERT((uintptr_t(t) & 1) == 0); bits = uintptr_t(t) | uintptr_t(flag); } T *ptr() const { return (T *)(bits & ~uintptr_t(1)); } bool flag() const { return (bits & 1) != 0; } void setPtr(T *t) { JS_ASSERT((uintptr_t(t) & 1) == 0); bits = uintptr_t(t) | uintptr_t(flag()); } void setFlag() { bits |= 1; } void unsetFlag() { bits &= ~uintptr_t(1); } void set(T *t, bool flag) { JS_ASSERT((uintptr_t(t) & 1) == 0); bits = uintptr_t(t) | flag; } }; template static inline void Reverse(T *beg, T *end) { while (beg != end) { if (--end == beg) return; T tmp = *beg; *beg = *end; *end = tmp; ++beg; } } template static inline T * Find(T *beg, T *end, const T &v) { for (T *p = beg; p != end; ++p) { if (*p == v) return p; } return end; } template static inline typename Container::ElementType * Find(Container &c, const typename Container::ElementType &v) { return Find(c.begin(), c.end(), v); } template void ForEach(InputIterT begin, InputIterT end, CallableT f) { for (; begin != end; ++begin) f(*begin); } template static inline T Min(T t1, T t2) { return t1 < t2 ? t1 : t2; } template static inline T Max(T t1, T t2) { return t1 > t2 ? t1 : t2; } /* Allows a const variable to be initialized after its declaration. */ template static T& InitConst(const T &t) { return const_cast(t); } template JS_ALWAYS_INLINE T & ImplicitCast(U &u) { T &t = u; return t; } template class AutoScopedAssign { private: JS_DECL_USE_GUARD_OBJECT_NOTIFIER T *addr; T old; public: AutoScopedAssign(T *addr, const T &value JS_GUARD_OBJECT_NOTIFIER_PARAM) : addr(addr), old(*addr) { JS_GUARD_OBJECT_NOTIFIER_INIT; *addr = value; } ~AutoScopedAssign() { *addr = old; } }; } /* namespace js */ #endif /* jstl_h_ */