Skip to content

Commit dde9c6c

Browse files
committed
Hide static data members
1 parent bd96dba commit dde9c6c

21 files changed

+139
-62
lines changed

src/blocks/eventblocks.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ CompilerValue *EventBlocks::compileWhenKeyPressed(Compiler *compiler)
173173
return nullptr;
174174
}
175175

176+
IAudioInput *EventBlocks::audioInput()
177+
{
178+
return m_audioInput;
179+
}
180+
181+
void EventBlocks::setAudioInput(IAudioInput *audioInput)
182+
{
183+
m_audioInput = audioInput;
184+
}
185+
176186
BLOCK_EXPORT bool event_whentouchingobject_predicate(Target *target, const StringPtr *name)
177187
{
178188
static const StringPtr MOUSE_STR = StringPtr("_mouse_");
@@ -198,10 +208,10 @@ BLOCK_EXPORT bool event_whentouchingobject_predicate(Target *target, const Strin
198208

199209
BLOCK_EXPORT bool event_whengreaterthan_loudness_predicate(ExecutionContext *ctx, double value)
200210
{
201-
if (!EventBlocks::audioInput)
202-
EventBlocks::audioInput = AudioInput::instance().get();
211+
if (!EventBlocks::audioInput())
212+
EventBlocks::setAudioInput(AudioInput::instance().get());
203213

204-
auto audioLoudness = EventBlocks::audioInput->audioLoudness();
214+
auto audioLoudness = EventBlocks::audioInput()->audioLoudness();
205215
return (audioLoudness->getLoudness() > value);
206216
}
207217

src/blocks/eventblocks.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class LIBSCRATCHCPP_TEST_EXPORT EventBlocks : public IExtension
2020

2121
void registerBlocks(IEngine *engine) override;
2222

23-
static inline IAudioInput *audioInput = nullptr;
23+
static IAudioInput *audioInput();
24+
static void setAudioInput(IAudioInput *audioInput);
2425

2526
private:
2627
static CompilerValue *compileWhenTouchingObject(Compiler *compiler);
@@ -35,6 +36,8 @@ class LIBSCRATCHCPP_TEST_EXPORT EventBlocks : public IExtension
3536
static CompilerValue *compileBroadcast(Compiler *compiler);
3637
static CompilerValue *compileBroadcastAndWait(Compiler *compiler);
3738
static CompilerValue *compileWhenKeyPressed(Compiler *compiler);
39+
40+
static inline IAudioInput *m_audioInput = nullptr;
3841
};
3942

4043
} // namespace libscratchcpp

src/blocks/sensingblocks.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,26 @@ void SensingBlocks::askNextQuestion()
431431
engine->questionAsked()(question->question);
432432
}
433433

