2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-06-04 07:36:43 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-04-01 05:46:35 +04:00
|
|
|
|
2011-12-28 20:48:54 +04:00
|
|
|
/*
|
|
|
|
* Miscellaneous uncategorized functionality. Please add new functionality to
|
|
|
|
* new headers, or to other appropriate existing headers, not here.
|
|
|
|
*/
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#ifndef mozilla_Util_h
|
|
|
|
#define mozilla_Util_h
|
2011-04-01 05:46:35 +04:00
|
|
|
|
2011-12-19 23:28:35 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2011-12-18 01:45:29 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-04-29 02:48:52 +04:00
|
|
|
#include "mozilla/Types.h"
|
|
|
|
|
2011-04-21 03:27:30 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2013-08-14 11:00:52 +04:00
|
|
|
#include "mozilla/Alignment.h"
|
2011-10-05 17:11:17 +04:00
|
|
|
|
2013-08-14 11:00:52 +04:00
|
|
|
namespace mozilla {
|
2011-04-29 02:48:52 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Small utility for lazily constructing objects without using dynamic storage.
|
|
|
|
* When a Maybe<T> is constructed, it is |empty()|, i.e., no value of T has
|
|
|
|
* been constructed and no T destructor will be called when the Maybe<T> is
|
|
|
|
* destroyed. Upon calling |construct|, a T object will be constructed with the
|
|
|
|
* given arguments and that object will be destroyed when the owning Maybe<T>
|
|
|
|
* is destroyed.
|
|
|
|
*
|
|
|
|
* N.B. GCC seems to miss some optimizations with Maybe and may generate extra
|
|
|
|
* branches/loads/stores. Use with caution on hot paths.
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template<class T>
|
2011-04-29 02:48:52 +04:00
|
|
|
class Maybe
|
|
|
|
{
|
|
|
|
AlignedStorage2<T> storage;
|
|
|
|
bool constructed;
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
T& asT() { return *storage.addr(); }
|
2011-04-29 02:48:52 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
Maybe() { constructed = false; }
|
|
|
|
~Maybe() { if (constructed) asT().~T(); }
|
|
|
|
|
|
|
|
bool empty() const { return !constructed; }
|
|
|
|
|
|
|
|
void construct() {
|
2012-06-04 07:36:43 +04:00
|
|
|
MOZ_ASSERT(!constructed);
|
2012-11-29 01:21:36 +04:00
|
|
|
::new (storage.addr()) T();
|
2012-06-04 07:36:43 +04:00
|
|
|
constructed = true;
|
2011-04-29 02:48:52 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<class T1>
|
|
|
|
void construct(const T1& t1) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
2012-11-29 01:21:36 +04:00
|
|
|
::new (storage.addr()) T(t1);
|
2012-06-04 07:36:43 +04:00
|
|
|
constructed = true;
|
2011-04-29 02:48:52 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<class T1, class T2>
|
|
|
|
void construct(const T1& t1, const T2& t2) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
2012-11-29 01:21:36 +04:00
|
|
|
::new (storage.addr()) T(t1, t2);
|
2012-06-04 07:36:43 +04:00
|
|
|
constructed = true;
|
2011-04-29 02:48:52 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<class T1, class T2, class T3>
|
|
|
|
void construct(const T1& t1, const T2& t2, const T3& t3) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
2012-11-29 01:21:36 +04:00
|
|
|
::new (storage.addr()) T(t1, t2, t3);
|
2012-06-04 07:36:43 +04:00
|
|
|
constructed = true;
|
2011-04-29 02:48:52 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<class T1, class T2, class T3, class T4>
|
|
|
|
void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
2012-11-29 01:21:36 +04:00
|
|
|
::new (storage.addr()) T(t1, t2, t3, t4);
|
2012-06-04 07:36:43 +04:00
|
|
|
constructed = true;
|
2011-04-29 02:48:52 +04:00
|
|
|
}
|
|
|
|
|
2013-05-30 16:29:56 +04:00
|
|
|
template<class T1, class T2, class T3, class T4, class T5>
|
|
|
|
void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
|
|
|
::new (storage.addr()) T(t1, t2, t3, t4, t5);
|
|
|
|
constructed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T1, class T2, class T3, class T4, class T5,
|
|
|
|
class T6>
|
|
|
|
void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
|
|
|
|
const T6& t6) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
|
|
|
::new (storage.addr()) T(t1, t2, t3, t4, t5, t6);
|
|
|
|
constructed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T1, class T2, class T3, class T4, class T5,
|
|
|
|
class T6, class T7>
|
|
|
|
void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
|
|
|
|
const T6& t6, const T7& t7) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
|
|
|
::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7);
|
|
|
|
constructed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T1, class T2, class T3, class T4, class T5,
|
|
|
|
class T6, class T7, class T8>
|
|
|
|
void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
|
|
|
|
const T6& t6, const T7& t7, const T8& t8) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
|
|
|
::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8);
|
|
|
|
constructed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T1, class T2, class T3, class T4, class T5,
|
|
|
|
class T6, class T7, class T8, class T9>
|
|
|
|
void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
|
|
|
|
const T6& t6, const T7& t7, const T8& t8, const T9& t9) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
|
|
|
::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9);
|
|
|
|
constructed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T1, class T2, class T3, class T4, class T5,
|
|
|
|
class T6, class T7, class T8, class T9, class T10>
|
|
|
|
void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
|
|
|
|
const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10) {
|
|
|
|
MOZ_ASSERT(!constructed);
|
|
|
|
::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
|
|
|
|
constructed = true;
|
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
T* addr() {
|
|
|
|
MOZ_ASSERT(constructed);
|
|
|
|
return &asT();
|
2011-04-29 02:48:52 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
T& ref() {
|
|
|
|
MOZ_ASSERT(constructed);
|
|
|
|
return asT();
|
2011-04-29 02:48:52 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
const T& ref() const {
|
|
|
|
MOZ_ASSERT(constructed);
|
|
|
|
return const_cast<Maybe*>(this)->asT();
|
2011-06-25 01:22:30 +04:00
|
|
|
}
|
|
|
|
|
2011-04-29 02:48:52 +04:00
|
|
|
void destroy() {
|
2012-06-04 07:36:43 +04:00
|
|
|
ref().~T();
|
|
|
|
constructed = false;
|
2011-04-29 02:48:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroyIfConstructed() {
|
2012-06-04 07:36:43 +04:00
|
|
|
if (!empty())
|
|
|
|
destroy();
|
2011-04-29 02:48:52 +04:00
|
|
|
}
|
2012-06-04 07:36:43 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
Maybe(const Maybe& other) MOZ_DELETE;
|
|
|
|
const Maybe& operator=(const Maybe& other) MOZ_DELETE;
|
2011-04-29 02:48:52 +04:00
|
|
|
};
|
|
|
|
|
2011-06-06 22:02:34 +04:00
|
|
|
/*
|
|
|
|
* Safely subtract two pointers when it is known that end >= begin. This avoids
|
|
|
|
* the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB
|
|
|
|
* set, the unsigned subtraction followed by right shift will produce -1, or
|
|
|
|
* size_t(-1), instead of the real difference.
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template<class T>
|
2011-06-06 22:02:34 +04:00
|
|
|
MOZ_ALWAYS_INLINE size_t
|
|
|
|
PointerRangeSize(T* begin, T* end)
|
|
|
|
{
|
2012-06-04 07:36:43 +04:00
|
|
|
MOZ_ASSERT(end >= begin);
|
|
|
|
return (size_t(end) - size_t(begin)) / sizeof(T);
|
2011-06-06 22:02:34 +04:00
|
|
|
}
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
/*
|
|
|
|
* Compute the length of an array with constant length. (Use of this method
|
|
|
|
* with a non-array pointer will not compile.)
|
|
|
|
*
|
|
|
|
* Beware of the implicit trailing '\0' when using this with string constants.
|
|
|
|
*/
|
|
|
|
template<typename T, size_t N>
|
2013-01-08 11:29:00 +04:00
|
|
|
MOZ_CONSTEXPR size_t
|
2011-10-11 09:50:08 +04:00
|
|
|
ArrayLength(T (&arr)[N])
|
|
|
|
{
|
2012-06-04 07:36:43 +04:00
|
|
|
return N;
|
2011-10-11 09:50:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the address one past the last element of a constant-length array.
|
|
|
|
*
|
|
|
|
* Beware of the implicit trailing '\0' when using this with string constants.
|
|
|
|
*/
|
|
|
|
template<typename T, size_t N>
|
2013-01-08 11:29:00 +04:00
|
|
|
MOZ_CONSTEXPR T*
|
2011-10-11 09:50:08 +04:00
|
|
|
ArrayEnd(T (&arr)[N])
|
|
|
|
{
|
2012-06-04 07:36:43 +04:00
|
|
|
return arr + ArrayLength(arr);
|
2011-10-11 09:50:08 +04:00
|
|
|
}
|
|
|
|
|
2011-04-21 03:27:30 +04:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* __cplusplus */
|
2011-04-01 05:46:35 +04:00
|
|
|
|
2013-01-06 11:37:25 +04:00
|
|
|
/*
|
|
|
|
* MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
|
2013-07-18 21:59:53 +04:00
|
|
|
* that can't use C++ template functions and for static_assert() calls that
|
2013-01-06 11:37:25 +04:00
|
|
|
* can't call ArrayLength() when it is not a C++11 constexpr function.
|
|
|
|
*/
|
2013-01-08 11:29:00 +04:00
|
|
|
#ifdef MOZ_HAVE_CXX11_CONSTEXPR
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) mozilla::ArrayLength(array)
|
|
|
|
#else
|
|
|
|
# define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
|
|
|
|
#endif
|
2013-01-06 11:37:25 +04:00
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_Util_h */
|