Skip to main content

expression-formatter-inlines.h File

C++ header file with inline implementations for the µTest++ expression formatter. More...

Included Headers

#include "micro-os-plus/micro-test-plus/reflection.h" #include <cstdio>

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacedetail

Internal implementation details for the µTest++ framework. More...

Description

C++ header file with inline implementations for the µTest++ expression formatter.

This header provides the inline and template method implementations for expression_formatter. The buffer accessors (str(), c_str(), clear(), empty(), append(), reserve()) are defined as inline functions for zero-overhead access. The template operators — operator<<(T*), operator<<(is_op<T>), operator<<(genuine_integral_value<T>), and operator<<(container) — are instantiated per concrete type at each call site.

The append_number_() helper is also a template, selecting between a std::to_chars path for standard numeric types and an snprintf fallback for long double on platforms where std::to_chars lacks support for 80-bit extended precision.

All definitions reside within the micro_os_plus::micro_test_plus namespace.

This file is intended solely for internal use within the framework and should not be included directly by user code.

File Listing

The file content with the documentation metadata removed is:

1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
43
44#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_EXPRESSION_FORMATTER_INLINES_H_
45#define MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_EXPRESSION_FORMATTER_INLINES_H_
46
47// ----------------------------------------------------------------------------
48
49#if defined(__cplusplus)
50
51// ----------------------------------------------------------------------------
52
54
55#include <cstdio>
56
57// ----------------------------------------------------------------------------
58
59#if defined(__GNUC__)
60#pragma GCC diagnostic push
61
62#pragma GCC diagnostic ignored "-Waggregate-return"
63#if defined(__clang__)
64#pragma clang diagnostic ignored "-Wunknown-warning-option"
65#pragma clang diagnostic ignored "-Wc++98-compat"
66#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
67#endif // defined(__clang__)
68#endif // defined(__GNUC__)
69
70// ============================================================================
71
73{
74 // --------------------------------------------------------------------------
75
83 {
84 }
85
86 // --------------------------------------------------------------------------
87
93 inline auto
94 expression_formatter::colour_ (const bool cond) const
95 {
96 return cond ? colours_.pass : colours_.fail;
97 }
98
99 // --------------------------------------------------------------------------
100
105 inline const std::string&
106 expression_formatter::str () const noexcept
107 {
108 return buffer_;
109 }
110
116 inline const char*
118 {
119 return buffer_.c_str ();
120 }
121
127 inline void
129 {
130 buffer_.clear ();
131 }
132
138 inline bool
140 {
141 return buffer_.empty ();
142 }
143
149 inline void
150 expression_formatter::append (size_t count, char ch)
151 {
152 buffer_.append (count, ch);
153 }
154
160 inline void
162 {
163 buffer_.reserve (capacity);
164 }
165
166 // --------------------------------------------------------------------------
167
175 template <typename T>
178 {
179 if (v == nullptr)
180 {
181 buffer_.append ("0x0");
182 return *this;
183 }
184
185#if defined(__GNUC__)
186#pragma GCC diagnostic push
187
188#if defined(__clang__)
189#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
190#endif // defined(__clang__)
191#endif // defined(__GNUC__)
192
193 char buff[20];
194 snprintf (buff, sizeof (buff), "%p", reinterpret_cast<void*> (v));
195 buffer_.append (buff);
196
197#if defined(__GNUC__)
198#pragma GCC diagnostic pop
199#endif // defined(__GNUC__)
200
201 return *this;
202 }
203
210 template <class T>
214 {
215 *this << detail::get (t);
216 return *this;
217 }
218
224 template <class T>
228 {
229 detail::append_number_ (buffer_, static_cast<long long> (v.get ()));
230 return *this;
231 }
232
239 template <class T>
242 expression_formatter::operator<< (const T& t)
243 {
244 *this << '{';
245 auto first = true;
246 for (const auto& arg : t)
247 {
248 *this << (first ? "" : ", ") << arg;
249 first = false;
250 }
251 *this << '}';
252 return *this;
253 }
254
255 // --------------------------------------------------------------------------
256 // Compound expression operators (without colour codes).
257
269 template <class Lhs_T, class Rhs_T>
272 {
273 return (*this << colour_ (op) << op.lhs () << " == " << op.rhs ()
274 << colours_.none);
275 }
276
289 template <class Lhs_T, class Rhs_T>
292 {
293 return (*this << colour_ (op) << op.lhs () << " != " << op.rhs ()
294 << colours_.none);
295 }
296
309 template <class Lhs_T, class Rhs_T>
312 {
313 return (*this << colour_ (op) << op.lhs () << " > " << op.rhs ()
314 << colours_.none);
315 }
316
330 template <class Lhs_T, class Rhs_T>
333 {
334 return (*this << colour_ (op) << op.lhs () << " >= " << op.rhs ()
335 << colours_.none);
336 }
337
349 template <class Lhs_T, class Rhs_T>
352 {
353 return (*this << colour_ (op) << op.lhs () << " < " << op.rhs ()
354 << colours_.none);
355 }
356
370 template <class Lhs_T, class Rhs_T>
373 {
374 return (*this << colour_ (op) << op.lhs () << " <= " << op.rhs ()
375 << colours_.none);
376 }
377
390 template <class Lhs_T, class Rhs_T>
393 {
394 return (*this << '(' << op.lhs () << colour_ (op) << " and "
395 << colours_.none << op.rhs () << ')');
396 }
397
410 template <class Lhs_T, class Rhs_T>
413 {
414 return (*this << '(' << op.lhs () << colour_ (op) << " or "
415 << colours_.none << op.rhs () << ')');
416 }
417
426 template <class T>
429 {
430 return (*this << colour_ (op) << "not " << op.operand () << colours_.none);
431 }
432
433#if defined(__cpp_exceptions)
446 template <class Callable_T, class Exception_T>
450 {
451 return (*this << colour_ (op) << "throws<"
453 << colours_.none);
454 }
455
466 template <class Callable_T>
470 {
471 return (*this << colour_ (op) << "throws" << colours_.none);
472 }
473
484 template <class Callable_T>
487 {
488 return (*this << colour_ (op) << "nothrow" << colours_.none);
489 }
490#endif // defined(__cpp_exceptions)
491
492 // --------------------------------------------------------------------------
493} // namespace micro_os_plus::micro_test_plus::detail
494
495#if defined(__GNUC__)
496#pragma GCC diagnostic pop
497#endif // defined(__GNUC__)
498
499// ----------------------------------------------------------------------------
500
501#endif // defined(__cplusplus)
502
503// ----------------------------------------------------------------------------
504
505#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_EXPRESSION_FORMATTER_INLINES_H_
506
507// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.17.0.