Skip to content

Commit

Permalink
register_string_with_delimiter function
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-urban committed Nov 28, 2024
1 parent aff412c commit 3ad47d5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 15 deletions.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
52 changes: 42 additions & 10 deletions src/pymodule/classhelper/c_objectprinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,43 @@ void init_c_objectprinter(pybind11::module& m)
py::arg("pos") = -1)
.def(
"register_optional_value",
py::overload_cast<const std::string&, std::optional<double>, std::string_view, int>(
&ObjectPrinter::register_optional_value<double>),
py::overload_cast<const std::string&,
std::optional<double>,
std::string_view,
std::string_view,
int>(&ObjectPrinter::register_optional_value<double>),
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<const std::string&, std::optional<int>, std::string_view, int>(
&ObjectPrinter::register_optional_value<int>),
py::overload_cast<const std::string&,
std::optional<int>,
std::string_view,
std::string_view,
int>(&ObjectPrinter::register_optional_value<int>),
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<const std::string&,
std::optional<std::string>,
std::string_view,
std::string_view,
int>(&ObjectPrinter::register_optional_value<std::string>),
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),
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//sourcehash: bb934fd819aa8d352a48df0ddf58799374ced0b1bd5529d1beb2bf77428d61a4
//sourcehash: e84430c2e53dad0f9e45166f9e70dd867396c97d3cad782004c83e54f3deef90

/*
This file contains docstrings for use in the Python bindings.
Expand Down Expand Up @@ -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
Expand Down
31 changes: 28 additions & 3 deletions src/themachinethatgoesping/tools/classhelper/objectprinter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,14 @@ class ObjectPrinter
template<typename t_value>
void register_optional_value(const std::string& name,
std::optional<t_value> 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);
}

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3ad47d5

Please sign in to comment.