Bug 1228641. Add a polyfill of std::initializer_list. r=froydnj

This commit is contained in:
Jeff Muizelaar 2015-12-01 20:43:17 -05:00
parent 37b3227e36
commit 5fe8c4e544
7 changed files with 91 additions and 0 deletions

View File

@ -20,6 +20,7 @@ new
algorithm algorithm
atomic atomic
deque deque
initializer_list
ios ios
iosfwd iosfwd
iostream iostream

View File

@ -548,6 +548,7 @@ image.h
imagehlp.h imagehlp.h
imm.h imm.h
initguid.h initguid.h
initializer_list
InterfaceDefs.h InterfaceDefs.h
InternetConfig.h InternetConfig.h
IntlResources.h IntlResources.h

62
mfbt/InitializerList.h Normal file
View File

@ -0,0 +1,62 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* * 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/. */
/* A polyfill for std::initializer_list if it doesn't exist */
#ifndef mozilla_InitializerList_h
#define mozilla_InitializerList_h
#include <mozilla/Compiler.h>
#include <mozilla/Attributes.h>
#if MOZ_USING_LIBCXX
# define MOZ_HAVE_INITIALIZER_LIST
#elif MOZ_USING_LIBSTDCXX && __GLIBCXX__ >= 20090421 // GCC 4.4.0
# define MOZ_HAVE_INITIALIZER_LIST
#elif _MSC_VER
# define MOZ_HAVE_INITIALIZER_LIST
#elif defined(MOZ_USING_STLPORT)
#else
# error "Unknown standard library situation"
#endif
#ifdef MOZ_HAVE_INITIALIZER_LIST
# include <initializer_list>
#else
/* Normally we would put things in mozilla:: however, std::initializer_list is a
* magic name used by the compiler and so we need to name it that way.
*/
namespace std
{
template<class T>
class initializer_list
{
/* This matches the representation used by GCC and Clang which
* are the only compilers we need to polyfill */
const T* mBegin;
size_t mSize;
/* This constructor is called directly by the compiler */
initializer_list(const T* begin, size_t size) : mBegin(begin), mSize(size) {}
public:
MOZ_CONSTEXPR initializer_list() : mBegin(nullptr), mSize(0) {}
typedef T value_type;
typedef const T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef const T* iterator;
typedef const T* const_iterator;
size_t size() const { return mSize; }
const T* begin() const { return mBegin; }
const T* end() const { return mBegin + mSize; }
};
}
#endif /* MOZ_HAS_INITIALIZER_LIST */
#endif /* mozilla_InitializerList_h */

View File

@ -44,6 +44,7 @@ EXPORTS.mozilla = [
'GuardObjects.h', 'GuardObjects.h',
'HashFunctions.h', 'HashFunctions.h',
'IndexSequence.h', 'IndexSequence.h',
'InitializerList.h',
'IntegerPrintfMacros.h', 'IntegerPrintfMacros.h',
'IntegerRange.h', 'IntegerRange.h',
'IntegerTypeTraits.h', 'IntegerTypeTraits.h',

View File

@ -0,0 +1,24 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
#include "mozilla/Assertions.h"
#include "mozilla/InitializerList.h"
int sum(std::initializer_list<int> p)
{
int result = 0;
for (auto i : p) {
result += i;
}
return result;
}
int
main()
{
MOZ_RELEASE_ASSERT(sum({1, 2, 3, 4, 5, 6}) == 7 * 3);
return 0;
}

View File

@ -19,6 +19,7 @@ CppUnitTests([
'TestFastBernoulliTrial', 'TestFastBernoulliTrial',
'TestFloatingPoint', 'TestFloatingPoint',
'TestFunction', 'TestFunction',
'TestInitializerList',
'TestIntegerPrintfMacros', 'TestIntegerPrintfMacros',
'TestIntegerRange', 'TestIntegerRange',
'TestJSONWriter', 'TestJSONWriter',

View File

@ -38,6 +38,7 @@ skip-if = os != 'win'
[TestGetURL] [TestGetURL]
[TestHashtables] [TestHashtables]
[TestID] [TestID]
[TestInitializerList]
[TestIntegerPrintfMacros] [TestIntegerPrintfMacros]
[TestIntegerRange] [TestIntegerRange]
[TestJSONWriter] [TestJSONWriter]