ALib C++ Library
Library Version: 2511 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 // Ensure TTo is a base of TFrom
33 ALIB_STATIC_ASSERT( SafeCast_not_allowed, std::is_base_of<TTo ALIB_COMMA TFrom>::value
34 || std::is_base_of<TFrom ALIB_COMMA TTo >::value,
35 "TFrom and TTo must be related by inheritance.")
36
37 if constexpr (std::is_polymorphic<TTo>::value) return dynamic_cast<TTo*>(derived);
38 else return static_cast<TTo*>(derived);
39}
40
41/// Checks if a given object equals a default-constructed value of the same type.
42/// This function is useful with types that are not otherwise testable, for example,
43/// type <c>std::thread::id</c>.
44/// @param t The instance to test.
45/// @return \c true if \p{t} is default-constructed, \c false otherwise.
46template<typename T>
47requires std::default_initializable<T>
48constexpr bool IsNull(const T& t) { return t==T{}; }
49
50/// The negation of #alib::lang::IsNull.
51/// @param t The instance to test.
52/// @return \c false if \p{t} is default-constructed, \c true otherwise.
53template<typename T>
54requires std::default_initializable<T>
55constexpr bool IsNotNull(const T& t) { return t!=T{}; }
56
57/// Assigns a default-constructed value to the given instance.
58/// This function is useful with types that are not otherwise nullable, for example,
59/// type <c>std::thread::id</c>.
60/// @param t The instance to test.
61template<typename T>
62requires std::default_initializable<T>
63constexpr void SetNull(T& t) { t= T{}; }
64
65
66//==================================================================================================
67/// This static inline namespace function calls the destructor <b>~T()</b>of given \p{object}.
68/// The use of this method is recommended instead of calling the destructor directly,
69/// to increase readability of the code.
70///
71/// \attention
72/// Pointers have to be dereferenced when passed as arguments.
73/// Otherwise, this method does nothing, as the "destructor of a pointer" is called.<br>
74/// Because, for example, container types would pass a pointer here, in case their
75/// (custom) stored type is a pointer type, no static assertion can be made here.
76///
77/// @tparam T The object type. Deduced by the compiler and not need to be given.
78/// @param object The object to destruct.
79//==================================================================================================
80template<typename T>
81inline
82void Destruct(T& object) { object.~T(); }
83
84} // namespace alib[::lang]
#define ALIB_WARNINGS_RESTORE
Definition alib.inl:719
#define ALIB_STATIC_ASSERT(CondVariable, Cond, Message)
Definition alib.inl:971
#define ALIB_WARNINGS_IGNORE_DOCS
Definition alib.inl:702
#define ALIB_EXPORT
Definition alib.inl:497
constexpr void SetNull(T &t)
Definition tmp.inl:63
constexpr bool IsNotNull(const T &t)
Definition tmp.inl:55
ALIB_WARNINGS_RESTORE TTo * SafeCast(TFrom *derived)
Definition tmp.inl:31
constexpr bool IsNull(const T &t)
Definition tmp.inl:48
@ Destruct
The main phase of termination that destructs everything.
Definition camp.inl:46