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>
28 * Class automates binding an AddOn interface.
30 * This interface is meant to be used as a base
31 * for classes that use function binding macros.
35 * #include <dali/devel-api/common/addon-binder.h>
36 * struct AddOnImageLoader : public Dali::DevelAddOn::AddOnBinder
38 * // Constructor requires the AddOn name and optional version (0 - any version accepted)
39 * AddOnImageLoader( const char* addonname ) : Dali::DevelAddOn::AddOnBinder( addonname, 0 ) {}
41 * ~AddOnImageLoader() = default;
44 * // Using ADDON_BIND_FUNCTION() macro requires function name (resolved by the AddOn) and function
45 * // signature. It will generate member function with correct binding.
48 * ADDON_BIND_FUNCTION( LoadBitmap, bool(const Dali::ImageLoader::Input&, Dali::Devel::PixelBuffer&) );
51 * ADDON_BIND_FUNCTION( LoadHeader, bool(const Dali::ImageLoader::Input&, unsigned int&, unsigned int&) );
53 * // GetFormatExtension
54 * ADDON_BIND_FUNCTION( GetFormatExtension, const char*() );
56 * // GetFormatMagicNumber
57 * ADDON_BIND_FUNCTION( GetFormatMagicNumber, uint16_t() );
60 * ADDON_BIND_FUNCTION( GetBitmapProfile, Bitmap::Profile() );
70 * @brief Constructor. Opens an AddOn and creates interface
71 * @param[in] addonName Name of AddOn
72 * @param[in] version Version of AddOn
74 explicit AddOnBinder( const char* addonName, uint32_t version = 0u )
76 mAddOnManager = Dali::Integration::AddOnManager::Get();
79 mAddOnHandle = mAddOnManager->GetAddOn(addonName);
82 mAddOnManager->GetAddOnInfo(addonName, mAddOnInfo);
90 virtual ~AddOnBinder() = default;
93 * @brief Looks up and converts c-style void* function into pointer of type T
94 * @param[in] funcName name of the function
95 * @return Returns a new pointer
98 T* ConvertFunction( const std::string& funcName )
102 auto ptr = mAddOnManager->GetGlobalProc(mAddOnHandle, funcName.c_str());
103 return *reinterpret_cast<T**>(&ptr);
109 * @brief Returns a handle to the AddOn library
110 * @return Handle object
112 Dali::AddOnLibrary GetHandle()
118 * @brief Returns pointer to the global AddOn function.
119 * @param[in] name Name of the function
120 * @return Valid pointer or nullptr
122 void* GetGlobalProc( const char* name )
124 return mAddOnManager ? mAddOnManager->GetGlobalProc( mAddOnHandle, name ) : nullptr;
128 * @brief Returns pointer to the instance AddOn function.
129 * @param[in] name Name of the function
130 * @return Valid pointer or nullptr
132 void* GetInstanceProc( const char* name )
134 return mAddOnManager ? mAddOnManager->GetInstanceProc( mAddOnHandle, name ) : nullptr;
138 * @brief Tests whether the interface is valid
139 * @return True if valid, false otherwise
141 virtual bool IsValid()
143 return GetHandle() != nullptr;
147 * @brief Returns AddOn info structure
148 * @return AddOn info structure.
150 const AddOnInfo& GetAddOnInfo() const
157 static DALI_CORE_API Dali::Integration::AddOnManager* mAddOnManager; ///< Pointer to the AddOn manager
159 Dali::AddOnLibrary mAddOnHandle { nullptr }; ///< Handle to the AddOn library
160 Dali::AddOnInfo mAddOnInfo {}; ///< Stored AddOnInfo structure
165 * Macro binds function as a member function of the class, for example, the call:
167 * ADDON_BIND_FUNCTION( SomeAddOnFunction, void(int, char*) );
169 * will create a std::function object named SomeAddOnFunction and bound to the AddOn library.
171 #define ADDON_BIND_FUNCTION( FUNCNAME, FUNCTYPE ) \
172 std::function<FUNCTYPE> FUNCNAME{ConvertFunction<FUNCTYPE>( std::string(#FUNCNAME) )};
177 #endif // DALI_ADDON_BINDER_H