Skip to content

Commit 336aaaf

Browse files
committed
Add DownloaderFactory class
1 parent f68f703 commit 336aaaf

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

src/internal/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ target_sources(scratchcpp
99
projecturl.cpp
1010
projecturl.h
1111
idownloader.h
12+
idownloaderfactory.h
1213
)
1314

1415
if (LIBSCRATCHCPP_NETWORK_SUPPORT)
1516
target_sources(scratchcpp
1617
PRIVATE
1718
downloader.cpp
1819
downloader.h
20+
downloaderfactory.cpp
21+
downloaderfactory.h
1922
)
2023
endif()

src/internal/downloaderfactory.cpp

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+
#include "downloaderfactory.h"
4+
#include "downloader.h"
5+
6+
using namespace libscratchcpp;
7+
8+
std::shared_ptr<DownloaderFactory> DownloaderFactory::m_instance = std::make_shared<DownloaderFactory>();
9+
10+
DownloaderFactory::DownloaderFactory()
11+
{
12+
}
13+
14+
std::shared_ptr<DownloaderFactory> DownloaderFactory::instance()
15+
{
16+
return m_instance;
17+
}
18+
19+
std::shared_ptr<IDownloader> DownloaderFactory::create() const
20+
{
21+
return std::make_shared<Downloader>();
22+
}

src/internal/downloaderfactory.h

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 "idownloaderfactory.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class DownloaderFactory : public IDownloaderFactory
11+
{
12+
public:
13+
DownloaderFactory();
14+
15+
static std::shared_ptr<DownloaderFactory> instance();
16+
std::shared_ptr<IDownloader> create() const override;
17+
18+
private:
19+
static std::shared_ptr<DownloaderFactory> m_instance;
20+
};
21+
22+
} // namespace libscratchcpp

src/internal/idownloaderfactory.h

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 IDownloader;
11+
12+
class IDownloaderFactory
13+
{
14+
public:
15+
virtual ~IDownloaderFactory() { }
16+
17+
virtual std::shared_ptr<IDownloader> create() const = 0;
18+
};
19+
20+
} // namespace libscratchcpp

test/network/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,20 @@ target_link_libraries(
1111
)
1212

1313
gtest_discover_tests(projecturl_test)
14+
15+
if (LIBSCRATCHCPP_NETWORK_SUPPORT)
16+
# downloaderfactory_test
17+
add_executable(
18+
downloaderfactory_test
19+
downloaderfactory_test.cpp
20+
)
21+
22+
target_link_libraries(
23+
downloaderfactory_test
24+
GTest::gtest_main
25+
scratchcpp
26+
cpr::cpr
27+
)
28+
29+
gtest_discover_tests(downloaderfactory_test)
30+
endif()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <internal/downloaderfactory.h>
2+
#include <internal/downloader.h>
3+
4+
#include "../common.h"
5+
6+
using namespace libscratchcpp;
7+
8+
TEST(DownloaderFactoryTest, Create)
9+
{
10+
auto factory = DownloaderFactory::instance();
11+
std::shared_ptr<IDownloader> downloader = factory->create();
12+
ASSERT_TRUE(downloader);
13+
ASSERT_TRUE(std::dynamic_pointer_cast<Downloader>(downloader));
14+
}

0 commit comments

Comments
 (0)