Skip to content

Commit 882ce8e

Browse files
reuktpoole
authored andcommitted
Singleton: Add new macros to simplify singleton creation
The INLINE macros allow singletons to be declared and defined in one line, without requiring a separate JUCE_IMPLEMENT_SINGLETON statement.
1 parent 5179f4e commit 882ce8e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+69
-138
lines changed

extras/Projucer/Source/Application/jucer_AutoUpdater.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,3 @@ void LatestVersionCheckerAndUpdater::downloadAndInstall (const VersionInfo::Asse
591591
}
592592
}));
593593
}
594-
595-
//==============================================================================
596-
JUCE_IMPLEMENT_SINGLETON (LatestVersionCheckerAndUpdater)

extras/Projucer/Source/Application/jucer_AutoUpdater.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class LatestVersionCheckerAndUpdater final : public DeletedAtShutdown,
4848
void checkForNewVersion (bool isBackgroundCheck);
4949

5050
//==============================================================================
51-
JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (LatestVersionCheckerAndUpdater)
51+
JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE (LatestVersionCheckerAndUpdater)
5252

5353
private:
5454
//==============================================================================

modules/juce_analytics/analytics/juce_Analytics.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,4 @@ void Analytics::setSuspended (bool shouldBeSuspended)
8181
isSuspended = shouldBeSuspended;
8282
}
8383

84-
JUCE_IMPLEMENT_SINGLETON (Analytics)
85-
8684
}

modules/juce_analytics/analytics/juce_Analytics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class JUCE_API Analytics : public DeletedAtShutdown
104104
void setSuspended (bool shouldBeSuspended);
105105

106106
#ifndef DOXYGEN
107-
JUCE_DECLARE_SINGLETON (Analytics, false)
107+
JUCE_DECLARE_SINGLETON_INLINE (Analytics, false)
108108
#endif
109109

110110
private:

modules/juce_audio_devices/native/juce_Midi_windows.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ struct MidiService final : public DeletedAtShutdown
18791879
return *getInstance()->internal.get();
18801880
}
18811881

1882-
JUCE_DECLARE_SINGLETON (MidiService, false)
1882+
JUCE_DECLARE_SINGLETON_INLINE (MidiService, false)
18831883

18841884
private:
18851885
std::unique_ptr<MidiServiceType> internal;
@@ -1889,8 +1889,6 @@ struct MidiService final : public DeletedAtShutdown
18891889
} };
18901890
};
18911891

1892-
JUCE_IMPLEMENT_SINGLETON (MidiService)
1893-
18941892
//==============================================================================
18951893
static int findDefaultDeviceIndex (const Array<MidiDeviceInfo>& available, const MidiDeviceInfo& defaultDevice)
18961894
{

modules/juce_core/memory/juce_Singleton.h

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ struct SingletonHolder : private MutexType // (inherited so we can use the empt
135135
std::atomic<Type*> instance { nullptr };
136136
};
137137

138+
#ifndef DOXYGEN
139+
#define JUCE_PRIVATE_DECLARE_SINGLETON(Classname, mutex, doNotRecreate, inlineToken, getter) \
140+
static inlineToken juce::SingletonHolder<Classname, mutex, doNotRecreate> singletonHolder; \
141+
friend juce::SingletonHolder<Classname, mutex, doNotRecreate>; \
142+
static Classname* JUCE_CALLTYPE getInstance() { return singletonHolder.getter(); } \
143+
static Classname* JUCE_CALLTYPE getInstanceWithoutCreating() noexcept { return singletonHolder.instance; } \
144+
static void JUCE_CALLTYPE deleteInstance() noexcept { singletonHolder.deleteInstance(); } \
145+
void clearSingletonInstance() noexcept { singletonHolder.clear (this); }
146+
#endif
138147

