How to use c++ std complex numbers in QtScript - c++

I try to find out how to use complex numbers in QtScripts such that slots defined with complex arguments can be called from a QtScript. Also basic algebra (+,-,exp, ... ) of complex-numbers should be accessible to the user from the script.
Just for illustration want I want to call is:
#include<complex>
typedef complex<double> Complex;
class MyCppClass : public QObject
{
Q_OBJECT
public:
...
public slots:
void mySignal(Complex rCValue); !! <<== should be callable from QtScript
...
}
Any ideas? Thx!

I think you must implement complex algebra in QtScript (something like http://examples.oreilly.com/9781565923928/text/8-6.txt) and then modify mySignal to accept an object like this.

It's not the final answer ... since as indicated above the operators +,- and * cannot be used for Complex quantities on the javascript side. But for those interested I'd like to share the following code pieces, which allow to trigger slots with complex arguments.
test.h:
#include <QtCore>
#include <QtScript>
#include <complex>
#include <iostream>
using namespace std;
typedef complex<double> Complex;
Q_DECLARE_METATYPE(Complex)
class TestClass : public QObject
{
Q_OBJECT
public:
TestClass() : QObject() {};
public slots:
void TestOutput(Complex rValue);
};
test.cpp:
#include "test.h"
void TestClass::TestOutput(Complex rValue)
{
cout << "received value "<< rValue << endl;
}
main.cpp:
#include "test.h"
QScriptValue toScriptValue(QScriptEngine *eng, const Complex& rValue)
{
QScriptValue obj = eng->newObject();
obj.setProperty("re",real(rValue));
obj.setProperty("im",imag(rValue));
return obj;
}
void fromScriptValue(const QScriptValue &obj, Complex& rValue)
{
double re=obj.property("re").toNumber();
double im=obj.property("im").toNumber();
rValue=Complex(re,im);
}
QScriptValue constructComplex(QScriptContext *context, QScriptEngine *engine)
{
Complex complex=Complex(2,1);
return engine->toScriptValue(complex);
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QScriptEngine eng;
// register our custom type
qScriptRegisterMetaType<Complex>(&eng, toScriptValue, fromScriptValue);
TestClass *test=new TestClass;
QObject *someObject = (QObject*)test;
QScriptValue objectValue = eng.newQObject(someObject);
eng.globalObject().setProperty("myObject", objectValue);
QScriptValue val = eng.evaluate("function Complex(real, imaginary) { this.re = real; this.im = imaginary;}; cadd = function (a, b) {return new Complex(a.re + b.re, a.im + b.im);};");
val = eng.evaluate("my_complex=new Complex(8,1); my_comp=new Complex(2,9); my_c=cadd(my_comp,my_complex);");
cout << "script"<< val.toString().toStdString() << endl;
Complex cval = qscriptvalue_cast<Complex>(val);
cout << "qscriptvalue_cast : "<< cval << endl;
val = eng.evaluate("myObject.TestOutput(my_c)");
return 0;
}

Related

How to pass smart pointer instead of "this" in C++ 11?

I am using c++11 compiler.
I have two classes - class Test and class TestHelper.
The class Test is a friend-to-class TestHelper.
The class Test is only which we can access from outside.
Now, we want to call Test API i.e. setVal(). This setVal() should call
Test2 API i.e. setX and is expecting this pointer. I don't want to use this pointer but want
to use a smart pointer instead. How can I do so?
The notion of this kind of desirability is because of the fact that in reality, my class Test is pretty big. So, I am trying to make a helper class for Test i.e.
class TestHelper;
class Test
{
friend class TestHelper;
int x;
public:
void display() {
std::cout << x;
}
void setVal(int val) {
TestHelper testH;
testH.setX(this, 324);
}
};
class TestHelper
{
public:
void setX(Test *test, int val) {
/** some algorithm here and then change val to something else */
test->x = val*100;
}
};
int main()
{
std::cout << "Hello World!\n";
Test x;
x.setVal(130);
}
I tried changing the prototype from void setX(Test *test, int val)
to void setX(std::shared_ptr<Test> test, int val) but don't know how to pass this pointer
as std::shared_ptr<Test> test here.
So here is working solution with shared pointers. The example doesn't even compile due to missing definitions so you have to restructure your code into headers and cpp files.
Test.h:
#ifndef TEST_H
#define TEST_H
#include <memory>
#include "TestHelper.h"
class Test : public std::enable_shared_from_this<Test>
{
private:
friend class TestHelper;
int x;
public:
void display();
void setVal(int val);
};
#endif
Test.cpp:
#include <iostream>
#include "Test.h"
void Test::display() {
std::cout << x;
}
void Test::setVal(int val) {
TestHelper testH;
testH.setX(shared_from_this(), 324);
}
TestHelper.h:
#ifndef TESTHELPER_H
#define TESTHELPER_H
class Test;
class TestHelper
{
public:
void setX(std::shared_ptr<Test> test, int val);
};
#endif
TestHelper.cpp:
#include <memory>
#include "TestHelper.h"
#include "Test.h"
void TestHelper::setX(std::shared_ptr<Test> test, int val) {
/** some algorithm here and then change val to something else */
test->x = val*100;
}
main.cpp:
#include <iostream>
#include <memory>
#include "Test.h"
int main(void){
std::cout << "Hello World!\n";
auto x = std::make_shared<Test>();
x->setVal(130);
x->display();
}
You can run it here: https://paiza.io/projects/e/79dehCx0RRAG4so-sVZcQw
I don't understand why you want this, here's a few variants that compile
reference
// Reference variant
#include <iostream>
class Test;
class TestHelper
{
public:
void setX(Test & test, int val);
};
class Test
{
friend class TestHelper;
int x;
public:
void display() {
std::cout << x;
}
void setVal(int val) {
TestHelper testH;
testH.setX(*this, 324);
}
};
void TestHelper::setX(Test &test, int val)
{
/** some algorithm here and then change val to something else */
test.x = val*100;
}
int main()
{
Test x;
x.setVal(130);
x.display();
}
http://cpp.sh/7t3ec
shared ptr
// Shared ptr variant
#include <iostream>
#include <memory> // Required for shared_ptrs
class Test;
class TestHelper
{
public:
void setX(std::shared_ptr<Test> test, int val);
};
class Test : public std::enable_shared_from_this<Test>
{
friend class TestHelper;
int x;
public:
void display() {
std::cout << x;
}
void setVal(int val) {
TestHelper testH;
testH.setX(shared_from_this(), 324);
}
};
void TestHelper::setX(std::shared_ptr<Test> test, int val)
{
/** some algorithm here and then change val to something else */
test->x = val*100;
}
int main()
{
auto x = std::make_shared<Test>(); // x needs to be created as shared_ptr or it won't work
x->setVal(130);
x->display();
}
http://cpp.sh/87ao2
Perhaps with these you can refine your question?

QtConcurrent::mappedReduced without mapped

Consider the following code (Qt 6.0.3, C++17):
const QVector<int> arr = {1, 2, 3, 4, 5};
auto future = QtConcurrent::mappedReduced<int>(arr, [](auto item) {
return item;
}, [](int& result, auto item) {
result += item;
});
As you can see the first lambda expression passed to QtConcurrent::mappedReduced looks unnecessary. That's why I want to find something like QtConcurrent::reduced in Qt 6. Or how can I refactor this code to use only 1 lambda expression?
You can use std::accumulate.
The goal of QtConcurrent mappedReduce is the apply an operation on all the items of container in a concurrent fashion and once done do the reduction.
In your case there's nothing to be done but the sum of the items so there's no need for mappedReduce.
Update:
Based on your comments here is an example using a custom class with the operator+ overloaded using a QList as container.
class MyClass
{
public:
MyClass(int x=0, int y=0):
_x(x),
_y(y)
{}
int x() const { return _x; }
int y() const { return _y; }
public:
int _x;
int _y;
};
MyClass operator+(const MyClass& left, const MyClass &right)
{
MyClass m;
m._x = left._x + right._x;
m._y = left._y + right._y;
return m;
}
Here is a version with std::accumulate
#include <QVector>
#include <QtDebug>
#include <numeric>
int main(int argc, char **argv)
{
Q_UNUSED(argc);
Q_UNUSED(argv);
QVector<MyClass> myClassList = {MyClass(12, 42), MyClass(23, 53)};
MyClass result = std::accumulate(myClassList.constBegin(), myClassList.constEnd(), MyClass());
qDebug() << result.x() << result.y();
return 0;
}
Update 2:
Based on #IlBeldus suggestion, here is the version were you use std::accumulate with QtConcurrent::run
#include <QtDebug>
#include <QVector>
#include <QtConcurrent>
#include <numeric>
MyClass myAccumulate(const QVector<MyClass> &input)
{
return std::accumulate(input.constBegin(), input.constEnd(), MyClass());
}
int main(int argc, char **argv)
{
Q_UNUSED(argc);
Q_UNUSED(argv);
QVector<MyClass> myClassList = {MyClass(12, 42), MyClass(23, 53)};
QFuture<MyClass> future = QtConcurrent::run(myAccumulate, myClassList);
result = future.result();
qDebug() << result.x() << result.y();
return 0;
}

Functionpointer based syntax for QMetaObject invokeMethod

I'm working on a simple wrapper for a IPC lib we are using.
I want to convert the events from this lib to calls on Qt slots.
Right now i have something like this:
void Caller::registerCallback(int id, QObject* reciever, const char* member)
{
_callbackMap[id] = std::make_pair(reciever, QString(member));
}
bool Caller::call(const SomeData data)
{
auto reciever = _callbackMap.value(data.id);
return QMetaObject::invokeMethod(reciever.first, reciever.second.toLocal8Bit(), Qt::QueuedConnection,
QGenericReturnArgument(),
Q_ARG(SomeData, data));
}
void Receiver::someCallback(SomeData data)
{
qDebug() << data.str;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Caller caller;
Receiver reciever;
caller.registerCallback(1, &reciever, "someCallback");
caller.call(SomeData({ "Hi", 1 }));
return a.exec();
}
struct SomeData {
QString str;
int id;
}; Q_DECLARE_METATYPE(SomeData);
This works quite well. But I don't like to register the callbacks as strings. I would prefer a compile time checking with a syntax like this:
caller.registerCallback(1, &reciever, &Reciever::someCallback);
I am aware of this implementation.
The slots I want to register always have exactly one argument and no return value.
I already found this request what could solve my problem but unfortunately this was never implemented.
Also this question doesn't help me as I'm not able to patch the moc we are using.
So is this really not possible with all the meta magic Qt is using?
EDIT:
I found a solution that works also when the Caller dose not know about the Receiver (what is actually what I need):
//Caller.h
class Caller : public QObject
{
Q_OBJECT
public:
Caller(QObject *parent = nullptr);
~Caller();
//void registerCallback(int id, QObject* reciever, const char *member);
template < class R, typename Func >
void inline registerCallback(int id, R reciever, Func callback)
{
using std::placeholders::_1;
registerCallbackImpl(id, reciever, std::bind(callback, reciever, _1));
};
bool call(const SomeData);
private:
QMap<int, std::pair<QObject *, std::function<void(SomeData)>> > _callbackMap;
void registerCallbackImpl(int id, QObject* reciever, std::function<void(SomeData)> callback);
};
//Caller.cpp
void Caller::registerCallbackImpl(int id, QObject* reciever, std::function<void(SomeData)> callback)
{
_callbackMap[id] = std::make_pair(reciever, callback);
}
bool Caller::call(const SomeData data)
{
auto reciever = _callbackMap.value(data.id).first;
auto fn = _callbackMap.value(data.id).second;
QMetaObject::invokeMethod(reciever, [reciever, fn, data]() {
std::invoke(fn, data);
fn(data);
}, Qt::QueuedConnection);
return true;
}
//main.cpp
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Caller caller;
Receiver reciever;
using std::placeholders::_1;
caller.registerCallback(2, &reciever, &Receiver::someCallback);
caller.call(SomeData({ "Hi2", 2 }));
return a.exec();
}
This soulution relies upon std::invoke and lambda.
Variant 1: use std::invoke directly instead of QMetaObject::invoke
Variant 2: use std::invoke inside a lambda, which is passed to QMetaObject::invoke
Variant 3: use MACRO instead of std::invoke in variant 2.
If you use QMetaObject::invoke you've got an option to choose connection type - Direct or Queued. In variant 1 the call is invoked immediately like in direct connection.
receiver.h
#ifndef RECEIVER_H
#define RECEIVER_H
#include <QObject>
#include <QDebug>
struct SomeData {
QString str;
int id;
};
//Q_DECLARE_METATYPE(SomeData);
class Receiver : public QObject
{
Q_OBJECT
public:
explicit Receiver(QObject *parent = nullptr) : QObject(parent) {}
void doSmth(SomeData data) {
qDebug() << data.str;
}
signals:
};
#endif // RECEIVER_H
caller.h
#ifndef CALLER_H
#define CALLER_H
#include <QObject>
#include <QMap>
#include <utility>
#include <map>
#include "receiver.h"
#define CALL_MEMBER_FN(object,ptrToMember) ((object)->*(ptrToMember))
typedef void (Receiver::*callback)(SomeData);
class Caller : public QObject
{
Q_OBJECT
public:
explicit Caller(QObject *parent = nullptr) : QObject(parent) { }
void registerCallback(int id, Receiver* receiver, callback c)
{
auto pair = std::make_pair(receiver, c);
_callbackMap.emplace(id, pair);
}
bool call(const SomeData data)
{
auto &receiver = _callbackMap.at(data.id);
return QMetaObject::invokeMethod(receiver.first, [data, receiver] () {
// method 1
std::invoke(receiver.second, receiver.first, data);
// method 2 (better not to use a MACRO)
CALL_MEMBER_FN(receiver.first, receiver.second)(data);
}, Qt::QueuedConnection);
}
bool call_invoke(const SomeData data)
{
auto &receiver = _callbackMap.at(data.id);
std::invoke(receiver.second, receiver.first, data);
return true;
}
signals:
private:
std::map<int,std::pair<Receiver*,callback>> _callbackMap;
};
#endif // CALLER_H
main.cpp
#include <QCoreApplication>
#include "receiver.h"
#include "caller.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Caller caller;
Receiver reciever;
caller.registerCallback(1, &reciever, &Receiver::doSmth);
caller.registerCallback(2, &reciever, &Receiver::doSmth);
caller.call(SomeData({ "Hi", 1 }));
caller.call_invoke(SomeData({ "Hi2", 2 }));
return a.exec();
}
An alternative approach might be to use a suitable std::function to capture the callback and then make use of QTimer::singleShot with a zero timeout to invoke the callback in the correct context.
struct SomeData {
QString str;
int id;
};
class Caller {
public:
using task = std::function<void(SomeData)>;
void registerCallback (int id, QObject *receiver, task t)
{
_callbackMap[id] = std::make_pair(receiver, t);
}
bool call (SomeData data)
{
auto receiver = _callbackMap.value(data.id);
QTimer::singleShot(0, receiver.first, [=](){ receiver.second(data); });
return true;
}
private:
QMap<int, std::pair<QObject *, task>> _callbackMap;
};
class Receiver: public QObject {
public:
void someCallback (SomeData data)
{
qDebug() << data.str;
}
};
Then use as...
Caller caller;
Receiver receiver;
caller.registerCallback(1, &receiver, [&](SomeData d){ receiver.someCallback(d); });
caller.call(SomeData({ "Hi", 1 }));

