Unverified Commit 16730fc6 authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #943

Fix compilation of the python binding and apply clang format
parents a685a67c 59f30d35
......@@ -18,12 +18,12 @@
#include "caf/config.hpp"
#include <set>
#include <chrono>
#include <thread>
#include <functional>
#include <iomanip>
#include <iostream>
#include <functional>
#include <set>
#include <thread>
#include <unordered_map>
CAF_PUSH_WARNINGS
......@@ -33,8 +33,8 @@ CAF_POP_WARNINGS
#include "caf/all.hpp"
#include "caf/io/all.hpp"
using std::cout;
using std::cerr;
using std::cout;
using std::endl;
namespace {
......@@ -91,15 +91,10 @@ def receive(timeout = None, msg_filter = no_receive_filter):
namespace caf {
void register_class(atom_value*, pybind11::module& m,
const std::string& name) {
auto repr_fun = [](atom_value x) {
return "atom('" + to_string(x) + "')";
};
auto cmp = [](atom_value x, atom_value y) {
return x == y;
};
std::string (*to_string_fun)(const atom_value&) = &to_string;
void register_class(atom_value*, pybind11::module& m, const std::string& name) {
auto repr_fun = [](atom_value x) { return "atom('" + to_string(x) + "')"; };
auto cmp = [](atom_value x, atom_value y) { return x == y; };
std::string (*to_string_fun)(atom_value) = &to_string;
pybind11::class_<atom_value>(m, name.c_str())
.def("__str__", to_string_fun)
.def("__repr__", repr_fun)
......@@ -112,13 +107,12 @@ namespace {
class binding {
public:
binding(std::string py_name, bool builtin_type)
: python_name_(std::move(py_name)),
builtin_(builtin_type) {
: python_name_(std::move(py_name)), builtin_(builtin_type) {
// nop
}
virtual ~binding() {
//nop
// nop
}
inline void docstring(std::string x) {
......@@ -201,13 +195,14 @@ template <class T>
class has_register_class {
private:
template <class U>
static auto test(U* x) -> decltype(register_class(x,
std::declval<pybind11::module&>(),
static auto test(U* x)
-> decltype(register_class(x, std::declval<pybind11::module&>(),
std::declval<const std::string&>()));
static auto test(...) -> std::false_type;
using type = decltype(test(static_cast<T*>(nullptr)));
public:
static constexpr bool value = std::is_same<type, void>::value;
};
......@@ -221,39 +216,29 @@ private:
static auto test(...) -> void;
using type = decltype(test(static_cast<T*>(nullptr)));
public:
static constexpr bool value = std::is_same<type, std::string>::value;
};
template <class T>
typename std::enable_if<
!has_register_class<T>::value
&& has_to_string<T>::value
>::type
typename std::enable_if<!has_register_class<T>::value
&& has_to_string<T>::value>::type
default_python_class_init(pybind11::module& m, const std::string& name) {
auto str_impl = [](const T& x) {
return to_string(x);
};
pybind11::class_<T>(m, name.c_str())
.def("__str__", str_impl);
auto str_impl = [](const T& x) { return to_string(x); };
pybind11::class_<T>(m, name.c_str()).def("__str__", str_impl);
}
template <class T>
typename std::enable_if<
!has_register_class<T>::value
&& !has_to_string<T>::value
>::type
typename std::enable_if<!has_register_class<T>::value
&& !has_to_string<T>::value>::type
default_python_class_init(pybind11::module& m, const std::string& name) {
auto str_impl = [](const T& x) {
return to_string(x);
};
auto str_impl = [](const T& x) { return to_string(x); };
pybind11::class_<T>(m, name.c_str());
}
template <class T>
typename std::enable_if<
has_register_class<T>::value
>::type
typename std::enable_if<has_register_class<T>::value>::type
default_python_class_init(pybind11::module& m, const std::string& name) {
register_class(static_cast<T*>(nullptr), m, name);
}
......@@ -269,7 +254,8 @@ public:
absolute_receive_timeout() = default;
absolute_receive_timeout(const absolute_receive_timeout&) = default;
absolute_receive_timeout& operator=(const absolute_receive_timeout&) = default;
absolute_receive_timeout& operator=(const absolute_receive_timeout&)
= default;
const clock_type::time_point& value() const {
return x_;
......@@ -307,7 +293,8 @@ public:
std::string pre_run;
std::string banner = default_banner;
using register_fun = std::function<void (pybind11::module&, const std::string&)>;
using register_fun = std::function<void(pybind11::module&,
const std::string&)>;
py_config() {
// allow CAF to convert native Python types to C++ types
......@@ -359,9 +346,7 @@ public:
oss << "import IPython" << endl
<< "c = IPython.Config()" << endl
<< "c.InteractiveShellApp.exec_lines = [" << endl
<< R"(""")"
<< full_pre_run
<< R"(""")" << endl
<< R"(""")" << full_pre_run << R"(""")" << endl
<< "]" << endl
<< "c.PromptManager.in_template = ' $: '" << endl
<< "c.PromptManager.in2_template = ' -> '" << endl
......@@ -378,7 +363,8 @@ public:
return bindings_;
}
const std::unordered_map<std::string, cpp_binding*>& portable_bindings() const {
const std::unordered_map<std::string, cpp_binding*>&
portable_bindings() const {
return portable_bindings_;
}
......@@ -417,7 +403,7 @@ private:
std::unordered_map<std::string, cpp_binding_ptr> cpp_bindings_;
std::unordered_map<std::string, py_binding_ptr> py_bindings_;
std::vector<std::function<void (pybind11::module&)>> register_funs_;
std::vector<std::function<void(pybind11::module&)>> register_funs_;
};
struct py_context {
const py_config& cfg;
......@@ -440,7 +426,6 @@ void set_py_exception_fill(std::ostream& oss, T&& x, Ts&&... xs) {
set_py_exception_fill(oss << std::forward<T>(x), std::forward<Ts>(xs)...);
}
template <class... Ts>
void set_py_exception(Ts&&... xs) {
std::ostringstream oss;
......@@ -462,8 +447,8 @@ void py_send(const pybind11::args& xs) {
std::string type_name = PyEval_GetFuncName((*i).ptr());
auto kvp = bindings.find(type_name);
if (kvp == bindings.end()) {
set_py_exception(R"(Unable to add element of type ")",
type_name, R"(" to message: type is unknown to CAF)");
set_py_exception(R"(Unable to add element of type ")", type_name,
R"(" to message: type is unknown to CAF)");
return;
}
kvp->second->append(mb, *i);
......@@ -480,8 +465,9 @@ pybind11::tuple tuple_from_message(const type_erased_tuple& msg) {
auto rtti = msg.type(i);
auto str = types.portable_name(rtti);
if (str == types.default_type_name()) {
set_py_exception("Unable to extract element #", i, " from message: ",
"could not get portable name of ", rtti.second->name());
set_py_exception("Unable to extract element #", i,
" from message: ", "could not get portable name of ",
rtti.second->name());
return pybind11::tuple{};
}
auto kvp = bindings.find(str);
......@@ -534,8 +520,8 @@ struct foo {
template <class Processor>
void serialize(Processor& proc, foo& f, const unsigned int) {
proc & f.x;
proc & f.y;
proc& f.x;
proc& f.y;
}
std::string to_string(const foo& x) {
......@@ -554,11 +540,11 @@ void register_class(foo*, pybind11::module& m, const std::string& name) {
}
#if PY_MAJOR_VERSION == 3
#define CAF_MODULE_INIT_RES PyObject*
#define CAF_MODULE_INIT_RET(res) return res;
# define CAF_MODULE_INIT_RES PyObject*
# define CAF_MODULE_INIT_RET(res) return res;
#else
#define CAF_MODULE_INIT_RES void
#define CAF_MODULE_INIT_RET(unused)
# define CAF_MODULE_INIT_RES void
# define CAF_MODULE_INIT_RET(unused)
#endif
CAF_MODULE_INIT_RES caf_module_init() {
......@@ -568,7 +554,8 @@ CAF_MODULE_INIT_RES caf_module_init() {
// add free functions
m.def("send", &py_send, "Sends a message to an actor")
.def("dequeue_message", &py_dequeue, "Receives the next message")
.def("dequeue_message_with_timeout", &py_dequeue_with_timeout, "Receives the next message")
.def("dequeue_message_with_timeout", &py_dequeue_with_timeout,
"Receives the next message")
.def("self", &py_self, "Returns the global self handle")
.def("atom", &atom_from_string, "Creates an atom from a string");
CAF_MODULE_INIT_RET(m.ptr())
......@@ -605,7 +592,7 @@ void caf_main(actor_system& system, const config& cfg) {
// create Python module for CAF
int py_res = 0;
if (!cfg.py_file.empty()) {
auto fp = fopen(cfg.py_file.c_str() , "r");
auto fp = fopen(cfg.py_file.c_str(), "r");
if (fp == nullptr) {
cerr << "Unable to open file " << cfg.py_file << endl;
Py_Finalize();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment