xtd 0.2.0
Loading...
Searching...
No Matches
xtd::object Class Reference
Inheritance diagram for xtd::object:
xtd::delegate< result_t(arguments_t...)>< value_t > xtd::abstract_object xtd::any_object xtd::attribute xtd::background_color xtd::beep xtd::box< type_t > xtd::collections::generic::list< type_t > xtd::compiler xtd::configuration::file_settings xtd::configuration::settings xtd::console_key_info xtd::cpp_language xtd::date_time xtd::debugstreambuf xtd::delegate< result_t()> xtd::delegate< result_t(arguments_t...)> xtd::diagnostics::process xtd::diagnostics::process_start_info xtd::diagnostics::stack_frame xtd::diagnostics::stack_trace xtd::diagnostics::stopwatch xtd::diagnostics::switch_base xtd::diagnostics::trace_event_cache xtd::diagnostics::trace_source xtd::distribution xtd::drawing::brush xtd::drawing::buffered_graphics xtd::drawing::buffered_graphics_context xtd::drawing::color xtd::drawing::drawing_2d::graphics_path xtd::drawing::drawing_2d::graphics_state xtd::drawing::drawing_2d::matrix xtd::drawing::font xtd::drawing::font_family xtd::drawing::graphics xtd::drawing::icon xtd::drawing::image xtd::drawing::imaging::bitmap_data xtd::drawing::imaging::color_palette xtd::drawing::imaging::encoder xtd::drawing::imaging::encoder_parameter xtd::drawing::imaging::encoder_parameters xtd::drawing::imaging::frame_dimension xtd::drawing::imaging::image_format xtd::drawing::imaging::property_item xtd::drawing::pen xtd::drawing::point xtd::drawing::point_f xtd::drawing::rectangle xtd::drawing::rectangle_f xtd::drawing::region xtd::drawing::size xtd::drawing::size_f xtd::drawing::string_format xtd::drawing::text::font_collection xtd::enum_object< enum_t > xtd::environment::xtd_library xtd::event_args xtd::foreground_color xtd::forms::application_context xtd::forms::component xtd::forms::const_tab_page_ref_less xtd::forms::control_layout_style xtd::forms::control_ref_less xtd::forms::country xtd::forms::create_params xtd::forms::cursor xtd::forms::domain_up_down::item xtd::forms::emoticon xtd::forms::enable_debug xtd::forms::flat_button_appearance xtd::forms::link xtd::forms::link_area xtd::forms::list_control::item xtd::forms::message xtd::forms::month_calendar::hit_test_info xtd::forms::padding xtd::forms::power_status xtd::forms::renderer xtd::forms::screen xtd::forms::selection_range xtd::forms::shadow xtd::forms::tab_page_ref_less xtd::forms::use_wait_cursor xtd::guid xtd::hash_code xtd::io::binary_reader xtd::io::binary_writer xtd::io::drive_info xtd::io::text_reader xtd::io::text_writer xtd::media::system_sound xtd::net::end_point xtd::net::ip_address xtd::net::ip_host_entry xtd::net::network_credential xtd::net::socket_address xtd::operating_system xtd::processor xtd::random xtd::reflection::assembly xtd::reset_color xtd::security::secure_string xtd::system_exception xtd::target_type xtd::threading::barrier xtd::threading::cancellation_token xtd::threading::cancellation_token_registration xtd::threading::cancellation_token_source xtd::threading::countdown_event xtd::threading::lock_guard xtd::threading::registered_wait_handle xtd::threading::spin_lock xtd::threading::thread xtd::threading::thread_local_object< value_t > xtd::threading::timer xtd::time_span xtd::time_zone_info xtd::time_zone_info::adjustement_rule xtd::time_zone_info::transition_time xtd::timers::timer xtd::toolkit xtd::tracestreambuf xtd::type_object xtd::uri xtd::ustring xtd::version xtd::web::css::css_reader xtd::web::css::css_writer xtd::web::css::property xtd::web::css::selector

Definition

