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