1 /* 2 * Copyright (c) 2020-2022, Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included 12 * in all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 #ifndef __MEDIA_FACTORY_H__ 23 #define __MEDIA_FACTORY_H__ 24 25 #include "mos_utilities.h" 26 #include <map> 27 #include "media_class_trace.h" 28 29 //! 30 //! \class MediaFactory 31 //! \brief Media factory 32 //! 33 template <typename KeyType, class T> 34 class MediaFactory 35 { 36 public: 37 typedef T *Type; 38 typedef Type (*Creator)(); 39 typedef Type (*PlaceCreator)(void *); 40 typedef std::map<KeyType, Creator> Creators; 41 typedef std::map<KeyType, PlaceCreator> PlaceCreators; 42 typedef std::map<KeyType, uint32_t> Sizes; 43 typedef typename Creators::iterator Iterator; 44 typedef typename Sizes::iterator Iterator_Sizes; 45 typedef typename PlaceCreators::iterator Iterator_PlaceCreators; 46 47 //! 48 //! \brief register one Class C with key. 49 //! \details Use the member template to register class C with key and C is the 50 //! derived class of base class T. 51 //! 52 //! \param [in] key 53 //! KeyType, the type alias of uint32_t. 54 //! 55 //! \param [in] forceReplace 56 //! if force to replace the exsiting Creator, default is false. 57 //! 58 //! \return true is returned if class C is successfully registerted with key 59 //! false is returned if key is already registered and doesn't register 60 //! class C with key. 61 //! 62 template <class C> 63 static bool Register(KeyType key, bool forceReplace = false) 64 { 65 Creators &creators = GetCreators(); 66 Sizes &sizes = GetSizes(); 67 PlaceCreators &placecreators = GetPlaceCreators(); 68 Iterator creator = creators.find(key); 69 if (creator == creators.end()) 70 { 71 std::pair<Iterator, bool> result = 72 creators.insert(std::make_pair(key, Create<C>)); 73 sizes.insert(std::make_pair(key, (uint32_t)sizeof(C))); 74 placecreators.insert(std::make_pair(key, PlaceCreate<C>)); 75 return result.second; 76 } 77 else 78 { 79 if (forceReplace) 80 { 81 creators.erase(creator); 82 std::pair<Iterator, bool> result = 83 creators.insert(std::make_pair(key, Create<C>)); 84 return result.second; 85 } 86 return true; //If it is registered, do nothing then return true. 87 } 88 } 89 90 //! 91 //! \brief create a new object that is registered with key. 92 //! \details create and return one new object that is registered with key. And Args is passed to create 93 //! the new object. 94 //! 95 //! \param [in] key 96 //! KeyType, the type alias of uint32_t. 97 //! 98 //! \return the derived object of T is returned if key is found and the object is created. 99 //! nullptr is returned if key is not found 100 //! Create(KeyType key)101 static Type Create( 102 KeyType key) 103 { 104 Creators &creators = GetCreators(); 105 Iterator creator = creators.find(key); 106 if (creator != creators.end()) 107 { 108 return (creator->second)(); 109 } 110 111 return nullptr; 112 } 113 PlaceCreate(KeyType key,void * privateData)114 static Type PlaceCreate( 115 KeyType key, void* privateData) 116 { 117 PlaceCreators &placecreators = GetPlaceCreators(); 118 Iterator_PlaceCreators placecreator = placecreators.find(key); 119 if (placecreator != placecreators.end()) 120 { 121 return (placecreator->second)(privateData); 122 }; 123 124 return nullptr; 125 } 126 ReturnClassSize(KeyType key)127 static uint32_t ReturnClassSize(KeyType key) 128 { 129 Sizes &sizes = GetSizes(); 130 Iterator_Sizes sizeit = sizes.find(key); 131 if (sizeit != sizes.end()) 132 { 133 return sizeit->second; 134 } 135 136 return 0; 137 } 138 //! 139 //! \brief check object that is registered with key. 140 //! \details check object that is registered with key. And Args is passed to check the object. 141 //! 142 //! \param [in] key 143 //! KeyType, the type alias of uint32_t. 144 //! 145 //! \return true if object exists, else not 146 //! IsRegistered(KeyType key)147 static bool IsRegistered( 148 KeyType key) 149 { 150 Creators &creators = GetCreators(); 151 return creators.find(key) != creators.end(); 152 } 153 private: 154 155 //! 156 //! \brief the callback function with key. 157 //! \details The member template to create the derived object 158 //! 159 //! \param [in] arg 160 //! ArgType, the type alias of class template parameter. 161 //! 162 //! \return the created object with arg input for C constructor. 163 //! 164 template <class C> Create()165 static Type Create() 166 { 167 // The factory only provide the method to create but not keep the pointer to the memory. 168 // So the memory free is owned by who call the factory to create it. 169 return MOS_New(C); 170 } 171 172 template <class C> PlaceCreate(void * privateData)173 static Type PlaceCreate(void *privateData) 174 { 175 // The factory only provide the method to create but not keep the pointer to the memory. 176 // So the memory free is owned by who call the factory to create it. 177 return new (privateData)C; 178 } 179 180 //! 181 //! \brief obtain the static pair table of param@ key and callback function 182 //! \details obtain the static pair table that is registerted with param@key and callback function 183 //! 184 //! \return return the static pair table about the param @key and callback function 185 //! GetCreators()186 static Creators &GetCreators() 187 { 188 static Creators creators; 189 190 return creators; 191 } 192 GetPlaceCreators()193 static PlaceCreators &GetPlaceCreators() 194 { 195 static PlaceCreators placecreators; 196 197 return placecreators; 198 } 199 GetSizes()200 static Sizes &GetSizes() 201 { 202 static Sizes sizes; 203 204 return sizes; 205 } 206 207 208 MEDIA_CLASS_DEFINE_END(MediaFactory) 209 }; 210 211 #define MEDIA_EXT_FLAG 0x10000000 212 213 #endif 214