xtd 0.2.0
is.h
Go to the documentation of this file.
1 #pragma once
5 #include "any.h"
6 #include "parse.h"
7 #include "types.h"
8 #include "unused.h"
9 #include <limits>
10 #include <memory>
11 #include <stdexcept>
12 
14 namespace xtd {
16  template<typename value_t>
17  inline bool is(bool value) {return false;}
18  template<typename value_t>
19  inline bool is(decimal value) {return false;}
20  template<typename value_t>
21  inline bool is(double value) {return false;}
22  template<typename value_t>
23  inline bool is(float value) {return false;}
24  template<typename value_t>
25  inline bool is(sbyte value) {return false;}
26  template<typename value_t>
27  inline bool is(int16 value) {return false;}
28  template<typename value_t>
29  inline bool is(int32 value) {return false;}
30  template<typename value_t>
31  inline bool is(int64 value) {return false;}
32  template<typename value_t>
33  inline bool is(slong value) {return false;}
34  template<typename value_t>
35  inline bool is(xtd::byte value) {return false;}
36  template<typename value_t>
37  inline bool is(uint16 value) {return false;}
38  template<typename value_t>
39  inline bool is(uint32 value) {return false;}
40  template<typename value_t>
41  inline bool is(uint64 value) {return false;}
42  template<typename value_t>
43  inline bool is(xtd::ulong value) {return false;}
45 
61  template<>
62  inline bool is<bool>(bool value) {
63  return true;
64  }
65 
81  template<>
82  inline bool is<decimal>(decimal value) {
83  return true;
84  }
85 
101  template<>
102  inline bool is<double>(double value) {
103  return true;
104  }
105 
121  template<>
122  inline bool is<float>(float value) {
123  return true;
124  }
125 
141  template<>
142  inline bool is<sbyte>(sbyte value) {
143  return true;
144  }
145 
161  template<>
162  inline bool is<int16>(int16 value) {
163  return true;
164  }
165 
181  template<>
182  inline bool is<int32>(int32 value) {
183  return true;
184  }
185 
201  template<>
202  inline bool is<int64>(int64 value) {
203  return true;
204  }
205 
221  template<>
222  inline bool is<slong>(slong value) {
223  return true;
224  }
225 
241  template<>
242  inline bool is<xtd::byte>(xtd::byte value) {
243  return true;
244  }
245 
261  template<>
262  inline bool is<uint16>(uint16 value) {
263  return true;
264  }
265 
281  template<>
282  inline bool is<uint32>(uint32 value) {
283  return true;
284  }
285 
301  template<>
302  inline bool is<uint64>(uint64 value) {
303  return true;
304  }
305 
321  template<>
322  inline bool is<xtd::ulong>(xtd::ulong value) {
323  return true;
324  }
325 
334  template<typename type_t>
335  bool is(std::any value) {
336  try {
337  std::any_cast<type_t>(value);
338  return true;
339  } catch (const std::bad_cast&) {
340  return false;
341  }
342  }
343 
352  template<>
353  inline bool is<std::any>(std::any value) {
354  return true;
355  }
356 
365  template<typename type_t, typename param_t>
366  bool is(const param_t* value) {
367  try {
368  if (value == nullptr) return false;
369  return dynamic_cast<const type_t*>(value) != nullptr;
370  } catch (const std::bad_cast&) {
371  return false;
372  }
373  }
374 
383  template<typename type_t, typename param_t>
384  bool is(const param_t& value) {
385  return is<type_t>(&value);
386  }
387 
396  template<typename type_t, typename param_t>
397  bool is(param_t* value) {
398  try {
399  if (value == nullptr) return false;
400  return dynamic_cast<type_t*>(value) != nullptr;
401  } catch (const std::bad_cast&) {
402  return false;
403  }
404  }
405 
414  template<typename type_t, typename param_t>
415  bool is(param_t& value) {
416  return is<type_t>(&value);
417  }
418 
427  template<typename new_type_t, typename current_type_t>
428  bool is(std::shared_ptr<current_type_t>& value) {
429  auto result = std::dynamic_pointer_cast<new_type_t>(value);
430  if (result) return true;
431  return false;
432  }
433 }
bool is< float >(float value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:122
Contains xtd fundamental types.
int_least8_t sbyte
Represents a 8-bit signed integer.
Definition: types.h:173
long double decimal
Represents a decimal-precision floating-point number.
Definition: types.h:96
Contains xtd::parse methods.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
bool is< double >(double value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:102
uint_least16_t uint16
Represents a 16-bit unsigned integer.
Definition: types.h:228
bool is< uint16 >(uint16 value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:262
bool is< int64 >(int64 value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:202
bool is< int32 >(int32 value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:182
Contains std::any type and std::bad_any_cast exception.
bool is< int16 >(int16 value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:162
bool is< bool >(bool value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:62
bool is< uint64 >(uint64 value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:302
bool is< sbyte >(sbyte value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:142
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition: types.h:206
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
int_least16_t int16
Represents a 16-bit signed integer.
Definition: types.h:118
bool is< slong >(slong value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:222
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition: types.h:272
uint_least64_t uint64
Represents a 64-bit unsigned integer.
Definition: types.h:250
int_least64_t int64
Represents a 64-bit signed integer.
Definition: types.h:140
uint_least8_t byte
Represents a 8-bit unsigned integer.
Definition: types.h:39
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition: types.h:239
bool is< decimal >(decimal value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:82
bool is(std::any value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:335
bool is< uint32 >(uint32 value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:282
Contains unused_ keyword.