1 #ifndef DALI_ADDON_BINDER_H
2 #define DALI_ADDON_BINDER_H
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <dali/integration-api/addon-manager.h>
27 * Class automates binding an AddOn interface.
29 * This interface is meant to be used as a base
30 * for classes that use function binding macros.
34 * #include <dali/devel-api/common/addon-binder.h>
35 * struct AddOnImageLoader : public Dali::DevelAddOn::AddOnBinder
37 * // Constructor requires the AddOn name and optional version (0 - any version accepted)
38 * AddOnImageLoader( const char* addonname ) : Dali::DevelAddOn::AddOnBinder( addonname, 0 ) {}
40 * ~AddOnImageLoader() = default;
43 * // Using ADDON_BIND_FUNCTION() macro requires function name (resolved by the AddOn) and function
44 * // signature. It will generate member function with correct binding.
47 * ADDON_BIND_FUNCTION( LoadBitmap, bool(const Dali::ImageLoader::Input&, Dali::Devel::PixelBuffer&) );
50 * ADDON_BIND_FUNCTION( LoadHeader, bool(const Dali::ImageLoader::Input&, unsigned int&, unsigned int&) );
52 * // GetFormatExtension
53 * ADDON_BIND_FUNCTION( GetFormatExtension, const char*() );
55 * // GetFormatMagicNumber
56 * ADDON_BIND_FUNCTION( GetFormatMagicNumber, uint16_t() );
59 * ADDON_BIND_FUNCTION( GetBitmapProfile, Bitmap::Profile() );
68 * @brief Constructor. Opens an AddOn and creates interface
69 * @param[in] addonName Name of AddOn
70 * @param[in] version Version of AddOn
72 explicit AddOnBinder(const char* addonName, uint32_t version = 0u)
74 mAddOnManager = Dali::Integration::AddOnManager::Get();
77 mAddOnHandle = mAddOnManager->GetAddOn(addonName);
80 mAddOnManager->GetAddOnInfo(addonName, mAddOnInfo);
88 virtual ~AddOnBinder() = default;
91 * @brief Looks up and converts c-style void* function into pointer of type T
92 * @param[in] funcName name of the function
93 * @return Returns a new pointer
96 T* ConvertFunction(const std::string& funcName)
100 auto ptr = mAddOnManager->GetGlobalProc(mAddOnHandle, funcName.c_str());
101 return *reinterpret_cast<T**>(&ptr);
107 * @brief Returns a handle to the AddOn library
108 * @return Handle object
110 Dali::AddOnLibrary GetHandle()
116 * @brief Returns pointer to the global AddOn function.
117 * @param[in] name Name of the function
118 * @return Valid pointer or nullptr
120 void* GetGlobalProc(const char* name)
122 return mAddOnManager ? mAddOnManager->GetGlobalProc(mAddOnHandle, name) : nullptr;
126 * @brief Returns pointer to the instance AddOn function.
127 * @param[in] name Name of the function
128 * @return Valid pointer or nullptr
130 void* GetInstanceProc(const char* name)
132 return mAddOnManager ? mAddOnManager->GetInstanceProc(mAddOnHandle, name) : nullptr;
136 * @brief Tests whether the interface is valid
137 * @return True if valid, false otherwise
139 virtual bool IsValid()
141 return GetHandle() != nullptr;
145 * @brief Returns AddOn info structure
146 * @return AddOn info structure.
148 const AddOnInfo& GetAddOnInfo() const
154 static DALI_CORE_API Dali::Integration::AddOnManager* mAddOnManager; ///< Pointer to the AddOn manager
156 Dali::AddOnLibrary mAddOnHandle{nullptr}; ///< Handle to the AddOn library
157 Dali::AddOnInfo mAddOnInfo{}; ///< Stored AddOnInfo structure
161 * Macro binds function as a member function of the class, for example, the call:
163 * ADDON_BIND_FUNCTION( SomeAddOnFunction, void(int, char*) );
165 * will create a std::function object named SomeAddOnFunction and bound to the AddOn library.
167 #define ADDON_BIND_FUNCTION(FUNCNAME, FUNCTYPE) \
168 std::function<FUNCTYPE> FUNCNAME{ConvertFunction<FUNCTYPE>(std::string(#FUNCNAME))};
173 #endif // DALI_ADDON_BINDER_H