1 #ifndef _TCUFACTORYREGISTRY_HPP
2 #define _TCUFACTORYREGISTRY_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program Tester Core
5  * ----------------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Generic registry class for factories
24  *//*--------------------------------------------------------------------*/
25 
26 #include "tcuDefs.hpp"
27 
28 #include <string>
29 #include <vector>
30 
31 namespace tcu
32 {
33 
34 class AbstractFactory
35 {
36 public:
37 								AbstractFactory				(void);
38 	virtual						~AbstractFactory			(void);
39 
40 	virtual const char*			getName						(void) const = 0;
41 };
42 
43 class GenericFactoryRegistry
44 {
45 public:
46 								GenericFactoryRegistry		(void);
47 								~GenericFactoryRegistry		(void);
48 
size(void) const49 	size_t						size						(void) const	{ return m_factories.size();	}
empty(void) const50 	bool						empty						(void) const	{ return m_factories.empty();	}
51 
52 	void						registerFactory				(AbstractFactory* factory);
53 
54 	AbstractFactory*			getFactoryByName			(const std::string& name);
55 	const AbstractFactory*		getFactoryByName			(const std::string& name) const;
56 
57 	AbstractFactory*			getFactoryByIndex			(size_t index);
58 	const AbstractFactory*		getFactoryByIndex			(size_t index) const;
59 
60 private:
61 								GenericFactoryRegistry		(const GenericFactoryRegistry&);
62 	GenericFactoryRegistry&		operator=					(const GenericFactoryRegistry&);
63 
64 	std::vector<AbstractFactory*>							m_factories;
65 };
66 
67 class FactoryBase : public AbstractFactory
68 {
69 public:
70 								FactoryBase					(const std::string& name, const std::string& description);
71 								~FactoryBase				(void);
72 
73 	const char*					getName						(void) const;
74 	const char*					getDescription				(void) const;
75 
76 private:
77 	const std::string			m_name;
78 	const std::string			m_description;
79 };
80 
81 template<class Factory>
82 class FactoryRegistry
83 {
84 public:
FactoryRegistry(void)85 								FactoryRegistry		(void) {}
~FactoryRegistry(void)86 								~FactoryRegistry	(void) {}
87 
empty(void) const88 	bool						empty				(void) const		{ return m_registry.empty();	}
size(void) const89 	size_t						size				(void) const		{ return m_registry.size();		}
getFactoryCount(void) const90 	size_t						getFactoryCount		(void) const		{ return m_registry.size();		}
91 
registerFactory(Factory * factory)92 	void						registerFactory		(Factory* factory)	{ m_registry.registerFactory(factory);	}
93 
94 	Factory*					getFactoryByName	(const std::string& name);
95 	const Factory*				getFactoryByName	(const std::string& name) const;
96 
97 	Factory*					getFactoryByIndex	(size_t index);
98 	const Factory*				getFactoryByIndex	(size_t index) const;
99 
getDefaultFactory(void)100 	Factory*					getDefaultFactory	(void)				{ return getFactoryByIndex(0);	}
getDefaultFactory(void) const101 	const Factory*				getDefaultFactory	(void) const		{ return getFactoryByIndex(0);	}
102 
103 private:
104 	GenericFactoryRegistry		m_registry;
105 };
106 
107 template<class Factory>
getFactoryByName(const std::string & name)108 inline Factory* FactoryRegistry<Factory>::getFactoryByName (const std::string& name)
109 {
110 	return static_cast<Factory*>(m_registry.getFactoryByName(name));
111 }
112 
113 template<class Factory>
getFactoryByName(const std::string & name) const114 inline const Factory* FactoryRegistry<Factory>::getFactoryByName (const std::string& name) const
115 {
116 	return static_cast<const Factory*>(m_registry.getFactoryByName(name));
117 }
118 
119 template<class Factory>
getFactoryByIndex(size_t index)120 inline Factory* FactoryRegistry<Factory>::getFactoryByIndex (size_t index)
121 {
122 	return static_cast<Factory*>(m_registry.getFactoryByIndex(index));
123 }
124 
125 template<class Factory>
getFactoryByIndex(size_t index) const126 inline const Factory* FactoryRegistry<Factory>::getFactoryByIndex (size_t index) const
127 {
128 	return static_cast<const Factory*>(m_registry.getFactoryByIndex(index));
129 }
130 
131 } // tcu
132 
133 #endif // _TCUFACTORYREGISTRY_HPP
134