xtd 1.0.0
Loading...
Searching...
No Matches
as.hpp
Go to the documentation of this file.
1
3#pragma once
4#include "any.hpp"
8#include "types.hpp"
9#include <memory>
10#include <variant>
11#define __XTD_CORE_INTERNAL__
14#undef __XTD_CORE_INTERNAL__
15#include "convert.hpp"
16#define __XTD_CORE_INTERNAL__
38#undef __XTD_CORE_INTERNAL__
39#include "linq/enumerable.hpp"
40#include "convert_pointer.hpp"
41#define __XTD_CORE_INTERNAL__
43#undef __XTD_CORE_INTERNAL__
44#include "convert_string.hpp"
45#define __XTD_CORE_INTERNAL__
47#undef __XTD_CORE_INTERNAL__
48#include "any_object.hpp"
49#include "enum_object.hpp"
50#define __XTD_CORE_INTERNAL__
52#undef __XTD_CORE_INTERNAL__
53
55template<typename new_type, typename current_type>
56[[nodiscard]] auto __convert_value__(current_type* value) -> new_type* {
57 return xtd::as<new_type>(value);
58}
59
60template<typename result_t, typename source_t>
61[[nodiscard]] inline auto xtd::linq::enumerable::cast(const xtd::collections::generic::ienumerable<source_t>& source) noexcept {
62 auto result = __opaque_xtd_linq_enumerable_collection__<result_t> {};
63 result = __opaque_xtd_linq_enumerable_collection__<result_t> {};
64 for (const auto& item : source)
65 result.items.push_back(xtd::as<result_t>(item));
66 return result;
67}
68
69template<typename type_t>
70template<typename target_t>
71[[nodiscard]] auto xtd::reference_wrapper_object<type_t>::to_object() const -> target_t {
73 return xtd::as<target_t>(to_object());
74}
75
76template<typename type_t>
77template<typename target_t>
78[[nodiscard]] auto xtd::reference_wrapper_object<type_t>::to_reference() const -> target_t {
80 return xtd::as<target_t>(to_reference());
81}
82
83template<typename type_t>
84template<typename target_t>
85[[nodiscard]] auto xtd::shared_ptr_object<type_t>::to_object() const -> target_t {
87 return xtd::as<target_t>(to_object());
88}
89
90template<typename type_t>
91template<typename target_t>
92[[nodiscard]] auto xtd::shared_ptr_object<type_t>::to_pointer() const -> target_t* {
93 if (to_pointer() == null) return null;
94 return xtd::as<target_t>(to_pointer());
95}
96
97template<typename type_t, typename deleter_t>
98template<typename target_t>
99[[nodiscard]] auto xtd::unique_ptr_object<type_t, deleter_t>::to_object() const -> target_t {
101 return xtd::as<target_t>(to_object());
102}
103
104template<typename type_t, typename deleter_t>
105template<typename target_t>
106[[nodiscard]] auto xtd::unique_ptr_object<type_t, deleter_t>::to_pointer() const -> target_t* {
107 if (to_pointer() == null) return null;
108 return xtd::as<target_t>(to_pointer());
109}
110
111template<typename source_type_t, xtd::usize source_rank, typename source_allocator_t, typename destination_type_t, xtd::usize destination_rank, typename destination_allocator_t>
112inline auto xtd::array<>::copy(const array<source_type_t, source_rank, source_allocator_t>& source_array, xtd::usize source_index, const array<destination_type_t, destination_rank, destination_allocator_t>& destination_array, xtd::usize destination_index, xtd::usize length) -> void {
113 if (source_array.rank() != destination_array.rank()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::rank);
114 for (auto r = xtd::usize {}; r < source_array.rank(); ++r)
115 if (source_array.get_length(r) != destination_array.get_length(r)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
116 if (source_index < source_array.get_lower_bound(0) || destination_index < destination_array.get_lower_bound(0)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument_out_of_range);
117 if (source_index + length > source_array.length() || destination_index + length > destination_array.length()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
118
119 for (auto i = xtd::usize {}; i < length; ++i)
120 destination_array.data_->items[destination_index + i] = as<destination_type_t>(source_array.data_->items[source_index + i]);
121}
122
123namespace xtd {
124 template<typename type_t, typename ...args_t>
125 [[nodiscard]] inline auto as(const std::variant<args_t...>& value) -> type_t {
126 std::visit([&](auto&& arg) {if constexpr (!std::is_same_v<std::decay_t<decltype(arg)>, std::remove_cvref_t<type_t>>) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);}, value);
127 return std::get<type_t>(value);
128 }
129
130 template<typename type_t, typename ...args_t>
131 [[nodiscard]] inline auto as(std::variant<args_t...>& value) -> type_t {
132 std::visit([&](auto&& arg) {if constexpr (!std::is_same_v<std::decay_t<decltype(arg)>, std::remove_cvref_t<type_t>>) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);}, value);
133 return std::get<type_t>(value);
134 }
135}
136
137namespace std {
138 template<typename type_t>
139 [[nodiscard]] auto any_cast(const xtd::any_object& value) -> type_t {return xtd::as<type_t>(value);}
140 template<typename type_t>
141 [[nodiscard]] auto any_cast(xtd::any_object& value) -> type_t {return xtd::as<type_t>(value);}
142}
Contains xtd::any type and std::bad_any_cast exception.
Contains xtd::any_object class.
static auto copy(const array< source_type_t, source_rank, source_allocator_t > &source_array, const array< destination_type_t, destination_rank, destination_allocator_t > &destination_array) -> void
Copies a range of elements from an xtd::array starting at the first element and pastes them into anot...
Definition array_static.hpp:239
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
static auto cast(const ienumerable< source_t > &source) noexcept
Casts the elements of an xtd::collections::generic::ienumerable to the specified type.
auto to_reference() const -> type &
Gets the stored reference.
Definition reference_wrapper_object.hpp:238
auto to_object() const -> type_t &
Gets the stored object.
Definition reference_wrapper_object.hpp:228
Contains xtd::convert class.
Contains xtd::convert class.
Contains xtd::convert_string class.
Contains xtd::enum_object struct.
constexpr xtd::expressions::placeholder< index > arg
The xtd::expressions::arg instance is the index argument used by expression.
Definition arg.hpp:61
@ value
Represnets the constant operator precedence (42).
Definition operator_precedence.hpp:30
@ argument
The argument is not valid.
Definition exception_case.hpp:31
@ invalid_cast
The cast is not valid.
Definition exception_case.hpp:63
@ rank
The rank is not valid.
Definition exception_case.hpp:93
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
@ null_pointer
The pointer is null.
Definition exception_case.hpp:79
null_ptr null
Represents a null pointer value.
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto any_cast(const xtd::any &operand) -> type_t
Performs type-safe access to the contained object.
Definition any_cast.hpp:22
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
@ r
The R key.
Definition console_key.hpp:122
@ i
The I key.
Definition console_key.hpp:104
Contains xtd::invalid_cast_exception exception.
Contains xtd::linq::enumerable <type_t> class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
constexpr auto length() const noexcept -> size_type
Returns the length of the current read_only_span.
Definition read_only_span.hpp:213
constexpr auto is_empty() const noexcept -> bool
Returns a value that indicates whether the current xtd::read_only_span <type_t> is empty.
Definition read_only_span.hpp:209
Contains xtd::reference_wrapper_object class.
Contains xtd::shared_ptr_object class.
Contains xtd fundamental types.