| 1 | /* |
| 2 | * Copyright (c) 2017, 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 | //! |
| 23 | //! \file media_ddi_factory.h |
| 24 | //! \brief Defines one template class to create the instance of DDI media encode/decoder. |
| 25 | //! |
| 26 | |
| 27 | #ifndef _MEDIA_DDI_FACTORY_H_ |
| 28 | #define _MEDIA_DDI_FACTORY_H_ |
| 29 | |
| 30 | #include <map> |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | #include <utility> |
| 34 | #include <cstdarg> |
| 35 | |
| 36 | //! |
| 37 | //! \class MediaDdiFactory |
| 38 | //! \brief Media ddi factory class |
| 39 | //! |
| 40 | template <class T, class Arg> |
| 41 | class MediaDdiFactory |
| 42 | { |
| 43 | public: |
| 44 | typedef T *Type; |
| 45 | typedef Arg *ArgType; |
| 46 | typedef Type (*Creator)(ArgType); |
| 47 | typedef std::string KeyType; |
| 48 | typedef std::map<KeyType, Creator> Creators; |
| 49 | typedef typename Creators::iterator iterator; |
| 50 | |
| 51 | //! |
| 52 | //! \brief Register one Class C with key. |
| 53 | //! \details Use the member template to register class C with key and C is the |
| 54 | //! derived class of base class T. |
| 55 | //! |
| 56 | //! \param [in] key |
| 57 | //! KeyType, the type alias of std::string. |
| 58 | //! |
| 59 | //! \return True is returned if class C is successfully registerted with key |
| 60 | //! False is returned if key is already registered and doesn't register |
| 61 | //! class C with key. |
| 62 | //! |
| 63 | template <class C> |
| 64 | static bool RegisterCodec(const KeyType &key) |
| 65 | { |
| 66 | std::pair<iterator, bool> result = |
| 67 | GetCreators().insert(std::make_pair(key, create<C>)); |
| 68 | |
| 69 | return result.second; |
| 70 | } |
| 71 | |
| 72 | //! |
| 73 | //! \brief Create a new object that is registered with key. |
| 74 | //! \details Create and return one new object that is registered with key. And Args is passed to create |
| 75 | //! the new object. |
| 76 | //! |
| 77 | //! \param [in] key |
| 78 | //! KeyType, the type alias of std::string. |
| 79 | //! |
| 80 | //! \param [in] arg |
| 81 | //! ArgType, the type alias of Arg template parameter |
| 82 | //! |
| 83 | //! \return The derived object of T is returned if key is found and the object is created. |
| 84 | //! nullptr is returned if key is not found |
| 85 | //! |
| 86 | static Type CreateCodec( |
| 87 | const KeyType &key, |
| 88 | ArgType arg) |
| 89 | { |
| 90 | Creators &creators = GetCreators(); |
| 91 | iterator creator = creators.find(key); |
| 92 | if (creator != creators.end()) |
| 93 | return (creator->second)(arg); |
| 94 | |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | //! |
| 100 | //! \brief The callback function with key. |
| 101 | //! \details The member template to create the derived object |
| 102 | //! |
| 103 | //! \param [in] arg |
| 104 | //! ArgType, the type alias of class template parameter. |
| 105 | //! |
| 106 | //! \return The created object with arg input for C constructor. |
| 107 | //! |
| 108 | template <class C> |
| 109 | static Type create(ArgType arg) |
| 110 | { |
| 111 | return MOS_New(C, arg); |
| 112 | } |
| 113 | |
| 114 | //! |
| 115 | //! \brief Obtain the static pair table of key and callback function |
| 116 | //! \details Obtain the static pair table that is registerted with key and callback function |
| 117 | //! |
| 118 | //! \return Return the static pair table about the param @key and callback function |
| 119 | //! |
| 120 | static Creators &GetCreators() |
| 121 | { |
| 122 | static Creators creators; |
| 123 | |
| 124 | return creators; |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | //! |
| 129 | //! \class MediaDdiFactoryNoArg |
| 130 | //! \brief Media ddi factory no argument class |
| 131 | //! |
| 132 | template <class T> |
| 133 | class MediaDdiFactoryNoArg |
| 134 | { |
| 135 | public: |
| 136 | typedef T *Type; |
| 137 | typedef Type (*Creator)(); |
| 138 | typedef std::string KeyType; |
| 139 | typedef std::map<KeyType, Creator> Creators; |
| 140 | typedef typename Creators::iterator iterator; |
| 141 | |
| 142 | //! |
| 143 | //! \brief Register one Class C with key. |
| 144 | //! \details Use the member template to register class C with key and C is the |
| 145 | //! derived class of base class T. |
| 146 | //! |
| 147 | //! \param [in] key |
| 148 | //! KeyType, the type alias of std::string. |
| 149 | //! |
| 150 | //! \return True is returned if class C is successfully registerted with key |
| 151 | //! False is returned if key is already registered and doesn't register |
| 152 | //! class C with key. |
| 153 | //! |
| 154 | template <class C> |
| 155 | static bool RegisterCodec(const KeyType &key) |
| 156 | { |
| 157 | std::pair<iterator, bool> result = GetCreators().emplace(key, create<C>); |
| 158 | |
| 159 | return result.second; |
| 160 | } |
| 161 | |
| 162 | //! |
| 163 | //! \brief Create a new object that is registered with key. |
| 164 | //! \details Create and return one new object that is registered with key. And Args is passed to create |
| 165 | //! the new object. |
| 166 | //! |
| 167 | //! \param [in] key |
| 168 | //! KeyType, the type alias of std::string. |
| 169 | //! |
| 170 | //! \return The derived object of T is returned if key is found and the object is created. |
| 171 | //! nullptr is returned if key is not found |
| 172 | //! |
| 173 | static Type CreateCodec(const KeyType &key) |
| 174 | { |
| 175 | Creators &creators = GetCreators(); |
| 176 | iterator creator = creators.find(key); |
| 177 | if (creator != creators.end()) |
| 178 | return (creator->second)(); |
| 179 | |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | //! |
| 185 | //! \brief The callback function with key. |
| 186 | //! \details The member template to create the derived object |
| 187 | //! |
| 188 | //! \return The created object. |
| 189 | //! |
| 190 | template <class C> |
| 191 | static Type create() |
| 192 | { |
| 193 | return MOS_New(C); |
| 194 | } |
| 195 | |
| 196 | //! |
| 197 | //! \brief Obtain the static pair table of key and callback function |
| 198 | //! \details Obtain the static pair table that is registerted with param@key and callback function |
| 199 | //! |
| 200 | //! \return Return the static pair table about the param @key and callback function |
| 201 | //! |
| 202 | static Creators &GetCreators() |
| 203 | { |
| 204 | static Creators creators; |
| 205 | |
| 206 | return creators; |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | #endif /* _MEDIA_DDI_FACTORY_H_ */ |