139148
//==============================================================================
140149
/**
@@ -194,27 +203,28 @@ struct SingletonHolder : private MutexType // (inherited so we can use the empt
194203
@see JUCE_IMPLEMENT_SINGLETON, JUCE_DECLARE_SINGLETON_SINGLETHREADED
195204
*/
196205
#define JUCE_DECLARE_SINGLETON(Classname, doNotRecreateAfterDeletion) \
197-
\
198-
static juce::SingletonHolder<Classname, juce::CriticalSection, doNotRecreateAfterDeletion> singletonHolder; \
199-
friend juce::SingletonHolder<Classname, juce::CriticalSection, doNotRecreateAfterDeletion>; \
200-
\
201-
static Classname* JUCE_CALLTYPE getInstance() { return singletonHolder.get(); } \
202-
static Classname* JUCE_CALLTYPE getInstanceWithoutCreating() noexcept { return singletonHolder.instance; } \
203-
static void JUCE_CALLTYPE deleteInstance() noexcept { singletonHolder.deleteInstance(); } \
204-
void clearSingletonInstance() noexcept { singletonHolder.clear (this); }
206+
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::CriticalSection, doNotRecreateAfterDeletion, , get)
205207

208+
/**
209+
The same as JUCE_DECLARE_SINGLETON, but does not require a matching
210+
JUCE_IMPLEMENT_SINGLETON definition.
211+
*/
212+
#define JUCE_DECLARE_SINGLETON_INLINE(Classname, doNotRecreateAfterDeletion) \
213+
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::CriticalSection, doNotRecreateAfterDeletion, inline, get)
206214

207215
//==============================================================================
208216
/** This is a counterpart to the JUCE_DECLARE_SINGLETON macros.
209217
210218
After adding the JUCE_DECLARE_SINGLETON to the class definition, this macro has
211219
to be used in the cpp file.
220+
221+
This macro is not required for singletons declared with the INLINE macros, specifically
222+
JUCE_DECLARE_SINGLETON_INLINE, JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE, and
223+
JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE.
212224
*/
213225
#define JUCE_IMPLEMENT_SINGLETON(Classname) \
214-
\
215226
decltype (Classname::singletonHolder) Classname::singletonHolder;
216227

217-
218228
//==============================================================================
219229
/**
220230
Macro to declare member variables and methods for a singleton class.
@@ -236,15 +246,14 @@ struct SingletonHolder : private MutexType // (inherited so we can use the empt
236246
@see JUCE_IMPLEMENT_SINGLETON, JUCE_DECLARE_SINGLETON, JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL
237247
*/
238248
#define JUCE_DECLARE_SINGLETON_SINGLETHREADED(Classname, doNotRecreateAfterDeletion) \
239-
\
240-
static juce::SingletonHolder<Classname, juce::DummyCriticalSection, doNotRecreateAfterDeletion> singletonHolder; \
241-
friend decltype (singletonHolder); \
242-
\
243-
static Classname* JUCE_CALLTYPE getInstance() { return singletonHolder.get(); } \
244-
static Classname* JUCE_CALLTYPE getInstanceWithoutCreating() noexcept { return singletonHolder.instance; } \
245-
static void JUCE_CALLTYPE deleteInstance() noexcept { singletonHolder.deleteInstance(); } \
246-
void clearSingletonInstance() noexcept { singletonHolder.clear (this); }
249+
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, doNotRecreateAfterDeletion, , get)
247250

251+
/**
252+
The same as JUCE_DECLARE_SINGLETON_SINGLETHREADED, but does not require a matching
253+
JUCE_IMPLEMENT_SINGLETON definition.
254+
*/
255+
#define JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE(Classname, doNotRecreateAfterDeletion) \
256+
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, doNotRecreateAfterDeletion, inline, get)
248257

249258
//==============================================================================
250259
/**
@@ -262,15 +271,14 @@ struct SingletonHolder : private MutexType // (inherited so we can use the empt
262271
@see JUCE_IMPLEMENT_SINGLETON, JUCE_DECLARE_SINGLETON
263272
*/
264273
#define JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL(Classname) \
265-
\
266-
static juce::SingletonHolder<Classname, juce::DummyCriticalSection, false> singletonHolder; \
267-
friend decltype (singletonHolder); \
268-
\
269-
static Classname* JUCE_CALLTYPE getInstance() { return singletonHolder.getWithoutChecking(); } \
270-
static Classname* JUCE_CALLTYPE getInstanceWithoutCreating() noexcept { return singletonHolder.instance; } \
271-
static void JUCE_CALLTYPE deleteInstance() noexcept { singletonHolder.deleteInstance(); } \
272-
void clearSingletonInstance() noexcept { singletonHolder.clear (this); }
274+
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, false, , getWithoutChecking)
273275

