Commit 59f30d35 authored by Marcel Röthke's avatar Marcel Röthke

Apply clang-format to libcaf_python main.cpp

parent af202608
...@@ -18,12 +18,12 @@ ...@@ -18,12 +18,12 @@
#include "caf/config.hpp" #include "caf/config.hpp"
#include <set>
#include <chrono> #include <chrono>
#include <thread> #include <functional>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <functional> #include <set>
#include <thread>
#include <unordered_map> #include <unordered_map>
CAF_PUSH_WARNINGS CAF_PUSH_WARNINGS
...@@ -33,8 +33,8 @@ CAF_POP_WARNINGS ...@@ -33,8 +33,8 @@ CAF_POP_WARNINGS
#include "caf/all.hpp" #include "caf/all.hpp"
#include "caf/io/all.hpp" #include "caf/io/all.hpp"
using std::cout;
using std::cerr; using std::cerr;
using std::cout;
using std::endl; using std::endl;
namespace { namespace {
...@@ -91,19 +91,14 @@ def receive(timeout = None, msg_filter = no_receive_filter): ...@@ -91,19 +91,14 @@ def receive(timeout = None, msg_filter = no_receive_filter):
namespace caf { namespace caf {
void register_class(atom_value*, pybind11::module& m, void register_class(atom_value*, pybind11::module& m, const std::string& name) {
const std::string& name) { auto repr_fun = [](atom_value x) { return "atom('" + to_string(x) + "')"; };
auto repr_fun = [](atom_value x) { auto cmp = [](atom_value x, atom_value y) { return x == y; };
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; std::string (*to_string_fun)(atom_value) = &to_string;
pybind11::class_<atom_value>(m, name.c_str()) pybind11::class_<atom_value>(m, name.c_str())
.def("__str__", to_string_fun) .def("__str__", to_string_fun)
.def("__repr__", repr_fun) .def("__repr__", repr_fun)
.def("__eq__", cmp); .def("__eq__", cmp);
} }
namespace python { namespace python {
...@@ -112,13 +107,12 @@ namespace { ...@@ -112,13 +107,12 @@ namespace {
class binding { class binding {
public: public:
binding(std::string py_name, bool builtin_type) binding(std::string py_name, bool builtin_type)
: python_name_(std::move(py_name)), : python_name_(std::move(py_name)), builtin_(builtin_type) {
builtin_(builtin_type) {
// nop // nop
} }
virtual ~binding() { virtual ~binding() {
//nop // nop
} }
inline void docstring(std::string x) { inline void docstring(std::string x) {
...@@ -201,13 +195,14 @@ template <class T> ...@@ -201,13 +195,14 @@ template <class T>
class has_register_class { class has_register_class {
private: private:
template <class U> template <class U>
static auto test(U* x) -> decltype(register_class(x, static auto test(U* x)
std::declval<pybind11::module&>(), -> decltype(register_class(x, std::declval<pybind11::module&>(),
std::declval<const std::string&>())); std::declval<const std::string&>()));
static auto test(...) -> std::false_type; static auto test(...) -> std::false_type;
using type = decltype(test(static_cast<T*>(nullptr))); using type = decltype(test(static_cast<T*>(nullptr)));
public: public:
static constexpr bool value = std::is_same<type, void>::value; static constexpr bool value = std::is_same<type, void>::value;
}; };
...@@ -221,39 +216,29 @@ private: ...@@ -221,39 +216,29 @@ private:
static auto test(...) -> void; static auto test(...) -> void;
using type = decltype(test(static_cast<T*>(nullptr))); using type = decltype(test(static_cast<T*>(nullptr)));
public: public:
static constexpr bool value = std::is_same<type, std::string>::value; static constexpr bool value = std::is_same<type, std::string>::value;
}; };
template <class T> template <class T>
typename std::enable_if< typename std::enable_if<!has_register_class<T>::value
!has_register_class<T>::value && has_to_string<T>::value>::type
&& has_to_string<T>::value
>::type
default_python_class_init(pybind11::module& m, const std::string& name) { default_python_class_init(pybind11::module& m, const std::string& name) {
auto str_impl = [](const T& x) { auto str_impl = [](const T& x) { return to_string(x); };
return to_string(x); pybind11::class_<T>(m, name.c_str()).def("__str__", str_impl);
};
pybind11::class_<T>(m, name.c_str())
.def("__str__", str_impl);
} }
template <class T> template <class T>
typename std::enable_if< typename std::enable_if<!has_register_class<T>::value
!has_register_class<T>::value && !has_to_string<T>::value>::type
&& !has_to_string<T>::value
>::type
default_python_class_init(pybind11::module& m, const std::string& name) { default_python_class_init(pybind11::module& m, const std::string& name) {
auto str_impl = [](const T& x) { auto str_impl = [](const T& x) { return to_string(x); };
return to_string(x);
};
pybind11::class_<T>(m, name.c_str()); pybind11::class_<T>(m, name.c_str());
} }
template <class T> template <class T>
typename std::enable_if< typename std::enable_if<has_register_class<T>::value>::type
has_register_class<T>::value
>::type
default_python_class_init(pybind11::module& m, const std::string& name) { default_python_class_init(pybind11::module& m, const std::string& name) {
register_class(static_cast<T*>(nullptr), m, name); register_class(static_cast<T*>(nullptr), m, name);
} }
...@@ -269,7 +254,8 @@ public: ...@@ -269,7 +254,8 @@ public:
absolute_receive_timeout() = default; absolute_receive_timeout() = default;
absolute_receive_timeout(const 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 { const clock_type::time_point& value() const {
return x_; return x_;
...@@ -298,8 +284,8 @@ private: ...@@ -298,8 +284,8 @@ private:
void register_class(absolute_receive_timeout*, pybind11::module& m, void register_class(absolute_receive_timeout*, pybind11::module& m,
const std::string& name) { const std::string& name) {
pybind11::class_<absolute_receive_timeout>(m, name.c_str()) pybind11::class_<absolute_receive_timeout>(m, name.c_str())
.def(pybind11::init<>()) .def(pybind11::init<>())
.def(pybind11::init<int>()); .def(pybind11::init<int>());
} }
class py_config : public actor_system_config { class py_config : public actor_system_config {
...@@ -307,7 +293,8 @@ public: ...@@ -307,7 +293,8 @@ public:
std::string pre_run; std::string pre_run;
std::string banner = default_banner; 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() { py_config() {
// allow CAF to convert native Python types to C++ types // allow CAF to convert native Python types to C++ types
...@@ -359,9 +346,7 @@ public: ...@@ -359,9 +346,7 @@ public:
oss << "import IPython" << endl oss << "import IPython" << endl
<< "c = IPython.Config()" << endl << "c = IPython.Config()" << endl
<< "c.InteractiveShellApp.exec_lines = [" << endl << "c.InteractiveShellApp.exec_lines = [" << endl
<< R"(""")" << R"(""")" << full_pre_run << R"(""")" << endl
<< full_pre_run
<< R"(""")" << endl
<< "]" << endl << "]" << endl
<< "c.PromptManager.in_template = ' $: '" << endl << "c.PromptManager.in_template = ' $: '" << endl
<< "c.PromptManager.in2_template = ' -> '" << endl << "c.PromptManager.in2_template = ' -> '" << endl
...@@ -374,17 +359,18 @@ public: ...@@ -374,17 +359,18 @@ public:
return oss.str(); return oss.str();
} }
const std::unordered_map<std::string, binding*>& bindings() const { const std::unordered_map<std::string, binding*>& bindings() const {
return bindings_; return bindings_;
} }
const std::unordered_map<std::string, cpp_binding*>& portable_bindings() const { const std::unordered_map<std::string, cpp_binding*>&
return portable_bindings_; portable_bindings() const {
} return portable_bindings_;
}
const std::unordered_map<std::string, cpp_binding_ptr>& cpp_bindings() const { const std::unordered_map<std::string, cpp_binding_ptr>& cpp_bindings() const {
return cpp_bindings_; return cpp_bindings_;
} }
private: private:
template <class T> template <class T>
...@@ -417,7 +403,7 @@ private: ...@@ -417,7 +403,7 @@ private:
std::unordered_map<std::string, cpp_binding_ptr> cpp_bindings_; std::unordered_map<std::string, cpp_binding_ptr> cpp_bindings_;
std::unordered_map<std::string, py_binding_ptr> py_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 { struct py_context {
const py_config& cfg; const py_config& cfg;
...@@ -440,7 +426,6 @@ void set_py_exception_fill(std::ostream& oss, T&& x, Ts&&... xs) { ...@@ -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)...); set_py_exception_fill(oss << std::forward<T>(x), std::forward<Ts>(xs)...);
} }
template <class... Ts> template <class... Ts>
void set_py_exception(Ts&&... xs) { void set_py_exception(Ts&&... xs) {
std::ostringstream oss; std::ostringstream oss;
...@@ -462,8 +447,8 @@ void py_send(const pybind11::args& xs) { ...@@ -462,8 +447,8 @@ void py_send(const pybind11::args& xs) {
std::string type_name = PyEval_GetFuncName((*i).ptr()); std::string type_name = PyEval_GetFuncName((*i).ptr());
auto kvp = bindings.find(type_name); auto kvp = bindings.find(type_name);
if (kvp == bindings.end()) { if (kvp == bindings.end()) {
set_py_exception(R"(Unable to add element of type ")", set_py_exception(R"(Unable to add element of type ")", type_name,
type_name, R"(" to message: type is unknown to CAF)"); R"(" to message: type is unknown to CAF)");
return; return;
} }
kvp->second->append(mb, *i); kvp->second->append(mb, *i);
...@@ -476,12 +461,13 @@ pybind11::tuple tuple_from_message(const type_erased_tuple& msg) { ...@@ -476,12 +461,13 @@ pybind11::tuple tuple_from_message(const type_erased_tuple& msg) {
auto& bindings = s_context->cfg.portable_bindings(); auto& bindings = s_context->cfg.portable_bindings();
pybind11::tuple result(msg.size()); pybind11::tuple result(msg.size());
auto& types = self->system().types(); auto& types = self->system().types();
for (size_t i = 0; i < msg.size(); ++i) { for (size_t i = 0; i < msg.size(); ++i) {
auto rtti = msg.type(i); auto rtti = msg.type(i);
auto str = types.portable_name(rtti); auto str = types.portable_name(rtti);
if (str == types.default_type_name()) { if (str == types.default_type_name()) {
set_py_exception("Unable to extract element #", i, " from message: ", set_py_exception("Unable to extract element #", i,
"could not get portable name of ", rtti.second->name()); " from message: ", "could not get portable name of ",
rtti.second->name());
return pybind11::tuple{}; return pybind11::tuple{};
} }
auto kvp = bindings.find(str); auto kvp = bindings.find(str);
...@@ -534,8 +520,8 @@ struct foo { ...@@ -534,8 +520,8 @@ struct foo {
template <class Processor> template <class Processor>
void serialize(Processor& proc, foo& f, const unsigned int) { void serialize(Processor& proc, foo& f, const unsigned int) {
proc & f.x; proc& f.x;
proc & f.y; proc& f.y;
} }
std::string to_string(const foo& x) { std::string to_string(const foo& x) {
...@@ -545,20 +531,20 @@ std::string to_string(const foo& x) { ...@@ -545,20 +531,20 @@ std::string to_string(const foo& x) {
void register_class(foo*, pybind11::module& m, const std::string& name) { void register_class(foo*, pybind11::module& m, const std::string& name) {
std::string (*str_fun)(const foo&) = &to_string; std::string (*str_fun)(const foo&) = &to_string;
pybind11::class_<foo>(m, name.c_str()) pybind11::class_<foo>(m, name.c_str())
.def(pybind11::init<>()) .def(pybind11::init<>())
.def(pybind11::init<int, int>()) .def(pybind11::init<int, int>())
.def("__str__", str_fun) .def("__str__", str_fun)
.def("__repr__", str_fun) .def("__repr__", str_fun)
.def_readwrite("x", &foo::x) .def_readwrite("x", &foo::x)
.def_readwrite("y", &foo::y); .def_readwrite("y", &foo::y);
} }
#if PY_MAJOR_VERSION == 3 #if PY_MAJOR_VERSION == 3
#define CAF_MODULE_INIT_RES PyObject* # define CAF_MODULE_INIT_RES PyObject*
#define CAF_MODULE_INIT_RET(res) return res; # define CAF_MODULE_INIT_RET(res) return res;
#else #else
#define CAF_MODULE_INIT_RES void # define CAF_MODULE_INIT_RES void
#define CAF_MODULE_INIT_RET(unused) # define CAF_MODULE_INIT_RET(unused)
#endif #endif
CAF_MODULE_INIT_RES caf_module_init() { CAF_MODULE_INIT_RES caf_module_init() {
...@@ -567,10 +553,11 @@ CAF_MODULE_INIT_RES caf_module_init() { ...@@ -567,10 +553,11 @@ CAF_MODULE_INIT_RES caf_module_init() {
// add classes // add classes
// add free functions // add free functions
m.def("send", &py_send, "Sends a message to an actor") m.def("send", &py_send, "Sends a message to an actor")
.def("dequeue_message", &py_dequeue, "Receives the next message") .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,
.def("self", &py_self, "Returns the global self handle") "Receives the next message")
.def("atom", &atom_from_string, "Creates an atom from a string"); .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()) CAF_MODULE_INIT_RET(m.ptr())
} }
...@@ -590,7 +577,7 @@ public: ...@@ -590,7 +577,7 @@ public:
config() { config() {
add_message_type<foo>("foo"); add_message_type<foo>("foo");
opt_group{custom_options_, "python"} opt_group{custom_options_, "python"}
.add(py_file, "file,f", "Run script instead of interactive shell."); .add(py_file, "file,f", "Run script instead of interactive shell.");
} }
}; };
...@@ -605,7 +592,7 @@ void caf_main(actor_system& system, const config& cfg) { ...@@ -605,7 +592,7 @@ void caf_main(actor_system& system, const config& cfg) {
// create Python module for CAF // create Python module for CAF
int py_res = 0; int py_res = 0;
if (!cfg.py_file.empty()) { 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) { if (fp == nullptr) {
cerr << "Unable to open file " << cfg.py_file << endl; cerr << "Unable to open file " << cfg.py_file << endl;
Py_Finalize(); 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