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