Skip to content

Commit 9481957

Browse files
committed
ScratchConfiguration: Initialize when needed
1 parent c82dadb commit 9481957

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

include/scratchcpp/scratchconfiguration.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ class LIBSCRATCHCPP_EXPORT ScratchConfiguration
4747

4848
private:
4949
static const std::vector<std::shared_ptr<IExtension>> getExtensions();
50+
static std::shared_ptr<ScratchConfigurationPrivate> &getImpl();
5051

51-
static std::unique_ptr<ScratchConfigurationPrivate> impl;
52+
static inline std::shared_ptr<ScratchConfigurationPrivate> impl;
5253
};
5354

5455
} // namespace libscratchcpp

src/scratchconfiguration.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99

1010
using namespace libscratchcpp;
1111

12-
std::unique_ptr<ScratchConfigurationPrivate> ScratchConfiguration::impl = std::make_unique<ScratchConfigurationPrivate>();
13-
1412
/*! Registers the given extension. */
1513
void ScratchConfiguration::registerExtension(std::shared_ptr<IExtension> extension)
1614
{
17-
impl->registerExtension(extension);
15+
getImpl()->registerExtension(extension);
1816
}
1917

2018
/*! Returns the extension with the given name, or nullptr if it isn't registered. */
2119
IExtension *ScratchConfiguration::getExtension(const std::string &name)
2220
{
23-
return impl->getExtension(name);
21+
return getImpl()->getExtension(name);
2422
}
2523

2624
/*! Registers the given graphics effect. */
@@ -29,21 +27,21 @@ void ScratchConfiguration::registerGraphicsEffect(std::shared_ptr<IGraphicsEffec
2927
if (!effect)
3028
return;
3129

32-
impl->graphicsEffects[effect->name()] = effect;
30+
getImpl()->graphicsEffects[effect->name()] = effect;
3331
}
3432

3533
/*! Removes the given graphics effect. */
3634
void ScratchConfiguration::removeGraphicsEffect(const std::string &name)
3735
{
38-
impl->graphicsEffects.erase(name);
36+
getImpl()->graphicsEffects.erase(name);
3937
}
4038

4139
/*! Returns the graphics effect with the given name, or nullptr if it isn't registered. */
4240
IGraphicsEffect *ScratchConfiguration::getGraphicsEffect(const std::string &name)
4341
{
44-
auto it = impl->graphicsEffects.find(name);
42+
auto it = getImpl()->graphicsEffects.find(name);
4543

46-
if (it == impl->graphicsEffects.cend())
44+
if (it == getImpl()->graphicsEffects.cend())
4745
return nullptr;
4846
else
4947
return it->second.get();
@@ -76,5 +74,13 @@ int ScratchConfiguration::patchVersion()
7674

7775
const std::vector<std::shared_ptr<IExtension>> ScratchConfiguration::getExtensions()
7876
{
79-
return impl->extensions;
77+
return getImpl()->extensions;
78+
}
79+
80+
std::shared_ptr<ScratchConfigurationPrivate> &ScratchConfiguration::getImpl()
81+
{
82+
if (!impl)
83+
impl = std::make_unique<ScratchConfigurationPrivate>();
84+
85+
return impl;
8086
}

0 commit comments

Comments
 (0)