MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::Padded< T > Struct Template Reference

Formats data with padding into a writer's output stream Uses Formatter<T> under the hood Usually will use extract_padding_options to get padding options. More...

#include <formats.hpp>

Static Public Member Functions

template<WriterImpl WI>
static Result< size_t, typename Writer< WI >::ErrType > write_padded (Writer< WI > &writer, const PaddingOptions &padOpts, const FormatOptions &opts, const T &elem)
 

Detailed Description

template<typename T>
struct mtcore::Padded< T >

Formats data with padding into a writer's output stream Uses Formatter<T> under the hood Usually will use extract_padding_options to get padding options.

See also
extract_padding_options
Template Parameters
TType of data to pad

Definition at line 153 of file formats.hpp.

Member Function Documentation

◆ write_padded()

template<typename T>
template<WriterImpl WI>
Result< size_t, typename Writer< WI >::ErrType > mtcore::Padded< T >::write_padded ( Writer< WI > & writer,
const PaddingOptions & padOpts,
const FormatOptions & opts,
const T & elem )
static

Definition at line 791 of file formats.hpp.

791 {
792 // Do a dry run to get what the ending length will be
793 // Doing a dry run since we're so disconnected from the logic
794 // that we can't accurately tell the final length without "printing"
797 ensure(sizeRes.is_success());
798 size_t len = sizeRes.value();
799
800 // If we don't need to pad, just write it directly
801 if (len >= padOpts.padLen) {
803 }
804
805 switch (padOpts.alignment) {
807 auto padRes = writer.write_n_times(padOpts.pad, padOpts.padLen - len);
808 if (padRes.is_error()) {
809 return padRes.error();
810 }
811 return Formatter<T>::fmt(writer, opts, elem).with_success_val(padOpts.padLen);
812 }
815 if (contentRes.is_error()) {
816 return contentRes.error();
817 }
818 return writer.write_n_times(padOpts.pad, padOpts.padLen - len).with_success_val(padOpts.padLen);
819 }
821 auto leftPad = (padOpts.padLen - len) / 2;
822 auto rightPad = leftPad;
823
824 // If we have to be lopsided, put the extra padding on the right
825 if (leftPad + rightPad + len != padOpts.padLen) {
826 ++rightPad;
827 }
828 auto padLeftRes = writer.write_n_times(padOpts.pad, leftPad);
829 if (padLeftRes.is_error()) {
830 return padLeftRes.error();
831 }
833 if (contentRes.is_error()) {
834 return contentRes.error();
835 }
836 return writer.write_n_times(padOpts.pad, rightPad).with_success_val(padOpts.padLen);
837 }
838 default:
839 unreachable();
840 }
841 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
#define unreachable(...)
Marks code as unreachable.
Formats data with padding into a writer's output stream Uses Formatter<T> under the hood Usually will...
Definition formats.hpp:153
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this struct was generated from the following file: