[PATCH v2 1/5] rtems-utils.h: Create ostream_guard
Chris Johns
chrisj at rtems.org
Wed Jul 21 01:15:07 UTC 2021
Close, some minor formatting issues ....
On 21/7/21 1:06 am, Ryan Long wrote:
> ---
> rtemstoolkit/rtems-utils.h | 18 ++++++++++++++++++
> 1 file changed, 18 insertions(+)
>
> diff --git a/rtemstoolkit/rtems-utils.h b/rtemstoolkit/rtems-utils.h
> index 4ce9c68..c4a262e 100644
> --- a/rtemstoolkit/rtems-utils.h
> +++ b/rtemstoolkit/rtems-utils.h
> @@ -47,6 +47,24 @@ namespace rtems
> bool real = false,
> size_t line_length = 16,
> uint32_t offset = 0);
> +
> + /*
> + * Save and restore the output stream's settings.
> + */
> + struct ostream_guard {
> + std::ostream& o;
> + std::ios_base::fmtflags flags;
> +
> + ostream_guard ( std::ostream& o_ ) : o( o_ ), flags( o_.flags() )
The syntax I use and posted is:
ostream_guard (std::ostream& o_)
: o (o_),
flags (o_.flags ())
{
}
First the spaces are what the toolkit uses, there are no extra spaces but the
initialiser list is the important piece so I will explain why it is the way it is.
The way I have written the code lets you make changes to a struct (or class)
without impacting a single line. This struct is pretty simple but more complex
structs can have many initialisers and they need to be in order in the
initialiser list and if you add or move members the order needs to change. Being
able to visually inspect the struct and the list is important. Considers these
constructors ....
https://git.rtems.org/rtems-tools/tree/rtemstoolkit/rld-symbols.cpp#n72
> + {
> + }
> +
> + ~ostream_guard ()
> + {
> + o.flags( flags );
2 spaces please.
Thanks
Chris
More information about the devel
mailing list