ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
std_boxing.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of the \aliblong.<br>
4 * With the inclusion of this header compatibility features between \alib and the C++ standard
5 * library are provided.
6 *
7 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
8 * Published under \ref mainpage_license "Boost Software License".
9 **************************************************************************************************/
10#ifndef HPP_ALIB_COMPATIBILITY_STD_BOXING
11#define HPP_ALIB_COMPATIBILITY_STD_BOXING
12
13#if !defined(HPP_ALIB) && !defined(ALIB_DOX)
14# include "alib/alib.hpp"
15#endif
16
18
19#if !defined(HPP_ALIB_COMPATIBILITY_STD_CHARACTERS)
21#endif
22
23#if !defined(HPP_ALIB_BOXING_BOXING)
24# include "alib/boxing/boxing.hpp"
25#endif
26
27#if !defined(_GLIBCXX_FUNCTIONAL) && !defined(_FUNCTIONAL_)
28 #include <functional>
29#endif
30
31
32namespace alib { namespace boxing {
33
34
35#if defined(ALIB_DOX)
36/**
37 * This namespace contains sub-namespaces that provide compatibility of 3rd-party types and
38 * module \alib_boxing_nl.<br>
39 * The entities of those namespaces become available with the inclusion of optional "compatibility"
40 * headers found in folder \alibsrcdir{compatibility}.
41 */
42namespace compatibility {
43
44/**
45 * This namespace documents compatibility features of \alib_boxing_nl and the
46 * standard C++ class library found in namespace \c std.
47 */
48namespace std {
49#endif
50
51/**
52 * Specialization of struct \b %T_Boxer for template type <c>std::array<T, Size>></c>
53 * Instead of boxing a pointer to the array object, a boxed array is stored, hence a pointer
54 * to the first element contents and the array's length.
55 *
56 * To enable this behavior, header file
57 * "alib/compatibility/std_boxing.hpp"
58 *
59 * needs to be included in the corresponding compilation unit.
60 *
61 * Excluded from the specialization are character arrays.
62 * Boxing of \c std::array instances of those types is customized by the specialization of
63 * \alib{characters;T_CharArray}, as described in manual chapter
64 * \ref alib_boxing_strings "10. Boxing Character Strings".
65 *
66 * \note As mandatory, the specialization is defined in \ref alib::boxing.
67 * To keep the reference documentation of that namespace clean, it is documented here.
68 *
69 * @tparam TElement The element type of the array.
70 * @tparam N The size of the array.
71 */
72template<typename TElement, size_t N>
73struct T_Boxer<std::array<TElement, N>, ATMP_VOID_IF(!characters::TT_IsChar<TElement>::value) >
74{
75 /** Mapped type is \c TElement[]. */
77
78 /**
79 * Implementation of custom boxing for template class std::array
80 * @param target The placeholder of the target box.
81 * @param value The object to box.
82 */
83 static void Write( Placeholder& target, const std::array<TElement, N>& value)
84 {
85 target.Write( value.data(), static_cast<integer>( N ) );
86 }
87
88 /**
89 * Forbid unboxing by declaring Read as void.
90 * @param src Ignored.
91 */
92 static void Read( const Placeholder& src);
93};
94
95
96/**
97 * Specialization of struct \b %T_Boxer for template type <c>std::vector<T, std::allocator<T>></c>
98 * Instead of boxing a pointer to the vector object, a boxed array is stored, hence a pointer
99 * to the first element contents and the array length.
100 *
101 * To enable this behavior, header file
102 * "alib/compatibility/std_boxing.hpp"
103 *
104 * needs to be included in the corresponding compilation unit.
105 *
106 * Excluded from the specialization are character arrays.
107 * Boxing of \c std::vector instances of those types is customized by the specialization of
108 * \alib{characters;T_CharArray}, as described in manual chapter
109 * \ref alib_boxing_strings "10. Boxing Character Strings".
110 *
111 * \note As mandatory, the specialization is defined in \ref alib::boxing.
112 * To keep the reference documentation of that namespace clean, it is documented here.
113 *
114 * @tparam TElement The element type of the vector.
115 */
116DOX_MARKER([DOX_ALIB_BOXING_CUSTOM_VECTOR])
117template<typename TElement>
118struct T_Boxer<std::vector<TElement>, ATMP_VOID_IF(!characters::TT_IsChar<TElement>::value) >
119{
120 /** Mapped type is \c TElement[]. */
122
123 /**
124 * Implementation of custom boxing for template class std::vector
125 * @param target The placeholder of the target box.
126 * @param value The object to box.
127 */
128 static void Write( Placeholder& target, const std::vector<TElement>& value)
129 {
130 target.Write( value.data(), static_cast<integer>( value.size() ) );
131 }
132
133 /**
134 * Forbid unboxing by declaring Read as void.
135 * @param src Ignored.
136 */
137 static void Read( const Placeholder& src);
138};
139DOX_MARKER([DOX_ALIB_BOXING_CUSTOM_VECTOR])
140
141
142#if defined(ALIB_DOX)
143}} // namespace alib::boxing[::compatibility::std]
144#endif
145
146
147
148// #################################################################################################
149// ############# Utility methods in namespace alib::compatibility::std ################
150// #################################################################################################
151
152
153namespace compatibility { namespace std {
154
155/**
156 * Creates a deep copy of a boxed C++ array type by appending its contents to a given
157 * \c std::vector of corresponding element type.<br>
158 * Note that no type checks are performed on the given box.
159 *
160 * @tparam TElement The element type.
161 * @param target The target vector to fill.
162 * @param box The source box of type \p{TElement[]}.
163 */
164DOX_MARKER([DOX_ALIB_BOXING_SAMPLE_ARR_UNBOX_VECTOR_IMPLEMENTATION])
165template<typename TElement>
166inline void CopyToVector( ::std::vector<TElement>& target, const Box& box )
167{
168 target.reserve( target.size() + static_cast<size_t>( box.UnboxLength() ) );
169 for( integer i= 0 ; i < box.UnboxLength() ; ++i )
170 target.emplace_back( box.UnboxElement<TElement>( i ) );
171}
172DOX_MARKER([DOX_ALIB_BOXING_SAMPLE_ARR_UNBOX_VECTOR_IMPLEMENTATION])
173
174
175
177
178/**
179 * Initializes \alib_boxing_nl in respect to <c>std::string</c>-types.
180 *
181 * This method is \b not automatically invoked with function \alib{Bootstrap}, because support
182 * for boxing <c>std::string</c>-types is optional and provided with the inclusion of header
183 * \alibheader{compatibility/std_boxing.hpp}.
184 *
185 * In general boxing of <c>std::string</c>-types works well without the one-time invocation of
186 * this function at the bootstrap of a process.
187 * This method registers box-function \alib{boxing;FAppend} for <c>std::string</c>-types
188 * types when \ref alib_boxing_customizing_identity "custom boxing is bypassed" by wrapping the
189 * types in \c std::reference_wrapper<T>.
190 * The function is implemented with the help of \alib{boxing;FAppend::WrappedAppendable}
191 * for wrapped <c>std::string</c>-types, each for character types \b nchar and \b wchar.
192 *
193 * \note
194 * If invoked after bootstrap and modules \alib_threads_nl and \alib_monomem_nl are included in
195 * the \alibdist, mutex \alib{monomem;GlobalAllocatorLock} has to be locked prior to an
196 * invocation.
197 * Bootstrapping may look as follows:
198 * \snippet "gtest_main.cpp" DOX_ALIB_COMPATIBILITY_BOOTSTRAP
199 *
200 * \note
201 * Alternatively, bootstrapping can be performed until \alib{BootstrapPhases::PrepareConfig}
202 * and then this function can be invoked. In this case, no locking is necessary.
203 *
204 */
206{
207 #if ALIB_STRINGS && ALIB_BOXING
208
209 alib::boxing::BootstrapRegister<FAppend<nchar>, boxing::TMappedTo<::std::reference_wrapper<::std::string>>>( boxing::FAppend<nchar>::WrappedAppendable<::std::string> );
210 alib::boxing::BootstrapRegister<FAppend<wchar>, boxing::TMappedTo<::std::reference_wrapper<::std::string>>>( boxing::FAppend<wchar>::WrappedAppendable<::std::string> );
211
212 alib::boxing::BootstrapRegister<FAppend<nchar>, boxing::TMappedTo<::std::reference_wrapper<::std::wstring >>>( boxing::FAppend<nchar>::WrappedAppendable<::std::wstring >);
213 alib::boxing::BootstrapRegister<FAppend<wchar>, boxing::TMappedTo<::std::reference_wrapper<::std::wstring >>>( boxing::FAppend<wchar>::WrappedAppendable<::std::wstring >);
214 #if ALIB_SIZEOF_WCHAR_T == 4
215 alib::boxing::BootstrapRegister<FAppend<nchar>, boxing::TMappedTo<::std::reference_wrapper<::std::u16string>>>( boxing::FAppend<nchar>::WrappedAppendable<::std::u16string>);
216 alib::boxing::BootstrapRegister<FAppend<wchar>, boxing::TMappedTo<::std::reference_wrapper<::std::u16string>>>( boxing::FAppend<wchar>::WrappedAppendable<::std::u16string>);
217 #else
218 alib::boxing::BootstrapRegister<FAppend<nchar>, boxing::TMappedTo<::std::reference_wrapper<::std::u32string>>>( boxing::FAppend<nchar>::WrappedAppendable<::std::u32string>);
219 alib::boxing::BootstrapRegister<FAppend<wchar>, boxing::TMappedTo<::std::reference_wrapper<::std::u32string>>>( boxing::FAppend<wchar>::WrappedAppendable<::std::u32string>);
220 #endif
221
222 #endif
223}
224
225
226}}}} // namespace [alib::boxing::custom::std]
227
228
229#endif // HPP_ALIB_COMPATIBILITY_STD_BOXING
integer UnboxLength() const
Definition box.inl:979
TElementType & UnboxElement(integer idx) const
Definition box.inl:999
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:190
#define ATMP_VOID_IF(Cond)
Definition tmp.hpp:52
void CopyToVector(::std::vector< TElement > &target, const Box &box)
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
void Write(const TMapped &value)