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