From 3ad47d5954fff27123d90f18c70a84d3750954ff Mon Sep 17 00:00:00 2001 From: Peter Urban Date: Thu, 28 Nov 2024 22:37:07 +0100 Subject: [PATCH] register_string_with_delimiter function --- meson.build | 2 +- src/pymodule/classhelper/c_objectprinter.cpp | 52 +++++++++++++++---- .../.docstrings/objectprinter.doc.hpp | 27 +++++++++- .../tools/classhelper/objectprinter.hpp | 31 +++++++++-- 4 files changed, 97 insertions(+), 15 deletions(-) diff --git a/meson.build b/meson.build index ff48a69..3ab51e5 100644 --- a/meson.build +++ b/meson.build @@ -9,7 +9,7 @@ project( 'cpp', license: 'MPL-2.0', - version: '0.28.1', + version: '0.28.2', default_options: ['warning_level=2', 'buildtype=release', 'cpp_std=c++20'], meson_version: '>=1.3.2' #first version with clang-cl openmp support, ) diff --git a/src/pymodule/classhelper/c_objectprinter.cpp b/src/pymodule/classhelper/c_objectprinter.cpp index d0c2a2e..6464e2a 100644 --- a/src/pymodule/classhelper/c_objectprinter.cpp +++ b/src/pymodule/classhelper/c_objectprinter.cpp @@ -59,33 +59,43 @@ void init_c_objectprinter(pybind11::module& m) py::arg("pos") = -1) .def( "register_optional_value", - py::overload_cast, std::string_view, int>( - &ObjectPrinter::register_optional_value), + py::overload_cast, + std::string_view, + std::string_view, + int>(&ObjectPrinter::register_optional_value), DOC(themachinethatgoesping, tools, classhelper, ObjectPrinter, register_optional_value), py::arg("name"), py::arg("value"), - py::arg("value_info") = "", - py::arg("pos") = -1) + py::arg("value_info") = "", + py::arg("optional_value") = "Not set", + py::arg("pos") = -1) .def( "register_optional_value", - py::overload_cast, std::string_view, int>( - &ObjectPrinter::register_optional_value), + py::overload_cast, + std::string_view, + std::string_view, + int>(&ObjectPrinter::register_optional_value), DOC(themachinethatgoesping, tools, classhelper, ObjectPrinter, register_optional_value), py::arg("name"), py::arg("value"), - py::arg("value_info") = "", - py::arg("pos") = -1) + py::arg("value_info") = "", + py::arg("optional_value") = "Not set", + py::arg("pos") = -1) .def( "register_optional_value", py::overload_cast, std::string_view, + std::string_view, int>(&ObjectPrinter::register_optional_value), DOC(themachinethatgoesping, tools, classhelper, ObjectPrinter, register_optional_value), py::arg("name"), py::arg("value"), - py::arg("value_info") = "", - py::arg("pos") = -1) + py::arg("value_info") = "", + py::arg("optional_value") = "Not set", + py::arg("pos") = -1) .def("register_value_bytes", &ObjectPrinter::register_value_bytes, DOC(themachinethatgoesping, tools, classhelper, ObjectPrinter, register_value_bytes), @@ -120,6 +130,28 @@ void init_c_objectprinter(pybind11::module& m) py::arg("value"), py::arg("value_info") = "", py::arg("pos") = -1) + .def("register_string", + &ObjectPrinter::register_string, + DOC(themachinethatgoesping, tools, classhelper, ObjectPrinter, register_string), + py::arg("name"), + py::arg("value"), + py::arg("value_info") = "", + py::arg("pos") = -1, + py::arg("max_visible_elements") = 0) + .def("register_string_with_delimiters", + &ObjectPrinter::register_string_with_delimiters, + DOC(themachinethatgoesping, + tools, + classhelper, + ObjectPrinter, + register_string_with_delimiters), + py::arg("name"), + py::arg("value"), + py::arg("value_info") = "", + py::arg("delimiter_left") = "\"", + py::arg("delimiter_right") = "\"", + py::arg("pos") = -1, + py::arg("max_visible_elements") = 0) .def("register_section", &ObjectPrinter::register_section, DOC(themachinethatgoesping, tools, classhelper, ObjectPrinter, register_section), diff --git a/src/themachinethatgoesping/tools/classhelper/.docstrings/objectprinter.doc.hpp b/src/themachinethatgoesping/tools/classhelper/.docstrings/objectprinter.doc.hpp index 185ca31..8233840 100644 --- a/src/themachinethatgoesping/tools/classhelper/.docstrings/objectprinter.doc.hpp +++ b/src/themachinethatgoesping/tools/classhelper/.docstrings/objectprinter.doc.hpp @@ -1,4 +1,4 @@ -//sourcehash: bb934fd819aa8d352a48df0ddf58799374ced0b1bd5529d1beb2bf77428d61a4 +//sourcehash: e84430c2e53dad0f9e45166f9e70dd867396c97d3cad782004c83e54f3deef90 /* This file contains docstrings for use in the Python bindings. @@ -243,6 +243,31 @@ Parameter ``pos``: Parameter ``max_visible_elements``: maximum of chars that are printed (if 0, all elements are printed))doc"; +static const char *__doc_themachinethatgoesping_tools_classhelper_ObjectPrinter_register_string_with_delimiters = +R"doc(register a formatted string field for printing, with delimiters + +Parameter ``name``: + name of the variable + +Parameter ``value``: + value of the variable + +Parameter ``delimiter_left``: + left delimiter + +Parameter ``delimiter_right``: + right delimiter + +Parameter ``value_info``: + additional info (is printed in [] behind the variable) + +Parameter ``pos``: + position where the value is registers (if negative, the value is + appended) + +Parameter ``max_visible_elements``: + maximum of chars that are printed (if 0, all elements are printed))doc"; + static const char *__doc_themachinethatgoesping_tools_classhelper_ObjectPrinter_register_value = R"doc(register a single integer of floating point value for printing diff --git a/src/themachinethatgoesping/tools/classhelper/objectprinter.hpp b/src/themachinethatgoesping/tools/classhelper/objectprinter.hpp index 1727549..30c834e 100644 --- a/src/themachinethatgoesping/tools/classhelper/objectprinter.hpp +++ b/src/themachinethatgoesping/tools/classhelper/objectprinter.hpp @@ -405,13 +405,14 @@ class ObjectPrinter template void register_optional_value(const std::string& name, std::optional value, - std::string_view value_info = "", - int pos = -1) + std::string_view value_info = "", + std::string_view optional_value = "Not set", + int pos = -1) { if (value.has_value()) register_value(name, value.value(), value_info, pos); else - register_string(name, "Not set", std::string(value_info), pos); + register_string(name, std::string(optional_value), std::string(value_info), pos); } /** @@ -697,6 +698,30 @@ class ObjectPrinter } } } + /** + * @brief register a formatted string field for printing, with delimiters + * + * @param name name of the variable + * @param value value of the variable + * @param delimiter_left left delimiter + * @param delimiter_right right delimiter + * @param value_info additional info (is printed in [] behind the variable) + * @param pos position where the value is registers (if negative, the value is appended) + * @param max_visible_elements maximum of chars that are printed (if 0, all elements are + * printed) + */ + void register_string_with_delimiters(const std::string& name, + std::string value, + std::string value_info = "", + std::string delimiter_left = "\"", + std::string delimiter_right = "\"", + int pos = -1, + size_t max_visible_elements = 0) + { + value = delimiter_left + value + delimiter_right; + + register_string(name, value, value_info, pos, max_visible_elements); + } /** * @brief register a formatted string field for printing