Skip to content

Commit 0ca1721

Browse files
committed
reduce copypasta
1 parent a5d269d commit 0ca1721

18 files changed

+93
-91
lines changed

src/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ set(Source_Files__Resource
325325
${CMAKE_CURRENT_SOURCE_DIR}/resource/ResourceLoader.cpp
326326
${CMAKE_CURRENT_SOURCE_DIR}/resource/ResourceLoader.h
327327
${CMAKE_CURRENT_SOURCE_DIR}/resource/ResourceFactory.h
328+
${CMAKE_CURRENT_SOURCE_DIR}/resource/ResourceFactoryBinary.h
329+
${CMAKE_CURRENT_SOURCE_DIR}/resource/ResourceFactoryBinary.cpp
330+
${CMAKE_CURRENT_SOURCE_DIR}/resource/ResourceFactoryXML.h
331+
${CMAKE_CURRENT_SOURCE_DIR}/resource/ResourceFactoryXML.cpp
328332
)
329333
source_group("resource" FILES ${Source_Files__Resource})
330334

src/resource/ResourceFactory.h

+2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ namespace LUS {
88
class ResourceFactory {
99
public:
1010
virtual std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) = 0;
11+
protected:
12+
virtual bool FileHasValidFormatAndReader(std::shared_ptr<File> file) = 0;
1113
};
1214
} // namespace LUS
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "ResourceFactoryBinary.h"
2+
#include "spdlog/spdlog.h"
3+
4+
namespace LUS {
5+
bool ResourceFactoryBinary::FileHasValidFormatAndReader(std::shared_ptr<File> file) {
6+
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
7+
SPDLOG_ERROR("resource file format does not match factory format.");
8+
return false;
9+
}
10+
11+
if (file->Reader == nullptr) {
12+
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
13+
file->InitData->Path);
14+
return false;
15+
}
16+
17+
return true;
18+
};
19+
} // namespace LUS

src/resource/ResourceFactoryBinary.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "ResourceFactory.h"
4+
5+
namespace LUS {
6+
class ResourceFactoryBinary : public ResourceFactory {
7+
protected:
8+
bool FileHasValidFormatAndReader(std::shared_ptr<File> file) override;
9+
};
10+
} // namespace LUS

src/resource/ResourceFactoryXML.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "ResourceFactoryXML.h"
2+
#include "spdlog/spdlog.h"
3+
4+
namespace LUS {
5+
bool ResourceFactoryXML::FileHasValidFormatAndReader(std::shared_ptr<File> file) {
6+
if (file->InitData->Format != RESOURCE_FORMAT_XML) {
7+
SPDLOG_ERROR("resource file format does not match factory format.");
8+
return false;
9+
}
10+
11+
if (file->XmlDocument == nullptr) {
12+
SPDLOG_ERROR("Failed to load resource: File has no XML document ({} - {})", file->InitData->Type,
13+
file->InitData->Path);
14+
return false;
15+
}
16+
17+
return true;
18+
};
19+
} // namespace LUS

src/resource/ResourceFactoryXML.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "ResourceFactory.h"
4+
5+
namespace LUS {
6+
class ResourceFactoryXML : public ResourceFactory {
7+
protected:
8+
bool FileHasValidFormatAndReader(std::shared_ptr<File> file) override;
9+
};
10+
} // namespace LUS

src/resource/factory/ArrayFactory.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44

