PrevUpHomeNext

utx::print


utx::print - utx::print_class

utx::print is the object of template print class utx::print_class to print many different values without worrying about their types. It is used to output any printable information to char based standard stream, aka std::cout, std::wcout, etc. However the ouput stream can be changed. utx::print is the main print utility of utxcpp. It can print many different types of values by one call. utx::print has a variadic template member function to do the printing, by one call, it is pre-defined format printer, every items are splitted by a space, std::endl or '\n' character will be added at the end. Almost every printable value can be printed by utx::print, and other values can be printed by utx::print after overloading operator<<. utx::print is the most important api of utxcpp.

utx::print has been updated.

utx::print or utx::print_class has been updated since Nov. 17, 2023. From now on, utx::print and old print-all have been merged to one utx::print, that means utx::print can not only print many different types of single printable values, but also can print many different types of printable containers, ranges, and tuples.

In addition, utx::print can print nested ranges, tuples, ranges of tuples, and or tuples of ranges.

utx::print_class

utx::print_class is a template class

template <utx::chara char_xt>
	requires utx::same_as<utx::remove_cvref_t<char_xt>, char_xt>
class print_class;

The utx::chara concept requires the type char_xt must be one of char, char8_t, char16_t, char32_t, wchar_t.

The "requires" concept constrains that the type char_xt must not have const and reference.

So char or wchar_t is accepted by utx::print_class template, but const char, char &, or const char &, etc. are not accepted by utx::print_class template.

Currently, only char and wchar_t are implemented by c++ standard ostream, so char8_t, char16_t, char32_t are not available for utx::print_class now.

Polymorphism Friendly

utx::print_class is inheritence and polymorphism friendly. It has virtual destructor. Its member data or member functions are protected or public.

Constructors

utx::print_class constructors

A constructor must at least set to initialize __sout to attach an external ostream, such as std::cout, or std::wcout.

4x2 constructors

////////////////////////////////////////////////////////////////////////////////
// These two constructors init to attach external ostream

print_class(ostream_type & __external_ostream__);
print_class(ofstream_type & __external_ofstream__);

////////////////////////////////////////////////////////////////////////////////
// These two constructors init to attach external ostream,
// and init the three delimiters.

print_class(
	ostream_type & __external_ostream__,
	const utx::delim_base<string_type> & __init_nested_range_delim__,
	const utx::delim_base<string_type> & __init_range_delim__,
	const utx::delim_base<string_type> & __init_value_delim__
);
print_class(
	ofstream_type & __external_ofstream__,
	const utx::delim_base<string_type> & __init_nested_range_delim__,
	const utx::delim_base<string_type> & __init_range_delim__,
	const utx::delim_base<string_type> & __init_value_delim__
);

////////////////////////////////////////////////////////////////////////////////
// These two constructors init to attach external ostream,
// and init if it prints \n at the end, 
// and if it aligns left for next line printed.


print_class(
	ostream_type & __external_ostream__,
	utx::mb __newline__,
	utx::mb __align_left__
	);
print_class(
	ofstream_type & __external_ofstream__,
	utx::mb __newline__,
	utx::mb __align_left__
);

////////////////////////////////////////////////////////////////////////////////
// These two constructors init what all above constructors can init.

print_class(
	ostream_type & __external_ostream__,
	const utx::delim_base<string_type> & __init_nested_range_delim__,
	const utx::delim_base<string_type> & __init_range_delim__,
	const utx::delim_base<string_type> & __init_value_delim__,
	utx::mb __newline__,
	utx::mb __align_left__
);
print_class(
	ofstream_type & __external_ofstream__,
	const utx::delim_base<string_type> & __init_nested_range_delim__,
	const utx::delim_base<string_type> & __init_range_delim__,
	const utx::delim_base<string_type> & __init_value_delim__,
	utx::mb __newline__,
	utx::mb __align_left__
);

Public Member Functions

Lock and Mutex Support

utx::print_class such as utx::print does not include any threading and mutex header, however, you can pass a mutex to utx::print, that mutex will be constrained and matched by utx::param_mutex, then the mutex will be locked and unlocked inside the utx::print calling stack.

std::mutex mutex;

start_a_thread;
// then use mutex ...
utx::print(mutex, "Hell", "World!");	// mutex will be locked and unlocked inside utx::print .

See utx::param_mutex

std::fmtflags rules

Every call of utx::print, utx::wprint, etc., the std::boolalpha, std::fixed, and std::setprecision(10) will be set. The user-defined flags can change them, but keep in the current call session, and will be lost after ending current print call, never existed in next print call. No matter how many times of calling utx::print, utx::wprint, etc., and no matter how the user set fmt-flags to utx::print, utx::wprint, etc., the standard output std::cout, std::cerr, std::wcout etc. will be not affected.

Public Header

#include <utxcpp/core.hpp>

Notice

[Note] Note
  • It prints a newline character at the end, but can be changed with .set_newline member function.
  • It separates each single value by a space, each range or tuple by \n, each two-dim range or tuple by \n\n, but can be changed by .set_delim member function.
  • It can print nested ranges and nested tuples, nested ranges of tuples, and nested tuples of ranges.
  • concept requirements:
    • Every single value must satisfy utx::kspt::printable or utx::kspt::printable_adapter<char_type>, char_type is a related char-type, such as char or wchar_t .
    • All single values stored in the range or tuple must satisfy printable too.
    • Basic printable types satisfy utx::kspt::printable .
    • Non-basic types must implement operator<< to satisfy utx::kspt::printable .

c++ Example

#include <utxcpp/core.hpp>

int main()
{
	utx::print(true, ':', 3.14, "kg", "waters");
}

See Also

utx::printe

utx::printnl

utx::wprint

utx::wprinte

utx::wprintnl

Last revised: February 24, 2024 at 08:57:24 GMT


PrevUpHomeNext