reworked to ElemBase *

pull/2005/head
CwTCwT 2018-06-09 23:29:50 +02:00
parent 5b4b16f360
commit 959c0e8907
4 changed files with 33 additions and 48 deletions

View File

@ -1,5 +1,7 @@
#include "BlenderCustomData.h" #include "BlenderCustomData.h"
#include "BlenderDNA.h"
#include <array> #include <array>
#include <functional>
namespace Assimp { namespace Assimp {
namespace Blender { namespace Blender {
@ -20,52 +22,32 @@ namespace Assimp {
/** /**
* @brief pointer to function read memory for n CustomData types * @brief pointer to function read memory for n CustomData types
*/ */
typedef bool(*PRead)(void *pOut, const size_t cnt, const FileDatabase &db); typedef bool (*PRead)(ElemBase *pOut, const size_t cnt, const FileDatabase &db);
/** typedef ElemBase * (*PCreate)(const size_t cnt);
* @brief pointer to function read memory for cnt CustomData types typedef void (*PDestroy)(ElemBase *);
*/
typedef uint8_t *(*PAlloc)(const size_t cnt);
/**
* @brief helper macro to define Structure specific read function
* for ex: when used like
*
* IMPL_STRUCT_READ(MLoop)
*
* following function is implemented
*
* bool readMLoop(void *v, const size_t cnt, const FileDatabase &db) {
* return read<MLoop>(db.dna["MLoop"], static_cast<MLoop *>(v), cnt, db);
* }
*/
#define IMPL_STRUCT_READ(ty) \ #define IMPL_STRUCT_READ(ty) \
bool read##ty(void *v, const size_t cnt, const FileDatabase &db) { \ bool read##ty(ElemBase *v, const size_t cnt, const FileDatabase &db) { \
return read<ty>(db.dna[#ty], static_cast<ty *>(v), cnt, db); \ return read<ty>(db.dna[#ty], static_cast<ty *>(v), cnt, db); \
} }
/** #define IMPL_STRUCT_CREATE(ty) \
* @brief helper macro to define Structure specific alloc function ElemBase *create##ty(const size_t cnt) { \
* for ex: when used like return new ty[cnt]; \
* }
* IMPL_STRUCT_ALLOC(MLoop)
* #define IMPL_STRUCT_DESTROY(ty) \
* following function is implemented void destroy##ty(ElemBase *p) { \
* delete[]p; \
* void * allocMLoop(const size_t cnt) {
* return new uint8_t[cnt * sizeof MLoop];
* }
*/
#define IMPL_STRUCT_ALLOC(ty) \
uint8_t *alloc##ty(const size_t cnt) { \
return static_cast<uint8_t *>(malloc(cnt * sizeof(ty))); \
} }
/** /**
* @brief helper macro to define Structure functions * @brief helper macro to define Structure functions
*/ */
#define IMPL_STRUCT(ty) \ #define IMPL_STRUCT(ty) \
IMPL_STRUCT_ALLOC(ty) \ IMPL_STRUCT_READ(ty) \
IMPL_STRUCT_READ(ty) IMPL_STRUCT_CREATE(ty) \
IMPL_STRUCT_DESTROY(ty)
// supported structures for CustomData // supported structures for CustomData
IMPL_STRUCT(MVert) IMPL_STRUCT(MVert)
@ -83,26 +65,29 @@ namespace Assimp {
*/ */
struct CustomDataTypeDescription { struct CustomDataTypeDescription {
PRead Read; ///< function to read one CustomData type element PRead Read; ///< function to read one CustomData type element
PAlloc Alloc; ///< function to allocate n type elements PCreate Create; ///< function to allocate n type elements
PDestroy Destroy;
CustomDataTypeDescription(PRead read, PAlloc alloc) CustomDataTypeDescription(PRead read, PCreate create, PDestroy destroy)
: Read(read) : Read(read)
, Alloc(alloc) , Create(create)
, Destroy(destroy)
{} {}
}; };
/** /**
* @brief helper macro to define Structure type specific CustomDataTypeDescription * @brief helper macro to define Structure type specific CustomDataTypeDescription
* @note IMPL_STRUCT_READ for same ty must be used earlier to implement the typespecific read function * @note IMPL_STRUCT_READ for same ty must be used earlier to implement the typespecific read function
*/ */
#define DECL_STRUCT_CUSTOMDATATYPEDESCRIPTION(ty) \ #define DECL_STRUCT_CUSTOMDATATYPEDESCRIPTION(ty) \
CustomDataTypeDescription(&read##ty, &alloc##ty) CustomDataTypeDescription{&read##ty, &create##ty, &destroy##ty}
/** /**
* @brief helper macro to define CustomDataTypeDescription for UNSUPPORTED type * @brief helper macro to define CustomDataTypeDescription for UNSUPPORTED type
*/ */
#define DECL_UNSUPPORTED_CUSTOMDATATYPEDESCRIPTION \ #define DECL_UNSUPPORTED_CUSTOMDATATYPEDESCRIPTION \
CustomDataTypeDescription(nullptr, nullptr) CustomDataTypeDescription{nullptr, nullptr, nullptr}
/** /**
* @brief descriptors for data pointed to from CustomDataLayer.data * @brief descriptors for data pointed to from CustomDataLayer.data
@ -164,15 +149,15 @@ namespace Assimp {
return cdtype >= 0 && cdtype < CD_NUMTYPES; return cdtype >= 0 && cdtype < CD_NUMTYPES;
} }
bool readCustomData(std::shared_ptr<uint8_t> &out, const int cdtype, const size_t cnt, const FileDatabase &db) { bool readCustomData(std::shared_ptr<ElemBase> &out, const int cdtype, const size_t cnt, const FileDatabase &db) {
if (!isValidCustomDataType(cdtype)) { if (!isValidCustomDataType(cdtype)) {
throw Error((Formatter::format(), "CustomData.type ", cdtype, " out of index")); throw Error((Formatter::format(), "CustomData.type ", cdtype, " out of index"));
} }
const CustomDataTypeDescription cdtd = customDataTypeDescriptions[cdtype]; const CustomDataTypeDescription cdtd = customDataTypeDescriptions[cdtype];
if (cdtd.Read && cdtd.Alloc) { if (cdtd.Read && cdtd.Create && cdtd.Destroy) {
// allocate cnt elements and parse them from file // allocate cnt elements and parse them from file
out.reset(cdtd.Alloc(cnt), free); out.reset(cdtd.Create(cnt), cdtd.Destroy);
return cdtd.Read(out.get(), cnt, db); return cdtd.Read(out.get(), cnt, db);
} }
return false; return false;

View File

@ -329,7 +329,7 @@ public:
* @return true when read was successful * @return true when read was successful
*/ */
template <int error_policy> template <int error_policy>
bool ReadCustomDataPtr(std::shared_ptr<uint8_t>&out, int cdtype, const char* name, const FileDatabase& db) const; bool ReadCustomDataPtr(std::shared_ptr<ElemBase>&out, int cdtype, const char* name, const FileDatabase& db) const;
private: private:
@ -833,7 +833,7 @@ private:
* @param[in] db to read elements from * @param[in] db to read elements from
* @return true when ok * @return true when ok
*/ */
bool readCustomData(std::shared_ptr<uint8_t> &out, int cdtype, size_t cnt, const FileDatabase &db); bool readCustomData(std::shared_ptr<ElemBase> &out, int cdtype, size_t cnt, const FileDatabase &db);
} // end Blend } // end Blend

View File

@ -310,7 +310,7 @@ void Structure :: ReadField(T& out, const char* name, const FileDatabase& db) co
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// field parsing for raw untyped data (like CustomDataLayer.data) // field parsing for raw untyped data (like CustomDataLayer.data)
template <int error_policy> template <int error_policy>
bool Structure::ReadCustomDataPtr(std::shared_ptr<uint8_t>&out, int cdtype, const char* name, const FileDatabase& db) const { bool Structure::ReadCustomDataPtr(std::shared_ptr<ElemBase>&out, int cdtype, const char* name, const FileDatabase& db) const {
const StreamReaderAny::pos old = db.reader->GetCurrentPos(); const StreamReaderAny::pos old = db.reader->GetCurrentPos();

View File

@ -399,7 +399,7 @@ struct CustomDataLayer : ElemBase {
int active_mask; int active_mask;
int uid; int uid;
char name[64]; char name[64];
std::shared_ptr<uint8_t> data; // must be converted to real type according type member, usage of uint8_t since std::shared_ptr<ElemBase> data; // must be converted to real type according type member
CustomDataLayer() CustomDataLayer()
: ElemBase() : ElemBase()