xtd 0.2.0
Loading...
Searching...
No Matches
box.hpp
Go to the documentation of this file.
1
4#pragma once
7#include "convert_string.hpp"
8#include "icomparable.hpp"
9#include "iequatable.hpp"
11#include "iequatable.hpp"
12#include "iformatable.hpp"
13#include "is.hpp"
14#include "enum.hpp"
15#include "object.hpp"
16#include "string.hpp"
17#include "types.hpp"
18
20namespace xtd {
52 template<typename type_t>
53 class box : public xtd::object, public xtd::icomparable<box<type_t>>, public xtd::iequatable<box<type_t>>, public xtd::iformatable {
54 public:
55 using underlying_type = type_t;
57
60 box() = default;
63 box(const type_t& value) : value_(value) {}
66 template<typename ...args_t>
67 box(args_t&& ...args) : value_(args...) {}
69
71 box(const box&) = default;
72 box(box&&) = default;
73 box& operator =(const box&) = default;
74 box& operator =(const type_t& value) {
75 value_ = value;
76 return *this;
77 };
79
81
85 const type_t& value() const noexcept {return value_;}
88 type_t& value() noexcept {return value_;}
91 box& value(const type_t& value) {
92 value_ = value;
93 return *this;
94 }
96
98
100 operator type_t() const noexcept {return value_;}
102
104
106 int32 compare_to(const box& value) const noexcept override {return equals(value) ? 0 : xtd::collections::generic::helpers::comparer<type_t> {}(value_, value.value_) ? -1 : 1;}
107 bool equals(const object& obj) const noexcept override {return is<box<type_t>>(obj) && equals(static_cast<const box<type_t>&>(obj));}
108 bool equals(const box& value) const noexcept override {return xtd::collections::generic::helpers::equator<type_t> {}(value_, value.value_);}
109 xtd::string to_string() const noexcept override {return std::is_integral<type_t>::value || std::is_floating_point<type_t>::value || std::is_enum<type>::value || std::is_pointer<type>::value || std::is_base_of<xtd::object, type_t>::value ? xtd::string::format("{}", value_) : typeof_<type_t>().full_name();}
113 xtd::string to_string(const xtd::string& format) const {return to_string(format, std::locale {});}
118 xtd::string to_string(const xtd::string& format, const std::locale& loc) const override {return xtd::string::format(xtd::string::format("{{:{}}}", format), value_);}
120
122
127 static type_t parse(const xtd::string& value) {return xtd::parse<type_t>(value);}
128
133 static bool try_parse(const xtd::string& value, type_t& result) noexcept {return xtd::try_parse<type_t>(value.chars(), result);}
135
136 private:
137 type_t value_ {};
138 };
139}
Represents text as a sequence of character units.
Definition basic_string.hpp:79
Represents a boxed object.
Definition box.hpp:53
box & value(const type_t &value)
Sets de underlying value.
Definition box.hpp:91
type_t & value() noexcept
Gets the underlying value.
Definition box.hpp:88
bool equals(const object &obj) const noexcept override
Determines whether the specified object is equal to the current object.
Definition box.hpp:107
box(args_t &&...args)
Initialize a new xtd::box object with specified value.
Definition box.hpp:67
bool equals(const box &value) const noexcept override
Indicates whether the current object is equal to another object of the same type.
Definition box.hpp:108
static bool try_parse(const xtd::string &value, type_t &result) noexcept
Converts the string to its type_t equivalent. A return value indicates whether the conversion succeed...
Definition box.hpp:133
xtd::string to_string() const noexcept override
Returns a xtd::string that represents the current object.
Definition box.hpp:109
box(const type_t &value)
Initialize a new xtd::box object with specified value.
Definition box.hpp:63
box()=default
Initialize a new xtd::box object.
int32 compare_to(const box &value) const noexcept override
Compares the current instance with another object of the same type.
Definition box.hpp:106
const type_t & value() const noexcept
Gets the underlying value.
Definition box.hpp:85
static type_t parse(const xtd::string &value)
Converts the string to its type_t equivalent.
Definition box.hpp:127
xtd::string to_string(const xtd::string &format, const std::locale &loc) const override
Converts the value of this instance to its equivalent string representation, using the specified form...
Definition box.hpp:118
xtd::string to_string(const xtd::string &format) const
Converts the value of this instance to its equivalent string representation, using the specified form...
Definition box.hpp:113
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.hpp:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
Provides functionality to format the value of an object into a string representation.
Definition iformatable.hpp:35
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:42
Contains xtd::convert_string class.
Contains enum_ and enum_ut_ keywords.
Contains xtd::collections::generic::helpers::equator struct.
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 ...
xtd::string format(const xtd::string &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:20
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
Contains xtd::collections::generic::helpers::comparer struct.
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
Contains xtd::iformatable interface.
Contains xtd::invalid_cast_exception exception.
Contains xtd::is method.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::object class.
Contains xtd::string alias.
Implements a function object for compare data.
Definition comparer.hpp:36
Implements a function object for performing comparisons. Unless specialised, invokes operator== on ty...
Definition equator.hpp:37
Contains xtd fundamental types.