Knife is a simple, elegant, very convenient C++11 Syntactic-Sugar Lib, for mixed-language programming with Python, SQLite and Bash. It was developed with a focus on enabling simple and natural way to mixed-language programming. enjoy the advantages of those language directly.
Use Knife if you need a mixed-language programming library that:
- Can write those languages directly without trivial work like initialization and finalization
- Supports variable number of arguments and formatted string mechanism to avoid
sprintf(...)
works - Manipulate variables nicely, as long as variable names are same in C++ and those languages
- Cross platform support for multithreading acceleration with a handy speed-up interface
- Extendible to write your own language environment like Lua, R, Matlab, etc.
Documentation are still writing(sorry for my busy life).
Knife is compatible with: Python / Anaconda, Bash / Bourne shell.
$bash, $py, and $sql are singletons and functors, which restrict the instantiation of a class to one object and overload the function operator()
.
They can act like a function, for example void $bash(const char* cmd, ...)
(but not a function), that take the command(or command's format_placeholder) as input and execute it, we call them Environments, which is extendible to other languages(we will talk about it later)
#include <knife.hpp>
int main() {
printf("\nDemo of $bash\n");
$bash("mkdir hello"); // execute commands in the bash environment
$bash("ls | grep ell");// "ell" in "hello"
$bash("rm -r hello");
printf("\nDemo of $py\n");
$py("msg, pi = %s, %f", "\'hello\'", 3.1415926f); // Formatted String
$py("print (msg)");
$py_get(float, pi); // Same Variable Name: pi
std::cout << pi << std::endl;
printf("\nDemo of $sql\n");
$sql("CREATE TABLE Foo(ID INTEGER, Msg TEXT);");
int ID = 1;
const char *Msg = "hello";
$sql.insert_easily("Foo", ID, Msg); // Same Variable Name:ID, Msg
$sql("SELECT * FROM Foo;");
std::cout << $sql.query_result()[0][1] << std::endl;
$sql("DROP TABLE Foo;");
return 0;
}
the output of those code will be:
Outputs of C++(like ptintf / std::cout) are white
Outputs of Environments are yellow
Prompts have different color corespond to the language
Commands that the Environments execute are blue
let's explain those codes:
$bash("mkdir hello"); // execute commands in the bash environment
$bash("ls | grep ell");// "ell" in "hello"
$bash("rm -r hello");
firstly, $bash("mkdir hello");
means the $bash singletons functors takes the string "mkdir hello"
as input, then mkdir will be executed in the your Bash Environment, which makes a directory in your ./
path.
then ls lists the files in the current working directory, grep the "ell" from the output of ls, rm
$py("msg, pi = %s, %f", "\'hello\'", 3.1415926f); // Formatted String
$py("print (msg)");
$py_get(float, pi); // Same Variable Name: pi
std::cout << pi << std::endl;
$sql("CREATE TABLE Foo(ID INTEGER, Msg TEXT);");
int ID = 1;
const char *Msg = "hello";
$sql.insert_easily("Foo", ID, Msg); // Same Variable Name:ID, Msg
$sql("SELECT * FROM Foo;");
std::cout << $sql.query_result()[0][1] << std::endl;
$sql("DROP TABLE Foo;");
TODO gcc 4.9 clang x.x C++ 11
TODO
TODO Why bash sqlite and python? lua matlab, may be supported later
this style of readme file imitates the readme of Keras, which is one of my favorite project for it's easy, friendly, handy usage ^_^