Skip to content

Commit bb4c7c3

Browse files
author
N3xoX1
committed
OS: Templatize InterfacePtr for types from the loader module
1 parent fc91354 commit bb4c7c3

3 files changed

Lines changed: 77 additions & 327 deletions

File tree

Lines changed: 51 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -1,280 +1,109 @@
11
#include "filesystem_interfaces.h"
22
#include <nxemu-module-spec/system_loader.h>
33

4-
IVirtualDirectoryPtr::IVirtualDirectoryPtr() :
5-
m_directory(nullptr)
4+
template<typename InterfaceType>
5+
InterfacePtr<InterfaceType>::InterfacePtr() :
6+
m_ptr(nullptr)
67
{
78
}
89

9-
IVirtualDirectoryPtr::IVirtualDirectoryPtr(IVirtualDirectory * ptr) :
10-
m_directory(ptr)
10+
template<typename InterfaceType>
11+
InterfacePtr<InterfaceType>::InterfacePtr(InterfaceType* ptr) :
12+
m_ptr(ptr)
1113
{
1214
}
1315

14-
IVirtualDirectoryPtr::~IVirtualDirectoryPtr()
16+
template<typename InterfaceType>
17+
InterfacePtr<InterfaceType>::InterfacePtr(InterfacePtr&& other) noexcept
18+
: m_ptr(other.m_ptr)
1519
{
16-
if (m_directory != nullptr)
17-
{
18-
m_directory->Release();
19-
}
20-
m_directory = nullptr;
20+
other.m_ptr = nullptr;
2121
}
2222

23-
IVirtualDirectoryPtr & IVirtualDirectoryPtr::operator=(IVirtualDirectory * ptr)
23+
template<typename InterfaceType>
24+
InterfacePtr<InterfaceType>::~InterfacePtr()
2425
{
25-
if (m_directory != nullptr)
26+
if (m_ptr)
2627
{
27-
m_directory->Release();
28+
m_ptr->Release();
29+
m_ptr = nullptr;
2830
}
29-
m_directory = ptr;
30-
return *this;
3131
}
3232

33-
IVirtualDirectoryPtr& IVirtualDirectoryPtr::operator=(IVirtualDirectoryPtr && other) noexcept
33+
template<typename InterfaceType>
34+
InterfacePtr<InterfaceType> & InterfacePtr<InterfaceType>::operator=(InterfaceType * ptr)
3435
{
35-
if (this != &other)
36+
if (m_ptr != ptr && m_ptr != nullptr)
3637
{
37-
if (m_directory)
38-
{
39-
m_directory->Release();
40-
}
41-
m_directory = other.m_directory;
42-
other.m_directory = nullptr;
38+
m_ptr->Release();
4339
}
40+
m_ptr = ptr;
4441
return *this;
4542
}
4643

47-
IVirtualDirectory ** IVirtualDirectoryPtr::GetAddressForSet()
48-
{
49-
if (m_directory)
50-
{
51-
m_directory->Release();
52-
m_directory = nullptr;
53-
}
54-
return &m_directory;
55-
}
56-
57-
IVirtualDirectory * IVirtualDirectoryPtr::operator->() const
58-
{
59-
return m_directory;
60-
}
61-
62-
IVirtualDirectory & IVirtualDirectoryPtr::operator*() const
63-
{
64-
return *m_directory;
65-
}
66-
67-
IVirtualDirectoryPtr::operator bool() const
68-
{
69-
return m_directory != nullptr;
70-
}
71-
72-
IVirtualFilePtr::IVirtualFilePtr() :
73-
m_file(nullptr)
74-
{
75-
}
76-
77-
IVirtualFilePtr::IVirtualFilePtr(IVirtualFile * file) :
78-
m_file(file)
79-
{
80-
}
81-
82-
IVirtualFilePtr::IVirtualFilePtr(IVirtualFilePtr && other) noexcept
83-
{
84-
m_file = other.m_file;
85-
other.m_file = nullptr;
86-
}
87-
88-
IVirtualFilePtr::~IVirtualFilePtr()
89-
{
90-
if (m_file != nullptr)
91-
{
92-
m_file->Release();
93-
}
94-
m_file = nullptr;
95-
}
96-
97-
IVirtualFilePtr & IVirtualFilePtr::operator=(IVirtualFile * ptr)
44+
template<typename InterfaceType>
45+
InterfacePtr<InterfaceType>& InterfacePtr<InterfaceType>::operator=(InterfacePtr&& other) noexcept
9846
{
99-
if (m_file != ptr && m_file != nullptr)
47+
if (this != &other)
10048
{
101-
m_file->Release();
49+
if (m_ptr) {
50+
m_ptr->Release();
51+
}
52+
m_ptr = other.m_ptr;
53+
other.m_ptr = nullptr;
10254
}
103-
m_file = ptr;
10455
return *this;
10556
}
10657

107-
IVirtualFilePtr & IVirtualFilePtr::operator=(IVirtualFilePtr && other) noexcept
58+
template<typename InterfaceType>
59+
InterfaceType** InterfacePtr<InterfaceType>::GetAddressForSet()
10860
{
109-
if (m_file != nullptr)
61+
if (m_ptr)
11062
{
111-
m_file->Release();
63+
m_ptr->Release();
64+
m_ptr = nullptr;
11265
}
113-
m_file = other.m_file;
114-
other.m_file = nullptr;
115-
return *this;
66+
return &m_ptr;
11667
}
11768

118-
IVirtualFile * IVirtualFilePtr::operator->() const
69+
template<typename InterfaceType>
70+
InterfaceType* InterfacePtr<InterfaceType>::operator->() const
11971
{
120-
return m_file;
72+
return m_ptr;
12173
}
12274