Supports all classes in the xtd class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all classes in the xtd. It is the root of the type hierarchy.

Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:40
#define core_export_
Define shared library export.
Definition core_export.h:13
Namespace
xtd
Library
xtd.core
Examples
The following example defines a point type derived from the xtd::object class and overrides many of the virtual methods of the xtd::object class. In addition, the example shows how to call many of the static and instance methods of the xtd::object class.
#include <xtd/console>
using namespace xtd;
// The Point class is derived from System.Object.
class point : public object {
public:
int x, y;
point(int x, int y) : x {x}, y {y} {}
bool equals(const object& obj) const noexcept override {
// If this and obj do not refer to the same type, then they are not equal.
if (obj.get_type() != get_type()) return false;
// Return true if x and y fields match.
auto& other = static_cast<const point&>(obj);
return x == other.x && y == other.y;
}
// Return the XOR of the x and y fields.
size_t get_hash_code() const noexcept override {
return x ^ y;
}
// Return the point's value as a string.
ustring to_string() const noexcept override {
return ustring::format("({}, {})", x, y);
}
// Return a copy of this point object by making a simple field copy.
uptr<point> copy() const {
return memberwise_clone<point>();
}
};
auto main() -> int {
// Construct a Point object.
auto p1 = point {1, 2};
// Make another Point object that is a copy of the first.
auto p2 = p1.copy();
// Make another variable that references the first point object.
auto& p3 = p1;
// The line below displays false because p1 and p2 refer to two different objects.
// The line below displays true because p1 and p2 refer to two different objects that have the same value.
// The line below displays true because p1 and p3 refer to one object.
// The line below displays: p1's value is: (1, 2)
console::write_line("p1's value is: {}", p1.to_string());
}
// This code produces the following output:
//
// false
// true
// true
// p1's value is: (1, 2)
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
virtual bool equals(const object &obj) const noexcept
Determines whether the specified object is equal to the current object.
static bool reference_equals(const object_a_t &object_a, const object_b_t &object_b) noexcept
Determines whether the specified object instances are the same instance.
Definition object.h:136
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:52
std::unique_ptr< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.h:25
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
@ other
The operating system is other.
@ y
The Y key.
@ x
The X key.
@ point
Specifies a printer's point (1/72 inch) as the unit of measure.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Examples
boxing.cpp, delegate.cpp, event.cpp, format_class_object.cpp, hash_code.cpp, iclonable.cpp, iformatable.cpp, iformatable_vs_to_string.cpp, lock.cpp, lock_guard_keyword.cpp, monitor_lock.cpp, object.cpp, object_and_iequatable.cpp, object_get_type.cpp, object_memberwise_clone.cpp, object_reference_equals.cpp, object_to_string.cpp, and send_message_to_form.cpp.

Public Constructors

 object ()=default
 Create a new instance of the ultimate base class object.
 

Public Methods

virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual xtd::ustring to_string () const noexcept
 Returns a xtd::ustring that represents the current object.
 

Public Static Methods

template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<typename object_a_t , typename object_b_t >
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 

Constructor & Destructor Documentation

◆ object()

xtd::object::object ( )
default

Create a new instance of the ultimate base class object.

Remarks
This constructor is called by constructors in derived classes, but it can also be used to directly create an instance of the object class.

Member Function Documentation

◆ equals() [1/2]

virtual bool xtd::object::equals ( const object obj) const
virtualnoexcept

Determines whether the specified object is equal to the current object.

Parameters
objThe object to compare with the current object.
Returns
true if the specified object is equal to the current object. otherwise, false.
Examples
The following code example compares the current instance with another object.
#include <xtd/console>
using namespace xtd;
auto main() -> int {
auto object1 = new_ptr<object>();
auto object2 = new_ptr<object>();
auto object3 = object2;
console::write_line(object1->equals(*object3));
console::write_line(*object1 == *object3);
object3 = object1;
console::write_line(object1->equals(*object3));
console::write_line(*object1 == *object3);
}
// This code produces the following output:
//
// false
// false
// true
// true

