xtd 0.2.0
Loading...
Searching...
No Matches

◆ register_any_stringer()

template<class type_t , class function_t >
void xtd::register_any_stringer ( const function_t &  func)
inline

Register an any stringer method for a specified type.

Parameters
funcFunction to register any stringer for specified type.
Namespace
xtd
Library
xtd.core
Examples
Show how to register your own class.
#include <xtd/console>
#include <xtd/environment>
#include <xtd/register_any_stringer>
#include <xtd/string>
#include <xtd/unregister_any_stringer>
using namespace xtd;
class character {
public:
character(const string& name, const string& rank) noexcept : name_(name), rank_(rank) {}
const string& name() const noexcept {return name_;}
const string& rank() const noexcept {return rank_;}
string to_string() const noexcept {return name_ + " (" + rank_ + ")";}
private:
string name_;
string rank_;
};
auto main() -> int {
auto value = std::make_any<int>(42);
value = std::make_any<string>("Star Trek: The Next Generation");
value = std::make_any<character>("Jean-Luc Picard", "Captain");
console::out << "Before register_any_stringer : " << string::format("{}", value) << environment::new_line;
register_any_stringer<character>([](auto value) {return value.to_string();});
console::out << "After register_any_stringer : " << string::format("{}", value) << environment::new_line;
unregister_any_stringer<character>();
console::out << "After unregister_any_stringer : " << string::format("{}", value) << environment::new_line;
}
// This code produces the following output :
//
// 42
// Star Trek: The Next Generation
// Before register_any_stringer : (unregistered)
// After register_any_stringer : Jean-Luc Picard (Captain)
// After unregister_any_stringer : (unregistered)
static std::ostream out
Gets the standard output stream. A std::basic_ostream<char_t> that represents the standard output str...
Definition console.h:52
static xtd::string new_line() noexcept
Gets the newline string defined for this environment.
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition to_string.h:41
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10