LCOV - code coverage report
Current view: top level - source/ps/containers - Span.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 12 15 80.0 %
Date: 2023-01-19 00:18:29 Functions: 19 28 67.9 %

          Line data    Source code
       1             : /* Copyright (C) 2022 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_PS_SPAN
      19             : #define INCLUDED_PS_SPAN
      20             : 
      21             : #include <cstddef>
      22             : #include <iterator>
      23             : #include <type_traits>
      24             : 
      25             : namespace PS
      26             : {
      27             : 
      28             : /**
      29             :  * Simplifed version of std::span (C++20) as we don't support the original one
      30             :  * yet. The naming intentionally follows the STL version to make the future
      31             :  * replacement easier with less blame changing.
      32             :  * It supports only very basic subset of std::span functionality.
      33             :  * TODO: remove as soon as std::span become available.
      34             :  */
      35             : template<typename T>
      36             : class span
      37             : {
      38             : public:
      39             :     using element_type = T;
      40             :     using value_type = std::remove_cv_t<T>;
      41             :     using size_type = size_t;
      42             :     using pointer = T*;
      43             :     using reference = T&;
      44             :     using iterator = pointer;
      45             : 
      46           1 :     constexpr span()
      47           1 :         : m_Pointer(nullptr), m_Extent(0) {}
      48             : 
      49           7 :     constexpr span(iterator first, size_type extent)
      50           7 :         : m_Pointer(first), m_Extent(extent) {}
      51             : 
      52           0 :     constexpr span(iterator first, iterator last)
      53           0 :         : m_Pointer(first), m_Extent(static_cast<size_type>(last - first)) {}
      54             : 
      55             :     template<typename OtherT, size_t N>
      56         103 :     constexpr span(const std::array<OtherT, N>& arr)
      57         103 :         : m_Pointer(arr.data()), m_Extent(arr.size()) {}
      58             : 
      59             :     constexpr span(const span& other) = default;
      60             : 
      61             :     constexpr span& operator=(const span& other) = default;
      62             : 
      63             :     ~span() = default;
      64             : 
      65           8 :     constexpr size_type size() const { return m_Extent; }
      66           8 :     constexpr bool empty() const { return size() == 0; }
      67           7 :     constexpr reference operator[](size_type index) const { return *(m_Pointer + index); }
      68           0 :     constexpr pointer data() const { return m_Pointer; }
      69             : 
      70         103 :     constexpr iterator begin() const { return m_Pointer; }
      71         103 :     constexpr iterator end() const { return m_Pointer + m_Extent; }
      72             : 
      73           7 :     constexpr span subspan(size_type offset) const { return {m_Pointer + offset, m_Extent - offset}; }
      74             : 
      75             : private:
      76             :     pointer m_Pointer;
      77             :     size_type m_Extent;
      78             : };
      79             : 
      80             : template<typename T, size_t N>
      81             : span(const std::array<T, N>&) -> span<const T>;
      82             : 
      83             : } // namespace PS
      84             : 
      85             : #endif // INCLUDED_PS_SPAN

Generated by: LCOV version 1.13