Skip to content

Commit d4ebd0e

Browse files
committed
Replace download progress callback with a signal
1 parent 118be3e commit d4ebd0e

File tree

9 files changed

+21
-28
lines changed

9 files changed

+21
-28
lines changed

include/scratchcpp/project.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "spimpl.h"
88

99
#include "global.h"
10+
#include "signal.h"
1011

1112
namespace libscratchcpp
1213
{
@@ -42,7 +43,7 @@ class LIBSCRATCHCPP_EXPORT Project
4243

4344
std::shared_ptr<IEngine> engine() const;
4445

45-
void setDownloadProgressCallback(const std::function<void(unsigned int, unsigned int)> &&f);
46+
sigslot::signal<unsigned int, unsigned int> &downloadProgressChanged();
4647

4748
private:
4849
spimpl::unique_impl_ptr<ProjectPrivate> impl;

src/internal/iprojectdownloader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string>
66
#include <vector>
77
#include <functional>
8+
#include <scratchcpp/signal.h>
89

910
namespace libscratchcpp
1011
{
@@ -18,7 +19,7 @@ class IProjectDownloader
1819
virtual bool downloadAssets(const std::vector<std::string> &assetIds) = 0;
1920
virtual void cancel() = 0;
2021

21-
virtual void setDownloadProgressCallback(const std::function<void(unsigned int, unsigned int)> &f) = 0;
22+
virtual sigslot::signal<unsigned int, unsigned int> &downloadProgressChanged() = 0;
2223

2324
virtual const std::string &json() const = 0;
2425
virtual const std::vector<std::string> &assets() const = 0;

src/internal/projectdownloader.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,7 @@ bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)
149149
m_assets[index] = downloader->text();
150150
m_downloadedAssetCount++;
151151
std::cout << "Downloaded assets: " << m_downloadedAssetCount << " of " << count << std::endl;
152-
153-
m_downloadProgressCallbackMutex.lock();
154-
155-
if (m_downloadProgressCallback)
156-
m_downloadProgressCallback(m_downloadedAssetCount, count);
157-
158-
m_downloadProgressCallbackMutex.unlock();
152+
m_downloadProgressChanged(m_downloadedAssetCount, count);
159153
m_assetsMutex.unlock();
160154
}
161155
}
@@ -182,11 +176,9 @@ void ProjectDownloader::cancel()
182176
m_downloadedAssetCount = 0;
183177
}
184178

185-
void ProjectDownloader::setDownloadProgressCallback(const std::function<void(unsigned int, unsigned int)> &f)
179+
sigslot::signal<unsigned int, unsigned int> &ProjectDownloader::downloadProgressChanged()
186180
{
187-
m_downloadProgressCallbackMutex.lock();
188-
m_downloadProgressCallback = f;
189-
m_downloadProgressCallbackMutex.unlock();
181+
return m_downloadProgressChanged;
190182
}
191183

192184
const std::string &ProjectDownloader::json() const

src/internal/projectdownloader.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ProjectDownloader : public IProjectDownloader
2323
bool downloadAssets(const std::vector<std::string> &assetIds) override;
2424
void cancel() override;
2525

26-
void setDownloadProgressCallback(const std::function<void(unsigned int, unsigned int)> &f) override;
26+
sigslot::signal<unsigned int, unsigned int> &downloadProgressChanged() override;
2727

2828
const std::string &json() const override;
2929
const std::vector<std::string> &assets() const override;
@@ -38,8 +38,7 @@ class ProjectDownloader : public IProjectDownloader
3838
std::atomic<unsigned int> m_downloadedAssetCount = 0;
3939
bool m_cancel = false;
4040
std::mutex m_cancelMutex;
41-
std::function<void(unsigned int, unsigned int)> m_downloadProgressCallback = nullptr;
42-
std::mutex m_downloadProgressCallbackMutex;
41+
sigslot::signal<unsigned int, unsigned int> m_downloadProgressChanged;
4342
};
4443