55
namespace LUS {
66
std::shared_ptr<IResource> ResourceFactoryBinaryArrayV0::ReadResource(std::shared_ptr<File> file) {
7-
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
8-
SPDLOG_ERROR("resource file format does not match factory format.");
9-
return nullptr;
10-
}
11-
12-
if (file->Reader == nullptr) {
13-
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
14-
file->InitData->Path);
7+
if (!FileHasValidFormatAndReader()) {
158
return nullptr;
169
}
1710

src/resource/factory/ArrayFactory.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22

33
#include "resource/Resource.h"
4-
#include "resource/ResourceFactory.h"
4+
#include "resource/BinaryResourceFactory.h"
55

66
namespace LUS {
7-
class ResourceFactoryBinaryArrayV0 : public ResourceFactory {
7+
class ResourceFactoryBinaryArrayV0 : public ResourceFactoryBinary {
88
public:
99
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1010
};

src/resource/factory/BlobFactory.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44

55
namespace LUS {
66
std::shared_ptr<IResource> ResourceFactoryBinaryBlobV0::ReadResource(std::shared_ptr<File> file) {
7-
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
8-
SPDLOG_ERROR("resource file format does not match factory format.");
9-
return nullptr;
10-
}
11-
12-
if (file->Reader == nullptr) {
13-
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
14-
file->InitData->Path);
7+
if (!FileHasValidFormatAndReader()) {
158
return nullptr;
169
}
1710

src/resource/factory/BlobFactory.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22

33
#include "resource/Resource.h"
4-
#include "resource/ResourceFactory.h"
4+
#include "resource/BinaryResourceFactory.h"
55

66
namespace LUS {
7-
class ResourceFactoryBinaryBlobV0 : public ResourceFactory {
7+
class ResourceFactoryBinaryBlobV0 : public ResourceFactoryBinary {
88
public:
99
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1010
};

src/resource/factory/DisplayListFactory.cpp

+2-17
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,7 @@ uint32_t ResourceFactoryDisplayList::GetCombineLERPValue(std::string valStr) {
129129
}
130130

131131
std::shared_ptr<IResource> ResourceFactoryBinaryDisplayListV0::ReadResource(std::shared_ptr<File> file) {
132-
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
133-
SPDLOG_ERROR("resource file format does not match factory format.");
134-
return nullptr;
135-
}
136-
137-
if (file->Reader == nullptr) {
138-
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
139-
file->InitData->Path);
132+
if (!FileHasValidFormatAndReader()) {
140133
return nullptr;
141134
}
142135

@@ -173,18 +166,10 @@ std::shared_ptr<IResource> ResourceFactoryBinaryDisplayListV0::ReadResource(std:
173166
}
174167

175168
std::shared_ptr<IResource> ResourceFactoryXMLDisplayListV0::ReadResource(std::shared_ptr<File> file) {
176-
if (file->InitData->Format != RESOURCE_FORMAT_XML) {
177-
SPDLOG_ERROR("resource file format does not match factory format.");
169+
if (!FileHasValidFormatAndReader()) {
178170
return nullptr;
179171
}
180172

181-
if (file->XmlDocument == nullptr) {
182-
SPDLOG_ERROR("Failed to load resource: File has no XML document ({} - {})", file->InitData->Type,
183-
file->InitData->Path);
184-
return nullptr;
185-
}
186-
187-
188173
auto dl = std::make_shared<DisplayList>(file->InitData);
189174

190175
auto child = file->XmlDocument->FirstChildElement()->FirstChildElement();

src/resource/factory/DisplayListFactory.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
#pragma once
22

33
#include "resource/Resource.h"
4-
#include "resource/ResourceFactory.h"
4+
#include "resource/BinaryResourceFactory.h"
5+
#include "resource/XMLResourceFactory.h"
56

67
namespace LUS {
7-
class ResourceFactoryDisplayList : public ResourceFactory {
8+
class ResourceFactoryDisplayList {
89
protected:
910
uint32_t GetCombineLERPValue(std::string valStr);
1011
};
1112

12-
class ResourceFactoryBinaryDisplayListV0 : public ResourceFactoryDisplayList {
13+
class ResourceFactoryBinaryDisplayListV0 : public ResourceFactoryDisplayList, public BinaryResourceFactory {
1314
public:
1415
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1516
};
1617

17-
class ResourceFactoryXMLDisplayListV0 : public ResourceFactoryDisplayList {
18+
class ResourceFactoryXMLDisplayListV0 : public ResourceFactoryDisplayList, public XMLResourceFactory {
1819
public:
1920
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
2021
};

src/resource/factory/MatrixFactory.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44

55
namespace LUS {
66
std::shared_ptr<IResource> ResourceFactoryBinaryMatrixV0::ReadResource(std::shared_ptr<File> file) {
7-
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
8-
SPDLOG_ERROR("resource file format does not match factory format.");
9-
return nullptr;
10-
}
11-
12-
if (file->Reader == nullptr) {
13-
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
14-
file->InitData->Path);
7+
if (!FileHasValidFormatAndReader()) {
158
return nullptr;
169
}
1710

src/resource/factory/MatrixFactory.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22

33
#include "resource/Resource.h"
4-
#include "resource/ResourceFactory.h"
4+
#include "resource/BinaryResourceFactory.h"
55

66
namespace LUS {
7-
class ResourceFactoryBinaryMatrixV0 : public ResourceFactory {
7+
class ResourceFactoryBinaryMatrixV0 : public ResourceFactoryBinary {
88
public:
99
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1010
};

src/resource/factory/TextureFactory.cpp

+4-18
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@
55
namespace LUS {
66

77
std::shared_ptr<IResource> ResourceFactoryBinaryTextureV0::ReadResource(std::shared_ptr<File> file) {
8-
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
9-
SPDLOG_ERROR("resource file format does not match factory format.");
10-
return nullptr;
11-
}
12-
13-
if (file->Reader == nullptr) {
14-
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
15-
file->InitData->Path);
8+
if (!FileHasValidFormatAndReader()) {
169
return nullptr;
1710
}
1811

@@ -30,16 +23,9 @@ std::shared_ptr<IResource> ResourceFactoryBinaryTextureV0::ReadResource(std::sha
3023
}
3124

3225
std::shared_ptr<IResource> ResourceFactoryBinaryTextureV1::ReadResource(std::shared_ptr<File> file) {
33-
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
34-
SPDLOG_ERROR("resource file format does not match factory format.");
35-
return nullptr;
36-
}
37-
38-
if (file->Reader == nullptr) {
39-
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
40-
file->InitData->Path);
41-
return nullptr;
42-
}
26+
if (!FileHasValidFormatAndReader()) {
27+
return nullptr;
28+
}
4329

4430
auto texture = std::make_shared<Texture>(file->InitData);
4531

src/resource/factory/TextureFactory.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#pragma once
22

33
#include "resource/Resource.h"
4-
#include "resource/ResourceFactory.h"
4+
#include "resource/BinaryResourceFactory.h"
55

66
namespace LUS {
7-
class ResourceFactoryBinaryTextureV0 : public ResourceFactory {
7+
class ResourceFactoryBinaryTextureV0 : public ResourceFactoryBinary {
88
public:
99
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1010
};
1111

12-
class ResourceFactoryBinaryTextureV1 : public ResourceFactory {
12+
class ResourceFactoryBinaryTextureV1 : public ResourceFactoryBinary {
1313
public:
1414
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1515
};

src/resource/factory/VertexFactory.cpp

+2-16
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44

55
namespace LUS {
66
std::shared_ptr<IResource> ResourceFactoryBinaryVertexV0::ReadResource(std::shared_ptr<File> file) {
7-
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
8-
SPDLOG_ERROR("resource file format does not match factory format.");
9-
return nullptr;
10-
}
11-
12-
if (file->Reader == nullptr) {
13-
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
14-
file->InitData->Path);
7+
if (!FileHasValidFormatAndReader()) {
158
return nullptr;
169
}
1710

@@ -39,14 +32,7 @@ std::shared_ptr<IResource> ResourceFactoryBinaryVertexV0::ReadResource(std::shar
3932
}
4033

4134
std::shared_ptr<IResource> ResourceFactoryXMLVertexV0::ReadResource(std::shared_ptr<File> file) {
42-
if (file->InitData->Format != RESOURCE_FORMAT_XML) {
43-
SPDLOG_ERROR("resource file format does not match factory format.");
44-
return nullptr;
45-
}
46-
47-
if (file->XmlDocument == nullptr) {
48-
SPDLOG_ERROR("Failed to load resource: File has no XML document ({} - {})", file->InitData->Type,
49-
file->InitData->Path);
35+
if (!FileHasValidFormatAndReader()) {
5036
return nullptr;
5137
}
5238

src/resource/factory/VertexFactory.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#pragma once
22

33
#include "resource/Resource.h"
4-
#include "resource/ResourceFactory.h"
4+
#include "resource/BinaryResourceFactory.h"
5+
#include "resource/XMLResourceFactory.h"
56

67
namespace LUS {
7-
class ResourceFactoryBinaryVertexV0 : public ResourceFactory {
8+
class ResourceFactoryBinaryVertexV0 : public ResourceFactoryBinary {
89
public:
910
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1011
};
1112

12-
class ResourceFactoryXMLVertexV0 : public ResourceFactory {
13+
class ResourceFactoryXMLVertexV0 : public ResourceFactoryXML {
1314
public:
1415
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1516
};

0 commit comments

Comments
 (0)