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

Shows how to use xtd::boxing methods.

#include <xtd/collections/specialized/string_key_value_pair>
#include <xtd/collections/generic/list>
#include <xtd/boxing>
#include <xtd/console>
#include <xtd/environment>
using namespace xtd;
using namespace xtd::collections::generic;
// This class is not realistic, it is just to illustrate the example.
class setup_serializer : public object {
public:
using pair_collection = list<string_key_value_pair>;
setup_serializer() = default;
// The add_key_value method has an xtd::object as the second parameter to force
// the application to use the boxing method for native and non-native setup parameters.
void add_key_value(const string& key, const object& value) {
key_values_.push_back({key, value.to_string()});
}
void title(const string& title) {title_ = title;}
string to_string() const noexcept override {
auto result = string::format("[{}]{}{}", title_, environment::new_line());
for (const auto& [key, value] : key_values_)
result += string::format("{}={}{}", key, value, environment::new_line());
return result;
}
private:
pair_collection key_values_;
string title_;
};
struct setup {
int x = 0;
int y = 0;
int width = 0;
int height = 0;
string text;
};
auto main() -> int {
auto my_application_setup = setup {10, 10, 300, 300, "My application", {1, 2, 3}};
auto serializer = setup_serializer {};
serializer.title("My application setup");
serializer.add_key_value("x", boxing(my_application_setup.x));
serializer.add_key_value("y", boxing(my_application_setup.y));
serializer.add_key_value("width", boxing(my_application_setup.width));
serializer.add_key_value("height", boxing(my_application_setup.height));
serializer.add_key_value("text", boxing(my_application_setup.text));
serializer.add_key_value("version", boxing(my_application_setup.version));
console::write_line("result :{0}{0}{1}", environment::new_line(), serializer);
}
// This code produces the following output :
//
// result :
//
// [My application setup]
// x=10
// y=10
// width=300
// height=300
// text=My application
// version=1.2.3
//
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
virtual void push_back(const type_t &value)
Appends the given element value to the end of the container.
Definition list.h:778
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
virtual xtd::string to_string() const noexcept
Returns a xtd::string that represents the current object.
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition version.h:114
auto boxing(const type_t &value) noexcept
Allows to box an object.
Definition boxing.h:49
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd::collections::specialized namespace contains specialized and strongly-typed collections; for ...
Definition string_collection.h:13
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10