Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Improvements to print overloads and add support for printing std::string_view - develop #1069

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions libraries/eosiolib/core/eosio/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#pragma once
#include <utility>
#include <string>


#include <string_view>
#include <type_traits>

namespace eosio {
namespace internal_use_do_not_use {
Expand Down Expand Up @@ -44,6 +44,7 @@ namespace eosio {
__attribute__((eosio_wasm_import))
void printhex(const void*, uint32_t);
}

};

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ namespace eosio {
* @param len - number of chars to print
*/
inline void printl( const char* ptr, size_t len ) {
internal_use_do_not_use::prints_l(ptr, len);
internal_use_do_not_use::prints_l(ptr, len);
}

/**
Expand All @@ -96,7 +97,27 @@ namespace eosio {
* @param ptr - a null terminated string
*/
inline void print( const char* ptr ) {
internal_use_do_not_use::prints(ptr);
internal_use_do_not_use::prints(ptr);
}

/**
* Prints string
*
* @ingroup console
* @param str - an std::string
*/
inline void print( const std::string& str ) {
internal_use_do_not_use::prints_l(str.c_str(), str.size());
}

/**
* Prints string
*
* @ingroup console
* @param str - an std::string_view
*/
inline void print( std::string_view str ) {
internal_use_do_not_use::prints_l(str.data(), str.size());
}

/**
Expand Down Expand Up @@ -160,16 +181,12 @@ namespace eosio {
*
* @ingroup console
* @param t to be printed
* @pre T must implements print() function
* @pre T must implement print() function
*/
template<typename T, std::enable_if_t<!std::is_integral<std::decay_t<T>>::value, int> = 0>
inline void print( T&& t ) {
if constexpr (std::is_same<std::decay_t<T>, std::string>::value)
internal_use_do_not_use::prints_l( t.c_str(), t.size() );
else if constexpr (std::is_same<std::decay_t<T>, char*>::value)
internal_use_do_not_use::prints(t);
else
t.print();
template <typename T>
inline auto print(T&& t) -> std::void_t<decltype(t.print())>
{
std::forward<T>(t).print();
}

/**
Expand All @@ -179,7 +196,7 @@ namespace eosio {
* @param s null terminated string to be printed
*/
inline void print_f( const char* s ) {
internal_use_do_not_use::prints(s);
internal_use_do_not_use::prints(s);
}

/**
Expand Down Expand Up @@ -229,7 +246,8 @@ namespace eosio {
* @endcode
*/
template<typename Arg, typename... Args>
void print( Arg&& a, Args&&... args ) {
auto print( Arg&& a, Args&&... args ) -> std::enable_if_t<sizeof...(Args) != 0, void>
{
print(std::forward<Arg>(a));
print(std::forward<Args>(args)...);
}
Expand Down