add Callback API in ExporterProperties

pull/3280/head
Yingying Wang 2020-06-09 11:20:48 -07:00
parent ce838dfafb
commit 578a7ac502
2 changed files with 87 additions and 64 deletions

View File

@ -580,10 +580,24 @@ ExportProperties::ExportProperties(const ExportProperties &other)
: mIntProperties(other.mIntProperties) : mIntProperties(other.mIntProperties)
, mFloatProperties(other.mFloatProperties) , mFloatProperties(other.mFloatProperties)
, mStringProperties(other.mStringProperties) , mStringProperties(other.mStringProperties)
, mMatrixProperties(other.mMatrixProperties) { , mMatrixProperties(other.mMatrixProperties)
//wangyi 0608
, mCallbackProperties(other.mCallbackProperties){
// empty // empty
} }
//wangyi 0608
bool ExportProperties::SetPropertyCallback(const char *szName, std::function<void *(void *)> &f) {
return SetGenericProperty<std::function<void *(void *)>>(mCallbackProperties, szName, f);
}
std::function<void *(void *)> ExportProperties::GetPropertyCallback(const char *szName) const {
return GetGenericProperty<std::function<void *(void *)>>(mCallbackProperties, szName, 0);
}
//wangyi 0608
bool ExportProperties::HasPropertyCallback(const char *szName) const {
return HasGenericProperty<std::function<void *(void *)>>(mCallbackProperties, szName);
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Set a configuration property // Set a configuration property
bool ExportProperties::SetPropertyInteger(const char* szName, int iValue) { bool ExportProperties::SetPropertyInteger(const char* szName, int iValue) {

View File

@ -47,16 +47,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define AI_EXPORT_HPP_INC #define AI_EXPORT_HPP_INC
#ifdef __GNUC__ #ifdef __GNUC__
# pragma GCC system_header #pragma GCC system_header
#endif #endif
#ifndef ASSIMP_BUILD_NO_EXPORT #ifndef ASSIMP_BUILD_NO_EXPORT
#include "cexport.h" #include "cexport.h"
#include <map> #include <map>
//wangyi 0608
#include <functional>
namespace Assimp { namespace Assimp {
class ExporterPimpl; class ExporterPimpl;
class IOSystem; class IOSystem;
class ProgressHandler; class ProgressHandler;
@ -84,7 +86,7 @@ class ASSIMP_API ExportProperties;
class ASSIMP_API Exporter { class ASSIMP_API Exporter {
public: public:
/** Function pointer type of a Export worker function */ /** Function pointer type of a Export worker function */
typedef void (*fpExportFunc)(const char*, IOSystem*, const aiScene*, const ExportProperties*); typedef void (*fpExportFunc)(const char *, IOSystem *, const aiScene *, const ExportProperties *);
/** Internal description of an Assimp export format option */ /** Internal description of an Assimp export format option */
struct ExportFormatEntry { struct ExportFormatEntry {
@ -98,8 +100,7 @@ public:
unsigned int mEnforcePP; unsigned int mEnforcePP;
// Constructor to fill all entries // Constructor to fill all entries
ExportFormatEntry( const char* pId, const char* pDesc, const char* pExtension, fpExportFunc pFunction, unsigned int pEnforcePP = 0u) ExportFormatEntry(const char *pId, const char *pDesc, const char *pExtension, fpExportFunc pFunction, unsigned int pEnforcePP = 0u) {
{
mDescription.id = pId; mDescription.id = pId;
mDescription.description = pDesc; mDescription.description = pDesc;
mDescription.fileExtension = pExtension; mDescription.fileExtension = pExtension;
@ -108,9 +109,8 @@ public:
} }
ExportFormatEntry() : ExportFormatEntry() :
mExportFunction() mExportFunction(),
, mEnforcePP() mEnforcePP() {
{
mDescription.id = NULL; mDescription.id = NULL;
mDescription.description = NULL; mDescription.description = NULL;
mDescription.fileExtension = NULL; mDescription.fileExtension = NULL;
@ -142,7 +142,7 @@ public:
* *
* @param pIOHandler The IO handler to be used in all file accesses * @param pIOHandler The IO handler to be used in all file accesses
* of the Importer. */ * of the Importer. */
void SetIOHandler( IOSystem* pIOHandler); void SetIOHandler(IOSystem *pIOHandler);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Retrieves the IO handler that is currently set. /** Retrieves the IO handler that is currently set.
@ -151,7 +151,7 @@ public:
* handler is active as long the application doesn't supply its own * handler is active as long the application doesn't supply its own
* custom IO handler via #SetIOHandler(). * custom IO handler via #SetIOHandler().
* @return A valid IOSystem interface, never NULL. */ * @return A valid IOSystem interface, never NULL. */
IOSystem* GetIOHandler() const; IOSystem *GetIOHandler() const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Checks whether a default IO handler is active /** Checks whether a default IO handler is active
@ -171,7 +171,7 @@ public:
* disable progress reporting. * disable progress reporting.
* @note Progress handlers can be used to abort the loading * @note Progress handlers can be used to abort the loading
* at almost any time.*/ * at almost any time.*/
void SetProgressHandler(ProgressHandler* pHandler); void SetProgressHandler(ProgressHandler *pHandler);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Exports the given scene to a chosen file format. Returns the exported /** Exports the given scene to a chosen file format. Returns the exported
@ -191,10 +191,10 @@ public:
* Any IO handlers set via #SetIOHandler are ignored here. * Any IO handlers set via #SetIOHandler are ignored here.
* @note Use aiCopyScene() to get a modifiable copy of a previously * @note Use aiCopyScene() to get a modifiable copy of a previously
* imported scene. */ * imported scene. */
const aiExportDataBlob* ExportToBlob(const aiScene* pScene, const char* pFormatId, const aiExportDataBlob *ExportToBlob(const aiScene *pScene, const char *pFormatId,
unsigned int pPreprocessing = 0u, const ExportProperties* pProperties = nullptr); unsigned int pPreprocessing = 0u, const ExportProperties *pProperties = nullptr);
const aiExportDataBlob* ExportToBlob( const aiScene* pScene, const std::string& pFormatId, const aiExportDataBlob *ExportToBlob(const aiScene *pScene, const std::string &pFormatId,
unsigned int pPreprocessing = 0u, const ExportProperties* pProperties = nullptr); unsigned int pPreprocessing = 0u, const ExportProperties *pProperties = nullptr);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Convenience function to export directly to a file. Use /** Convenience function to export directly to a file. Use
@ -229,10 +229,10 @@ public:
* @return AI_SUCCESS if everything was fine. * @return AI_SUCCESS if everything was fine.
* @note Use aiCopyScene() to get a modifiable copy of a previously * @note Use aiCopyScene() to get a modifiable copy of a previously
* imported scene.*/ * imported scene.*/
aiReturn Export( const aiScene* pScene, const char* pFormatId, const char* pPath, aiReturn Export(const aiScene *pScene, const char *pFormatId, const char *pPath,
unsigned int pPreprocessing = 0u, const ExportProperties* pProperties = nullptr); unsigned int pPreprocessing = 0u, const ExportProperties *pProperties = nullptr);
aiReturn Export( const aiScene* pScene, const std::string& pFormatId, const std::string& pPath, aiReturn Export(const aiScene *pScene, const std::string &pFormatId, const std::string &pPath,
unsigned int pPreprocessing = 0u, const ExportProperties* pProperties = nullptr); unsigned int pPreprocessing = 0u, const ExportProperties *pProperties = nullptr);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Returns an error description of an error that occurred in #Export /** Returns an error description of an error that occurred in #Export
@ -244,17 +244,17 @@ public:
* *
* @note The returned function remains valid until one of the * @note The returned function remains valid until one of the
* following methods is called: #Export, #ExportToBlob, #FreeBlob */ * following methods is called: #Export, #ExportToBlob, #FreeBlob */
const char* GetErrorString() const; const char *GetErrorString() const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Return the blob obtained from the last call to #ExportToBlob */ /** Return the blob obtained from the last call to #ExportToBlob */
const aiExportDataBlob* GetBlob() const; const aiExportDataBlob *GetBlob() const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Orphan the blob from the last call to #ExportToBlob. This means /** Orphan the blob from the last call to #ExportToBlob. This means
* the caller takes ownership and is thus responsible for calling * the caller takes ownership and is thus responsible for calling
* the C API function #aiReleaseExportBlob to release it. */ * the C API function #aiReleaseExportBlob to release it. */
const aiExportDataBlob* GetOrphanedBlob() const; const aiExportDataBlob *GetOrphanedBlob() const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Frees the current blob. /** Frees the current blob.
@ -264,7 +264,7 @@ public:
* automatically by the destructor. The only reason to call * automatically by the destructor. The only reason to call
* it manually would be to reclaim as much storage as possible * it manually would be to reclaim as much storage as possible
* without giving up the #Exporter instance yet. */ * without giving up the #Exporter instance yet. */
void FreeBlob( ); void FreeBlob();
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Returns the number of export file formats available in the current /** Returns the number of export file formats available in the current
@ -290,7 +290,7 @@ public:
* for. Valid range is 0 to #Exporter::GetExportFormatCount * for. Valid range is 0 to #Exporter::GetExportFormatCount
* @return A description of that specific export format. * @return A description of that specific export format.
* NULL if pIndex is out of range. */ * NULL if pIndex is out of range. */
const aiExportFormatDesc* GetExportFormatDescription( size_t pIndex ) const; const aiExportFormatDesc *GetExportFormatDescription(size_t pIndex) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Register a custom exporter. Custom export formats are limited to /** Register a custom exporter. Custom export formats are limited to
@ -303,7 +303,7 @@ public:
* registered. A common cause that would prevent an exporter * registered. A common cause that would prevent an exporter
* from being registered is that its format id is already * from being registered is that its format id is already
* occupied by another format. */ * occupied by another format. */
aiReturn RegisterExporter(const ExportFormatEntry& desc); aiReturn RegisterExporter(const ExportFormatEntry &desc);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Remove an export format previously registered with #RegisterExporter /** Remove an export format previously registered with #RegisterExporter
@ -314,11 +314,11 @@ public:
* 'id' field of #aiExportFormatDesc. * 'id' field of #aiExportFormatDesc.
* @note Calling this method on a format description not yet registered * @note Calling this method on a format description not yet registered
* has no effect.*/ * has no effect.*/
void UnregisterExporter(const char* id); void UnregisterExporter(const char *id);
protected: protected:
// Just because we don't want you to know how we're hacking around. // Just because we don't want you to know how we're hacking around.
ExporterPimpl* pimpl; ExporterPimpl *pimpl;
}; };
class ASSIMP_API ExportProperties { class ASSIMP_API ExportProperties {
@ -332,6 +332,8 @@ public:
typedef std::map<KeyType, ai_real> FloatPropertyMap; typedef std::map<KeyType, ai_real> FloatPropertyMap;
typedef std::map<KeyType, std::string> StringPropertyMap; typedef std::map<KeyType, std::string> StringPropertyMap;
typedef std::map<KeyType, aiMatrix4x4> MatrixPropertyMap; typedef std::map<KeyType, aiMatrix4x4> MatrixPropertyMap;
//wangyi 0608
typedef std::map<KeyType, std::function<void *(void *)>> CallbackPropertyMap;
public: public:
/** Standard constructor /** Standard constructor
@ -345,7 +347,7 @@ public:
* This copies the configuration properties of another ExportProperties. * This copies the configuration properties of another ExportProperties.
* @see ExportProperties(const ExportProperties& other) * @see ExportProperties(const ExportProperties& other)
*/ */
ExportProperties(const ExportProperties& other); ExportProperties(const ExportProperties &other);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Set an integer configuration property. /** Set an integer configuration property.
@ -360,7 +362,7 @@ public:
* floating-point property has no effect - the loader will call * floating-point property has no effect - the loader will call
* GetPropertyFloat() to read the property, but it won't be there. * GetPropertyFloat() to read the property, but it won't be there.
*/ */
bool SetPropertyInteger(const char* szName, int iValue); bool SetPropertyInteger(const char *szName, int iValue);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Set a boolean configuration property. Boolean properties /** Set a boolean configuration property. Boolean properties
@ -369,27 +371,30 @@ public:
* #GetPropertyBool and vice versa. * #GetPropertyBool and vice versa.
* @see SetPropertyInteger() * @see SetPropertyInteger()
*/ */
bool SetPropertyBool(const char* szName, bool value) { bool SetPropertyBool(const char *szName, bool value) {
return SetPropertyInteger(szName,value); return SetPropertyInteger(szName, value);
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Set a floating-point configuration property. /** Set a floating-point configuration property.
* @see SetPropertyInteger() * @see SetPropertyInteger()
*/ */
bool SetPropertyFloat(const char* szName, ai_real fValue); bool SetPropertyFloat(const char *szName, ai_real fValue);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Set a string configuration property. /** Set a string configuration property.
* @see SetPropertyInteger() * @see SetPropertyInteger()
*/ */
bool SetPropertyString(const char* szName, const std::string& sValue); bool SetPropertyString(const char *szName, const std::string &sValue);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Set a matrix configuration property. /** Set a matrix configuration property.
* @see SetPropertyInteger() * @see SetPropertyInteger()
*/ */
bool SetPropertyMatrix(const char* szName, const aiMatrix4x4& sValue); bool SetPropertyMatrix(const char *szName, const aiMatrix4x4 &sValue);
//wangyi 0608
bool SetPropertyCallback(const char *szName, std::function<void *(void *)> &f);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Get a configuration property. /** Get a configuration property.
@ -404,8 +409,8 @@ public:
* floating-point property has no effect - the loader will call * floating-point property has no effect - the loader will call
* GetPropertyFloat() to read the property, but it won't be there. * GetPropertyFloat() to read the property, but it won't be there.
*/ */
int GetPropertyInteger(const char* szName, int GetPropertyInteger(const char *szName,
int iErrorReturn = 0xffffffff) const; int iErrorReturn = 0xffffffff) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Get a boolean configuration property. Boolean properties /** Get a boolean configuration property. Boolean properties
@ -414,16 +419,16 @@ public:
* #GetPropertyBool and vice versa. * #GetPropertyBool and vice versa.
* @see GetPropertyInteger() * @see GetPropertyInteger()
*/ */
bool GetPropertyBool(const char* szName, bool bErrorReturn = false) const { bool GetPropertyBool(const char *szName, bool bErrorReturn = false) const {
return GetPropertyInteger(szName,bErrorReturn)!=0; return GetPropertyInteger(szName, bErrorReturn) != 0;
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Get a floating-point configuration property /** Get a floating-point configuration property
* @see GetPropertyInteger() * @see GetPropertyInteger()
*/ */
ai_real GetPropertyFloat(const char* szName, ai_real GetPropertyFloat(const char *szName,
ai_real fErrorReturn = 10e10f) const; ai_real fErrorReturn = 10e10f) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Get a string configuration property /** Get a string configuration property
@ -431,8 +436,8 @@ public:
* The return value remains valid until the property is modified. * The return value remains valid until the property is modified.
* @see GetPropertyInteger() * @see GetPropertyInteger()
*/ */
const std::string GetPropertyString(const char* szName, const std::string GetPropertyString(const char *szName,
const std::string& sErrorReturn = "") const; const std::string &sErrorReturn = "") const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Get a matrix configuration property /** Get a matrix configuration property
@ -440,37 +445,42 @@ public:
* The return value remains valid until the property is modified. * The return value remains valid until the property is modified.
* @see GetPropertyInteger() * @see GetPropertyInteger()
*/ */
const aiMatrix4x4 GetPropertyMatrix(const char* szName, const aiMatrix4x4 GetPropertyMatrix(const char *szName,
const aiMatrix4x4& sErrorReturn = aiMatrix4x4()) const; const aiMatrix4x4 &sErrorReturn = aiMatrix4x4()) const;
// ------------------------------------------------------------------- //wangyi 0608
/** Determine a integer configuration property has been set. std::function<void *(void *)> GetPropertyCallback(const char* szName) const;
// -------------------------------------------------------------------
/** Determine a integer configuration property has been set.
* @see HasPropertyInteger() * @see HasPropertyInteger()
*/ */
bool HasPropertyInteger(const char* szName) const; bool HasPropertyInteger(const char *szName) const;
/** Determine a boolean configuration property has been set. /** Determine a boolean configuration property has been set.
* @see HasPropertyBool() * @see HasPropertyBool()
*/ */
bool HasPropertyBool(const char* szName) const; bool HasPropertyBool(const char *szName) const;
/** Determine a boolean configuration property has been set. /** Determine a boolean configuration property has been set.
* @see HasPropertyFloat() * @see HasPropertyFloat()
*/ */
bool HasPropertyFloat(const char* szName) const; bool HasPropertyFloat(const char *szName) const;
/** Determine a String configuration property has been set. /** Determine a String configuration property has been set.
* @see HasPropertyString() * @see HasPropertyString()
*/ */
bool HasPropertyString(const char* szName) const; bool HasPropertyString(const char *szName) const;
/** Determine a Matrix configuration property has been set. /** Determine a Matrix configuration property has been set.
* @see HasPropertyMatrix() * @see HasPropertyMatrix()
*/ */
bool HasPropertyMatrix(const char* szName) const; bool HasPropertyMatrix(const char *szName) const;
//wangyi 0608
bool HasPropertyCallback(const char *szName) const;
protected: protected:
/** List of integer properties */ /** List of integer properties */
IntPropertyMap mIntProperties; IntPropertyMap mIntProperties;
@ -482,23 +492,22 @@ protected:
/** List of Matrix properties */ /** List of Matrix properties */
MatrixPropertyMap mMatrixProperties; MatrixPropertyMap mMatrixProperties;
//wangyi 0608
CallbackPropertyMap mCallbackProperties;
}; };
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
inline inline const aiExportDataBlob *Exporter::ExportToBlob(const aiScene *pScene, const std::string &pFormatId,
const aiExportDataBlob* Exporter::ExportToBlob( const aiScene* pScene, const std::string& pFormatId, unsigned int pPreprocessing, const ExportProperties *pProperties) {
unsigned int pPreprocessing, const ExportProperties* pProperties) return ExportToBlob(pScene, pFormatId.c_str(), pPreprocessing, pProperties);
{
return ExportToBlob(pScene,pFormatId.c_str(),pPreprocessing, pProperties);
} }
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
inline inline aiReturn Exporter ::Export(const aiScene *pScene, const std::string &pFormatId,
aiReturn Exporter :: Export( const aiScene* pScene, const std::string& pFormatId, const std::string &pPath, unsigned int pPreprocessing,
const std::string& pPath, unsigned int pPreprocessing, const ExportProperties *pProperties) {
const ExportProperties* pProperties) return Export(pScene, pFormatId.c_str(), pPath.c_str(), pPreprocessing, pProperties);
{
return Export(pScene,pFormatId.c_str(),pPath.c_str(),pPreprocessing, pProperties);
} }
} // namespace Assimp } // namespace Assimp