Skip to content

Commit 276bbbb

Browse files
committed
Add ProjectDownloaderFactory class
1 parent 70b0af0 commit 276bbbb

File tree

7 files changed

+105
-0
lines changed

7 files changed

+105
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ if (LIBSCRATCHCPP_NETWORK_SUPPORT)
7777
GIT_TAG 3b15fa82ea74739b574d705fea44959b58142eb8) # 1.10.5
7878
FetchContent_MakeAvailable(cpr)
7979
target_link_libraries(scratchcpp PRIVATE cpr::cpr)
80+
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_NETWORK_SUPPORT)
8081
endif()
8182

8283
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_LIBRARY)

src/internal/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ target_sources(scratchcpp
1111
idownloader.h
1212
idownloaderfactory.h
1313
iprojectdownloader.h
14+
iprojectdownloaderfactory.h
15+
projectdownloaderfactory.cpp
16+
projectdownloaderfactory.h
1417
)
1518

1619
if (LIBSCRATCHCPP_NETWORK_SUPPORT)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <memory>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class IProjectDownloader;
11+
12+
class IProjectDownloaderFactory
13+
{
14+
public:
15+
virtual ~IProjectDownloaderFactory() { }
16+
17+
virtual std::shared_ptr<IProjectDownloader> create() const = 0;
18+
};
19+
20+
} // namespace libscratchcpp
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "projectdownloaderfactory.h"
4+
#ifdef LIBSCRATCHCPP_NETWORK_SUPPORT
5+
#include "projectdownloader.h"
6+
#else
7+
#include "projectdownloaderstub.h"
8+
#endif
9+
10+
using namespace libscratchcpp;
11+
12+
std::shared_ptr<ProjectDownloaderFactory> ProjectDownloaderFactory::m_instance = std::make_shared<ProjectDownloaderFactory>();
13+
14+
ProjectDownloaderFactory::ProjectDownloaderFactory()
15+
{
16+
}
17+
18+
std::shared_ptr<ProjectDownloaderFactory> ProjectDownloaderFactory::instance()
19+
{
20+
return m_instance;
21+
}
22+
23+
std::shared_ptr<IProjectDownloader> ProjectDownloaderFactory::create() const
24+
{
25+
#ifdef LIBSCRATCHCPP_NETWORK_SUPPORT
26+
return std::make_shared<ProjectDownloader>();
27+
#else
28+
return std::make_shared<ProjectDownloaderStub>();
29+
#endif
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "iprojectdownloaderfactory.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class ProjectDownloaderFactory : public IProjectDownloaderFactory
11+
{
12+
public:
13+
ProjectDownloaderFactory();
14+
15+
static std::shared_ptr<ProjectDownloaderFactory> instance();
16+
std::shared_ptr<IProjectDownloader> create() const override;
17+
18+
private:
19+
static std::shared_ptr<ProjectDownloaderFactory> m_instance;
20+
};
21+
22+
} // namespace libscratchcpp

test/network/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,19 @@ if (LIBSCRATCHCPP_NETWORK_SUPPORT)
4343
)
4444

4545
gtest_discover_tests(projectdownloader_test)
46+
47+
# projectdownloaderfactory_test
48+
add_executable(
49+
projectdownloaderfactory_test
50+
projectdownloaderfactory_test.cpp
51+
)
52+
53+
target_link_libraries(
54+
projectdownloaderfactory_test
55+
GTest::gtest_main
56+
scratchcpp
57+
cpr::cpr
58+
)
59+
60+
gtest_discover_tests(projectdownloaderfactory_test)
4661
endif()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <internal/projectdownloaderfactory.h>
2+
#include <internal/projectdownloader.h>
3+
4+
#include "../common.h"
5+
6+
using namespace libscratchcpp;
7+
8+
TEST(ProjectDownloaderFactoryTest, Create)
9+
{
10+
auto factory = ProjectDownloaderFactory::instance();
11+
std::shared_ptr<IProjectDownloader> downloader = factory->create();
12+
ASSERT_TRUE(downloader);
13+
ASSERT_TRUE(std::dynamic_pointer_cast<ProjectDownloader>(downloader));
14+
}

0 commit comments

Comments
 (0)