oboy/src/qt/oboy.cpp

75 lines
1.5 KiB
C++
Raw Normal View History

2019-05-11 20:00:33 +02:00
#include "oboy.h"
2019-05-11 21:13:09 +02:00
#include <caml/mlvalues.h>
#include <caml/callback.h>
2019-05-15 14:03:53 +02:00
#include <caml/alloc.h>
2019-06-06 14:39:11 +02:00
#include <caml/memory.h>
2019-05-15 14:03:53 +02:00
#include <caml/misc.h>
#include <caml/osdeps.h>
2019-05-11 21:13:09 +02:00
2019-05-11 20:00:33 +02:00
OBoy::OBoy(QObject *parent) : QObject(parent)
{
}
2019-05-11 21:13:09 +02:00
2019-06-06 14:39:11 +02:00
// https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html
// http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/calling_ocaml.html
2019-05-11 21:13:09 +02:00
QString OBoy::name() const
{
2019-06-06 14:39:11 +02:00
CAMLparam0();
2019-05-14 13:54:17 +02:00
value * closure_f = caml_named_value("oboy_name");
2019-05-11 21:13:09 +02:00
if (closure_f == nullptr) {
2019-06-06 14:39:11 +02:00
CAMLdrop;
2019-05-11 21:13:09 +02:00
return QString("<Unreachable>");
}
const char *str = String_val(caml_callback(*closure_f, Val_unit));
2019-06-06 14:39:11 +02:00
CAMLdrop;
2019-05-11 21:13:09 +02:00
return QString(str);
}
QString OBoy::version() const
{
2019-06-06 14:39:11 +02:00
CAMLparam0();
2019-05-14 13:54:17 +02:00
value * closure_f = caml_named_value("oboy_version");
2019-05-11 21:13:09 +02:00
if (closure_f == nullptr) {
2019-06-06 14:39:11 +02:00
CAMLdrop;
2019-05-11 21:13:09 +02:00
return QString("<Unreachable>");
}
const char *str = String_val(caml_callback(*closure_f, Val_unit));
2019-06-06 14:39:11 +02:00
CAMLdrop;
2019-05-11 21:13:09 +02:00
return QString(str);
}
2019-05-15 14:03:53 +02:00
bool OBoy::load(const QString &path)
{
2019-06-06 14:39:11 +02:00
CAMLparam0();
CAMLlocal1(ocaml_path) ;
2019-05-15 14:03:53 +02:00
value * closure_f = caml_named_value("oboy_load");
if (closure_f == nullptr) {
2019-06-06 14:39:11 +02:00
CAMLdrop;
2019-05-15 14:03:53 +02:00
return false;
}
QString truncated(path);
truncated.remove(0, 7); // remove file://
QByteArray ba = truncated.toLocal8Bit();
2019-06-06 14:39:11 +02:00
ocaml_path = caml_copy_string_of_os(ba.data());
2019-05-15 14:03:53 +02:00
2019-06-07 14:25:34 +02:00
_loaded = Bool_val(caml_callback(*closure_f, ocaml_path));
this->loadedChanged(_loaded);
2019-06-06 14:39:11 +02:00
CAMLdrop;
2019-06-07 14:25:34 +02:00
return _loaded;
}
bool OBoy::loaded() const
{
return _loaded;
2019-05-15 14:03:53 +02:00
}