diff --git a/MemoryStore.go b/MemoryStore.go index bec7f12..95ffbf3 100644 --- a/MemoryStore.go +++ b/MemoryStore.go @@ -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() +} diff --git a/main.go b/main.go index a3e57a5..c9a1dfc 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/qbolt.h b/qbolt.h index 4f11c33..8ca0ed9 100644 --- a/qbolt.h +++ b/qbolt.h @@ -52,6 +52,8 @@ extern "C" { #endif +extern GoInt64 GetMagic(); + extern GoInt64 Bolt_Options_New(); extern GoInt64 Bolt_Options_New_Readonly(); diff --git a/qbolt/boltdb.cpp b/qbolt/boltdb.cpp new file mode 100644 index 0000000..1772a70 --- /dev/null +++ b/qbolt/boltdb.cpp @@ -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); + } +} diff --git a/qbolt/boltdb.h b/qbolt/boltdb.h new file mode 100644 index 0000000..317e013 --- /dev/null +++ b/qbolt/boltdb.h @@ -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 diff --git a/qbolt/interop.cpp b/qbolt/interop.cpp new file mode 100644 index 0000000..5ecb514 --- /dev/null +++ b/qbolt/interop.cpp @@ -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(); +} diff --git a/qbolt/interop.h b/qbolt/interop.h new file mode 100644 index 0000000..6278e01 --- /dev/null +++ b/qbolt/interop.h @@ -0,0 +1,19 @@ +#ifndef INTEROP_H +#define INTEROP_H + +#include "../qbolt.h" +#include + +class Interop +{ +public: + Interop(); + + static QString GetError(GoInterface err); + + static GoString toGoString_WeakRef(QByteArray *qba); + + static int64_t GetMagic(); +}; + +#endif // INTEROP_H diff --git a/qbolt/main.cpp b/qbolt/main.cpp index b48f94e..7785f88 100644 --- a/qbolt/main.cpp +++ b/qbolt/main.cpp @@ -1,8 +1,17 @@ #include "mainwindow.h" #include +#include "interop.h" + +#include 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(); diff --git a/qbolt/mainwindow.cpp b/qbolt/mainwindow.cpp index e30b907..c16ff58 100644 --- a/qbolt/mainwindow.cpp +++ b/qbolt/mainwindow.cpp @@ -1,7 +1,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include "../qbolt.h" +#include "boltdb.h" #include #include @@ -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; } diff --git a/qbolt/qbolt.pro b/qbolt/qbolt.pro index ba15683..996997a 100644 --- a/qbolt/qbolt.pro +++ b/qbolt/qbolt.pro @@ -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