Skip to main content

libraries/eosiolib/core/eosio/print.hpp

More...

Namespaces

Name
eosio
eosio::internal_use_do_not_use

Classes

Name
classeosio::iostream

Detailed Description

Copyright: defined in eos/LICENSE

Source code


#pragma once
#include <utility>
#include <string>



namespace eosio {
namespace internal_use_do_not_use {
extern "C" {
__attribute__((eosio_wasm_import))
void prints(const char*);

__attribute__((eosio_wasm_import))
void prints_l(const char*, uint32_t);

__attribute__((eosio_wasm_import))
void printi(int64_t);

__attribute__((eosio_wasm_import))
void printui(uint64_t);

__attribute__((eosio_wasm_import))
void printi128(const int128_t*);

__attribute__((eosio_wasm_import))
void printui128(const uint128_t*);

__attribute__((eosio_wasm_import))
void printsf(float);

__attribute__((eosio_wasm_import))
void printdf(double);

__attribute__((eosio_wasm_import))
void printqf(const long double*);

__attribute__((eosio_wasm_import))
void printn(uint64_t);

__attribute__((eosio_wasm_import))
void printhex(const void*, uint32_t);
}
};

inline void printhex( const void* ptr, uint32_t size) {
internal_use_do_not_use::printhex(ptr, size);
}

inline void printl( const char* ptr, size_t len ) {
internal_use_do_not_use::prints_l(ptr, len);
}

inline void print( const char* ptr ) {
internal_use_do_not_use::prints(ptr);
}

template <typename T, std::enable_if_t<std::is_integral<std::decay_t<T>>::value &&
std::is_signed<std::decay_t<T>>::value, int> = 0>
inline void print( T num ) {
if constexpr(std::is_same<T, int128_t>::value)
internal_use_do_not_use::printi128(&num);
else if constexpr(std::is_same<T, char>::value)
internal_use_do_not_use::prints_l( &num, 1 );
else
internal_use_do_not_use::printi(num);
}

template <typename T, std::enable_if_t<std::is_integral<std::decay_t<T>>::value &&
!std::is_signed<std::decay_t<T>>::value, int> = 0>
inline void print( T num ) {
if constexpr(std::is_same<T, uint128_t>::value)
internal_use_do_not_use::printui128(&num);
else if constexpr(std::is_same<T, bool>::value)
internal_use_do_not_use::prints(num?"true":"false");
else
internal_use_do_not_use::printui(num);
}

inline void print( float num ) { internal_use_do_not_use::printsf( num ); }

inline void print( double num ) { internal_use_do_not_use::printdf( num ); }

inline void print( long double num ) { internal_use_do_not_use::printqf( &num ); }

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();
}

inline void print_f( const char* s ) {
internal_use_do_not_use::prints(s);
}

template <typename Arg, typename... Args>
inline void print_f( const char* s, Arg val, Args... rest ) {
while ( *s != '\0' ) {
if ( *s == '%' ) {
print( val );
print_f( s+1, rest... );
return;
}
internal_use_do_not_use::prints_l( s, 1 );
s++;
}
}

template<typename Arg, typename... Args>
void print( Arg&& a, Args&&... args ) {
print(std::forward<Arg>(a));
print(std::forward<Args>(args)...);
}

class iostream {};


template<typename T>
inline iostream& operator<<( iostream& out, const T& v ) {
print( v );
return out;
}


static iostream cout;
}

Updated on 2022-12-05 at 15:38:08 +0000