ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
ALib.Compatibility.StdBoxtraits.H
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8#ifndef H_ALIB_BOXING_STDBOXTRAITS
9#define H_ALIB_BOXING_STDBOXTRAITS
10#pragma once
11#ifndef INL_ALIB
12# include "alib/alib.inl"
13#endif
14
16
17#if ALIB_BOXING
18
19#include "ALib.Lang.H"
20#include "ALib.Boxing.H"
21#include "ALib.Strings.H"
22
23//============================================= Exports ============================================
24namespace alib::boxing {
25
26#if DOXYGEN
27/// This namespace contains sub-namespaces that provide compatibility of 3rd-party types and
28/// module \alib_boxing_nl.<br>
29/// The entities of those namespaces become available with the inclusion of specific headers
30/// that import a certain C++20 Module or inject the functionality into a namespace in a
31/// traditional fashion, for example, header #"F;ALib.Boxing.StdFunctors.H".
32namespace compatibility {
33
34/// This namespace documents compatibility features of \alib_boxing_nl and the
35/// standard C++ class library found in namespace \c std.
36namespace std {
37#endif
38
39/// Specialization of struct \b %BoxTraits for template type <c>std::array<T, Size>></c>
40/// Instead of boxing a pointer to the array object, a boxed array is stored, hence a pointer
41/// to the first element contents and the array's length.
42///
43/// To enable this behavior, header-file #"F;ALib.Compatibility.StdBoxtraits.H" needs to be included in
44/// the corresponding compilation unit.
45///
46/// Excluded from the specialization are character arrays.
47/// Boxing of \c std::array instances of those types is customized by the specialization of
48/// #"ArrayTraits", as described in manual chapter
49/// #"alib_boxing_strings".
50///
51/// \note As mandatory, the specialization is defined in #"alib::boxing".
52/// To keep the reference documentation of that namespace clean, it is documented here.
53///
54/// @tparam TElement The element type of the array.
55/// @tparam N The size of the array.
56template<typename TElement, size_t N>
57requires ( !characters::IsCharacter<TElement> )
58struct BoxTraits<std::array<TElement, N> >
59{
60 /// Mapped type is \b TElement[].
61 using Mapping= TElement;
62
63 /// Mapped as array-type
64 static constexpr bool IsArray= true;
65
66 /// Implementation of custom boxing for template class std::array
67 /// @param box The placeholder of the box box.
68 /// @param value The object to box.
69 static void Write( Placeholder& box, const std::array<TElement, N>& value)
70 {
71 box.Write( value.data(), integer( N ) );
72 }
73
74 /// Forbid unboxing by declaring Read as void.
75 /// @param box Ignored.
76 static void Read( const Placeholder& box);
77};
78
79/// Specialization of struct \b %BoxTraits for template type <c>std::vector<T, std::allocator<T>></c>
80/// Instead of boxing a pointer to the vector object, a boxed array is stored, hence a pointer
81/// to the first element contents and the array length.
82///
83/// To enable this behavior, the header-file #"F;ALib.Compatibility.StdBoxtraits.H" needs to be
84/// included in the corresponding compilation unit.
85///
86/// Excluded from the specialization are character arrays.
87/// Boxing of \c std::vector instances of those types is customized by the specialization of
88/// #"ArrayTraits", as described in manual chapter
89/// #"alib_boxing_strings".
90///
91/// \note As mandatory, the specialization is defined in #"alib::boxing".
92/// To keep the reference documentation of that namespace clean, it is documented here.
93///
94/// @tparam TElement The element type of the vector.
95DOX_MARKER([DOX_BOXING_CUSTOM_VECTOR])
96template<typename TElement>
98struct BoxTraits<std::vector<TElement>>
99{
100 /// Mapped type is \c TElement[].
101 using Mapping= TElement;
102
103 /// Mapped as array-type
104 static constexpr bool IsArray= true;
105
106
107 /// Implementation of custom boxing for template <c>class std::vector</c>.
108 /// @param box The placeholder of the box box.
109 /// @param value The object to box.
110 static void Write( Placeholder& box, const std::vector<TElement>& value) {
111 box.Write( value.data(), integer( value.size() ) );
112 }
113
114 /// Forbid unboxing by declaring Read as void.
115 /// @param box Ignored.
116 static void Read( const Placeholder& box);
117};
118DOX_MARKER([DOX_BOXING_CUSTOM_VECTOR])
119
120
121#if DOXYGEN
122}} // namespace alib::boxing[::compatibility::std]
123#endif
124
125//##################################################################################################
126//###################### Utility methods in namespace alib::compatibility::std #####################
127//##################################################################################################
128namespace compatibility { namespace std {
129
130/// Creates a deep copy of a boxed C++ array type by appending its contents to a given
131/// \c std::vector of corresponding element type.<br>
132/// Note that no type checks are performed on the given box.
133///
134/// @tparam TElement The element type.
135/// @param target The target vector to fill.
136/// @param box The source box of type \p{TElement[]}.
137DOX_MARKER([DOX_BOXING_SAMPLE_ARR_UNBOX_VECTOR_IMPLEMENTATION])
138template<typename TElement>
139inline void CopyToVector( ::std::vector<TElement>& target, const Box& box )
140{
141 target.reserve( target.size() + size_t( box.UnboxLength() ) );
142 for( integer i= 0 ; i < box.UnboxLength() ; ++i )
143 target.emplace_back( box.UnboxElement<TElement>( i ) );
144}
145DOX_MARKER([DOX_BOXING_SAMPLE_ARR_UNBOX_VECTOR_IMPLEMENTATION])
146
147
148
150
151/// Initializes \alib_boxing_nl in respect to <c>std::string</c>-types.
152///
153/// This method is \b not automatically invoked with function #"Bootstrap", because support
154/// for boxing <c>std::string</c>-types is optional and provided with the inclusion of header
155/// #"F;ALib.Compatibility.StdBoxtraits.H".
156///
157/// In general, boxing of <c>std::string</c>-types works well without the one-time invocation of
158/// this function at the bootstrap of a process.
159/// This method registers box-function #"FAppend" for <c>std::string</c>-types
160/// types when #"alib_boxing_customizing_identity;custom boxing is bypassed" by wrapping the
161/// types in \c std::reference_wrapper<T>.
162/// The function is implemented with the help of #"FAppend::WrappedAppendable"
163/// for wrapped <c>std::string</c>-types, each for character types \b nchar and \b wchar.
164///
165/// \note
166/// If invoked \b after bootstrap and modules \alib_threads_nl and \alib_monomem_nl are included in
167/// the \alibbuild, mutex #"GLOBAL_ALLOCATOR_LOCK" has to be locked before an
168/// invocation.
169/// Bootstrapping may look as follows:
170/// \snippet "gtest_main.cpp" DOX_COMPATIBILITY_BOOTSTRAP
171///
172/// \note
173/// (Note, that the curly brackets create a compound that releases the automatic owner instance
174/// after the call.) <br>
175/// Alternatively, bootstrapping can be performed until #"BootstrapPhases::PrepareConfig;2"
176/// and then this function can be invoked. In this case, no locking is necessary.
177///
179{
180 #if ALIB_STRINGS && ALIB_BOXING
181
188 #if ALIB_SIZEOF_WCHAR_T == 4
191 #else
194 #endif
195
196 #endif
197}
198
199}}} // namespace [alib::boxing::custom::std]
200
201#endif // ALIB_BOXING
202#endif // H_ALIB_BOXING_STDBOXTRAITS
TElementType & UnboxElement(integer idx) const
Definition box.inl:845
integer UnboxLength() const
Definition box.inl:828
void CopyToVector(::std::vector< TElement > &target, const Box &box)
void BootstrapRegister(typename TFDecl::Signature function)
Definition box.inl:1173
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
static void WrappedAppendable(const Box &self, strings::TAString< TChar, TAllocator > &target)
static void Write(Placeholder &box, const std::array< TElement, N > &value)
static void Write(Placeholder &box, const std::vector< TElement > &value)
constexpr void Write(const TIntegral &value)