xref: /aosp_15_r20/external/intel-media-driver/media_driver/linux/common/ddi/media_libva_caps_factory.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
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_libva_caps_factory.h
24 //! \brief    Defines one template class to create the instance of DDI MediaLibvaCaps
25 //!
26 
27 #ifndef _MEDIA_LIBVA_CAPS_FACTORY_H_
28 #define _MEDIA_LIBVA_CAPS_FACTORY_H_
29 
30 #include <map>
31 #include <string>
32 #include <vector>
33 #include <utility>
34 #include <cstdarg>
35 
36 //!
37 //! \class  MediaLibvaCapsFactory
38 //! \brief  Media libva caps factory
39 //!
40 template <class T, class Arg>
41 class MediaLibvaCapsFactory
42 {
43 public:
44     typedef T *Type;
45     typedef Arg *ArgType;
46     typedef Type (*Creator)(ArgType);
47     typedef uint32_t 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>
RegisterCaps(KeyType key)64     static bool RegisterCaps(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     //!
CreateCaps(KeyType key,ArgType arg)86     static Type CreateCaps(
87         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 #endif /*  _MEDIA_DDI_FACTORY_H_ */
129