add GetMagic() check, move c/go interop into separate classes

This commit is contained in:
mappu 2017-05-20 14:57:51 +12:00
parent 8ce03cbed6
commit a94a330345
10 changed files with 130 additions and 31 deletions

View File

@ -49,4 +49,8 @@ func (this *GoMemoryStore) Delete(i ObjectReference) {
delete(this.items, int64(i))
}
var gms = NewGoMemoryStore()
var gms *GoMemoryStore = nil
func init() {
gms = NewGoMemoryStore()
}

View File

@ -8,6 +8,11 @@ import (
"github.com/boltdb/bolt"
)
//export GetMagic
func GetMagic() int64 {
return 0x10203040
}
//export Bolt_Options_New
func Bolt_Options_New() ObjectReference {
b := *bolt.DefaultOptions

View File

@ -52,6 +52,8 @@ extern "C" {
#endif
extern GoInt64 GetMagic();
extern GoInt64 Bolt_Options_New();
extern GoInt64 Bolt_Options_New_Readonly();

33
qbolt/boltdb.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "boltdb.h"
BoltDB::BoltDB()
{
}
BoltDB* BoltDB::createFrom(QString filePath, QString &errorOut)
{
auto opts = Bolt_Options_New_Readonly();
QByteArray filePathBytes(filePath.toUtf8());
GoString filePathGS = Interop::toGoString_WeakRef(&filePathBytes);
auto open_ret = Bolt_Open(filePathGS, 0444, opts);
if (open_ret.r1.v != nullptr) {
errorOut = Interop::GetError(open_ret.r1);
return nullptr;
}
BoltDB *ret = new BoltDB();
ret->gmsDbRef = open_ret.r0;
return ret;
}
BoltDB::~BoltDB()
{
auto err = Bolt_Close(this->gmsDbRef);
if (err.v != nullptr) {
// Error closing database:
// errorOut = Interop::GetError(err);
}
}

19
qbolt/boltdb.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef BOLTDB_H
#define BOLTDB_H
#include "interop.h"
class BoltDB
{
protected:
BoltDB();
GoInt64 gmsDbRef;
public:
static BoltDB* createFrom(QString filePath, QString &errorOut);
~BoltDB();
};
#endif // BOLTDB_H

23
qbolt/interop.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "interop.h"
Interop::Interop()
{
}
QString Interop::GetError(GoInterface err) {
if (err.v == nullptr) {
return ""; // no error
}
GoString gs = Error_Error(err);
return QString::fromUtf8(gs.p, gs.n);
}
GoString Interop::toGoString_WeakRef(QByteArray *qba) {
return GoString{qba->data(), qba->length()};
}
int64_t Interop::GetMagic() {
return ::GetMagic();
}

19
qbolt/interop.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef INTEROP_H
#define INTEROP_H
#include "../qbolt.h"
#include <QString>
class Interop
{
public:
Interop();
static QString GetError(GoInterface err);
static GoString toGoString_WeakRef(QByteArray *qba);
static int64_t GetMagic();
};
#endif // INTEROP_H

View File

@ -1,8 +1,17 @@
#include "mainwindow.h"
#include <QApplication>
#include "interop.h"
#include <QDebug>
int main(int argc, char *argv[])
{
int64_t magic = Interop::GetMagic();
if (magic != 0x10203040) {
qDebug() << "bad magic " << magic;
return 1;
}
QApplication a(argc, argv);
MainWindow w;
w.show();

View File

@ -1,7 +1,7 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "../qbolt.h"
#include "boltdb.h"
#include <QFileDialog>
#include <QMessageBox>
@ -18,14 +18,6 @@ MainWindow::~MainWindow()
delete ui;
}
static QString GetError(GoInterface err) {
if (err.v == nullptr) {
return ""; // no error
}
GoString gs = Error_Error(err);
return QString::fromUtf8(gs.p, gs.n);
}
void MainWindow::on_actionOpen_database_triggered()
{
@ -34,28 +26,16 @@ void MainWindow::on_actionOpen_database_triggered()
return;
}
auto opts = Bolt_Options_New_Readonly();
QByteArray filePathBytes(file.toUtf8());
GoString filePathGS = {filePathBytes.data(), filePathBytes.length()};
auto open_ret = Bolt_Open(filePathGS, 0444, opts);
if (open_ret.r1.v != nullptr) {
// Open
QString error;
auto *bdb = BoltDB::createFrom(file, error);
if (bdb == nullptr) {
QMessageBox qmb;
qmb.setText(tr("Error opening database: %s").arg(GetError(open_ret.r1)));
qmb.setText(tr("Error opening database: %s").arg(error));
qmb.exec();
return;
}
// do something
// close database
auto err = Bolt_Close(open_ret.r0);
if (err.v != nullptr) {
QMessageBox qmb;
qmb.setText(tr("Error closing database: %s").arg(GetError(err)));
qmb.exec();
return;
}
// Free
delete bdb;
}

View File

@ -27,9 +27,14 @@ QMAKE_LIBS += ../qbolt.a
SOURCES += main.cpp\
mainwindow.cpp
mainwindow.cpp \
interop.cpp \
boltdb.cpp
HEADERS += mainwindow.h
HEADERS += mainwindow.h \
interop.h \
boltdb.h \
../qbolt.h
FORMS += mainwindow.ui