Program Listing for File span.h

Return to documentation for file (include\playback\resource-info\detail\span.h)

/******************************************************************************

© Intel Corporation.

This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you ("License"). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.

 This software and the related documents are provided as is, with no express
or implied warranties, other than those that are expressly stated in the
License.

******************************************************************************/

#pragma once

#include <algorithm>
#include <array>
#include "utility/assert.h"
#include <cstddef>
#include <cstring>
#include <vector>

namespace gpa {
namespace playback {
namespace detail {

template<typename T>
class Span
{
public:
    inline Span() = default;

    inline Span(std::nullptr_t)
    {
    }

    inline Span(T& data)
        : mData{&data}
        , mCount{1}
    {
    }

    inline Span(T* data, size_t count)
        : mData{count ? data : nullptr}
        , mCount{data ? count : 0}
    {
    }

    template<size_t Count>
    inline Span(std::array<T, Count>& container)
        : mData{Count ? container.data() : nullptr}
        , mCount{Count}
    {
    }

    inline Span(std::vector<T>& container)
        : mData{!container.empty() ? container.data() : nullptr}
        , mCount{container.size()}
    {
    }

    inline T const& operator[](size_t index) const
    {
        GPA_CHECK_RET(!empty() && "operator[] called on empty Span<>", GPA_DUMMY);
        GPA_CHECK_RET(index < mCount && "Array subscript is out of range", GPA_DUMMY);
        return mData[index];
    }

    inline T& operator[](size_t index)
    {
        GPA_CHECK_RET(!empty() && "operator[] called on empty Span<>", GPA_DUMMY);
        GPA_CHECK_RET(index < mCount && "Array subscript is out of range", GPA_DUMMY);
        return mData[index];
    }

    inline T const* begin() const
    {
        return mData;
    }

    inline T* begin()
    {
        return mData;
    }

    inline T const* end() const
    {
        return mData + mCount;
    }

    inline T* end()
    {
        return mData + mCount;
    }

    inline T const& front() const
    {
        GPA_CHECK_RET(!empty() && "front() called on empty Span<>", GPA_DUMMY);
        return operator[](0);
    }

    inline T& front()
    {
        GPA_CHECK_RET(!empty() && "front() called on empty Span<>", GPA_DUMMY);
        return operator[](0);
    }

    inline T const& back() const
    {
        GPA_CHECK_RET(!empty() && "back() called on empty Span<>", GPA_DUMMY);
        return operator[](mCount - 1);
    }

    inline T& back()
    {
        GPA_CHECK_RET(!empty() && "back() called on empty Span<>", GPA_DUMMY);
        return operator[](mCount - 1);
    }

    inline bool empty() const
    {
        return !mData;
    }

    inline size_t size() const
    {
        return mCount;
    }

    inline size_t size_bytes() const
    {
        return mCount * sizeof(T);
    }

    inline void clear()
    {
        mData = nullptr;
        mCount = 0;
    }

    inline T const* data() const
    {
        return mData;
    }

    inline T* data()
    {
        return mData;
    }

private:
    T* mData{nullptr};
    size_t mCount{0};
};

template<typename T>
inline bool operator==(Span<T> const& lhs, Span<T> const& rhs)
{
    return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}

template<typename T>
inline bool operator!=(Span<T> const& lhs, Span<T> const& rhs)
{
    return !(lhs == rhs);
}

template<typename T>
inline bool operator<(Span<T> const& lhs, Span<T> const& rhs)
{
    return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}

template<typename T>
inline Span<T const> MakeSpan(T const* pData, size_t count)
{
    return pData && count ? Span<T const>(pData, count) : Span<T const>{};
}

template<typename T>
inline Span<T> MakeSpan(T* pData, size_t count)
{
    return pData && count ? Span<T>(pData, count) : Span<T>{};
}

inline Span<char const> MakeSpan(char const* cStr)
{
    return cStr ? Span<char const>(cStr, strlen(cStr) + 1) : Span<char const>{};
}

inline Span<char> MakeSpan(char* cStr)
{
    return cStr ? Span<char>(cStr, strlen(cStr) + 1) : Span<char>{};
}

}  // namespace detail
}  // namespace playback
}  // namespace gpa