xtd 0.2.0
box.h
Go to the documentation of this file.
1 #pragma once
5 #include "convert_string.h"
6 #include "icomparable.h"
7 #include "iequatable.h"
9 #include "iequatable.h"
10 #include "enum.h"
11 #include "object.h"
12 #include "ustring.h"
13 #include "types.h"
14 
16 namespace xtd {
46  template<typename type_t>
47  class box : public xtd::icomparable<box<type_t>>, public xtd::iequatable<box<type_t>>, public xtd::object {
48  public:
49  using underlying_type = type_t;
51 
54  box() = default;
57  box(const type_t& value) : value_(value) {}
60  template<typename ...args_t>
61  box(args_t&& ...args) : value_(args...) {}
63 
65  box(const box&) = default;
66  box(box&&) = default;
67  box& operator =(const box&) = default;
68  box& operator =(const type_t& value) {
69  value_ = value;
70  return *this;
71  };
73 
75 
79  const type_t& value() const noexcept {return value_;}
82  type_t& value() noexcept {return value_;}
85  box& value(const type_t& value) {
86  value_ = value;
87  return *this;
88  }
90 
92 
94  operator type_t() const noexcept {return value_;}
96 
98 
100  bool equals(const box& value) const noexcept override {return value_ == value.value_;}
101 
102  int32 compare_to(const box& value) const noexcept override {
103  if (value_ == value.value_) return 0;
104  if (value_ < value.value_) return -1;
105  return 1;
106  }
107 
111  static type_t parse(const xtd::ustring& value) {return xtd::parse<type_t>(value);}
112 
113  xtd::ustring to_string() const noexcept override {return xtd::ustring::format("{}", value_);}
117  xtd::ustring to_string(const xtd::ustring& format) const noexcept {
118  return xtd::ustring::format(xtd::ustring::format("{{:{}}}", format), value_);
119  }
120 
125  static bool try_parse(const xtd::ustring& value, type_t& result) noexcept {return xtd::try_parse<type_t>(value, result);}
127 
128  private:
129  type_t value_ {};
130  };
131 }
Contains xtd fundamental types.
Contains xtd::iequatable interface.
Contains xtd::icomparable interface.
Represents a boxed object.
Definition: box.h:47
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition: icomparable.h:17
box()=default
Initialize a new xtd::box object.
xtd::ustring to_string() const noexcept override
Returns a sxd::ustring that represents the current object.
Definition: box.h:113
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
const type_t & value() const noexcept
Gets the underlying value.
Definition: box.h:79
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
static bool try_parse(const xtd::ustring &value, type_t &result) noexcept
Converts the string to its type_t equivalent. A return value indicates whether the conversion succeed...
Definition: box.h:125
Contains enum_ and enum_ut_ keywords.
box(args_t &&...args)
Initialize a new xtd::box object with specified value.
Definition: box.h:61
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: iequatable.h:18
static type_t parse(const xtd::ustring &value)
Converts the string to its type_t equivalent.
Definition: box.h:111
xtd::ustring to_string(const xtd::ustring &format) const noexcept
Converts the value of this instance to its equivalent string representation, using the specified form...
Definition: box.h:117
box & value(const type_t &value)
Sets de underlying value.
Definition: box.h:85
Contains xtd::ustring class.
Contains xtd::convert_string class.
Contains xtd::object class.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:32
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
type_t & value() noexcept
Gets the underlying value.
Definition: box.h:82
box(const type_t &value)
Initialize a new xtd::box object with specified value.
Definition: box.h:57
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition: ustring.h:744
Contains xtd::invalid_cast_exception exception.
int32 compare_to(const box &value) const noexcept override
Compares the current instance with another object of the same type.
Definition: box.h:102
bool equals(const box &value) const noexcept override
Indicates whether the current object is equal to another object of the same type. ...
Definition: box.h:100