Reimplemented in xtd::box< type_t >, xtd::configuration::file_settings, xtd::console_key_info, xtd::date_time, xtd::delegate< result_t()>, xtd::delegate< result_t(arguments_t...)>< value_t >, xtd::delegate< result_t(arguments_t...)>, xtd::diagnostics::stack_frame, xtd::diagnostics::switch_base, xtd::guid, xtd::media::system_sound, xtd::net::ip_address, xtd::threading::cancellation_token, xtd::threading::event_wait_handle, xtd::threading::mutex, xtd::threading::semaphore, xtd::time_span, xtd::time_zone_info::transition_time, xtd::time_zone_info::adjustement_rule, xtd::time_zone_info, xtd::toolkit, xtd::type_object, xtd::uri, xtd::version, xtd::drawing::brush, xtd::drawing::color, xtd::drawing::drawing_2d::conical_gradient_brush, xtd::drawing::drawing_2d::graphics_path, xtd::drawing::drawing_2d::graphics_state, xtd::drawing::drawing_2d::hatch_brush, xtd::drawing::drawing_2d::linear_gradient_brush, xtd::drawing::drawing_2d::matrix, xtd::drawing::drawing_2d::radial_gradient_brush, xtd::drawing::font, xtd::drawing::font_family, xtd::drawing::icon, xtd::drawing::image, xtd::drawing::imaging::color_palette, xtd::drawing::imaging::encoder, xtd::drawing::imaging::encoder_parameter, xtd::drawing::imaging::encoder_parameters, xtd::drawing::imaging::frame_dimension, xtd::drawing::imaging::image_format, xtd::drawing::imaging::property_item, xtd::drawing::pen, xtd::drawing::point, xtd::drawing::point_f, xtd::drawing::rectangle, xtd::drawing::rectangle_f, xtd::drawing::region, xtd::drawing::size, xtd::drawing::size_f, xtd::drawing::solid_brush, xtd::drawing::texture_brush, xtd::forms::control, xtd::forms::control_layout_style, xtd::forms::country, xtd::forms::cursor, xtd::forms::domain_up_down::item, xtd::forms::emoticon, xtd::forms::enable_debug, xtd::forms::flat_button_appearance, xtd::forms::horizontal_control_layout_style, xtd::forms::image_list, xtd::forms::link, xtd::forms::link_area, xtd::forms::list_control::item, xtd::forms::menu, xtd::forms::message_notifier_button, xtd::forms::padding, xtd::forms::renderer, xtd::forms::shadow, xtd::forms::status_bar_panel, xtd::forms::tool_bar_button, xtd::forms::vertical_control_layout_style, xtd::ustring, xtd::any_object, and xtd::hash_code.

◆ equals() [2/2]

template<typename object_a_t , typename object_b_t >
static bool xtd::object::equals ( const object_a_t &  object_a,
const object_b_t &  object_b 
)
inlinestaticnoexcept

Determines whether the specified object instances are considered equal.

Parameters
object_aThe first object to compare.
object_bThe second object to compare.
Returns
true if object_a is the same instance as object_b or if both are null references or if object_a(object_b) returns true. otherwise, false.
Examples
The following code example compares different objects.
#include <xtd/console>
using namespace xtd;
auto main() -> int {
ustring s1 = "Tom";
ustring s2 = "Carol";
console::write_line("object::equals(\"{0}\", \"{1}\") => {2}", s1, s2, object::equals(s1, s2));
s1 = "Tom";
s2 = "Tom";
console::write_line("object::equals(\"{0}\", \"{1}\") => {2}", s1, s2, object::equals(s1, s2));
s1 = "";
s2 = "Tom";
console::write_line("object::equals(\"{0}\", \"{1}\") => {2}", s1, s2, object::equals(s1, s2));
s1 = "Carol";
s2 = "";
console::write_line("object::equals(\"{0}\", \"{1}\") => {2}", s1, s2, object::equals(s1, s2));
s1 = "";
s2 = "";
console::write_line("object::equals(\"{0}\", \"{1}\") => {2}", s1, s2, object::equals(s1, s2));
}
// This code produces the following output:
//
// object::equals("Tom", "Carol") => false
// object::equals("Tom", "Tom") => true
// object::equals("", "Tom") => false
// object::equals("Carol", "") => false
// object::equals("", "") => true

◆ get_hash_code()

virtual size_t xtd::object::get_hash_code ( ) const
virtualnoexcept

Serves as a hash function for a particular type.

Returns
size_t A hash code for the current object.

Reimplemented in xtd::hash_code, and xtd::ustring.

◆ get_type()

virtual type_object xtd::object::get_type ( ) const
virtualnoexcept

Gets the type of the current instance.

Returns
The type instance that represents the exact runtime type of the current instance.
Examples
The following code example demonstrates that xtd::object::get_type returns the runtime type of the current instance.
#include <xtd/as>
#include <xtd/console>
using namespace xtd;
class my_base_class : public object {
};
class my_derived_class: public my_base_class {
};
auto main() -> int {
auto my_base = my_base_class {};
auto my_derived = my_derived_class {};
object& o = as<object>(my_derived);
my_base_class& b = as<my_base_class>(my_derived);
console::write_line("my_base: type is {}", my_base.get_type());
console::write_line("my_derived: type is {}", my_derived.get_type());
console::write_line("object o = my_derived: type is {}", o.get_type());
console::write_line("my_base_class b = my_derived: type is {}", b.get_type());
console::write_line("my_base: type is {}", typeof_(my_base));
console::write_line("my_derived: type is {}", typeof_(my_derived));
console::write_line("object o = my_derived: type is {}", typeof_(o));
console::write_line("my_base_class b = my_derived: type is {}", typeof_(b));
}
// This code produces the following output:
//
// my_base: type is my_base_class
// my_derived: type is my_derived_class
// object o = my_derived: type is my_derived_class
// my_base_class b = my_derived: type is my_derived_class
//
// my_base: type is my_base_class
// my_derived: type is my_derived_class
// object o = my_derived: type is my_derived_class
// my_base_class b = my_derived: type is my_derived_class
#define typeof_
Used to obtain the type object of a specified type or object.
Definition typeof.h:45
@ b
The B key.
@ o
The O key.
Examples
object.cpp.

