// Copyright (C) 2008-2012 NVIDIA Corporation. // Copyright (C) 2019 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #ifndef QSSGDATAREF_H #define QSSGDATAREF_H // // W A R N I N G // ------------- // // This file is not part of the Qt API. It exists purely as an // implementation detail. This header file may change from version to // version without notice, or even be removed. // // We mean it. // #include #include #include QT_BEGIN_NAMESPACE template struct QSSGDataView { const T *mData; qsizetype mSize; explicit QSSGDataView(const QList &data) : mData(data.constData()), mSize(data.size()) { Q_ASSERT(mSize >= 0); } template explicit QSSGDataView(const QVarLengthArray &data) : mData(data.constData()), mSize(data.size()) { Q_ASSERT(mSize >= 0); } QSSGDataView(const T *inData, qsizetype inSize) : mData(inData), mSize(inSize) { Q_ASSERT(mSize >= 0); } constexpr QSSGDataView() : mData(nullptr), mSize(0) {} qsizetype size() const { return mSize; } const T *begin() const { return mData; } const T *end() const { return mData + mSize; } const T& first() const { Q_ASSERT(!isEmpty()); return *begin(); } const T& last() const { Q_ASSERT(!isEmpty()); return *(end()-1); } const T &operator[](int index) const { Q_ASSERT(index > -1); Q_ASSERT(index < mSize); return mData[index]; } bool isEmpty() const { return (mSize == 0); } void clear() { mData = nullptr; mSize = 0; } operator const void *() { return reinterpret_cast(mData); } }; template<> struct QSSGDataView { const quint8 *mData; qsizetype mSize; explicit QSSGDataView(const QByteArray &data) : mData(reinterpret_cast(data.constBegin())), mSize(data.size()) { Q_ASSERT(mSize >= 0); } template explicit QSSGDataView(const QList &data) : mData(reinterpret_cast(data.constData())), mSize(data.size()*sizeof(T)) { Q_ASSERT(mSize >= 0); } QSSGDataView(const quint8 *inData, qsizetype inSize) : mData(inData), mSize(inSize) { Q_ASSERT(mSize >= 0); } template QSSGDataView(const T *inData, qsizetype inSize) : mData(reinterpret_cast(inData)), mSize(inSize*sizeof(T)) { Q_ASSERT(mSize >= 0); } constexpr QSSGDataView() : mData(nullptr), mSize(0) {} qsizetype size() const { return mSize; } bool isEmpty() const { return (mSize == 0); } const quint8 *begin() const { return mData; } const quint8 *end() const { return mData + mSize; } const quint8 &operator[](int index) const { Q_ASSERT(index > -1); Q_ASSERT(index < mSize); return mData[index]; } void clear() { mData = nullptr; mSize = 0; } operator const void *() { return reinterpret_cast(mData); } }; using QSSGByteView = QSSGDataView; template inline QSSGDataView toDataView(const T &type) { return QSSGDataView(&type, 1); } template inline QSSGDataView toDataView(const QList &type) { return QSSGDataView(type); } template inline QSSGByteView toByteView(const T &type) { return QSSGByteView(&type, 1); } template inline QSSGByteView toByteView(const QList &type) { return QSSGByteView(type); } template<> inline QSSGByteView toByteView(const QByteArray &type) { return QSSGByteView(type); } inline QSSGByteView toByteView(const char *str) { return QSSGByteView(str, qstrlen(str)); } template inline QSSGDataView toDataView(const T *type, qsizetype count) { return QSSGDataView(type, count); } template inline QSSGByteView toByteView(const T *type, qsizetype count) { return QSSGByteView(type, count); } template struct QSSGDataRef { T *mData; qsizetype mSize; QSSGDataRef(T *inData, qsizetype inSize) : mData(inData), mSize(inSize) { Q_ASSERT(inSize >= 0); } QSSGDataRef() : mData(nullptr), mSize(0) {} qsizetype size() const { return mSize; } T *begin() { return mData; } T *end() { return mData + mSize; } T *begin() const { return mData; } T *end() const { return mData + mSize; } T& first() { Q_ASSERT(!isEmpty()); return *begin(); } T& last() { Q_ASSERT(!isEmpty()); return *(end()-1); } const T &first() const { Q_ASSERT(!isEmpty()); return *begin(); } const T &last() const { Q_ASSERT(!isEmpty()); return *(end()-1); } bool isEmpty() const { return (mSize == 0); } T &operator[](qsizetype index) { Q_ASSERT(index >= 0); Q_ASSERT(index < mSize); return mData[index]; } const T &operator[](qsizetype index) const { Q_ASSERT(index >= 0); Q_ASSERT(index < mSize); return mData[index]; } void clear() { mData = nullptr; mSize = 0; } operator QSSGDataView() const { return QSSGDataView(mData, mSize); } operator void *() { return reinterpret_cast(mData); } }; using QSSGByteRef = QSSGDataRef; template inline QSSGDataRef toDataRef(T &type) { return QSSGDataRef(&type, 1); } template inline QSSGByteRef toByteRef(T &type) { return QSSGByteRef(reinterpret_cast(&type), sizeof(T)); } template inline QSSGDataRef toDataRef(T *type, qsizetype count) { return QSSGDataRef(type, count); } template inline QSSGByteRef toByteRef(T *type, qsizetype count) { return QSSGByteRef(reinterpret_cast(type), sizeof(T) * count); } QT_END_NAMESPACE #endif // QSSGDATAREF_H