1 /*========================== begin_copyright_notice ============================
2 
3 Copyright (C) 2017-2021 Intel Corporation
4 
5 SPDX-License-Identifier: MIT
6 
7 ============================= end_copyright_notice ===========================*/
8 
9 #define CIF_FORWARD_DECLARE(TYPE, STRUCT_NAME)                                \
10   template <CIF::Version_t ver> TYPE STRUCT_NAME
11 
12 #define CIF_FORWARD_DECLARE_S(NAME) CIF_FORWARD_DECLARE(struct, NAME)
13 #define CIF_FORWARD_DECLARE_C(NAME) CIF_FORWARD_DECLARE(class, NAME)
14 
15 #if defined CIF_ULT
16 #  define CIF_ULT_EXPOSE(FUNC_NAME)               \
17       template<typename ... ArgsT>                \
18       auto FUNC_NAME##_CIF_ULT(ArgsT && ... args) -> decltype(this->FUNC_NAME(std::forward<ArgsT>(args)...)) {\
19            return FUNC_NAME(std::forward<ArgsT>(args)...);   \
20       }
21 #else
22 #  define CIF_ULT_EXPOSE(FUNC_NAME)
23 #endif
24 
25 #define CIF_DECLARE_INTERFACE(NAME, ID)                            \
26     template <CIF::Version_t version = CIF::InvalidVersion>        \
27     struct NAME;                                                   \
28                                                                    \
29     template <>                                                    \
30     struct NAME<CIF::BaseVersion>                                  \
31         : public CIF::NamedCIF<CIF::InterfaceIdCoder::Enc(ID)>     \
32     {                                                              \
33         struct Impl;                                               \
34         virtual Impl * GetImpl(){ return pImpl; }                  \
35         virtual const Impl * GetImpl() const { return pImpl; }     \
36     protected:                                                     \
37         ~NAME()override;                                           \
38         Impl * pImpl;                                              \
39         template<typename ... ArgsT>                               \
40         NAME(ArgsT &&... args);                                    \
41         NAME(Impl * pImpl);                                        \
42     };                                                             \
43     using NAME##Base = NAME<CIF::BaseVersion>;
44 
45 // For common (i.e. shared) interfaces (such as builtins), access to pimpl
46 // is blocked, becasue we can't reliably check if given dynamic library
47 // knows the correct underlying version of interface's pimpl class
48 #define CIF_DECLARE_COMMON_INTERFACE(NAME, ID)                     \
49     template <CIF::Version_t version = CIF::InvalidVersion>        \
50     struct NAME;                                                   \
51                                                                    \
52     template <>                                                    \
53     struct NAME<CIF::BaseVersion>                                  \
54         : public CIF::NamedCIF<CIF::InterfaceIdCoder::Enc(ID)>     \
55     {                                                              \
56         struct Impl;                                               \
57     protected:                                                     \
58         virtual Impl * GetImpl(){ return pImpl; }                  \
59         virtual const Impl * GetImpl() const { return pImpl; }     \
60         ~NAME()override;                                           \
61         Impl * pImpl;                                              \
62         template<typename ... ArgsT>                               \
63         NAME(ArgsT &&... args);                                    \
64         NAME(Impl * pImpl);                                        \
65     };                                                             \
66     using NAME##Base = NAME<CIF::BaseVersion>;
67 
68 #define CIF_DEFINE_INTERFACE_VER(NAME, VER)                         \
69     template<>                                                      \
70     struct NAME<VER> : public CIF::VersionedCIF<NAME, VER>
71 
72 #define CIF_DEFINE_INTERFACE_VER_WITH_COMPATIBILITY(NAME, VER, COMPATIBILITY_VER)\
73     template<>                                                                   \
74     struct NAME<VER> : public CIF::VersionedCIF<NAME, VER, COMPATIBILITY_VER>
75 
76 #define CIF_INHERIT_CONSTRUCTOR() using VersionedCIF::VersionedCIF;
77 
78 #define CIF_DECLARE_INTERFACE_DEPENDENCIES(DEPENDEE_INTERFACE, ...)
79 
80 #define CIF_GENERATE_VERSIONS_LIST(INTERFACE)\
81     template<>\
82     struct INTERFACE<CIF::TraitsSpecialVersion> : public CIF::SupportedVersions<INTERFACE>, public CIF::UsedInterfaces<> {};
83 
84 #define CIF_GENERATE_VERSIONS_LIST_WITH_BASE(INTERFACE, BASE)\
85     template<>\
86     struct INTERFACE<CIF::TraitsSpecialVersion> : public CIF::SupportedVersions<INTERFACE, BASE>, public CIF::UsedInterfaces<> {};
87 
88 #define CIF_GENERATE_VERSIONS_LIST_AND_DECLARE_INTERFACE_DEPENDENCIES(INTERFACE, ...)\
89     template<>\
90     struct INTERFACE<CIF::TraitsSpecialVersion> : public CIF::SupportedVersions<INTERFACE>, public CIF::UsedInterfaces<__VA_ARGS__> {};
91 
92 #define CIF_GENERATE_VERSIONS_LIST_WITH_BASE_AND_DECLARE_INTERFACE_DEPENDENCIES(INTERFACE, BASE, ...)\
93     template<>\
94     struct INTERFACE<CIF::TraitsSpecialVersion> : public CIF::SupportedVersions<INTERFACE, BASE>, public CIF::UsedInterfaces<__VA_ARGS__> {};
95 
96 #define CIF_MARK_LATEST_VERSION(LATEST_INTERFACE_ALIAS, INTERFACE)\
97     using LATEST_INTERFACE_ALIAS = INTERFACE<INTERFACE<CIF::TraitsSpecialVersion>::GetLatestSupportedVersion()>;
98 
99 #define CIF_DECLARE_INTERFACE_PIMPL(NAME) struct NAME<CIF::BaseVersion>::Impl
100 #define CIF_TYPE_PIMPL(NAME) NAME<CIF::BaseVersion>::Impl
101 #define CIF_PIMPL_DECLARE_CONSTRUCTOR(...) Impl(__VA_ARGS__)
102 #define CIF_PIMPL_DECLARE_DESTRUCTOR() ~Impl()
103 #define CIF_GET_INTERFACE_CLASS(NAME, VER) NAME<VER>
104 #define CIF_GET_PIMPL() GetImpl()
105 #define CIF_EXPORT_ENTRY_POINTS_STATIC(...)                         \
106     extern CIF::CIFMain *CreateCIFMainImpl(){                       \
107         return new CIF::CIFMainImplStatic<__VA_ARGS__>;             \
108     }
109 #define CIF_DEFINE_INTERFACE_TO_PIMPL_FORWARDING_CTOR_DTOR(NAME) \
110     template<typename ... ArgsT> \
111     inline NAME<CIF::BaseVersion>::NAME(ArgsT &&... args)\
112                 : pImpl(new Impl(std::forward<ArgsT>(args)...)){ pImpl->Retain(this); }\
113     inline NAME<CIF::BaseVersion>::NAME(Impl *pImpl)\
114                 : pImpl(pImpl){ pImpl->Retain(this); }\
115     inline NAME<CIF::BaseVersion>::~NAME() { pImpl->Release(this); }
116 
117 #define CIF_PIMPL(T)\
118     T<CIF::BaseVersion>::Impl
119 #define CIF_PIMPL_TYPENAME(T)\
120      typename T<CIF::BaseVersion>::Impl
121 
122 #define CREATE_CIF_PIMPL(T, ...)\
123     new CIF_PIMPL_T(T)(__VA_ARGS__)
124 #if defined(_WIN32)
125     #define CIF_CALLING_CONV __cdecl
126 #else
127     #define CIF_CALLING_CONV
128 #endif
129