◆ memberwise_clone()

template<typename object_t >
xtd::uptr< object_t > xtd::object::memberwise_clone ( ) const
inline

Creates a shallow copy of the current object.

Returns
A shallow copy of the current object.
Examples
The following example illustrates the xtd::object::memberwise_clone method. It defines a shallow_copy method that calls the xtd::object::memberwise_clone method to perform a shallow copy operation on a person object. It also defines a deep_copy method that performs a deep copy operation on a person object.
#include <xtd/console>
using namespace xtd;
class id_info : public object {
public:
int id_number = 0;
explicit id_info(int id_number) : id_number {id_number} {}
};
class person : public object {
public:
int age = 0;
ptr<ustring> name = new_ptr<ustring>();
ptr<::id_info> id_info = new_ptr<::id_info>(0);
uptr<person> shallow_copy() {
return memberwise_clone<person>();
}
uptr<person> deep_copy() {
auto other = memberwise_clone<person>();
other->id_info = new_ptr<::id_info>(this->id_info->id_number);
other->name = new_ptr<ustring>(*name);
return other;
}
};
void display_values(const person p) {
console::write_line(" name: {0:s}, age: {1:d}", *p.name, p.age);
console::write_line(" value: {0:d}", p.id_info->id_number);
}
auto main() -> int {
// Create an instance of person and assign values to its fields.
auto p1 = new_uptr<person>();
p1->age = 42;
*p1->name = "Sam";
*p1->id_info = id_info {6565};
// Perform a shallow copy of p1 and assign it to p2.
auto p2 = p1->shallow_copy();
// Display values of p1, p2
console::write_line("Original values of p1 and p2:");
console::write_line(" p1 instance values: ");
display_values(*p1);
console::write_line(" p2 instance values:");
display_values(*p2);
// Change the value of p1 properties and display the values of p1 and p2.
p1->age = 32;
*p1->name = "Frank";
p1->id_info->id_number = 7878;
console::write_line("\nValues of p1 and p2 after changes to p1:");
console::write_line(" p1 instance values: ");
display_values(*p1);
console::write_line(" p2 instance values:");
display_values(*p2);
// Make a deep copy of p1 and assign it to p3.
auto p3 = p1->deep_copy();
// Change the members of the p1 class to new values to show the deep copy.
*p1->name = "George";
p1->age = 39;
p1->id_info->id_number = 8641;
console::write_line("\nValues of p1 and p3 after changes to p1:");
console::write_line(" p1 instance values: ");
display_values(*p1);
console::write_line(" p3 instance values:");
display_values(*p3);
}
// This code produces the following output:
//
// Original values of p1 and p2:
// p1 instance values:
// name: Sam, age: 42
// value: 6565
// p2 instance values:
// name: Sam, age: 42
// value: 6565
//
// Values of p1 and p2 after changes to p1:
// p1 instance values:
// name: Frank, age: 32
// value: 7878
// p2 instance values:
// name: Frank, age: 42
// value: 7878
//
// Values of p1 and p3 after changes to p1:
// p1 instance values:
// name: George, age: 39
// value: 8641
// p3 instance values:
// name: Frank, age: 32
// value: 7878
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.h:27
@ p
The P key.
In this example, the person::id_info property returns an id_info object. As the output from the example shows, when a person object is cloned by calling the xtd::object::memberwise_clone method, the cloned person object is an independent copy of the original object, except that they share the same person::id_info object reference. As a result, modifying the clone's person::id_info property changes the original object's person::id_info property. On the other hand, when a deep copy operation is performed, the cloned person object, including its person::id_info property, can be modified without affecting the original object.
Remarks
The xtd::object::memberwise_clone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.
For example, consider an object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B. In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy of C. The example illustrates the difference between a shallow and a deep copy operation.
There are numerous ways to implement a deep copy operation if the shallow copy operation performed by the xtd::object::memberwise_clone method does not meet your needs. These include the following:
  • Call a class constructor of the object to be copied to create a second object with property values taken from the first object. This assumes that the values of an object are entirely defined by its class constructor.
  • Call the xtd::object::memberwise_clone method to create a shallow copy of an object, and then assign new objects whose values are the same as the original object to any properties or fields whose values are reference types. The deep_copy method in the example illustrates this approach.
  • Serialize the object to be deep copied, and then restore the serialized data to a different object variable.
  • Use reflection with recursion to perform the deep copy operation.

