[Clam-devel] Refactoring the factory: new CLAM::Registrator class based

Pau Arumi parumi at iua.upf.edu
Thu Jun 7 10:55:27 PDT 2007


Commited on revision 10203

  * Factory refactoring step 2 of 3
  * Registrator now uses NewRegistry (based on objects not static 
methods)
  * TODO: remove the old Registry and interface duplication
  * Patch from Andreas (thanks)


little advice: let's use add [PATCH] in the mail subject to
know which mails contain patches, ok?




En/na Andreas Calvo ha escrit:
> In order to register plugins, which aren't statically defined since you 
> must explore a library and load a specific one, a small refactoring of 
> the factory was needed.
> First step was to create a complete separate Factory (called 
> LadspaFactory), which stores LadspaWrapperCreator objects and returns a 
> new LadspaWrapper object.
> With this patch, a new CLAM::Registrator has been added, supporting 
> class objects.
> So now, every call to the registrator actually registers it twice, one 
> with the old and the new one.
> With the new one, notice that a call for Create of the class.
> 
> 
> ------------------------------------------------------------------------
> 
> Index: CLAM/test/UnitTests/FactoryTest/FactoryRegistratorTest.cxx
> ===================================================================
> --- CLAM/test/UnitTests/FactoryTest/FactoryRegistratorTest.cxx	(revision 10200)
> +++ CLAM/test/UnitTests/FactoryTest/FactoryRegistratorTest.cxx	(working copy)
> @@ -20,10 +20,15 @@
>  {
>  	CPPUNIT_TEST_SUITE( FactoryRegistratorTest );
>  	CPPUNIT_TEST( testCreate_ReturnsAnObjectOfTheTemplateType );
> +	CPPUNIT_TEST( NewtestCreate_ReturnsAnObjectOfTheTemplateType );
> +	CPPUNIT_TEST( testConstructorPassingFactory_RegistersCreator );
> +	CPPUNIT_TEST( NewtestConstructorPassingFactory_RegistersCreator );
>  	CPPUNIT_TEST( testConstructorPassingKeyAndFactory_RegistersCreator );
> -	CPPUNIT_TEST( testConstructorPassingFactory_RegistersCreator );
> +	CPPUNIT_TEST( NewtestConstructorPassingKeyAndFactory_RegistersCreator );
>  	CPPUNIT_TEST( testConstructorPassingKey_RegistersCreator );
> +	CPPUNIT_TEST( NewtestConstructorPassingKey_RegistersCreator );
>  	CPPUNIT_TEST( testDefaultConstructor_RegistersCreator );
> +	CPPUNIT_TEST( NewtestDefaultConstructor_RegistersCreator );
>  	CPPUNIT_TEST( testRegistratorsAsStaticObjects_FactoryUnicity );
>  	CPPUNIT_TEST( testRegistratorsAsStaticObjects_ProductsRegistered );
>  	CPPUNIT_TEST_SUITE_END();
> @@ -40,7 +45,15 @@
>  		CLAMTEST_ASSERT_EQUAL_RTTYPES( DummyProductFoo, *created );
>  		delete created;
>  	}
> +	void NewtestCreate_ReturnsAnObjectOfTheTemplateType()
> +	{
> +		 DummyProduct* created =
> +			 MyFactoryType::Registrator<DummyProductFoo>::Create();
>  
> +		CLAMTEST_ASSERT_EQUAL_RTTYPES( DummyProductFoo, *created );
> +		delete created;
> +	}
> +
>  	void testConstructorPassingKeyAndFactory_RegistersCreator()
>  	{
>  		MyFactoryType fact;
> @@ -54,7 +67,20 @@
>  		CLAMTEST_ASSERT_EQUAL_RTTYPES( DummyProductFoo, *created );
>  		delete created;
>  	}
> +	void NewtestConstructorPassingKeyAndFactory_RegistersCreator()
> +	{
> +		MyFactoryType fact;
> +		const char* fooClassName = "DummyProductFoo";
>  
> +		MyFactoryType::Registrator<DummyProductFoo> regt( fooClassName, fact );
> +
> +		DummyProduct *created =
> +			fact.NewCreate( fooClassName );
> +
> +		CLAMTEST_ASSERT_EQUAL_RTTYPES( DummyProductFoo, *created );
> +		delete created;
> +	}
> +
>  	void testConstructorPassingFactory_RegistersCreator()
>  	{
>  		DummyProductFoo dummy; //needed for calling GetClassName()
> @@ -71,7 +97,23 @@
>  		CLAMTEST_ASSERT_EQUAL_RTTYPES( DummyProductFoo, *created );
>  		delete created;
>  	}
> +	void NewtestConstructorPassingFactory_RegistersCreator()
> +	{
> +		DummyProductFoo dummy; //needed for calling GetClassName()
> +		// here we use the name just for creating. Not for registrating
> +		const char* fooClassName = dummy.GetClassName();
>  
> +		MyFactoryType fact;
> +		// passing just the factory to the constructor
> +		MyFactoryType::Registrator<DummyProductFoo> regt( fact );
> +
> +		DummyProduct *created =
> +			fact.NewCreate( fooClassName );
> +
> +		CLAMTEST_ASSERT_EQUAL_RTTYPES( DummyProductFoo, *created );
> +		delete created;
> +	}
> +
>  	void testConstructorPassingKey_RegistersCreator()
>  	{
>  		MyFactoryType &theFactory = MyFactoryType::GetInstance();
> @@ -90,7 +132,25 @@
>  		theFactory.Clear();
>  
>  	}
> +	void NewtestConstructorPassingKey_RegistersCreator()
> +	{
> +		MyFactoryType &theFactory = MyFactoryType::GetInstance();
> +		theFactory.Clear();
>  
> +		DummyProductFoo dummy;
> +		MyFactoryType::Registrator<DummyProductFoo> regt( dummy.GetClassName() );
> +
> +		DummyProduct *created =
> +			theFactory.NewCreate( dummy.GetClassName() );
> +
> +		CLAMTEST_ASSERT_EQUAL_RTTYPES( DummyProductFoo, *created );
> +
> +		// tear down
> +		delete created;
> +		theFactory.Clear();
> +
> +	}
> +
>  	void testDefaultConstructor_RegistersCreator()
>  	{
>  		MyFactoryType &theFactory = MyFactoryType::GetInstance();
> @@ -108,7 +168,24 @@
>  		delete created;
>  		theFactory.Clear();
>  	}
> +	void NewtestDefaultConstructor_RegistersCreator()
> +	{
> +		MyFactoryType &theFactory = MyFactoryType::GetInstance();
> +		theFactory.Clear();
>  
> +		MyFactoryType::Registrator<DummyProductFoo> DummyRegt;
> +
> +		DummyProductFoo dummy;
> +		DummyProduct *created =
> +			theFactory.NewCreate( dummy.GetClassName() );
> +
> +		CLAMTEST_ASSERT_EQUAL_RTTYPES( DummyProductFoo, *created );
> +
> +		// tear down
> +		delete created;
> +		theFactory.Clear();
> +	}
> +
>  	void testRegistratorsAsStaticObjects_ProductsRegistered()
>  	{
>  		FactoryOfAs& theFactory = FactoryOfAs::GetInstance();
> Index: CLAM/src/Base/Factory.hxx
> ===================================================================
> --- CLAM/src/Base/Factory.hxx	(revision 10200)
> +++ CLAM/src/Base/Factory.hxx	(working copy)
> @@ -92,7 +92,11 @@
>  	}
>  
>  
> -	void Clear() { _registry.RemoveAllCreators(); }
> +	void Clear()
> +	{ 
> +		_registry.RemoveAllCreators();
> +		_newregistry.RemoveAllCreators();
> +	}
>  
>  
>  	void AddCreator(const RegistryKey name, CreatorMethod creator)
> @@ -396,6 +400,7 @@
>  			mKey=key;
>  //			std::cout << CLAM_MODULE << "Registrator(key,factory) " << mKey << std::endl;
>  			fact.AddCreatorWarningRepetitions( mKey, Create );
> +			fact.NewAddCreatorWarningRepetitions( mKey, new ConcreteCreator() );
>  		}
>  
>  		Registrator( TheFactoryType& fact ) {
> @@ -403,12 +408,14 @@
>  			mKey=dummy.GetClassName();
>  //			std::cout << CLAM_MODULE << "Registrator(factory) " << dummy.GetClassName() << std::endl;
>  			fact.AddCreatorWarningRepetitions( dummy.GetClassName(), Create );
> +			fact.NewAddCreatorWarningRepetitions( dummy.GetClassName(), new ConcreteCreator() );
>  		}
>  
>  		Registrator( RegistryKey key ) {
>  			mKey=key;
>  //			std::cout << CLAM_MODULE << "Registrator(key) " << mKey << std::endl;
>  			TheFactoryType::GetInstance().AddCreatorWarningRepetitions( mKey, Create );
> +			TheFactoryType::GetInstance().NewAddCreatorWarningRepetitions( mKey, new ConcreteCreator() );
>  		}
>  
>  		Registrator( ) {
> @@ -416,6 +423,7 @@
>  			mKey=dummy.GetClassName();
>  //			std::cout << CLAM_MODULE << "Registrator() " << mKey << std::endl;
>  			TheFactoryType::GetInstance().AddCreatorWarningRepetitions( mKey, Create );
> +			TheFactoryType::GetInstance().NewAddCreatorWarningRepetitions( mKey, new ConcreteCreator() );
>  		}
>  		~Registrator() {
>  //			std::cout << CLAM_MODULE << "~Registrator() " << mKey << std::endl;
> @@ -424,7 +432,6 @@
>  		static AbstractProduct* Create() {
>  			return new ConcreteProductType;
>  		}
> -
>  		class ConcreteCreator : public Creator
>  		{
>  		public:
> @@ -439,7 +446,6 @@
>  		RegistryKey mKey;
>  
>  	};
> -
>  	int Count() { return _registry.Count(); }
>  
>  private:
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Clam-devel mailing list
> Clam-devel at llistes.projectes.lafarga.org
> https://llistes.projectes.lafarga.org/cgi-bin/mailman/listinfo/clam-devel





More information about the clam-devel mailing list