Loading...
Searching...
No Matches
jsclassregistrar.h
Go to the documentation of this file.
#pragma once
#include <functional>
#include <unordered_map>
#include <QMap>
#include <QJSEngine>
#include "pappsomspp/export-import-config.h"
namespace pappso
{
// This macro is fundamental for the registration of C++ QObject-derived
// classes that we want to expose to JavaScript (QJSEngine/QQmlEngine).
// This macros is added at the end of the class declaration in the
// pappso name space (pappso, in the header file) in the form:
// PAPPSO_REGISTER_JS_CLASS(pappso, BasePlotContext)
//
// This macro uses the getNameSpaceClassNameJsConstructorRegistrarMap()
// function in the pappso namespace which returns a map of maps:
// The inner map maps the namespace with the class_name
// The outer map maps the inner map to the class' static member function
// that performs the actual registration of the class to the QJSEngine
// that is passed as parameter. What the
// static void registerJsConstructor(QJSEngine *engine);
// function does is simply to register a constructor in the javascript
// global object that allows the JavaScript developer to call a constructor
// like so:
// var base_plot_context = new BasePlotContext(<any param>)
#define STRINGIFY_NS(ns) #ns
#define PAPPSO_REGISTER_JS_CLASS(NS_IDENT, CLASS_NAME) \
struct JsRegHelper_##CLASS_NAME \
{ \
JsRegHelper_##CLASS_NAME() \
{ \
pappso::getNameSpaceClassNameJsConstructorRegistrarMap().insert( \
{{QStringLiteral(STRINGIFY_NS(NS_IDENT)), QStringLiteral(#CLASS_NAME)}, \
[](QJSEngine *engine) { NS_IDENT::CLASS_NAME::registerJsConstructor(engine); }}); \
} \
}; \
static JsRegHelper_##CLASS_NAME jsRegHelperInstance_##CLASS_NAME;
// Key type: pair of namespace and class name
using NamespaceClassnamePairAsKey = std::pair<QString, QString>;
using RegisterFunc = std::function<void(QJSEngine *)>;
// Hash functor for NsClassKey
struct NamespaceClassnameAsKeyHash
{
std::size_t
operator()(const NamespaceClassnamePairAsKey &key) const noexcept
{
// Combine the hashes of the two QStrings
return qHash(key.first) ^ (qHash(key.second) << 1);
}
};
// Equality functor (needed because we use QString)
struct AreNamespaceClassnamePairsEqual
{
bool
operator()(const NamespaceClassnamePairAsKey &a,
const NamespaceClassnamePairAsKey &b) const noexcept
{
return a.first == b.first && a.second == b.second;
}
};
PMSPP_LIB_DECL std::unordered_map<NamespaceClassnamePairAsKey,
RegisterFunc,
NamespaceClassnameAsKeyHash,
AreNamespaceClassnamePairsEqual> &
getNameSpaceClassNameJsConstructorRegistrarMap();
PMSPP_LIB_DECL void registerJsConstructorForNameSpaceClassNameInRegistrarMap(
const QString &name_space, const QString &class_name, QJSEngine *engine);
PMSPP_LIB_DECL void registerJsConstructorForEachClassInRegistrarMap(QJSEngine *engine);
} // namespace pappso
export-import-config.h
PMSPP_LIB_DECL
#define PMSPP_LIB_DECL
Definition export-import-config.h:14
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
pappso::registerJsConstructorForNameSpaceClassNameInRegistrarMap
void registerJsConstructorForNameSpaceClassNameInRegistrarMap(const QString &name_space, const QString &class_name, QJSEngine *engine)
Definition jsclassregistrar.cpp:50
pappso::PeptideIonNter::a
@ a
Definition peptide.h:77
pappso::PeptideIonNter::b
@ b
Definition peptide.h:74
pappso::RegisterFunc
std::function< void(QJSEngine *)> RegisterFunc
Definition jsclassregistrar.h:50
pappso::registerJsConstructorForEachClassInRegistrarMap
void registerJsConstructorForEachClassInRegistrarMap(QJSEngine *engine)
Definition jsclassregistrar.cpp:22
pappso::getNameSpaceClassNameJsConstructorRegistrarMap
std::unordered_map< NamespaceClassnamePairAsKey, RegisterFunc, NamespaceClassnameAsKeyHash, AreNamespaceClassnamePairsEqual > & getNameSpaceClassNameJsConstructorRegistrarMap()
Definition jsclassregistrar.cpp:10
pappso::NamespaceClassnamePairAsKey
std::pair< QString, QString > NamespaceClassnamePairAsKey
Definition jsclassregistrar.h:49
pappso::AreNamespaceClassnamePairsEqual
Definition jsclassregistrar.h:65
pappso::AreNamespaceClassnamePairsEqual::operator()
bool operator()(const NamespaceClassnamePairAsKey &a, const NamespaceClassnamePairAsKey &b) const noexcept
Definition jsclassregistrar.h:67
pappso::NamespaceClassnameAsKeyHash
Definition jsclassregistrar.h:54
pappso::NamespaceClassnameAsKeyHash::operator()
std::size_t operator()(const NamespaceClassnamePairAsKey &key) const noexcept
Definition jsclassregistrar.h:56