◆ reference_equals()

template<typename object_a_t , typename object_b_t >
static bool xtd::object::reference_equals ( const object_a_t &  object_a,
const object_b_t &  object_b 
)
inlinestaticnoexcept

Determines whether the specified object instances are the same instance.

Parameters
object_aThe first object to compare.
object_bThe second object to compare.
Returns
true if object_a is the same instance as object_b or if both are null references; otherwise, false.
Examples
The following code example uses xtd::object::reference_equals to determine if two objects are the same instance.
#include <xtd/console>
using namespace xtd;
auto main() -> int {
object* o = nullptr;
object* p = null;
object* q = new object;
p = q;
delete q;
}
// This code produces the following output:
//
// true
// true
// false
object()=default
Create a new instance of the ultimate base class object.
std::nullptr_t null
Represents a null pointer value.
@ q
The Q key.

◆ to_string()

virtual xtd::ustring xtd::object::to_string ( ) const
virtualnoexcept

Returns a xtd::ustring that represents the current object.

Returns
A string that represents the current object.
Examples
The following code example demonstrates what to_string returns.
#include <xtd/xtd>
using namespace xtd;
namespace examples {
namespace object_test {
class object1 : public object {
};
}
}
auto main() -> int {
ptr<object> obj1 = new_ptr<examples::object_test::object1>();
console::write_line(obj1->to_string());
ptr<object> obj2 = new_ptr<date_time>(1971, 1, 5, 23, 5, 0);
console::write_line(obj2->to_string());
ptr<object> obj3 = new_ptr<boolean_object>();
console::write_line(obj3->to_string());
}
// This code produces the following output:
//
// examples::object_test::object1
// Tue Jan 5 23:05:00 1971
// false

Reimplemented in xtd::any_object, xtd::box< type_t >, xtd::collections::generic::list< type_t >, xtd::compiler, xtd::configuration::file_settings, xtd::cpp_language, xtd::date_time, xtd::diagnostics::stack_frame, xtd::diagnostics::stack_trace, xtd::distribution, xtd::enum_object< enum_t >, xtd::environment::xtd_library, xtd::guid, xtd::io::drive_info, xtd::io::file_system_info, xtd::io::string_writer, xtd::net::dns_end_point, xtd::net::end_point, xtd::net::ip_address, xtd::net::ip_end_point, xtd::net::socket_address, xtd::operating_system, xtd::processor, xtd::system_exception, xtd::target_type, xtd::threading::thread_local_object< value_t >, xtd::time_span, xtd::time_zone_info, xtd::toolkit, xtd::type_object, xtd::uri, xtd::ustring, xtd::version, xtd::web::css::property, xtd::drawing::brush, xtd::drawing::color, xtd::drawing::drawing_2d::graphics_path, xtd::drawing::drawing_2d::matrix, xtd::drawing::font, xtd::drawing::font_family, xtd::drawing::graphics, xtd::drawing::icon, xtd::drawing::imaging::image_format, xtd::drawing::pen, xtd::drawing::point, xtd::drawing::point_f, xtd::drawing::rectangle, xtd::drawing::rectangle_f, xtd::drawing::size, xtd::drawing::size_f, xtd::forms::control, xtd::forms::control_layout_style, xtd::forms::country, xtd::forms::create_params, xtd::forms::cursor, xtd::forms::domain_up_down::item, xtd::forms::emoticon, xtd::forms::file_dialog, xtd::forms::horizontal_control_layout_style, xtd::forms::link_area, xtd::forms::list_control::item, xtd::forms::menu, xtd::forms::menu_item, xtd::forms::message, xtd::forms::month_calendar, xtd::forms::numeric_up_down, xtd::forms::padding, xtd::forms::progress_bar, xtd::forms::radio_button, xtd::forms::renderer, xtd::forms::selection_range, xtd::forms::status_bar_panel, xtd::forms::tool_bar_button, xtd::forms::track_bar, xtd::forms::up_down_button, and xtd::forms::vertical_control_layout_style.

Examples
boxing.cpp.

The documentation for this class was generated from the following file: