Integrate a python scripting console for a C++ library - c++

I have a library with classes and public methods written in C++.
I would like, from inside an interactive program written in C++ and Qt, to send commands to a parser in Python language which in turn converts them to call to methods and functions of my library.
Something similar to what is done in Octave/Matlab, a string is processed by a parser which then executes internally the commands.
Somewhere in my C++ library I have a function
int myFooCPPfunction(int value)
{
return value*value;
}
then during the execution of my program, I want to start a console and type in Python syntax:
for i in range(0,20):
print("%d" % myFooCPPfunction(i))
The command I gave then updates the internal state of my program for example.
I think it is a matter of writing Python code that links to C++. It seems to me that things like boost::python already do it...I ask your suggestion on which is the better way to write the bindings.
Second point: how to integrate that thing in an interactive shell launched from a Qt application?
Some online projects like QConsole should do something similar, but QConsole appears to be a very outdated project.
Thank you!

PythonQt was created for exactly this purpose.

Related

How can I get return data when using command line commands?

I inherited a c++ application that runs some commands using 'ShellExecuteEx', specifically using appcmd.exe. I would like to be able to run the command and then get the return data as I would see it if I ran it from the commandline.
I don't necessarily want to use ShellExecuteEx, but if I don't use that I need to run something pretty simple, as I am not used to c++ programming. My background is more of C# and similar languages.

Attach interactive console to embedded python script

I have a Python Script that I run from a C++ GUI Application.
I want to get the output of that Script into a Python Console and have the ability to manipulate them before calling another Python Function from C++.
My Question: Is that possible by just redirection stdin & stdout to Files?
Is there a better way using pure python?
Please note that I donĀ“t want to spawn the console from the C++ Programm but from outside the C++ Programm.
You should be able to adapt the approach in this answer to your needs. The example it links to uses UDP sockets for transferring commands to/from the interactive interpreter, but you could easily change that to pull data from stdin (or wherever) instead.
The key thing to take away from the example is the use of the builtin InteractiveConsole's push() method to determine whether the input is:
Well-formed Python snippet that may be evaluated as-is
A syntactically invalid snippet, or
A snippet that may become valid, but more input is needed

Need guidance in non C/C++ based graphics interface

I have prepared a program launcher in C++ (the only language I know), which accepts strings as program trigger.
But however hard I try I'm unable to give it a GUI like look; using graphics.h's graph functions creates graphics but in cmd like window.
All I need is a small widget/window like thing that sits on the desktop where a user can type the command string (and error messages can be displayed as usual).
Can I get a code snippet for such a widget/window?
The input string may be copied to a .txt file and my prog will read it from there.
P.S.- I'm a beginner & rookie so please avoid complex alternatives.
Well, one of the easiest options in my opinion is to use .NET Framework:
http://10rem.net/blog/2010/03/04/my-first-windows-cplusplus-application-in-ages-hello-world-in-win32-with-visual-cplusplus-2010
http://msdn.microsoft.com/en-us/library/bb384845.aspx

writing pexpect like program in c++ on Linux

Is there any way of writing pexpect like small program which can launch a process and pass the password to that process?
I don't want to install and use pexpect python library but want to know the logic behind it so that using linux system apis I can build something similar.
You could just use "expect". It is very light weight and is made to do what youre describing.
For very simple cases, empty is one option. It's a lightweight C program, and it can be used straight from a shell script and doesn't require Tcl.
For Debian/Ubuntu, the package is empty-expect.

Embedding a Ruby interpreter in a C++ app

I'm hoping to use Ruby as a scripting language for my game engine. I've found the usual articles describing how to call Ruby classes from C++ code and vice versa (e.g. here) but I can't quite see how to do what I want with that way of working...
My engine currently uses a little language I wrote myself with Flex and Bison, and a little stack based virtual machine. Scripts don't always run right through from start to finish, for instance they sometimes includes commands like "sleep for 2 seconds" or "wait until character has finished walking", so the scheduler keeps tabs on the status of each script and an instruction pointer, and knows when to resume them, and so on.
So it seems that I really need some kind of embedded Ruby interpreter that I can exercise a certain degree of control over, rather than simply calling Ruby methods. Or am I just being obtuse and missing something?
I'm working in Microsoft Visual C++, so ideally any solution would compile nice and easily in that.
Here's an example including error handling.
#include <iostream>
#include <ruby.h>
using namespace std;
int main(void)
{
ruby_init();
ruby_init_loadpath();
int status;
rb_load_protect(rb_str_new2("./test.rb"), 0, &status);
if (status) {
VALUE rbError = rb_funcall(rb_gv_get("$!"), rb_intern("message"), 0);
cerr << StringValuePtr(rbError) << endl;
};
ruby_finalize();
return status;
}
You're on the right track. The key is to do something similar to the section on Embedding Concepts in the link you posted. In simple terms it's little more than:
ruby_init();
ruby_script("some_script");
You may need to copy over all the #ifdef stuff from main.c to get everything working. From then it's a matter of building an API to your C++ functions you want to expose, and depending on your design, multi-threading the thing.
You could always re-design the way the scripts work to suit the script engine rather than trying to get the engine to work with the original scripting paradigms.
So, if you had this:
proc:
action 1
action 2
sleep a bit
action 3
end
which would require the script to be suspended on the sleep line, do this:
proc
action1
action2
set timer (time, callback_proc)
end
callback_proc
action3
end
which lets the scripting engine finish each method neatly. It wouldn't need many changes to the hosting side - each version requires some form of event system which can restart the script engine.
There is a guide about how to embed ruby into a C++ application. That may be helpful. Otherwise go to the Ruby documentation.
The Embed Ruby in C article may be helpful, too.
I think what your going to want to do is use Ruby's threads. I've done a fair bit of digging through the Ruby threading code and I know that (where pthreads is available) sleep is implemented in a non-blocking fashion using pthread_cond_timedwait. This unblocks the interpreter so that other threads can continue execution.
Your own code is going to have to respect Ruby's threads when it comes to interacting with the Ruby interpreter. They've still got a Global VM Lock in Ruby which means you should be careful about modifying anything in the interpreter without having the lock yourself.