Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ByteBuffer.h
Go to the documentation of this file.
1/*
2* Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
3*
4* This program 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* This program 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 this program; if not, write to the Free Software
16* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18/*
19Copyright (c) 2018 DIVIDE-Studio
20Copyright (c) 2009 Ionut Cava
21
22This file is part of DIVIDE Framework.
23
24Permission is hereby granted, free of charge, to any person obtaining a copy of
25this software
26and associated documentation files (the "Software"), to deal in the Software
27without restriction,
28including without limitation the rights to use, copy, modify, merge, publish,
29distribute, sublicense,
30and/or sell copies of the Software, and to permit persons to whom the Software
31is furnished to do so,
32subject to the following conditions:
33
34The above copyright notice and this permission notice shall be included in all
35copies or substantial portions of the Software.
36
37THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38IMPLIED,
39INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
40PARTICULAR PURPOSE AND NONINFRINGEMENT.
41IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
42DAMAGES OR OTHER LIABILITY,
43WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44CONNECTION WITH THE SOFTWARE
45OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46
47*/
48#pragma once
49#ifndef DVD_CORE_BYTE_BUFFER_H_
50#define DVD_CORE_BYTE_BUFFER_H_
51
52namespace Divide {
53
55 public:
56 ByteBufferException(bool add, size_t pos, size_t esize, size_t size);
57 void printPosError() const;
58
59 private:
60 bool _add;
61 size_t _pos;
62 size_t _esize;
63 size_t _size;
64};
65
67template <typename T>
68struct Unused {
70 {
71 }
72};
73
75 public:
76 static const U8 BUFFER_FORMAT_VERSION { 10u };
77 static const size_t DEFAULT_SIZE { 0x1000 };
78
79 ByteBuffer() noexcept = default;
80
82 void clear() noexcept;
83
85 template<class T, size_t N>
86 void addMarker(const T (&pattern)[N]);
87
89 template<class T, size_t N>
90 void readSkipToMarker(const T ( &pattern )[N]);
91
93 template <typename T>
94 void put(size_t pos, const T& value);
95
97 template <typename T>
98 ByteBuffer& operator<<(const T& value);
99
101 template <typename T>
102 ByteBuffer& operator>>(T& value);
103
105 template <typename U>
106 ByteBuffer& operator>>(Unused<U>& value);
107
109 template <typename T>
110 void readSkip();
111
113 void readSkip(size_t skip) noexcept;
114
116 template <typename T>
117 void readNoSkip(T& value);
118
120 template <typename T>
121 void read(T& out);
122
124 template <typename T>
125 void readNoSkipFrom(size_t pos, T& out) const;
126
128 void read(Byte *dest, size_t len);
129
131 void readPackXYZ(F32& x, F32& y, F32& z);
133 void appendPackXYZ(F32 x, F32 y, F32 z);
134
138 void appendPackGUID(U64 guid);
139
141 void append(const Byte *src, size_t cnt);
142
144 template <typename T>
145 void append(const T* src, const size_t cnt);
146
148 [[nodiscard]] Byte operator[](size_t pos) const;
149
151 [[nodiscard]] size_t rpos() const noexcept;
154 [[nodiscard]] size_t rpos(size_t rpos_) noexcept;
156 [[nodiscard]] size_t wpos() const noexcept;
158 [[nodiscard]] size_t wpos(size_t wpos_) noexcept;
160 [[nodiscard]] size_t bufferSize() const noexcept;
162 [[nodiscard]] bool bufferEmpty() const noexcept;
164 [[nodiscard]] size_t storageSize() const noexcept;
166 [[nodiscard]] bool storageEmpty() const noexcept;
167
169 void resize(size_t newsize);
171 void reserve(size_t resize);
172
174 [[nodiscard]] const Byte* contents() const noexcept;
175
177 void put(size_t pos, const Byte *src, size_t cnt);
178
180 [[nodiscard]] bool dumpToFile(const ResourcePath& path, std::string_view fileName, const U8 version = BUFFER_FORMAT_VERSION);
184 [[nodiscard]] bool loadFromFile(const ResourcePath& path, std::string_view fileName, const U8 version = BUFFER_FORMAT_VERSION);
185
186 private:
188 template <typename T>
189 void append(const T& value);
190
191 protected:
192 size_t _rpos = 0u, _wpos = 0u;
194};
195
196} // namespace Divide
197#endif //DVD_CORE_BYTE_BUFFER_H_
198
199#include "ByteBuffer.inl"
size_t storageSize() const noexcept
Returns the total size (in bytes) of the underlying storage, regardles of wpos and rpos.
Definition: ByteBuffer.inl:357
void readNoSkip(T &value)
Reads sizeof(T) data from the buffer and saves it into 'value' but does NOT move the read head forwar...
Definition: ByteBuffer.inl:154
void readSkipToMarker(const T(&pattern)[N])
Skip (consume) everything in the buffer until we find the specified list of items....
Definition: ByteBuffer.inl:65
U64 readPackGUID()
Reads a packed U64 from the buffer and returns it. Reading moves the read head forward!
Definition: ByteBuffer.inl:276
const Byte * contents() const noexcept
Returns a raw pointer to the underlying storage data. Does NOT depend on the read head position!...
Definition: ByteBuffer.inl:377
size_t rpos() const noexcept
Returns the current read head position.
Definition: ByteBuffer.inl:331
void readNoSkipFrom(size_t pos, T &out) const
Reads sizeof(T) data from the buffer and returns it but does NOT move the read head forward!
Definition: ByteBuffer.inl:246
void resize(size_t newsize)
Resizes the underlying storage to 'newsize' bytes and resets the read and write head positions.
Definition: ByteBuffer.inl:365
void append(const Byte *src, size_t cnt)
Appends 'cnt' bytes from 'src' to the buffer.
Definition: ByteBuffer.cpp:33
static const U8 BUFFER_FORMAT_VERSION
Definition: ByteBuffer.h:76
void read(T &out)
Reads sizeof(T) data from the buffer and returns it. Reading moves the read head forward!
Definition: ByteBuffer.inl:239
size_t wpos() const noexcept
Returns the current write head position.
Definition: ByteBuffer.inl:340
bool loadFromFile(const ResourcePath &path, std::string_view fileName, const U8 version=BUFFER_FORMAT_VERSION)
Definition: ByteBuffer.cpp:57
void clear() noexcept
Resets the entire storage and the read and write positions.
Definition: ByteBuffer.cpp:28
bool storageEmpty() const noexcept
Returns true if the underlying storage is empty, regardless of wpos and rpos.
Definition: ByteBuffer.inl:361
vector< Byte > _storage
Definition: ByteBuffer.h:193
static const size_t DEFAULT_SIZE
Definition: ByteBuffer.h:77
void appendPackXYZ(F32 x, F32 y, F32 z)
Packes x,y and z into a single U32 and appends it to the buffer.
Definition: ByteBuffer.inl:296
ByteBuffer() noexcept=default
void addMarker(const T(&pattern)[N])
Add a specific list of items in order to mark a special point in the buffer. Can be used for skipping...
Definition: ByteBuffer.inl:56
size_t bufferSize() const noexcept
Returns the size (in bytes) of the data inside of the buffer (wpos - rpos)
Definition: ByteBuffer.inl:349
void readSkip()
Moves the read head sizeof(T) units forward.
Definition: ByteBuffer.inl:204
void put(size_t pos, const T &value)
Inserts 'value' at the specified position overriding any existing data at that location....
Definition: ByteBuffer.inl:105
void appendPackGUID(U64 guid)
Packes guid into a multiple I32s and appends them to the buffer.
Definition: ByteBuffer.inl:305
bool dumpToFile(const ResourcePath &path, std::string_view fileName, const U8 version=BUFFER_FORMAT_VERSION)
Saves the entire buffer contents to file. Always appends the version at the end of the file.
Definition: ByteBuffer.cpp:47
void reserve(size_t resize)
Reserves 'resize' bytes of additional storage in the underlying storage structure without changing th...
Definition: ByteBuffer.inl:371
void readPackXYZ(F32 &x, F32 &y, F32 &z)
Reads a packed U32 from the buffer and unpackes it into x,y,z. Reading moves the read head forward!
Definition: ByteBuffer.inl:267
bool bufferEmpty() const noexcept
Returns true if the read position and the write position are identical.
Definition: ByteBuffer.inl:353
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::byte Byte
uint8_t U8
constexpr resolve_uac< A, B >::return_type add(const A &a, const B &b) noexcept
eastl::vector< Type > vector
Definition: Vector.h:42
uint64_t U64
Helper for using >> instead of readSkip()
Definition: ByteBuffer.h:68