434+
IAudioInput *SensingBlocks::audioInput()
435+
{
436+
return m_audioInput;
437+
}
438+
439+
void SensingBlocks::setAudioInput(IAudioInput *audioInput)
440+
{
441+
m_audioInput = audioInput;
442+
}
443+
444+
IClock *SensingBlocks::clock()
445+
{
446+
return m_clock;
447+
}
448+
449+
void SensingBlocks::setClock(IClock *clock)
450+
{
451+
m_clock = clock;
452+
}
453+
434454
BLOCK_EXPORT bool sensing_touching_mouse(Target *target)
435455
{
436456
IEngine *engine = target->engine();
@@ -556,10 +576,10 @@ BLOCK_EXPORT void sensing_setdragmode(Sprite *sprite, bool draggable)
556576

557577
BLOCK_EXPORT double sensing_loudness()
558578
{
559-
if (!SensingBlocks::audioInput)
560-
SensingBlocks::audioInput = AudioInput::instance().get();
579+
if (!SensingBlocks::audioInput())
580+
SensingBlocks::setAudioInput(AudioInput::instance().get());
561581

562-
auto audioLoudness = SensingBlocks::audioInput->audioLoudness();
582+
auto audioLoudness = SensingBlocks::audioInput()->audioLoudness();
563583
return audioLoudness->getLoudness();
564584
}
565585

@@ -773,9 +793,9 @@ BLOCK_EXPORT double sensing_current_second()
773793

774794
BLOCK_EXPORT double sensing_dayssince2000()
775795
{
776-
if (!SensingBlocks::clock)
777-
SensingBlocks::clock = Clock::instance().get();
796+
if (!SensingBlocks::clock())
797+
SensingBlocks::setClock(Clock::instance().get());
778798

779-
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(SensingBlocks::clock->currentSystemTime().time_since_epoch()).count();
799+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(SensingBlocks::clock()->currentSystemTime().time_since_epoch()).count();
780800
return ms / 86400000.0 - 10957.0;
781801
}

src/blocks/sensingblocks.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ class LIBSCRATCHCPP_TEST_EXPORT SensingBlocks : public IExtension
2929
static void clearQuestions();
3030
static void askQuestion(ExecutionContext *ctx, const StringPtr *question);
3131

32-
static inline IAudioInput *audioInput = nullptr;
33-
static inline IClock *clock = nullptr;
32+
static IAudioInput *audioInput();
33+
static void setAudioInput(IAudioInput *audioInput);
34+
35+
static IClock *clock();
36+
static void setClock(IClock *clock);
3437

3538
private:
3639
struct Question
@@ -73,6 +76,9 @@ class LIBSCRATCHCPP_TEST_EXPORT SensingBlocks : public IExtension
7376
static void askNextQuestion();
7477

7578
static inline std::vector<std::unique_ptr<Question>> m_questions;
79+
80+
static inline IAudioInput *m_audioInput = nullptr;
81+
static inline IClock *m_clock = nullptr;
7682
};
7783

7884
} // namespace libscratchcpp

src/engine/compiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ std::shared_ptr<ExecutableCode> Compiler::compile(Block *startBlock, CodeType co
6060
}
6161
}
6262

63-
impl->builder = impl->builderFactory->create(impl->ctx, procedurePrototype, codeType);
63+
impl->builder = impl->builderFactory()->create(impl->ctx, procedurePrototype, codeType);
6464
impl->substackTree.clear();
6565
impl->substackHit = false;
6666
impl->emptySubstack = false;
@@ -743,5 +743,5 @@ const std::unordered_set<std::string> &Compiler::unsupportedBlocks() const
743743
std::shared_ptr<CompilerContext> Compiler::createContext(IEngine *engine, Target *target)
744744
{
745745
CompilerPrivate::initBuilderFactory();
746-
return CompilerPrivate::builderFactory->createCtx(engine, target);
746+
return CompilerPrivate::builderFactory()->createCtx(engine, target);
747747
}

src/engine/compiler_p.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target) :
2626

2727
void CompilerPrivate::initBuilderFactory()
2828
{
29-
if (!builderFactory)
30-
builderFactory = CodeBuilderFactory::instance().get();
29+
if (!m_builderFactory)
30+
m_builderFactory = CodeBuilderFactory::instance().get();
3131
}
3232

3333
void CompilerPrivate::substackEnd()
@@ -67,3 +67,13 @@ void CompilerPrivate::substackEnd()
6767
if (!block && !substackTree.empty())
6868
substackEnd();
6969
}
70+
71+
ICodeBuilderFactory *CompilerPrivate::builderFactory()
72+
{
73+
return m_builderFactory;
74+
}
75+
76+
void CompilerPrivate::setBuilderFactory(ICodeBuilderFactory *builderFactory)
77+
{
78+
m_builderFactory = builderFactory;
79+
}

src/engine/compiler_p.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <vector>
77
#include <unordered_set>
88

