summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/foundation/IOStreams.h
blob: 855e5209844aa64365c055d55e15b04b99303448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/****************************************************************************
**
** Copyright (C) 1993-2009 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#ifndef QT3DS_FOUNDATION_IO_STREAMS_H
#define QT3DS_FOUNDATION_IO_STREAMS_H

#include "EABase/eabase.h"
#include "foundation/Qt3DSDataRef.h"
#include "foundation/Qt3DSAssert.h"
#include "foundation/Qt3DSIntrinsics.h"
#include "foundation/TrackingAllocator.h"
#include "foundation/Qt3DSMath.h"
#include "foundation/Qt3DSFlags.h"
#include "foundation/Utils.h"
#include "foundation/Qt3DSRefCounted.h"

#include <QFile>
#include <QString>

namespace qt3ds {
class NVFoundationBase;

namespace foundation {

    class IOutStream
    {
    protected:
        virtual ~IOutStream() {}
    public:
        virtual bool Write(NVConstDataRef<QT3DSU8> data) = 0;
        void WriteWithLen(NVConstDataRef<QT3DSU8> data)
        {
            Write(data.size());
            Write(data);
        }
        template <typename TDataType>
        void Write(const TDataType &type)
        {
            Write(toU8ConstDataRef(type));
        }
        template <typename TDataType>
        void Write(const TDataType *data, QT3DSU32 numItems)
        {
            Write(toU8ConstDataRef(data, numItems));
        }
        void Write(const wchar_t *data)
        {
            if (data == NULL)
                data = L"";
            // Write the null character at the end of the string.
            // This just makes reading and debugging a lot less error prone.
            // at the expense of 2 bytes.
            Write(data, (QT3DSU32)StrLen(data) + 1);
        }
        template <typename TDataType>
        void WriteWithLen(const TDataType *data, QT3DSU32 numItems)
        {
            WriteWithLen(toU8ConstDataRef(data, numItems));
        }
    };

    class IInStream
    {
    protected:
        virtual ~IInStream() {}
    public:
        // Semantics are precisely that you return the amount read
        virtual QT3DSU32 Read(NVDataRef<QT3DSU8> data) = 0;

        QT3DSU32 SafeRead(NVDataRef<QT3DSU8> data)
        {
            QT3DSU32 amountRead = Read(data);
            QT3DS_ASSERT(amountRead == data.size());
            if (amountRead < data.size())
                intrinsics::memZero(data.begin() + amountRead, data.size() - amountRead);
            return amountRead;
        }
        QT3DSU32 ReadWithLen(NVDataRef<QT3DSU8> data)
        {
            QT3DSU32 len = 0;
            Read(len);
            return SafeRead(toDataRef(data.begin(), len));
        }
        template <typename TDataType>
        QT3DSU32 Read(TDataType &type)
        {
            return SafeRead(toU8DataRef(type));
        }
        template <typename TDataType>
        QT3DSU32 Read(TDataType *data, QT3DSU32 numItems)
        {
            return SafeRead(toU8DataRef(data, numItems));
        }
    };

    struct SeekPosition
    {
        enum Enum {
            Unknown,
            Begin,
            Current,
            End,
        };
    };

    class ISeekable
    {
    protected:
        virtual ~ISeekable() {}
    public:
        virtual void SetPosition(QT3DSI64 inOffset, SeekPosition::Enum inEnum) = 0;
        virtual QT3DSI64 GetPosition() const = 0;
        virtual QT3DSI64 GetLength() const
        {
            ISeekable &seekable(const_cast<ISeekable &>(*this));
            QT3DSI64 currentPos(GetPosition());
            seekable.SetPosition(0, SeekPosition::End);
            QT3DSI64 retval(GetPosition());
            seekable.SetPosition(currentPos, SeekPosition::Begin);
            return retval;
        }
    };

    class ISeekableIOStream : public IInStream, public IOutStream, public ISeekable
    {
    };

    struct FileOpenFlagValues
    {
        enum Enum {
            Open = 1, // Without this flag, function fails if file exists
            Truncate = 1 << 1, // Truncate the file so an immediate close will empty it.
            Create = 1 << 2,
            Write = 1 << 3,
        };
    };

    typedef NVFlags<FileOpenFlagValues::Enum, int> FileOpenFlags;

    static inline FileOpenFlags FileReadFlags() { return FileOpenFlags(FileOpenFlagValues::Open); }

    static inline FileOpenFlags FileWriteFlags()
    {
        return FileOpenFlags(FileOpenFlagValues::Create | FileOpenFlagValues::Open
                             | FileOpenFlagValues::Write | FileOpenFlagValues::Truncate);
    }
    static inline FileOpenFlags FileAppendFlags()
    {
        return FileOpenFlags(FileOpenFlagValues::Create | FileOpenFlagValues::Open
                             | FileOpenFlagValues::Write);
    }

    class CFileSeekableIOStream : public ISeekableIOStream
    {
    protected:
        QFile m_File;

    public:
        // Enabling append also enables reading from the file while being able to
        // write to it.
        CFileSeekableIOStream(const char *inFile, FileOpenFlags inFlags);
#ifdef WIDE_IS_DIFFERENT_TYPE_THAN_CHAR16_T
        CFileSeekableIOStream(const wchar_t *inFile, FileOpenFlags inFlags);
#endif

        CFileSeekableIOStream(const char16_t *inFile, FileOpenFlags inFlags);
        CFileSeekableIOStream(const QString &inFIle, FileOpenFlags inFlags);
        virtual ~CFileSeekableIOStream();
        virtual bool IsOpen();
        void SetPosition(QT3DSI64 inOffset, SeekPosition::Enum inEnum) override;
        QT3DSI64 GetPosition() const override;
        QT3DSU32 Read(NVDataRef<QT3DSU8> data) override;
        bool Write(NVConstDataRef<QT3DSU8> data) override;

    private:
        void openFile(const QString &path, FileOpenFlags inFlags);
    };

    class CMemorySeekableIOStream : public ISeekableIOStream
    {
    protected:
        NVAllocatorCallback &m_Allocator;
        const char *m_AllocationName;
        QT3DSU8 *m_Data;
        QT3DSU32 m_Size;
        QT3DSU32 m_Offset;
        QT3DSU32 m_Capacity;

    public:
        CMemorySeekableIOStream(NVAllocatorCallback &inAlloc, const char *inAllocName);
        virtual ~CMemorySeekableIOStream();
        void SetPosition(QT3DSI64 inOffset, SeekPosition::Enum inEnum) override;
        QT3DSI64 GetPosition() const override { return m_Offset; }
        QT3DSI64 GetLength() const override { return m_Size; }
        QT3DSU32 Read(NVDataRef<QT3DSU8> data) override;
        bool Write(NVConstDataRef<QT3DSU8> data) override;

        // Add in the standard container functions
        QT3DSU8 *begin() { return m_Data; }
        QT3DSU8 *end() { return m_Data + m_Size; }
        const QT3DSU8 *begin() const { return m_Data; }
        const QT3DSU8 *end() const { return m_Data + m_Size; }
        void clear()
        {
            m_Offset = 0;
            m_Size = 0;
        }
        QT3DSU32 size() const { return m_Size; }
        void reserve(QT3DSU32 inNewSize);
        void resize(QT3DSU32 inNewSize)
        {
            reserve(inNewSize);
            m_Size = inNewSize;
        }
    };

    // Simple, one way input stream.
    struct SMemoryInStream : public IInStream
    {
        const QT3DSU8 *m_Begin;
        const QT3DSU8 *m_End;
        SMemoryInStream(const QT3DSU8 *b, const QT3DSU8 *e)
            : m_Begin(b)
            , m_End(e)
        {
        }

        QT3DSU32 Read(NVDataRef<QT3DSU8> data) override
        {
            size_t available = m_End - m_Begin;
            size_t requested = data.size();
            size_t amount = NVMin(available, requested);
            qt3ds::intrinsics::memCopy(data.mData, m_Begin, (QT3DSU32)amount);
            m_Begin += amount;
            return (QT3DSU32)amount;
        }
    };

    class WriteBufferedOutStream : public IOutStream, public NVRefCounted
    {
    public:
        virtual IOutStream &wrappedStream() = 0;
        virtual void flush() = 0;

        static NVScopedRefCounted<WriteBufferedOutStream>
        Create(NVFoundationBase &fnd, IOutStream &stream, QT3DSU32 totalBufferSize = 128 * 1024);
    };
}
}

#endif