• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
RegisterCodec(const KeyType & key)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     //!
CreateCodec(const KeyType & key,ArgType arg)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>
create(ArgType arg)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     //!
GetCreators()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>
RegisterCodec(const KeyType & key)155     static bool RegisterCodec(const KeyType &key)
156     {
157         std::pair<iterator, bool> result =
158             GetCreators().insert(std::make_pair(key, create<C>));
159 
160         return result.second;
161     }
162 
163     //!
164     //! \brief    Create a new object that is registered with key.
165     //! \details  Create and return one new object that is registered with key. And Args is passed to create
166     //!           the new object.
167     //!
168     //! \param    [in] key
169     //!           KeyType, the type alias of std::string.
170     //!
171     //! \return   The derived object of T is returned if key is found and the object is created.
172     //!           nullptr is returned if key is not found
173     //!
CreateCodec(const KeyType & key)174     static Type CreateCodec(const KeyType &key)
175     {
176         Creators &creators = GetCreators();
177         iterator  creator  = creators.find(key);
178         if (creator != creators.end())
179             return (creator->second)();
180 
181         return nullptr;
182     }
183 
184 private:
185     //!
186     //! \brief    The callback function with key.
187     //! \details  The member template to create the derived object
188     //!
189     //! \return   The created object.
190     //!
191     template <class C>
create()192     static Type create()
193     {
194         return MOS_New(C);
195     }
196 
197     //!
198     //! \brief    Obtain the static pair table of key and callback function
199     //! \details  Obtain the static pair table that is registerted with param@key and callback function
200     //!
201     //! \return   Return the static pair table about the param @key and callback function
202     //!
GetCreators()203     static Creators &GetCreators()
204     {
205         static Creators creators;
206 
207         return creators;
208     }
209 };
210 
211 #endif /*  _MEDIA_DDI_FACTORY_H_ */
212