xo-alloc / xo-object: utest coverage + assorted bugfixes

This commit is contained in:
Roland Conybeare 2025-08-07 18:32:14 -05:00
commit f411b2683c
4 changed files with 56 additions and 17 deletions

View file

@ -144,7 +144,8 @@ namespace xo {
class color_impl {
public:
color_impl(coloring_control_flags flags, color_spec_type spec, Contents && contents)
: flags_{flags}, spec_{spec}, contents_{std::forward<Contents>(contents)} {}
: flags_{flags},
spec_{spec}, contents_{std::forward<Contents>(contents)} {}
color_spec_type const & spec() const { return spec_; }
std::uint32_t color() const { return spec_.code(); }
@ -162,32 +163,49 @@ namespace xo {
} /*print*/
private:
/* controls independently what to print
/** controls independently what to print
* \033[38;5;117m hello, world! \033[0m
* <------------> <-----------> <----->
* color_on contents color_off
*/
**/
coloring_control_flags flags_ = coloring_control_flags::none;
/** color to use, when @ref color_enabled_ is on **/
color_spec_type spec_;
/** contents to print surrounded by color escapes **/
Contents contents_;
}; /*color_impl*/
template <typename Contents>
color_impl<Contents> with_color_if(bool color_enabled, color_spec_type const & spec, Contents && contents) {
return color_impl<Contents>((color_enabled
? coloring_control_flags::all
: coloring_control_flags::contents),
spec,
std::forward<Contents>(contents));
}
template <typename Contents>
color_impl<Contents> with_color(color_spec_type const & spec, Contents && contents) {
return color_impl<Contents>(coloring_control_flags::all, spec, std::forward<Contents>(contents));
return color_impl<Contents>(coloring_control_flags::all,
spec,
std::forward<Contents>(contents));
}
inline color_impl<int>
color_on(color_spec_type const & spec) {
return color_impl<int>(coloring_control_flags::color_on, spec, 0);
return color_impl<int>(coloring_control_flags::color_on,
spec,
0);
}
inline color_impl<int>
color_off(color_spec_type const & spec) {
/* any spec other than color_spec_type::none() works here */
return color_impl<int>(coloring_control_flags::color_off, spec, 0);
return color_impl<int>(coloring_control_flags::color_off,
spec,
0);
}
template <typename Contents>

View file

@ -20,12 +20,19 @@ namespace xo {
/** @class ppstate
* @brief hold pretty-printer state
*
* Need a log_streambuf instance to keep track of indent.
* Application code will likely prefer @ref pretty_printer
*
* Use:
* @code
* ppconfig ppc;
* ppstate pps(&cout, 0, &ppc);
* log_streambuf sbuf(buf_z);
* stringstream ss(&sbuf);
* ppstate pps(0, &ppc, &ss, &sbuf);
*
* pps.pretty("first");
* pps.pretty("second");
* @endcode
**/
struct ppstate {
using streambuf_type = log_streambuf<char, std::char_traits<char>>;
@ -176,6 +183,8 @@ namespace xo {
/** @class ppstate_standalone
* @brief like ppstate, but also holds streambuf
*
* editor bait: pretty_printer prettyprinter
*/
struct ppstate_standalone : public ppstate {
explicit ppstate_standalone(std::ostream * os, std::uint32_t ci, const ppconfig * config)
@ -377,8 +386,9 @@ namespace xo {
ppii.pps()->write(" ");
/* must color here, because we may keep the output if it fits! */
if (!ppii.pps()->print_upto(with_color(color_spec_type::yellow(), // tag_config::tag_color,
concat((char const *)":", tag.name()))))
if (!ppii.pps()->print_upto(with_color_if(tag_config::tag_color_enabled,
color_spec_type::yellow(), // tag_config::tag_color,
concat((char const *)":", tag.name()))))
return false;
ppii.pps()->write(" ");
@ -395,8 +405,9 @@ namespace xo {
if (tag.prefix_space())
ppii.pps()->write(" ");
ppii.pps()->write(with_color(color_spec_type::yellow(), //tag_config::tag_color,
concat((char const *)":", tag.name())));
ppii.pps()->write(with_color_if(tag_config::tag_color_enabled,
color_spec_type::yellow(), //tag_config::tag_color,
concat((char const *)":", tag.name())));
ppii.pps()->newline_indent(ppii.ci1());
ppii.pps()->pretty(tag.value());
@ -446,6 +457,5 @@ namespace xo {
return *this;
}
} /*namespace print*/
} /*namespace xo*/

View file

@ -181,7 +181,7 @@ namespace xo {
if (PrefixSpace)
s << " ";
s << with_color(tag_config::tag_color, concat((char const *)":", tag.name()))
s << with_color_if(tag_config::tag_color_enabled, tag_config::tag_color, concat((char const *)":", tag.name()))
<< " ";
if (TagStyle == tagstyle::autoescape)
@ -204,7 +204,7 @@ namespace xo {
if (PrefixSpace)
s << " ";
s << with_color(tag_config::tag_color, concat((char const *)":", tag.name()))
s << with_color_if(tag_config::tag_color_enabled, tag_config::tag_color, concat((char const *)":", tag.name()))
<< " ";
if (TagStyle == tagstyle::autoescape)

View file

@ -6,18 +6,29 @@
#include <cstdint>
namespace xo {
/* Tag here b/c we want header-only library */
/** @class tag_config_impl
* @brief configuration for tag-printing
*
* note: Tag here b/c we want header-only library
**/
template <typename Tag>
struct tag_config_impl {
/* color to use for tags
/** true to enable colored tag printing **/
static bool tag_color_enabled;
/** color to use for tags
* os << tag("foo", foovalue)
* to produces output like
* :foo foovalue
* with :foo using .tag_color
*/
**/
static color_spec_type tag_color;
}; /*tag_config_impl*/
template <typename Tag>
bool
tag_config_impl<Tag>::tag_color_enabled = true;
/** default value is a light gray **/
template <typename Tag>
color_spec_type
tag_config_impl<Tag>::tag_color = color_spec_type::xterm(245);