How to achieve QT-like syntax of signal connections with Boost::Signal - qt

In QT we can connect signals and slots using the following simple syntax:
connect(pObject1, signal1, pObject2, slot2)
For instance, one can write something like:
A a;
B b;
connect(&a, SIGNAL(valueChanged(int)), &a, SLOT(setValue(int)));
With Boost::Signal the syntax we would write it this way:
A a;
B b;
a.valueChanged.connect(boost::bind(&B::SetValue, &b, _1))
IMHO, the boost signal's syntax is more complicated. Is there a way to make the Boost::Signal's syntax more QT like.

The thing with Qt is that it goes through a code generation phase during compilation, that Boost can't do. That means that Qt can do some very clever syntactic things that can't be copied without going through a similar process.
To quote Wikipedia:
Known as the moc, this is a tool that is run on the sources of a Qt program. It interprets certain macros from the C++ code as annotations, and uses them to generate additional C++ code with "Meta Information" about the classes used in the program. This meta information is used by Qt to provide programming features not available natively in C++: the signal/slot system, introspection and asynchronous function calls.
(I can't get the link to work, but it's http://en.wikipedia.org/wiki/Qt_(framework))
Edit: I think the Wikipedia quote is quite clear that the signal/slot system is implemented using the moc. I doubt very much that there's any way to use the same syntax without using a similar system.

Related

Reason to use Qt standard library function wrappers

Is there any reason to use Qt standard function wrappers like qstrncpy instead of strncpy?
I could not find any hint in documentation. And I'm curious if there is any functional difference. It looks like making code dependent on Qt, even in not mandatory places.
I found this: Qt wrapper for C libraries
But it doesn't answer my question.
These methods are part of Qt's efforts for platform-independence. Qt tries to hide platform differences and use the best each platform has to offer, replicating that functionality on platforms where it is not available. Here is what the documentation of qstrncpy has to say:
A safe strncpy() function.
Copies at most len bytes from src (stopping at len or the terminating '\0' whichever comes first) into dst and returns a pointer to dst. Guarantees that dst is '\0'-terminated. If src or dst is nullptr, returns nullptr immediately.
[…]
Note: When compiling with Visual C++ compiler version 14.00 (Visual C++ 2005) or later, internally the function strncpy_s will be used.
So qstrncpy is safer than strncpy.
The Qt wrappers for these functions are safer than the standard ones because they guarantee the destination string will always be null-terminated. strncpy() does not guarantee this.
In C11, strncpy_s() and other _s() suffixed functions were added as safe string functions. However, they are not available in any C++ standard, they are C-only. The Qt wrappers fix this.

What is the basic structure of a Julia program?

What are the basic structural aspects that should exist in Julia code I am writing? I will link some other languages' implementation of this for reference.
I am looking for officially sanctioned components for the language itself, not one's opinion on best practices.
Basic structure of a C program
Basic structure of a Java program
First off, if you are new to Julia and the structure of writing a program therein, I suggest you check out the Official Julia Docs for a great explanation as to how to get started writing code in Julia.
If you are coming from a language like Java and read the documentation linked above, you may be a little confused as to where the docs on the structure of a Julia program are. To my understanding, that doc does not exist for a few reasons.
Julia as a language imposes very little on you as the programmer. This can lead to a bit of uncertainty and doubt with all of the newfound freedom. There are some basic structures that should be followed, despite the flexibility the language provides:
using and import statements are generally made at the very top of the file.
That's about it! (Feel free to edit if you think there's more).
It's also worth checking out the Julia Style Guide for things like:
Write functions, not just scripts:
Writing code as a series of steps at the top level is a quick way to get started solving a problem, but you should try to divide a program into functions as soon as possible. Functions are more reusable and testable, and clarify what steps are being done and what their inputs and outputs are. Furthermore, code inside functions tends to run much faster than the top-level code, due to how Julia's compiler works.
In general, Julia is flexible. There are very few things that you have to include in your program.
It's important to designate the difference between writing a simple Julia script and creating a project in Julia.
While there are limited structural suggestions for creating a simple script, there are a number of suggestions related to how one can structure a Julia Project. In fact, many of these aspects are built into Julia itself! You can find out more about creating Julia Projects (which should have a similar if not the same structure as Julia packages) here.
Note: If you are trying to find the structure of a Package in Julia, a great resource would be PackageTemplate.jl.
Larger Julia programs tend to first have modules that are included, then any const declaration of const global variables, then one or more functions, then a statement that calls the first function run.
However, your question may be due to the fact that you already know that C and Java have a certain structure mandated in order to compile and run.
Perhaps comparing a "Hello, World!" minimal programs would help.
Here is a Julia minimal program:
println("Hello, World!")
Julia its inherits its free program structuring more from Fortran and especially Python than C or Java. Here is a Python minimal program:
print("Hello, World!")
Here is a Fortran77 minimal program:
print *, "Hello, World!"
On the other hand, C requires, for the same function, a main() wrapper function and inclusion of code for printing to screen. Here is a C minimal program:
#include <stdio.h>
int main()
{
return printf("Hello, World!\n");
}
Java also requires that its main() function be wrapped within a user defined class. Here is a Java minimal program:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
So, Julia simply does not have certain structural requirements that you find well documented for the C or Java languages.

