Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
ByteArray.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Core module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_BYTEARRAY_HPP
8 #define NAZARA_BYTEARRAY_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Core/String.hpp>
12 #include <vector>
13 
14 namespace Nz
15 {
16  class AbstractHash;
17 
18  class NAZARA_CORE_API ByteArray
19  {
20  using Container = std::vector<UInt8>;
21 
22  public:
23  // types:
24  using allocator_type = Container::allocator_type;
25  using const_iterator = Container::const_iterator;
26  using const_reference = Container::const_reference;
27  using const_pointer = Container::const_pointer;
28  using const_reverse_iterator = Container::const_reverse_iterator;
29  using difference_type = Container::difference_type;
30  using pointer = Container::pointer;
31  using iterator = Container::iterator;
32  using reference = Container::reference;
33  using reverse_iterator = Container::reverse_iterator;
34  using size_type = Container::size_type;
35  using value_type = Container::value_type;
36 
37  // construct/destroy:
38  inline ByteArray() = default;
39  inline explicit ByteArray(size_type n);
40  inline ByteArray(const void* buffer, size_type n);
41  inline ByteArray(size_type n, value_type value);
42  template <class InputIterator> ByteArray(InputIterator first, InputIterator last);
43  ByteArray(const ByteArray& other) = default;
44  ByteArray(ByteArray&& other) = default;
45  ~ByteArray() = default;
46 
47  inline iterator Append(const void* buffer, size_type size);
48  inline iterator Append(const ByteArray& other);
49  template <class InputIterator> void Assign(InputIterator first, InputIterator last);
50  inline void Assign(size_type n, value_type value);
51 
52  inline reference Back();
53  inline const_reference Back() const;
54 
55  inline void Clear(bool keepBuffer = false);
56 
57  inline iterator Erase(const_iterator pos);
58  inline iterator Erase(const_iterator first, const_iterator last);
59 
60  inline reference Front();
61  inline const_reference Front() const;
62 
63  inline allocator_type GetAllocator() const;
64  inline pointer GetBuffer();
65  inline size_type GetCapacity() const noexcept;
66  inline const_pointer GetConstBuffer() const;
67  inline size_type GetMaxSize() const noexcept;
68  inline size_type GetSize() const noexcept;
69  inline ByteArray GetSubArray(const_iterator startPos, const_iterator endPos) const;
70 
71  inline iterator Insert(const_iterator pos, const void* buffer, size_type n);
72  inline iterator Insert(const_iterator pos, const ByteArray& other);
73  inline iterator Insert(const_iterator pos, size_type n, value_type byte);
74  template <class InputIterator> iterator Insert(const_iterator pos, InputIterator first, InputIterator last);
75  inline bool IsEmpty() const noexcept;
76 
77  inline void PopBack();
78  inline void PopFront();
79  inline iterator Prepend(const void* buffer, size_type size);
80  inline iterator Prepend(const ByteArray& other);
81  inline void PushBack(value_type byte);
82  inline void PushFront(value_type byte);
83 
84  inline void Reserve(size_type bufferSize);
85  inline void Resize(size_type newSize);
86  inline void Resize(size_type newSize, value_type byte);
87 
88  inline void ShrinkToFit();
89  inline void Swap(ByteArray& other);
90 
91  String ToHex() const;
92  inline String ToString() const;
93 
94  // STL interface
95  inline iterator begin() noexcept;
96  inline const_iterator begin() const noexcept;
97  inline bool empty() const noexcept;
98  inline iterator end() noexcept;
99  inline const_iterator end() const noexcept;
100  inline reverse_iterator rbegin() noexcept;
101  inline const_reverse_iterator rbegin() const noexcept;
102  inline reverse_iterator rend() noexcept;
103  inline const_reverse_iterator rend() const noexcept;
104  inline const_iterator cbegin() const noexcept;
105  inline const_iterator cend() const noexcept;
106  inline const_reverse_iterator crbegin() const noexcept;
107  inline const_reverse_iterator crend() const noexcept;
108  inline size_type size() const noexcept;
109 
110  // Operators
111  NAZARA_CORE_API friend std::ostream& operator<<(std::ostream& out, const Nz::ByteArray& byteArray);
112 
113  inline reference operator[](size_type pos);
114  inline const_reference operator[](size_type pos) const;
115  inline ByteArray& operator=(const ByteArray& array) = default;
116  inline ByteArray& operator=(ByteArray&& array) = default;
117  inline ByteArray operator+(const ByteArray& array) const;
118  inline ByteArray& operator+=(const ByteArray& array);
119 
120  inline bool operator==(const ByteArray& rhs) const;
121  inline bool operator!=(const ByteArray& rhs) const;
122  inline bool operator<(const ByteArray& rhs) const;
123  inline bool operator<=(const ByteArray& rhs) const;
124  inline bool operator>(const ByteArray& rhs) const;
125  inline bool operator>=(const ByteArray& rhs) const;
126 
127  private:
128  Container m_array;
129  };
130 
131  inline bool HashAppend(AbstractHash* hash, const ByteArray& byteArray);
132 }
133 
134 namespace std
135 {
136  void swap(Nz::ByteArray& lhs, Nz::ByteArray& rhs);
137 }
138 
139 #include <Nazara/Core/ByteArray.inl>
140 
141 #endif // NAZARA_BYTEARRAY_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Core class that represents the behaviour of the hash classes.
Definition: AbstractHash.hpp:18
bool operator<(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1517
String operator+(char character, const String &string)
Concatenates the character to the string.
Definition: String.cpp:5506
bool operator>(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1566
Core class that represents a string.
Definition: String.hpp:22
STL namespace.
bool operator!=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1503
Core class that represents an array of bytes.
Definition: ByteArray.hpp:18
bool operator==(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1469
bool operator>=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1580
bool operator<=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1552