Skip to content

Commit 5aa7183

Browse files
committed
first version that compiles but does not create the api yet and crashes at runtime
1 parent 6831660 commit 5aa7183

File tree

11 files changed

+199
-0
lines changed

11 files changed

+199
-0
lines changed

scripter-api/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.4)
2+
project(scripting VERSION 1.0 LANGUAGES CXX)
3+
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
4+
5+
set(PYBIND11_PYTHON_VERSION 3.4)
6+
7+
find_package(pybind11 CONFIG)
8+
9+
include_directories(src)
10+
11+
add_executable(scripting
12+
src/main.cpp
13+
src/scripter.cpp
14+
)
15+
target_link_libraries(scripting
16+
pybind11::embed
17+
)
18+
19+
PYBIND11_ADD_MODULE(scripterapi
20+
src/scripterAPI/scripterAPI.cpp
21+
# src/scripterAPI/document.cpp
22+
# src/scripterAPI/margin.cpp
23+
)

scripter-api/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Providing an API for manipuling a document
2+
3+
Demoing how to provide an API that let's a Python script manipulate a document strucuture in the c++ application.
4+
5+
- The application is called "Sample".
6+
- Its API for Python is called "ScripterAPI"
7+
- The `Document` data structure (and its "sub" elements) contains the data of the c++ appplication.
8+
- From Python you import a module called "Sample" and access the document as `Sample.document`
9+
10+
~~~.sh
11+
$ mkdir build
12+
$ cmake -Dpybind11_DIR=/home/ale/bin/pybind11/share/cmake/pybind11 ..
13+
$ make
14+
$ ./scripting
15+
~~~

scripter-api/python/set-bar.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scripterapi
2+
3+
# print(Sample.margin.top)
4+
# Sample.margin.top = 150
5+
# print(Sample.margin.top)
6+
print(Sample.document.a)
7+
print(Sample.a)

scripter-api/src/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <pybind11/pybind11.h>
3+
#include <pybind11/embed.h>
4+
5+
#include "sample/document.h"
6+
// #include "sample/margin.h"
7+
//
8+
#include "scripter.h"
9+
10+
namespace py = pybind11;
11+
12+
int main()
13+
{
14+
Sample::Document document;
15+
16+
Scripter scripter{document};
17+
scripter.runFile("../python/set-bar.py");
18+
// std::cout << "document a " << document.a << std::endl;
19+
std::cout << "document top " << document.margin.top << std::endl;
20+
// std::cout << "document page 1 top" document.page.at(0).margin.top << std::endl;
21+
}
22+

scripter-api/src/sample/document.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef SAMPLEDOCUMENT_H
2+
#define SAMPLEDOCUMENT_H
3+
#include <vector>
4+
#include "margin.h"
5+
#include "page.h"
6+
namespace Sample
7+
{
8+
class Document
9+
{
10+
public:
11+
int a{0};
12+
Margin margin;
13+
std::vector<Page> page;
14+
};
15+
16+
}
17+
#endif

scripter-api/src/sample/margin.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef SAMPLEDMARGIN_H
2+
#define SAMPLEDMARGIN_H
3+
4+
namespace Sample
5+
{
6+
class Margin
7+
{
8+
public:
9+
double top{0};
10+
double bottom{0};
11+
double left{0};
12+
double right{0};
13+
void set(double t, double r, double b, double l) {
14+
top = t;
15+
right = r;
16+
bottom = b;
17+
left = l;
18+
}
19+
};
20+
}
21+
#endif

scripter-api/src/sample/page.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef SAMPLEPAGE_H
2+
#define SAMPLEPAGE_H
3+
#include "margin.h"
4+
namespace Sample
5+
{
6+
class Page
7+
{
8+
public:
9+
Margin margin;
10+
};
11+
12+
}
13+
#endif
14+

scripter-api/src/scripter.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <memory>
2+
#include "scripter.h"
3+
#include "scripterAPI/scripterAPI.h"
4+
#include "sample/document.h"
5+
6+
using namespace pybind11::literals; // for the ""_a
7+
8+
void Scripter::runFile(std::string fileName)
9+
{
10+
// Sample::Document document;
11+
//Sample::Margin saMargin;
12+
auto document = std::make_unique<Sample::Document>();
13+
14+
ScripterAPI::ScripterAPI scripterAPI;
15+
scripterAPI.setDocument(std::move(document));
16+
17+
18+
auto module = py::module::import("scripterapi");
19+
20+
auto locals = py::dict("Sample"_a=py::cast(scripterAPI, py::return_value_policy::reference));
21+
22+
py::eval_file(fileName, py::globals(), locals);
23+
24+
document = scripterAPI.getDocument();
25+
26+
// assert(scripter.bar == 5);
27+
}

scripter-api/src/scripter.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef SCRIPTER_H
2+
#define SCRIPTER_H
3+
#include <string>
4+
5+
#include <pybind11/pybind11.h>
6+
#include <pybind11/embed.h>
7+
8+
#include "sample/document.h"
9+
10+
namespace py = pybind11;
11+
12+
class Scripter
13+
{
14+
public:
15+
Scripter(Sample::Document &document) :
16+
document{document} {};
17+
void runFile(std::string fileName);
18+
private:
19+
Sample::Document &document;
20+
21+
py::scoped_interpreter guard{};
22+
};
23+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <pybind11/pybind11.h>
2+
3+
#include "scripterAPI/scripterAPI.h"
4+
5+
namespace py = pybind11;
6+
7+
PYBIND11_MODULE(scripterapi, m) {
8+
py::class_<ScripterAPI::ScripterAPI>(m, "Sample")
9+
.def(py::init<>())
10+
.def_readwrite("a", &ScripterAPI::ScripterAPI::a)
11+
;
12+
}

0 commit comments

Comments
 (0)