Debian Codegraph @

xbmc/utils/Archive.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #pragma once
10
11 #include "utils/XTimeUtils.h"
12
13 #include <cstdint>
14 #include <cstring>
15 #include <memory>
16 #include <string>
17 #include <vector>
18
19 #define CARCHIVE_BUFFER_MAX 4096
20
21 namespace XFILE
22 {
23 class CFile;
24 }
25 class CVariant;
26 class IArchivable;
27 namespace KODI::TIME
28 {
29 struct SystemTime;
30 }
31
32 class CArchive
33 {
34 public:
35 CArchive(XFILE::CFile* pFile, int mode);
36 ~CArchive();
37
38 /* CArchive support storing and loading of all C basic integer types
39 * C basic types was chosen instead of fixed size ints (int16_t - int64_t) to support all integer typedefs
40 * For example size_t can be typedef of unsigned int, long or long long depending on platform
41 * while int32_t and int64_t are usually unsigned short, int or long long, but not long
42 * and even if int and long can have same binary representation they are different types for compiler
43 * According to section 5.2.4.2.1 of C99 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
44 * minimal size of short int is 16 bits
45 * minimal size of int is 16 bits (usually 32 or 64 bits, larger or equal to short int)
46 * minimal size of long int is 32 bits (larger or equal to int)
47 * minimal size of long long int is 64 bits (larger or equal to long int) */
48 // storing
49 CArchive& operator<<(float f);
50 CArchive& operator<<(double d);
51 CArchive& operator<<(short int s);
52 CArchive& operator<<(unsigned short int us);
53 CArchive& operator<<(int i);
54 CArchive& operator<<(unsigned int ui);
55 CArchive& operator<<(long int l);
56 CArchive& operator<<(unsigned long int ul);
57 CArchive& operator<<(long long int ll);
58 CArchive& operator<<(unsigned long long int ull);
59 CArchive& operator<<(bool b);
60 CArchive& operator<<(char c);
•61 CArchive& operator<<(const std::string &str);
62 CArchive& operator<<(const std::wstring& wstr);
63 CArchive& operator<<(const KODI::TIME::time_point& time);
64 CArchive& operator<<(IArchivable& obj);
65 CArchive& operator<<(const CVariant& variant);
66 CArchive& operator<<(const std::vector<std::string>& strArray);
67 CArchive& operator<<(const std::vector<int>& iArray);
68
69 // loading
70 inline CArchive& operator>>(float& f)
71 {
72 return streamin(&f, sizeof(f));
73 }
74
75 inline CArchive& operator>>(double& d)
76 {
77 return streamin(&d, sizeof(d));
78 }
79
80 inline CArchive& operator>>(short int& s)
81 {
82 return streamin(&s, sizeof(s));
83 }
84
85 inline CArchive& operator>>(unsigned short int& us)
86 {
87 return streamin(&us, sizeof(us));
88 }
89
90 inline CArchive& operator>>(int& i)
91 {
92 return streamin(&i, sizeof(i));
93 }
94
95 inline CArchive& operator>>(unsigned int& ui)
96 {
97 return streamin(&ui, sizeof(ui));
98 }
99
100 inline CArchive& operator>>(long int& l)
101 {
102 return streamin(&l, sizeof(l));
103 }
104
105 inline CArchive& operator>>(unsigned long int& ul)
106 {
107 return streamin(&ul, sizeof(ul));
108 }
109
110 inline CArchive& operator>>(long long int& ll)
111 {
112 return streamin(&ll, sizeof(ll));
113 }
114
115 inline CArchive& operator>>(unsigned long long int& ull)
116 {
117 return streamin(&ull, sizeof(ull));
118 }
119
120 inline CArchive& operator>>(bool& b)
121 {
122 return streamin(&b, sizeof(b));
123 }
124
125 inline CArchive& operator>>(char& c)
126 {
127 return streamin(&c, sizeof(c));
128 }
129
•130 CArchive& operator>>(std::string &str);
131 CArchive& operator>>(std::wstring& wstr);
132 CArchive& operator>>(KODI::TIME::time_point& time);
133 CArchive& operator>>(IArchivable& obj);
134 CArchive& operator>>(CVariant& variant);
135 CArchive& operator>>(std::vector<std::string>& strArray);
136 CArchive& operator>>(std::vector<int>& iArray);
137
138 bool IsLoading() const;
139 bool IsStoring() const;
140
141 void Close();
142
143 enum Mode {load = 0, store};
144
145 protected:
146 inline CArchive &streamout(const void *dataPtr, size_t size)
147 {
148 auto ptr = static_cast<const uint8_t *>(dataPtr);
149 /* Note, the buffer is flushed as soon as it is full (m_BufferRemain == size) rather
150 * than waiting until we attempt to put more data into an already full buffer */
151 if (m_BufferRemain > size)
152 {
153 memcpy(m_BufferPos, ptr, size);
154 m_BufferPos += size;
155 m_BufferRemain -= size;
156 return *this;
157 }
158
159 return streamout_bufferwrap(ptr, size);
160 }
161
162 inline CArchive &streamin(void *dataPtr, size_t size)
163 {
164 auto ptr = static_cast<uint8_t *>(dataPtr);
165 /* Note, refilling the buffer is deferred until we know we need to read more from it */
166 if (m_BufferRemain >= size)
167 {
168 memcpy(ptr, m_BufferPos, size);
169 m_BufferPos += size;
170 m_BufferRemain -= size;
171 return *this;
172 }
173
174 return streamin_bufferwrap(ptr, size);
175 }
176
177 XFILE::CFile* m_pFile; //non-owning
178 int m_iMode;
179 std::unique_ptr<uint8_t[]> m_pBuffer;
180 uint8_t *m_BufferPos;
181 size_t m_BufferRemain;
182
183 private:
184 void FlushBuffer();
185 CArchive &streamout_bufferwrap(const uint8_t *ptr, size_t size);
186 void FillBuffer();
187 CArchive &streamin_bufferwrap(uint8_t *ptr, size_t size);
188 };