9+
#include "test_export.h"
10+
911
namespace libscratchcpp
1012
{
1113

@@ -17,8 +19,9 @@ class CompilerValue;
1719
class ICodeBuilderFactory;
1820
class ICodeBuilder;
1921

20-
struct CompilerPrivate
22+
class LIBSCRATCHCPP_TEST_EXPORT CompilerPrivate
2123
{
24+
public:
2225
enum class SubstackType
2326
{
2427
Loop,
@@ -43,10 +46,15 @@ struct CompilerPrivate
4346
bool emptySubstack = false;
4447
bool warp = false;
4548

46-
static inline ICodeBuilderFactory *builderFactory = nullptr;
4749
std::shared_ptr<ICodeBuilder> builder;
4850

4951
std::unordered_set<std::string> unsupportedBlocks;
52+
53+
static ICodeBuilderFactory *builderFactory();
54+
static void setBuilderFactory(ICodeBuilderFactory *builderFactory);
55+
56+
private:
57+
static inline ICodeBuilderFactory *m_builderFactory = nullptr;
5058
};
5159

5260
} // namespace libscratchcpp

src/project_p.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313

1414
using namespace libscratchcpp;
1515

16-
IProjectDownloaderFactory *ProjectPrivate::downloaderFactory = nullptr;
17-
1816
ProjectPrivate::ProjectPrivate() :
1917
engine(std::make_shared<Engine>())
2018
{
21-
if (!downloaderFactory)
22-
downloaderFactory = ProjectDownloaderFactory::instance().get();
19+
if (!m_downloaderFactory)
20+
m_downloaderFactory = ProjectDownloaderFactory::instance().get();
2321

24-
downloader = downloaderFactory->create();
22+
downloader = m_downloaderFactory->create();
2523
}
2624

2725
ProjectPrivate::ProjectPrivate(const std::string &fileName) :
@@ -194,3 +192,13 @@ sigslot::signal<unsigned int, unsigned int> &ProjectPrivate::downloadProgressCha
194192
{
195193
return downloader->downloadProgressChanged();
196194
}
195+
196+
IProjectDownloaderFactory *ProjectPrivate::downloaderFactory()
197+
{
198+
return m_downloaderFactory;
199+
}
200+
201+
void ProjectPrivate::setDownloaderFactory(IProjectDownloaderFactory *downloaderFactory)
202+
{
203+
m_downloaderFactory = downloaderFactory;
204+
}

src/project_p.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ class IProjectReader;
1515
class IProjectDownloaderFactory;
1616
class IProjectDownloader;
1717

18-
struct LIBSCRATCHCPP_TEST_EXPORT ProjectPrivate
18+
class LIBSCRATCHCPP_TEST_EXPORT ProjectPrivate
1919
{
20+
public:
2021
ProjectPrivate();
2122
ProjectPrivate(const std::string &fileName);
2223
ProjectPrivate(const ProjectPrivate &) = delete;
@@ -35,8 +36,13 @@ struct LIBSCRATCHCPP_TEST_EXPORT ProjectPrivate
3536
std::atomic<bool> stopLoading = false;
3637
std::shared_ptr<IEngine> engine = nullptr;
3738

38-
static IProjectDownloaderFactory *downloaderFactory;
3939
std::shared_ptr<IProjectDownloader> downloader;
40+
41+
static IProjectDownloaderFactory *downloaderFactory();
42+
static void setDownloaderFactory(IProjectDownloaderFactory *downloaderFactory);
43+
44+
private:
45+
static inline IProjectDownloaderFactory *m_downloaderFactory = nullptr;
4046
};
4147

4248
}; // namespace libscratchcpp

src/scratch/sound_p.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55

66
using namespace libscratchcpp;
77

8-
IAudioOutput *SoundPrivate::audioOutput = nullptr;
9-
108
SoundPrivate::SoundPrivate()
119
{
1210
// NOTE: audioOutput must be initialized in the constructor to avoid static initialization order fiasco
13-
if (!audioOutput)
14-
audioOutput = AudioOutput::instance().get();
11+
if (!m_audioOutput)
12+
m_audioOutput = AudioOutput::instance().get();
13+
14+
player = m_audioOutput->createAudioPlayer();
15+
}
1516

16-
player = audioOutput->createAudioPlayer();
17+
void SoundPrivate::setAudioOutput(IAudioOutput *audioOutput)
18+
{
19+
m_audioOutput = audioOutput;
1720
}

0 commit comments

Comments
 (0)