aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/Singleton.h
diff options
context:
space:
mode:
authorJean-Francois Mauguit <jfmauguit@mac.com>2024-09-24 09:03:25 -0400
committerGitHub <noreply@github.com>2024-09-24 09:03:25 -0400
commitbab614c421ed7ae329d26bf028c4a3b1d2450f5a (patch)
tree12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/Winamp/Singleton.h
parent4bde6044fddf053f31795b9eaccdd2a5a527d21f (diff)
parent20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (diff)
downloadwinamp-bab614c421ed7ae329d26bf028c4a3b1d2450f5a.tar.gz
Merge pull request #5 from WinampDesktop/community
Merge to main
Diffstat (limited to 'Src/Winamp/Singleton.h')
-rw-r--r--Src/Winamp/Singleton.h138
1 files changed, 138 insertions, 0 deletions
diff --git a/Src/Winamp/Singleton.h b/Src/Winamp/Singleton.h
new file mode 100644
index 00000000..ff7cd560
--- /dev/null
+++ b/Src/Winamp/Singleton.h
@@ -0,0 +1,138 @@
+#ifndef __WASABI_SINGLETON_H
+#define __WASABI_SINGLETON_H
+
+#include "api/service/waservicefactoryi.h"
+#include "api/service/services.h"
+
+template <class api_t, class impl_t>
+class Singleton : public waServiceFactoryI
+{
+public:
+ Singleton() : impl( 0 ), orig( 0 ) {}
+
+ Singleton( impl_t *new_impl, bool existing ) : impl( 0 ), orig( 0 )
+ {
+ if ( existing )
+ Register( new_impl );
+ else
+ RegisterNew( new_impl );
+ }
+
+ Singleton( impl_t *&new_impl ) : impl( 0 ), orig( 0 ) { RegisterNew( new_impl ); }
+
+ ~Singleton() { Deregister(); }
+
+ void Register( impl_t *new_impl )
+ {
+ impl = new_impl;
+ WASABI_API_SVC->service_register( this );
+ }
+
+ void RegisterNew( impl_t *&new_impl )
+ {
+ if ( !new_impl )
+ {
+ orig = &new_impl;
+ new_impl = new impl_t;
+ }
+
+ impl = new_impl;
+ WASABI_API_SVC->service_register( this );
+ }
+
+ void Deregister()
+ {
+ if ( impl )
+ {
+ WASABI_API_SVC->service_deregister( this );
+ if ( orig )
+ {
+ delete impl;
+ *orig = 0;
+ }
+
+ impl = 0;
+ }
+ }
+
+ impl_t *impl;
+ impl_t **orig;
+
+ FOURCC svc_serviceType() { return WaSvc::UNIQUE; } // TODO: make this a (defaulted) template parameter
+ const char *svc_getServiceName() { return impl_t::getServiceName(); }
+ GUID svc_getGuid() { return impl_t::getServiceGuid(); } // GUID per service factory, can be INVALID_GUID
+ void *svc_getInterface( int global_lock = TRUE ) { return static_cast<api_t *>( impl ); }
+ int svc_supportNonLockingGetInterface() { return TRUE; }
+ int svc_releaseInterface( void *ifc ) { return 0; }
+ CfgItem *svc_getCfgInterface() { return NULL; }
+ const wchar_t *svc_getTestString() { return 0; }
+ int svc_notify( int msg, int param1 = 0, int param2 = 0 ) { return 0; }
+};
+
+
+template <class api_t, class impl_t>
+class Singleton2 : public waServiceFactoryI
+{
+public:
+ Singleton2() : impl( 0 ), orig( 0 ) {}
+
+ Singleton2( impl_t *new_impl, bool existing ) : impl( 0 ), orig( 0 )
+ {
+ if ( existing )
+ Register( new_impl );
+ else
+ RegisterNew( new_impl );
+ }
+
+ Singleton2( impl_t *&new_impl ) : impl( 0 ), orig( 0 ) { RegisterNew( new_impl ); }
+
+ ~Singleton2() { Deregister(); }
+
+ void Register( impl_t *new_impl )
+ {
+ impl = new_impl;
+ WASABI_API_SVC->service_register( this );
+ }
+
+ void RegisterNew( impl_t *&new_impl )
+ {
+ if ( !new_impl )
+ {
+ orig = &new_impl;
+ new_impl = new impl_t;
+ }
+
+ impl = new_impl;
+ WASABI_API_SVC->service_register( this );
+ }
+
+ void Deregister()
+ {
+ if ( impl )
+ {
+ WASABI_API_SVC->service_deregister( this );
+ if ( orig )
+ {
+ delete impl;
+ *orig = 0;
+ }
+
+ impl = 0;
+ }
+ }
+
+ impl_t *impl;
+ impl_t **orig;
+
+ FOURCC svc_serviceType() { return WaSvc::UNIQUE; } // TODO: make this a (defaulted) template parameter
+ const char *svc_getServiceName() { return impl_t::getServiceName(); }
+ GUID svc_getGuid() { return impl_t::getServiceGuid(); } // GUID per service factory, can be INVALID_GUID
+ void *svc_getInterface( int global_lock = TRUE ) { return static_cast<api_t *>( impl ); }
+ int svc_supportNonLockingGetInterface() { return TRUE; }
+ int svc_releaseInterface( void *ifc ) { return 0; }
+ CfgItem *svc_getCfgInterface() { return NULL; }
+ const wchar_t *svc_getTestString() { return 0; }
+ int svc_notify( int msg, int param1 = 0, int param2 = 0 ) { return 0; }
+};
+
+#endif \ No newline at end of file