123-
IVirtualFile & IVirtualFilePtr::operator*() const
75+
template<typename InterfaceType>
76+
InterfaceType& InterfacePtr<InterfaceType>::operator*() const
12477
{
125-
return *m_file;
78+
return *m_ptr;
12679
}
12780

128-
IVirtualFilePtr::operator bool() const
81+
template<typename InterfaceType>
82+
InterfacePtr<InterfaceType>::operator bool() const
12983
{
130-
return m_file != nullptr;
84+
return m_ptr != nullptr;
13185
}
13286

13387
std::vector<uint8_t> IVirtualFilePtr::ReadAllBytes() const
13488
{
135-
uint64_t dataSize = m_file->GetSize();
89+
uint64_t dataSize = m_ptr->GetSize();
13690
if (dataSize == 0)
13791
{
13892
return {};
13993
}
14094
std::vector<uint8_t> data;
14195
data.resize(dataSize);
142-
dataSize = m_file->ReadBytes(data.data(), data.size(), 0);
96+
dataSize = m_ptr->ReadBytes(data.data(), data.size(), 0);
14397
if (dataSize < data.size())
14498
{
14599
data.resize(dataSize);
146100
}
147101
return data;
148102
}
149103

150-
FileSysNCAPtr::FileSysNCAPtr() :
151-
m_ptr(nullptr)
152-
{
153-
}
154-
155-
FileSysNCAPtr::FileSysNCAPtr(IFileSysNCA* ptr) :
156-
m_ptr(ptr)
157-
{
158-
}
159-
160-
FileSysNCAPtr::FileSysNCAPtr(FileSysNCAPtr&& other) noexcept :
161-
m_ptr(other.m_ptr)
162-
{
163-
other.m_ptr = nullptr;
164-
}
165-
166-
FileSysNCAPtr::~FileSysNCAPtr()
167-
{
168-
if (m_ptr)
169-
{
170-
m_ptr->Release();
171-
m_ptr = nullptr;
172-
}
173-
}
174-
175-
FileSysNCAPtr& FileSysNCAPtr::operator=(FileSysNCAPtr&& other) noexcept
176-
{
177-
if (this != &other)
178-
{
179-
if (m_ptr)
180-
{
181-
m_ptr->Release();
182-
}
183-
m_ptr = other.m_ptr;
184-
other.m_ptr = nullptr;
185-
}
186-
return *this;
187-
}
188-
189-
IFileSysNCA* FileSysNCAPtr::operator->() const
190-
{
191-
return m_ptr;
192-
}
193-
194-
IFileSysNCA& FileSysNCAPtr::operator*() const
195-
{
196-
return *m_ptr;
197-
}
198-
199-
FileSysNCAPtr::operator bool() const
200-
{
201-
return m_ptr != nullptr;
202-
}
203-
204-
SaveDataFactoryPtr::SaveDataFactoryPtr() :
205-
m_ptr(nullptr)
206-
{
207-
}
208-
209-
SaveDataFactoryPtr::SaveDataFactoryPtr(SaveDataFactoryPtr && other) noexcept
210-
{
211-
m_ptr = other.m_ptr;
212-
other.m_ptr = nullptr;
213-
}
214-
215-
SaveDataFactoryPtr::~SaveDataFactoryPtr()
216-
{
217-
if (m_ptr)
218-
{
219-
m_ptr->Release();
220-
m_ptr = nullptr;
221-
}
222-
}
223-
224-
SaveDataFactoryPtr & SaveDataFactoryPtr::operator=(SaveDataFactoryPtr&& other) noexcept
225-
{
226-
if (m_ptr)
227-
{
228-
m_ptr->Release();
229-
m_ptr = nullptr;
230-
}
231-
m_ptr = other.m_ptr;
232-
other.m_ptr = nullptr;
233-
return *this;
234-
}
235-
236-
ISaveDataFactory ** SaveDataFactoryPtr::GetAddressForSet()
237-
{
238-
if (m_ptr)
239-
{
240-
m_ptr->Release();
241-
m_ptr = nullptr;
242-
}
243-
return &m_ptr;
244-
}
245-
246-
RomFsControllerPtr::RomFsControllerPtr() :
247-
m_ptr(nullptr)
248-
{
249-
}
250-
251-
RomFsControllerPtr::RomFsControllerPtr(RomFsControllerPtr && other) noexcept
252-
{
253-
m_ptr = other.m_ptr;
254-
other.m_ptr = nullptr;
255-
}
256-
257-
258-
RomFsControllerPtr::~RomFsControllerPtr()
259-
{
260-
if (m_ptr)
261-
{
262-
m_ptr->Release();
263-
m_ptr = nullptr;
264-
}
265-
}
266-
267-
IRomFsController ** RomFsControllerPtr::GetAddressForSet()
268-
{
269-
if (m_ptr)
270-
{
271-
m_ptr->Release();
272-
m_ptr = nullptr;
273-
}
274-
return &m_ptr;
275-
}
276-
277-
IRomFsController * RomFsControllerPtr::operator->() const
278-
{
279-
return m_ptr;
280-
}
104+
template class InterfacePtr<IVirtualDirectory>;
105+
template class InterfacePtr<IVirtualFile>;
106+
template class InterfacePtr<IFileSysNCA>;
107+
template class InterfacePtr<ISaveDataFactory>;
108+
template class InterfacePtr<IRomFsController>;
109+
template class InterfacePtr<IFileSysNACP>;

0 commit comments

Comments
 (0)