276+
/**
277+
The same as JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL, but does not require a matching
278+
JUCE_IMPLEMENT_SINGLETON definition.
279+
*/
280+
#define JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE(Classname) \
281+
JUCE_PRIVATE_DECLARE_SINGLETON (Classname, juce::DummyCriticalSection, false, inline, getWithoutChecking)
274282

275283
//==============================================================================
276284
#ifndef DOXYGEN

modules/juce_events/interprocess/juce_ChildProcessManager.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,4 @@ void ChildProcessManager::checkProcesses()
5555
timer.stopTimer();
5656
}
5757

58-
JUCE_IMPLEMENT_SINGLETON (ChildProcessManager)
59-
6058
} // namespace juce

modules/juce_events/interprocess/juce_ChildProcessManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace juce
5757
{
5858
public:
5959
#ifndef DOXYGEN
60-
JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (ChildProcessManager)
60+
JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL_INLINE (ChildProcessManager)
6161
#endif
6262

6363
/** Creates a new ChildProcess and starts it with the provided arguments.

modules/juce_events/native/juce_Messaging_android.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace Android
6565
Handler() : nativeHandler (LocalRef<jobject> (getEnv()->NewObject (AndroidHandler, AndroidHandler.constructor))) {}
6666
~Handler() { clearSingletonInstance(); }
6767

68-
JUCE_DECLARE_SINGLETON (Handler, false)
68+
JUCE_DECLARE_SINGLETON_INLINE (Handler, false)
6969

7070
bool post (jobject runnable)
7171
{
@@ -74,14 +74,12 @@ namespace Android
7474

7575
GlobalRef nativeHandler;
7676
};
77-
78-
JUCE_IMPLEMENT_SINGLETON (Handler)
7977
}
8078

8179
//==============================================================================
8280
struct AndroidMessageQueue final : private Android::Runnable
8381
{
84-
JUCE_DECLARE_SINGLETON_SINGLETHREADED (AndroidMessageQueue, true)
82+
JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE (AndroidMessageQueue, true)
8583

8684
AndroidMessageQueue()
8785
: self (CreateJavaInterface (this, "java/lang/Runnable"))
@@ -124,8 +122,6 @@ struct AndroidMessageQueue final : private Android::Runnable
124122
Android::Handler handler;
125123
};
126124

127-
JUCE_IMPLEMENT_SINGLETON (AndroidMessageQueue)
128-
129125
//==============================================================================
130126
void MessageManager::doPlatformSpecificInitialisation() { AndroidMessageQueue::getInstance(); }
131127
void MessageManager::doPlatformSpecificShutdown() { AndroidMessageQueue::deleteInstance(); }

modules/juce_events/native/juce_Messaging_linux.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class InternalMessageQueue
8585
}
8686

8787
//==============================================================================
88-
JUCE_DECLARE_SINGLETON (InternalMessageQueue, false)
88+
JUCE_DECLARE_SINGLETON_INLINE (InternalMessageQueue, false)
8989

9090
private:
9191
CriticalSection lock;
@@ -115,8 +115,6 @@ class InternalMessageQueue
115115
}
116116
};
117117

118-
JUCE_IMPLEMENT_SINGLETON (InternalMessageQueue)
119-
120118
//==============================================================================
121119
/*
122120
Stores callbacks associated with file descriptors (FD).
@@ -230,7 +228,7 @@ struct InternalRunLoop
230228
void removeListener (LinuxEventLoopInternal::Listener& listener) { listeners.remove (&listener); }
231229

232230
//==============================================================================
233-
JUCE_DECLARE_SINGLETON (InternalRunLoop, false)
231+
JUCE_DECLARE_SINGLETON_INLINE (InternalRunLoop, false)
234232

235233
private:
236234
using SharedCallback = std::shared_ptr<std::function<void()>>;
@@ -282,8 +280,6 @@ struct InternalRunLoop
282280
ListenerList<LinuxEventLoopInternal::Listener> listeners;
283281
};
284282

285-
JUCE_IMPLEMENT_SINGLETON (InternalRunLoop)
286-
287283
//==============================================================================
288284
namespace LinuxErrorHandling
289285
{

0 commit comments

Comments
 (0)