What does Qt Quick Compiler do exactly?

What does Qt Quick Compiler do exactly? My understanding was that it "compiles" QML/JS into C++ and integrates this into the final binary/executable. So, there is no JIT compilation or any other JS-related things during runtime.
However, I saw somewhere an article that claimed that it's not like this and actually it only "bundles" QML/JS into final binary/executable, but there is still some QML/JS-related overhead during runtime.
At the documentation page there is this explanation:
.qml files as well as accompanying .js files can be translated into
intermediate C++ source code. After compilation with a traditional
compiler, the code is linked into the application binary.
What is this "intermediate C++ source code"? Why not just "C++ source code"? That confuses me, but the last statement kinda promises that yes, it is a C++ code, and after compiling it with C++ compiler you will have a binary/executable without any additional compiling/interpretation during runtime.
Is it how it actually is?
The code is of an intermediate nature because it doesn't map Javascript directly to C++. E.g. var i = 1, j = 2, k = i+j is not translated to the C++ equivalent double i = 1., j = 2., k = i+j. Instead, the code is translated to a series of operations that directly manipulate the state of the JS virtual machine. JS semantics are not something you can get for free from C++: there will be runtime costs no matter how you implement it. There is no additional compiling nor interpretation, but the virtual machine that implements the JS state still has to exist.
That's not an overhead easy to get rid of without emitting a lot mostly dead code to cover all contexts in which a given piece of code might run, or doing just-in-time compilation that you wanted to avoid. That's the primary problem with JavaScript: its semantics are such that it's generally not possible to translate it to typical imperative statically typed code that gives rise to "standard" machine code.
Your question already contains the answer.
It compiles the code into C++, that is of intermediate nature as it is not enough to have C++-Code. You need binaries. So after the compilation to C++, the files are then compiled into binaries. Those are then linked.
The statement only says: We do not compile to binary, but to C++ instead. You need to compile it into a binary with your a C++-Compiler of your choice.
The bundeling happens, if you only put it into the resources (qrc-file). Putting it into the resources does not imply that you use the compiler.
Then there is the JIT compiler, that might (on supported platforms) do a Just-in-Time-Compilation. More on this here

export Qt function to Tcl

What is the best way to export the code written in Qt to the script language TCL. In the code Qt, I use the data structure in Qt like QMAP, QLIST other than those in STL, so the SWIG may not recognize them and neither some other macros in Qt.
You need a linking function with this signature:
extern "C" int theFunctionName(ClientData clientData, Tcl_Interp *interp,
int argc, char **argv)
which you register with Tcl_CreateCommand. (Or there's Tcl_CreateObjCommand, which uses a more efficient type management system, but in principle it's pretty similar.) This is what SWIG would construct for you, with lots of guidance, but it's not too hard to DIY and you'll probably end up with a better interface anyway. (SWIG's handling of enumerations and bit-sets is usually very unidiomatic from a Tcl perspective.)
That linking function has to decide the values in argv to create the values that are passed to your Qt/C++ code, and then it uses Tcl_SetResult or Tcl_SetObjResult to store the result back in the Tcl_Interp context before returning TCL_OK or TCL_ERROR (for success or an exception).
I guess a QMap would look like a Tcl dictionary, and a QList like a Tcl list, but the details could get quite tricky. The details depend on exactly what's going on (stuff like callbacks gets a little tricky, especially in threaded code!)

Qt: Is it possible to use mixins technique?

Qt library includes advanced meta-programming capabilities using they own preprocessing moc compiler. Does anyone knows, is it possible to create some kind of mix-ins via it? For example, i have a QString and want to add a method to it without sub-classing and changing existing code. Does Qt have such solutions for that?
I'm pretty sure that what the moc compiler isn't considered meta-programming according to the most common definition.
Furthermore, you can't add methods to a class using it.
Infact, in C++, you can never add methods to a class outside its declaration and moc (or any other QT utility) never actually touches the definition of the class. it only adds some meta-information to it and additional code which takes care of the signals and slots mechanism. This has very little to do with actual meta-programming.

Resources