LCOV - code coverage report
Current view: top level - mfbt - Array.h (source / functions) Hit Total Coverage
Test: output.info Lines: 15 24 62.5 %
Date: 2017-07-14 16:53:18 Functions: 182 302 60.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
       3             : /* This Source Code Form is subject to the terms of the Mozilla Public
       4             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       5             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       6             : 
       7             : /* A compile-time constant-length array with bounds-checking assertions. */
       8             : 
       9             : #ifndef mozilla_Array_h
      10             : #define mozilla_Array_h
      11             : 
      12             : #include "mozilla/Assertions.h"
      13             : #include "mozilla/Attributes.h"
      14             : #include "mozilla/Move.h"
      15             : #include "mozilla/ReverseIterator.h"
      16             : 
      17             : #include <stddef.h>
      18             : 
      19             : namespace mozilla {
      20             : 
      21             : template<typename T, size_t Length>
      22         100 : class Array
      23             : {
      24             :   T mArr[Length];
      25             : 
      26             : public:
      27       48813 :   Array() {}
      28             : 
      29             :   template <typename... Args>
      30           8 :   MOZ_IMPLICIT Array(Args&&... aArgs)
      31           8 :     : mArr{mozilla::Forward<Args>(aArgs)...}
      32             :   {
      33             :     static_assert(sizeof...(aArgs) == Length,
      34             :                   "The number of arguments should be equal to the template parameter Length");
      35           8 :   }
      36             : 
      37     8820148 :   T& operator[](size_t aIndex)
      38             :   {
      39     8820148 :     MOZ_ASSERT(aIndex < Length);
      40     8820148 :     return mArr[aIndex];
      41             :   }
      42             : 
      43      647301 :   const T& operator[](size_t aIndex) const
      44             :   {
      45      647301 :     MOZ_ASSERT(aIndex < Length);
      46      647301 :     return mArr[aIndex];
      47             :   }
      48             : 
      49           0 :   bool operator==(const Array<T, Length>& aOther) const
      50             :   {
      51           0 :     for (size_t i = 0; i < Length; i++) {
      52           0 :       if (mArr[i] != aOther[i]) {
      53           0 :         return false;
      54             :       }
      55             :     }
      56           0 :     return true;
      57             :   }
      58             : 
      59             :   typedef T*                        iterator;
      60             :   typedef const T*                  const_iterator;
      61             :   typedef ReverseIterator<T*>       reverse_iterator;
      62             :   typedef ReverseIterator<const T*> const_reverse_iterator;
      63             : 
      64             :   // Methods for range-based for loops.
      65         182 :   iterator begin() { return mArr; }
      66         105 :   const_iterator begin() const { return mArr; }
      67             :   const_iterator cbegin() const { return begin(); }
      68         182 :   iterator end() { return mArr + Length; }
      69         105 :   const_iterator end() const { return mArr + Length; }
      70             :   const_iterator cend() const { return end(); }
      71             : 
      72             :   // Methods for reverse iterating.
      73             :   reverse_iterator rbegin() { return reverse_iterator(end()); }
      74             :   const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
      75             :   const_reverse_iterator crbegin() const { return rbegin(); }
      76             :   reverse_iterator rend() { return reverse_iterator(begin()); }
      77             :   const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
      78             :   const_reverse_iterator crend() const { return rend(); }
      79             : };
      80             : 
      81             : template<typename T>
      82             : class Array<T, 0>
      83             : {
      84             : public:
      85           0 :   T& operator[](size_t aIndex)
      86             :   {
      87           0 :     MOZ_CRASH("indexing into zero-length array");
      88             :   }
      89             : 
      90           0 :   const T& operator[](size_t aIndex) const
      91             :   {
      92           0 :     MOZ_CRASH("indexing into zero-length array");
      93             :   }
      94             : };
      95             : 
      96             : }  /* namespace mozilla */
      97             : 
      98             : #endif /* mozilla_Array_h */

Generated by: LCOV version 1.13