list model C new and free

This commit is contained in:
Sebastian Borchers 2019-01-15 08:36:38 +01:00
parent 9dfc3f684c
commit 898c132805
5 changed files with 42 additions and 5 deletions

View File

@ -45,6 +45,7 @@
#include <QQuickTextureFactory>
#include <QQuickAsyncImageProvider>
#include <QQuickImageResponse>
#include <QAbstractListModel>
using std::string;
using std::cout;

View File

@ -32,12 +32,34 @@
//#############//
gml_list_model gml_list_model_new(void* go_ptr) {
try {
GmlListModel* glm = new GmlListModel(go_ptr);
return (gml_list_model)glm;
}
catch (std::exception& e) {
gml_error_log_exception(e.what());
return NULL;
}
catch (...) {
gml_error_log_exception();
return NULL;
}
}
void gml_list_model_free(gml_list_model lm) {
if (lm == NULL) {
return;
}
GmlListModel* glm = (GmlListModel*)lm;
delete glm;
lm = NULL;
}
}
//##########################//
//### GmlListModel Class ###//
//##########################//
GmlListModel::GmlListModel(
void* goPtr
) :
goPtr(goPtr) {}

View File

@ -30,4 +30,14 @@
#include "gml_includes.h"
class GmlListModel : public QAbstractListModel {
public:
GmlListModel(
void* goPtr
);
private:
void* goPtr;
};
#endif

View File

@ -40,6 +40,10 @@ import (
type ListModelHandler interface {
}
// Ensure the ListModel type implements the ListModelHandler interface.
var _ ListModelHandler = &ListModel{}
type ListModel struct {
freed bool
lm C.gml_list_model

View File

@ -46,9 +46,9 @@ func Save(v interface{}) unsafe.Pointer {
}
// Generate real fake C pointer.
// This pointer will not store any data, but will bi used for indexing purposes.
// Since Go doest allow to cast dangling pointer to unsafe.Pointer, we do rally allocate one byte.
// Why we need indexing, because Go doest allow C code to store pointers to Go data.
// This pointer will not store any data, but will be used for indexing purposes.
// Since Go does not allow to cast dangling pointer to unsafe.Pointer, we do really allocate one byte.
// Why we need indexing, because Go does not allow C code to store pointers to Go data.
ptr := C.malloc(C.size_t(1))
if ptr == nil {
panic("can't allocate 'cgo-pointer hack index pointer': ptr == nil")