4544
} // namespace libscratchcpp

src/project.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ std::shared_ptr<IEngine> Project::engine() const
100100
}
101101

102102
/*!
103-
* Sets the function which will be called when the asset download progress changes.
103+
* Emits when the asset download progress changes.
104104
* \note The first parameter is the number of downloaded assets and the latter is the number of all assets to download.
105105
*/
106-
void Project::setDownloadProgressCallback(const std::function<void(unsigned int, unsigned int)> &&f)
106+
sigslot::signal<unsigned int, unsigned int> &Project::downloadProgressChanged()
107107
{
108-
impl->setDownloadProgressCallback(f);
108+
return impl->downloadProgressChanged();
109109
}

src/project_p.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void ProjectPrivate::setScratchVersion(ScratchVersion version)
183183
std::cerr << "Unsupported Scratch version: " << static_cast<int>(version) << std::endl;
184184
}
185185

186-
void ProjectPrivate::setDownloadProgressCallback(const std::function<void(unsigned int, unsigned int)> &f)
186+
sigslot::signal<unsigned int, unsigned int> &ProjectPrivate::downloadProgressChanged()
187187
{
188-
downloader->setDownloadProgressCallback(f);
188+
return downloader->downloadProgressChanged();
189189
}

src/project_p.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct ProjectPrivate
2828
void detectScratchVersion();
2929
void setScratchVersion(ScratchVersion version);
3030

31-
void setDownloadProgressCallback(const std::function<void(unsigned int, unsigned int)> &f);
31+
sigslot::signal<unsigned int, unsigned int> &downloadProgressChanged();
3232

3333
ScratchVersion scratchVersion = ScratchVersion::Invalid;
3434
std::string fileName;

test/mocks/projectdownloadermock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ProjectDownloaderMock : public IProjectDownloader
1212
MOCK_METHOD(bool, downloadAssets, (const std::vector<std::string> &), (override));
1313
MOCK_METHOD(void, cancel, (), (override));
1414

15-
MOCK_METHOD(void, setDownloadProgressCallback, (const std::function<void(unsigned int, unsigned int)> &), (override));
15+
MOCK_METHOD((sigslot::signal<unsigned int, unsigned int> &), downloadProgressChanged, (), (override));
1616

1717
MOCK_METHOD(const std::string &, json, (), (const, override));
1818
MOCK_METHOD(const std::vector<std::string> &, assets, (), (const, override));

test/project/project_test.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using namespace libscratchcpp;
1010

1111
using ::testing::Return;
12+
using ::testing::ReturnRef;
1213

1314
class ProjectTest : public testing::Test
1415
{
@@ -158,7 +159,7 @@ TEST_F(ProjectTest, ScratchVersion)
158159
ASSERT_EQ(p.scratchVersion(), ScratchVersion::Scratch3);
159160
}
160161

161-
TEST(LoadProjectTest, DownloadProgressCallback)
162+
TEST(LoadProjectTest, DownloadProgressChanged)
162163
{
163164
ProjectDownloaderFactoryMock factory;
164165
auto downloader = std::make_shared<ProjectDownloaderMock>();
@@ -168,8 +169,7 @@ TEST(LoadProjectTest, DownloadProgressCallback)
168169
ProjectPrivate p;
169170
ProjectPrivate::downloaderFactory = nullptr;
170171

171-
auto lambda = [](unsigned int, unsigned int) {};
172-
// TODO: Check the function parameter, if possible (std::function doesn't have operator== for this)
173-
EXPECT_CALL(*downloader, setDownloadProgressCallback);
174-
p.setDownloadProgressCallback(lambda);
172+
sigslot::signal<unsigned int, unsigned int> signal;
173+
EXPECT_CALL(*downloader, downloadProgressChanged).WillOnce(ReturnRef(signal));
174+
ASSERT_EQ(&p.downloadProgressChanged(), &signal);
175175
}

0 commit comments

Comments
 (0)