Skip to content

Commit

Permalink
feat: replace py::class_ with py_class helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 6, 2024
1 parent 9cf94b2 commit 9c0e1c5
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

namespace endstone::detail {

template <typename Type, typename... Options>
pybind11::class_<Type, Options...> py_class(pybind11::handle scope, const char *name)
template <typename Type, typename... Options, typename... Extra>
pybind11::class_<Type, Options...> py_class(pybind11::handle scope, const char *name, Extra &&...extra)
{
static pybind11::class_<Type, Options...> instance = pybind11::class_<Type, Options...>(scope, name);
static pybind11::class_<Type, Options...> instance =
pybind11::class_<Type, Options...>(scope, name, std::forward<Extra>(extra)...);
return instance;
}

Expand Down
2 changes: 1 addition & 1 deletion src/endstone_python/endstone_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ void init_util(py::module_ &);
PYBIND11_MODULE(endstone_python, m) // NOLINT(*-use-anonymous-namespace)
{
init_logger(m);
init_util(m);
init_server(m);
init_plugin(m);
init_util(m);
}

} // namespace endstone::detail
7 changes: 5 additions & 2 deletions src/endstone_python/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

#include <pybind11/pybind11.h>

#include "endstone/detail/python.h"

namespace py = pybind11;

namespace endstone::detail {

void init_logger(py::module &m)
{
auto logger = py::class_<Logger>(m, "Logger");
auto logger = py_class<Logger>(m, "Logger");

py::enum_<Logger::Level>(logger, "Level")
.value("TRACE", Logger::Level::Trace)
Expand All @@ -33,7 +35,8 @@ void init_logger(py::module &m)
.value("CRITICAL", Logger::Level::Critical)
.export_values();

logger.def("set_level", &Logger::setLevel, py::arg("level"))
py_class<Logger>(m, "Logger")
.def("set_level", &Logger::setLevel, py::arg("level"))
.def("is_enabled_for", &Logger::isEnabledFor, py::arg("level"))
.def(
"trace", [](const Logger &logger, const std::string &message) { logger.trace(message); }, py::arg("msg"))
Expand Down
9 changes: 5 additions & 4 deletions src/endstone_python/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "endstone/detail/python.h"
#include "endstone/logger.h"
#include "endstone/plugin/plugin_loader.h"
#include "endstone/plugin/plugin_manager.h"
#include "endstone/server.h"
#include "forward.h"

namespace py = pybind11;

Expand Down Expand Up @@ -107,7 +107,7 @@ class PyPluginLoader : public PluginLoader {

void init_plugin(py::module &m)
{
py::class_<PluginDescription>(m, "PluginDescription")
py_class<PluginDescription>(m, "PluginDescription")
.def(py::init(&createPluginDescription), py::arg("name"), py::arg("version"),
py::arg("description") = py::none(), py::arg("authors") = py::none(), py::arg("prefix") = py::none())
.def_property_readonly("name", &PluginDescription::getName)
Expand All @@ -119,7 +119,7 @@ void init_plugin(py::module &m)

py_class<PluginLoader, PyPluginLoader>(m, "PluginLoader");

py::class_<Plugin, PyPlugin>(m, "Plugin")
py_class<Plugin, PyPlugin>(m, "Plugin")
.def(py::init<>())
.def("on_load", &Plugin::onLoad)
.def("on_enable", &Plugin::onEnable)
Expand All @@ -131,7 +131,8 @@ void init_plugin(py::module &m)
.def_property_readonly("enabled", &Plugin::isEnabled)
.def_property_readonly("name", &Plugin::getName);

py_class<PluginLoader, PyPluginLoader>(m, "PluginLoader").def(py::init<Server &>(), py::arg("server"))
py_class<PluginLoader, PyPluginLoader>(m, "PluginLoader")
.def(py::init<Server &>(), py::arg("server"))
.def("load_plugins", &PluginLoader::loadPlugins, py::arg("directory"),
py::return_value_policy::reference_internal)
.def("enable_plugin", &PluginLoader::enablePlugin, py::arg("plugin"))
Expand Down
4 changes: 2 additions & 2 deletions src/endstone_python/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "endstone/detail/python.h"
#include "endstone/logger.h"
#include "endstone/plugin/plugin_manager.h"
#include "forward.h"

namespace py = pybind11;

Expand All @@ -29,7 +29,7 @@ void init_server(py::module &m)
{
py_class<PluginManager>(m, "PluginManager");

py::class_<Server>(m, "Server")
py_class<Server>(m, "Server")
.def_property_readonly("logger", &Server::getLogger, py::return_value_policy::reference)
.def_property_readonly("plugin_manager", &Server::getPluginManager, py::return_value_policy::reference)
.def_property_readonly("name", &Server::getVersion)
Expand Down
3 changes: 2 additions & 1 deletion src/endstone_python/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <pybind11/pybind11.h>

#include "endstone/detail/python.h"
#include "endstone/util/color_format.h"

namespace py = pybind11;
Expand All @@ -24,7 +25,7 @@ namespace endstone::detail {

void init_util(py::module &m)
{
py::class_<ColorFormat>(m, "ColorFormat")
py_class<ColorFormat>(m, "ColorFormat")
.ADD_COLOR_FORMAT(BLACK)
.ADD_COLOR_FORMAT(DARK_BLUE)
.ADD_COLOR_FORMAT(DARK_GREEN)
Expand Down

0 comments on commit 9c0e1c5

Please sign in to comment.