QMetaType::Type from template type

Is it possible to determine QMetaType::Type value of a template argument.
I tried this:
template <class T>
class MyClass {
public:
int getType() {
return QMetaType::type(typeid(T).name());
}
};
But this returns always 0 (QMetaType::UnknownType) because Qt uses different type names than the compiler.
It should work like the following:
MyClass<int>().getType(); // 2 (QMetaType::Int)
MyClass<QString>().getType(); // 10 (QMetaType::QString)
MyClass<QRect>().getType(); // 19 (QMetaType::QRect)
MyClass<MyType>().getType(); // 1024 (Set by qRegisterMetaType)
I tested your code on Qt 5.12.4 and it seems to work.
You could also Q_DECLARE_METATYPE to register your custom type and then use qMetaTypeId() to get the metaType id.
Here my test code and example:
#include <QCoreApplication>
#include <QDebug>
#include <QMetaType>
#include <QRect>
#include <QMetaObject>
class MyType
{
public:
int _member;
};
// needed for getType2()
Q_DECLARE_METATYPE(MyType);
// needed for getType()
const int id = qRegisterMetaType<MyType>("MyType");
template <class T>
class MyClass {
public:
int getType() {
return QMetaType::type(typeid(T).name());
}
int getType2() {
return qMetaTypeId<T>();
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << MyClass<int>().getType();
qDebug() << MyClass<QString>().getType();
qDebug() << MyClass<QRect>().getType();
qDebug() << MyClass<MyType>().getType();
qDebug() << MyClass<int>().getType2();
qDebug() << MyClass<QString>().getType2();
qDebug() << MyClass<QRect>().getType2();
qDebug() << MyClass<MyType>().getType2();
return a.exec();
}
this outputs:
2
10
19
1024
2
10
19
1024

GTest fixture when constructor takes parameters?

I have a simple example class. It has one data member, which is a std::vector of pointers to armadillo matrices. the constructor takes such a vector as the only argument. here's file TClass.cpp:
#include <armadillo>
#include <vector>
class TClass {
private:
std::vector<arma::mat * > mats;
public:
TClass(std::vector<arma::mat * > m_);
arma::mat * GetM( int which ){ return( mats.at(which) );};
};
TClass::TClass(std::vector<arma::mat * > m_){
mats = m_;
}
I want to construct a GTest fixture to test member function GetM. Here is what I have done:
#include <gtest/gtest.h>
#include "TClass.cpp"
class TClassTest : public ::testing::Test {
protected:
int n;
int m;
std::vector<arma::mat * > M;
virtual void SetUp() {
n = 3;
m = 2;
arma::mat M1 = arma::randu<arma::mat>(n,m);
arma::mat M2 = arma::randu<arma::mat>(n,m);
M.push_back( &M1);
M.push_back( &M2);
}
// virtual void TearDown() {}
// initiate a TClass object
TClass T(M);
};
// my test
TEST_F(TClassTest, CanGetM1){
EXPECT_EQ( T.GetM(0), M.at(0) );
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
I compile this with g++ TClassTest.cpp -o tclass -larmadillo. It tells me that TClassTest.cpp:24: error: ‘M’ is not a type. I dont' understand why I cannot construct the TClass object in the fixture definition?
The object T cannot be initialized in the declaration of class TClassTest. Have you been writing Java lately? ;-)
To initialize it, you can do something like this:
class TClassTest : public ::testing::Test {
// ... (rest of code is fine as is)
virtual void SetUp() {
// ...
T = new TClass(M);
}
virtual void TearDown() { delete T; }
TClass *T;
};