From 7585c5c358e447a36649b3d8099f65cd8af34f74 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 23 Sep 2007 13:31:33 +0200 Subject: [PATCH] WIP reorganize singleton factory --- src/backend/mediaaccessfacade.hpp | 10 ++++++- .../{singleton.hpp => singletonfactory.hpp} | 26 +++++++++---------- src/common/singletonpolicies.hpp | 20 +++++++++++++- 3 files changed, 41 insertions(+), 15 deletions(-) rename src/common/{singleton.hpp => singletonfactory.hpp} (87%) diff --git a/src/backend/mediaaccessfacade.hpp b/src/backend/mediaaccessfacade.hpp index be02109e6..24baeb89f 100644 --- a/src/backend/mediaaccessfacade.hpp +++ b/src/backend/mediaaccessfacade.hpp @@ -25,18 +25,26 @@ #define BACKEND_INTERFACE_MEDIAACCESSFACADE_H +#include "common/singleton.hpp" + + namespace backend_interface { /** + * Interface to the backend layer: * provides functions for querying (opening) a media file, * detecting the channels or streams found within this file etc. - * Delegating to the actual backend functions + * Implemention delegating to the actual backend functions. */ class MediaAccessFacade { + public: + static Singleton instance; + + }; diff --git a/src/common/singleton.hpp b/src/common/singletonfactory.hpp similarity index 87% rename from src/common/singleton.hpp rename to src/common/singletonfactory.hpp index 5c9018f78..2647a6f2c 100644 --- a/src/common/singleton.hpp +++ b/src/common/singletonfactory.hpp @@ -1,5 +1,5 @@ /* - SINGLETON.hpp - template for implementing the singleton pattern + SINGLETONFACTORY.hpp - template for implementing the singleton pattern Copyright (C) CinelerraCV 2007, Christian Thaeter @@ -31,16 +31,16 @@ This code is heavily inspired by -#ifndef CINELERRA_SINGLETON_H -#define CINELERRA_SINGLETON_H +#ifndef CINELERRA_SINGLETONFACTORY_H +#define CINELERRA_SINGLETONFACTORY_H -#include "common/singletonpolicies.hpp" ///< several Policies usable together with singleton +#include "common/singletonpolicies.hpp" ///< several Policies usable together with SingletonFactory #include "common/util.hpp" #include "nobugcfg.h" -#include +//#include namespace cinelerra @@ -58,7 +58,7 @@ namespace cinelerra template class Life = singleton::Automatic, // how to manage Singleton Lifecycle template class Threading = singleton::IgnoreThreadsafety //TODO use Multithreaded!!! > - class Singleton + class SingletonFactory { typedef typename Threading::VolatileType SType; typedef typename Threading::Lock ThreadLock; @@ -66,7 +66,7 @@ namespace cinelerra static bool isDead_; public: - /** Interface to be used by Singleton's clients. + /** Interface to be used by SingletonFactory's clients. * Manages internally the instance creation, lifecycle * and access handling in a multithreaded context. * @return "the" single instance of class S @@ -107,15 +107,15 @@ namespace cinelerra }; - // Storage for Singleton's static fields... + // Storage for SingletonFactory's static fields... template < class SI, template class C, template class L, template class T > - typename Singleton::SType* - Singleton::pInstance_; + typename SingletonFactory::SType* + SingletonFactory::pInstance_; template < class SI, @@ -123,7 +123,7 @@ namespace cinelerra template class L, template class T > - bool Singleton::isDead_; + bool SingletonFactory::isDead_; @@ -134,9 +134,9 @@ namespace cinelerra /** @internal used to link together the Create policy and Life policy. * @return a functor object for invoking this->destroy() */ -/* singleton::DelFunc getDeleter() +/* SingletonFactory::DelFunc getDeleter() { - return boost::bind (&Singleton::destroy, + return boost::bind (&SingletonFactory::destroy, this); } */ diff --git a/src/common/singletonpolicies.hpp b/src/common/singletonpolicies.hpp index a5d499c12..b1d91412c 100644 --- a/src/common/singletonpolicies.hpp +++ b/src/common/singletonpolicies.hpp @@ -55,7 +55,7 @@ namespace cinelerra static S* create () { #ifdef DEBUG - static uint callCount (0); + static uint callCount = 0; ASSERT ( 0 == callCount++ ); #endif static char buff[sizeof(S)]; @@ -77,6 +77,24 @@ namespace cinelerra static S* create () { return new S; } static void destroy (S* pS) { delete pS; } }; + + + /** + * Policy for creating dynamic Singleton instance, with + * additional facility to support Mock testing. When injecting + * a Mock instance (typically a subclass of the product), the + * main Singleton instance is temporarily shaddowed. + */ + template + struct DynamicMockTestable + { + static S* create () { return new S; } + static void destroy (S* pS) { delete pS; } + }; + void injectSubclass (SI* mock) + { + + }