| 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 = creators.emplace(key, Create<C>); |
| 72 | sizes.emplace(key, (uint32_t)sizeof(C)); |
| 73 | placecreators.emplace(key, PlaceCreate<C>); |
| 74 | return result.second; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | if (forceReplace) |
| 79 | { |
| 80 | creators.erase(creator); |
| 81 | std::pair<Iterator, bool> result = creators.emplace(key, Create<C>); |
| 82 | return result.second; |
| 83 | } |
| 84 | return true; //If it is registered, do nothing then return true. |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | //! |
| 89 | //! \brief create a new object that is registered with key. |
| 90 | //! \details create and return one new object that is registered with key. And Args is passed to create |
| 91 | //! the new object. |
| 92 | //! |
| 93 | //! \param [in] key |
| 94 | //! KeyType, the type alias of uint32_t. |
| 95 | //! |
| 96 | //! \return the derived object of T is returned if key is found and the object is created. |
| 97 | //! nullptr is returned if key is not found |
| 98 | //! |
| 99 | static Type Create( |
| 100 | KeyType key) |
| 101 | { |
| 102 | Creators &creators = GetCreators(); |
| 103 | Iterator creator = creators.find(key); |
| 104 | if (creator != creators.end()) |
| 105 | { |
| 106 | return (creator->second)(); |
| 107 | } |
| 108 | |
| 109 | return nullptr; |
| 110 | } |
| 111 | |
| 112 | static Type PlaceCreate( |
| 113 | KeyType key, void* privateData) |
| 114 | { |
| 115 | PlaceCreators &placecreators = GetPlaceCreators(); |
| 116 | Iterator_PlaceCreators placecreator = placecreators.find(key); |
| 117 | if (placecreator != placecreators.end()) |
| 118 | { |
| 119 | return (placecreator->second)(privateData); |
| 120 | }; |
| 121 | |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
| 125 | static uint32_t ReturnClassSize(KeyType key) |
| 126 | { |
| 127 | Sizes &sizes = GetSizes(); |
| 128 | Iterator_Sizes sizeit = sizes.find(key); |
| 129 | if (sizeit != sizes.end()) |
| 130 | { |
| 131 | return sizeit->second; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | //! |
| 137 | //! \brief check object that is registered with key. |
| 138 | //! \details check object that is registered with key. And Args is passed to check the object. |
| 139 | //! |
| 140 | //! \param [in] key |
| 141 | //! KeyType, the type alias of uint32_t. |
| 142 | //! |
| 143 | //! \return true if object exists, else not |
| 144 | //! |
| 145 | static bool IsRegistered( |
| 146 | KeyType key) |
| 147 | { |
| 148 | Creators &creators = GetCreators(); |
| 149 | return creators.find(key) != creators.end(); |
| 150 | } |
| 151 | private: |
| 152 | |
| 153 | //! |
| 154 | //! \brief the callback function with key. |
| 155 | //! \details The member template to create the derived object |
| 156 | //! |
| 157 | //! \param [in] arg |
| 158 | //! ArgType, the type alias of class template parameter. |
| 159 | //! |
| 160 | //! \return the created object with arg input for C constructor. |
| 161 | //! |
| 162 | template <class C> |
| 163 | static Type Create() |
| 164 | { |
| 165 | // The factory only provide the method to create but not keep the pointer to the memory. |
| 166 | // So the memory free is owned by who call the factory to create it. |
| 167 | return MOS_New(C); |
| 168 | } |
| 169 | |
| 170 | template <class C> |
| 171 | static Type PlaceCreate(void *privateData) |
| 172 | { |
| 173 | // The factory only provide the method to create but not keep the pointer to the memory. |
| 174 | // So the memory free is owned by who call the factory to create it. |
| 175 | return new (privateData)C; |
| 176 | } |
| 177 | |
| 178 | //! |
| 179 | //! \brief obtain the static pair table of param@ key and callback function |
| 180 | //! \details obtain the static pair table that is registerted with param@key and callback function |
| 181 | //! |
| 182 | //! \return return the static pair table about the param @key and callback function |
| 183 | //! |
| 184 | static Creators &GetCreators() |
| 185 | { |
| 186 | static Creators creators; |
| 187 | |
| 188 | return creators; |
| 189 | } |
| 190 | |
| 191 | static PlaceCreators &GetPlaceCreators() |
| 192 | { |
| 193 | static PlaceCreators placecreators; |
| 194 | |
| 195 | return placecreators; |
| 196 | } |
| 197 | |
| 198 | static Sizes &GetSizes() |
| 199 | { |
| 200 | static Sizes sizes; |
| 201 | |
| 202 | return sizes; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | MEDIA_CLASS_DEFINE_END(MediaFactory) |
| 207 | }; |
| 208 | |
| 209 | #define MEDIA_EXT_FLAG 0x10000000 |
| 210 | |
| 211 | #endif |