xtd 0.2.0
Loading...
Searching...
No Matches
delegate_multicast.cpp

Shows how to use xtd::delegate class.

#define TRACE
#include <xtd/xtd>
using writer = delegate<void(const string& str)>;
void write_debug(const string& str) {
cdebug << str << environment::new_line;
}
class tracer {
public:
void write_trace(const string& str) {
ctrace << str << environment::new_line;
}
void write_trace2(const string& str) {
ctrace << "2 " << str << environment::new_line;
}
};
auto main() -> int {
auto write = writer {};
write += delegate_(const string & str) {
console::out << str << environment::new_line;
};
write += delegate_(auto str) {
console::error << str << environment::new_line;
};
write += write_debug;
write += {tracer(), &tracer::write_trace};
auto t = tracer {};
write += {t, &tracer::write_trace2}; // add
write -= {t, &tracer::write_trace2}; // then remove
struct writer {
writer() = default;
void operator()(const string& str) {
console::write_line(str);
}
};
write += writer();
write("Value to write");
}
// This code produces the following output :
//
// Value to write
// Value to write
// Value to write
// Value to write
// Value to write
std::ostream ctrace(nullptr)
Provides an std::ostream for xtd::diagnostics::trace.
#define delegate_
The declaration of a delegate type is similar to a method signature. It has a return value and any nu...
Definition delegate.hpp:900
@ write
Write access to the file. Data can be written to the file. Combine with Read for read/write access.
Definition file_access.hpp:19
@ t
The T key.
Definition console_key.hpp:126