ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
tmp.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_lang of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib::lang {
9
10/// A type-tag that is a type not equal to any other type.
11/// For example, this is used with macro \ref ALIB_HAS_METHOD.
12struct UnknownTag {};
13
14
16
17/// A concept to identify type <c>std::nullptr_t</c>.
18/// @tparam T The type to be tested for being \c std::nullptr_t.
19template <typename T>
20concept IsNullptr = std::same_as<T, std::nullptr_t>;
21
23
24/// Cast function that chooses either <c>static_cast</c> or <c>dynamic_cast</c>, dependent
25/// on whether type \p{TTo} is polymorphic or not.
26/// @tparam TTo The type to cast down to.
27/// @tparam TFrom The type to cast from.
28/// @param derived A pointer to the derived type.
29/// @return A pointer to the base type.
30template <typename TTo, typename TFrom>
31TTo* SafeCast(TFrom* derived)
32{
33 // Ensure TTo is a base of TFrom
34 ALIB_STATIC_ASSERT( SafeCast_not_allowed, std::is_base_of<TTo ALIB_COMMA TFrom>::value
35 || std::is_base_of<TFrom ALIB_COMMA TTo >::value,
36 "TFrom and TTo must be related by inheritance.")
37
38 if constexpr (std::is_polymorphic<TTo>::value) return dynamic_cast<TTo*>(derived);
39 else return static_cast<TTo*>(derived);
40}
41
42/// Checks if a given object equals a default-constructed value of the same type.
43/// This function is useful with types that are not otherwise testable, for example,
44/// type <c>std::thread::id</c>.
45/// @param t The instance to test.
46/// @return \c true if \p{t} is default-constructed, \c false otherwise.
47template<typename T>
48requires std::default_initializable<T>
49constexpr bool IsNull(const T& t) { return t==T{}; }
50
51/// The negation of #alib::lang::IsNull.
52/// @param t The instance to test.
53/// @return \c false if \p{t} is default-constructed, \c true otherwise.
54template<typename T>
55requires std::default_initializable<T>
56constexpr bool IsNotNull(const T& t) { return t!=T{}; }
57
58/// Assigns a default-constructed value to the given instance.
59/// This function is useful with types that are not otherwise nullable, for example,
60/// type <c>std::thread::id</c>.
61/// @param t The instance to test.
62template<typename T>
63requires std::default_initializable<T>
64constexpr void SetNull(T& t) { t= T{}; }
65
66
67//==================================================================================================
68/// This static inline namespace function calls the destructor <b>~T()</b>of given \p{object}.
69/// The use of this method is recommended instead of calling the destructor directly,
70/// to increase readability of the code.
71///
72/// \attention
73/// Pointers have to be dereferenced when passed as arguments.
74/// Otherwise, this method does nothing, as the "destructor of a pointer" is called.<br>
75/// Because, for example, container types would pass a pointer here, in case their
76/// (custom) stored type is a pointer type, no static assertion can be made here.
77///
78/// @tparam T The object type. Deduced by the compiler and not need to be given.
79/// @param object The object to destruct.
80//==================================================================================================
81template<typename T>
82inline
83void Destruct(T& object) { object.~T(); }
84
85} // namespace alib[::lang]
86
87
88
89
#define ALIB_WARNINGS_RESTORE
Definition alib.inl:705
#define ALIB_STATIC_ASSERT(CondVariable, Cond, Message)
Definition alib.inl:954
#define ALIB_WARNINGS_IGNORE_DOCS
Definition alib.inl:688
#define ALIB_EXPORT
Definition alib.inl:488
constexpr void SetNull(T &t)
Definition tmp.inl:64
constexpr bool IsNotNull(const T &t)
Definition tmp.inl:56
ALIB_WARNINGS_RESTORE TTo * SafeCast(TFrom *derived)
Definition tmp.inl:31
constexpr bool IsNull(const T &t)
Definition tmp.inl:49
@ Destruct
The main phase of termination that destructs everything.
Definition camp.inl:46