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 50 { 51 return m_factories.size(); 52 } empty(void) const53 bool empty(void) const 54 { 55 return m_factories.empty(); 56 } 57 58 void registerFactory(AbstractFactory *factory); 59 60 AbstractFactory *getFactoryByName(const std::string &name); 61 const AbstractFactory *getFactoryByName(const std::string &name) const; 62 63 AbstractFactory *getFactoryByIndex(size_t index); 64 const AbstractFactory *getFactoryByIndex(size_t index) const; 65 66 private: 67 GenericFactoryRegistry(const GenericFactoryRegistry &); 68 GenericFactoryRegistry &operator=(const GenericFactoryRegistry &); 69 70 std::vector<AbstractFactory *> m_factories; 71 }; 72 73 class FactoryBase : public AbstractFactory 74 { 75 public: 76 FactoryBase(const std::string &name, const std::string &description); 77 ~FactoryBase(void); 78 79 const char *getName(void) const; 80 const char *getDescription(void) const; 81 82 private: 83 const std::string m_name; 84 const std::string m_description; 85 }; 86 87 template <class Factory> 88 class FactoryRegistry 89 { 90 public: FactoryRegistry(void)91 FactoryRegistry(void) 92 { 93 } ~FactoryRegistry(void)94 ~FactoryRegistry(void) 95 { 96 } 97 empty(void) const98 bool empty(void) const 99 { 100 return m_registry.empty(); 101 } size(void) const102 size_t size(void) const 103 { 104 return m_registry.size(); 105 } getFactoryCount(void) const106 size_t getFactoryCount(void) const 107 { 108 return m_registry.size(); 109 } 110 registerFactory(Factory * factory)111 void registerFactory(Factory *factory) 112 { 113 m_registry.registerFactory(factory); 114 } 115 116 Factory *getFactoryByName(const std::string &name); 117 const Factory *getFactoryByName(const std::string &name) const; 118 119 Factory *getFactoryByIndex(size_t index); 120 const Factory *getFactoryByIndex(size_t index) const; 121 getDefaultFactory(void)122 Factory *getDefaultFactory(void) 123 { 124 return getFactoryByIndex(0); 125 } getDefaultFactory(void) const126 const Factory *getDefaultFactory(void) const 127 { 128 return getFactoryByIndex(0); 129 } 130 131 private: 132 GenericFactoryRegistry m_registry; 133 }; 134 135 template <class Factory> getFactoryByName(const std::string & name)136inline Factory *FactoryRegistry<Factory>::getFactoryByName(const std::string &name) 137 { 138 return static_cast<Factory *>(m_registry.getFactoryByName(name)); 139 } 140 141 template <class Factory> getFactoryByName(const std::string & name) const142inline const Factory *FactoryRegistry<Factory>::getFactoryByName(const std::string &name) const 143 { 144 return static_cast<const Factory *>(m_registry.getFactoryByName(name)); 145 } 146 147 template <class Factory> getFactoryByIndex(size_t index)148inline Factory *FactoryRegistry<Factory>::getFactoryByIndex(size_t index) 149 { 150 return static_cast<Factory *>(m_registry.getFactoryByIndex(index)); 151 } 152 153 template <class Factory> getFactoryByIndex(size_t index) const154inline const Factory *FactoryRegistry<Factory>::getFactoryByIndex(size_t index) const 155 { 156 return static_cast<const Factory *>(m_registry.getFactoryByIndex(index)); 157 } 158 159 } // namespace tcu 160 161 #endif // _TCUFACTORYREGISTRY_HPP 162