libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
jsclassregistrar.h
Go to the documentation of this file.
1
2#pragma once
3
4#include <functional>
5#include <unordered_map>
6
7#include <QtQml>
8#include <QMap>
9
11
12namespace pappso
13{
14
15// This macro is fundamental for the registration of C++ QObject-derived
16// classes that we want to expose to JavaScript (QJSEngine/QQmlEngine).
17// This macros is added at the end of the class declaration in the
18// pappso name space (pappso, in the header file) in the form:
19// PAPPSO_REGISTER_JS_CLASS(pappso, BasePlotContext)
20//
21// This macro uses the getNameSpaceClassNameJsConstructorRegistrarMap()
22// function in the pappso namespace which return a map of maps:
23// The inner map maps the namespace with the class_name
24// The out map maps the inner map to the class' static member function
25// that performs the actual registration of the class to the QJSEngine
26// that is passed as parameter. What the
27// static void registerJsConstructor(QJSEngine *engine);
28// function does is simply to register a constructor in the javascript
29// global object that allows the JavaScript developer to call a constructor
30// like so:
31// var base_plot_context = new BasePlotContext(<any param>)
32
33
34#define STRINGIFY_NS(ns) #ns
35#define PAPPSO_REGISTER_JS_CLASS(NS_IDENT, CLASS_NAME) \
36 struct JsRegHelper_##CLASS_NAME \
37 { \
38 JsRegHelper_##CLASS_NAME() \
39 { \
40 pappso::getNameSpaceClassNameJsConstructorRegistrarMap().insert( \
41 {{QStringLiteral(STRINGIFY_NS(NS_IDENT)), QStringLiteral(#CLASS_NAME)}, \
42 [](QJSEngine *engine) { NS_IDENT::CLASS_NAME::registerJsConstructor(engine); }}); \
43 } \
44 }; \
45 static JsRegHelper_##CLASS_NAME jsRegHelperInstance_##CLASS_NAME;
46
47
48// Key type: pair of namespace and class name
49using NamespaceClassnamePairAsKey = std::pair<QString, QString>;
50using RegisterFunc = std::function<void(QJSEngine *)>;
51
52// Hash functor for NsClassKey
54{
55 std::size_t
56 operator()(const NamespaceClassnamePairAsKey &key) const noexcept
57 {
58 // Combine the hashes of the two QStrings
59 return qHash(key.first) ^ (qHash(key.second) << 1);
60 }
61};
62
63// Equality functor (needed because we use QString)
65{
66 bool
68 const NamespaceClassnamePairAsKey &b) const noexcept
69 {
70 return a.first == b.first && a.second == b.second;
71 }
72};
73
76 NamespaceClassnameAsKeyHash,
77 AreNamespaceClassnamePairsEqual> &
79
81 const QString &name_space, const QString &class_name, QJSEngine *engine);
82
84
85
86} // namespace pappso
#define PMSPP_LIB_DECL
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
void registerJsConstructorForNameSpaceClassNameInRegistrarMap(const QString &name_space, const QString &class_name, QJSEngine *engine)
std::function< void(QJSEngine *)> RegisterFunc
void registerJsConstructorForEachClassInRegistrarMap(QJSEngine *engine)
std::unordered_map< NamespaceClassnamePairAsKey, RegisterFunc, NamespaceClassnameAsKeyHash, AreNamespaceClassnamePairsEqual > & getNameSpaceClassNameJsConstructorRegistrarMap()
std::pair< QString, QString > NamespaceClassnamePairAsKey
bool operator()(const NamespaceClassnamePairAsKey &a, const NamespaceClassnamePairAsKey &b) const noexcept
std::size_t operator()(const NamespaceClassnamePairAsKey &key) const noexcept