BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlar_arrayref.h
Go to the documentation of this file.
1/// @file bdlar_arrayref.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlar_arrayref.h -*-C++-*-
8#ifndef INCLUDED_BDLAR_ARRAYREF
9#define INCLUDED_BDLAR_ARRAYREF
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlar_arrayref bdlar_arrayref
15/// @brief Provide ...
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlar
19/// @{
20/// @addtogroup bdlar_arrayref
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlar_arrayref-purpose"> Purpose</a>
25/// * <a href="#bdlar_arrayref-classes"> Classes </a>
26/// * <a href="#bdlar_arrayref-description"> Description </a>
27///
28/// # Purpose {#bdlar_arrayref-purpose}
29/// Provide ...
30///
31/// # Classes {#bdlar_arrayref-classes}
32///
33/// - bdlar::ArrayRef: ...
34/// - bdlar::ArrayConstRef: ...
35///
36/// # Description {#bdlar_arrayref-description}
37/// This component provides ...
38///
39/// @}
40/** @} */
41/** @} */
42
43/** @addtogroup bdl
44 * @{
45 */
46/** @addtogroup bdlar
47 * @{
48 */
49/** @addtogroup bdlar_arrayref
50 * @{
51 */
52
53#include <bdlar_arrayvtable.h>
55#include <bdlar_typecategory.h>
56
58
59#include <bsl_type_traits.h>
60
61
62namespace bdlar {
63
64// Forward
65class AnyRef;
66class AnyConstRef;
67
68 // ==============
69 // class ArrayRef
70 // ==============
71
72class ArrayRef {
73 // DATA
74 void *d_object_p;
75 const ArrayVtable *d_vtable_p;
76
77 public:
78 // CREATORS
79
80 /// Create `ArrayRef` object that type erases the supplied `array` object.
81 template <class t_ARRAY>
82 explicit ArrayRef(t_ARRAY& array,
83 typename bsl::enable_if<
84 IsArray<t_ARRAY>::value>::type * = 0);
85
86 /// Create `ArrayRef` object.
87 /// \pre The behaviour is undefined unless
88 /// `objectAddress` and `vtable` point to the valid objects.
90
91 // MANIPULATORS
92
93 /// Invoke the specified `manipulator` on the array element at the
94 /// specified `index`. The supplied `manipulator` must be a callable type
95 /// that can be called as if it had the following signature:
96 /// @code
97 /// int manipulator(ELEMENT *refOrValue);
98 /// @endcode
99 /// Return the value from the invocation of `manipulator`.
100 ///
101 /// \pre The behavior is undefined unless `0 <= index < size()`.
102 template <class t_MANIPULATOR>
103 int manipulateElement(t_MANIPULATOR& manipulator, int index) const;
104
105 /// Reset the referred object to the default value.
106 void reset() const;
107
108 /// Set the size to the specified `newSize`. If `newSize > size()`, then
109 /// `newSize - size()` elements with default values are appended to the
110 /// array. If `newSize < size()`, then the `size() - newSize` elements at the end of the array are destroyed.
111 ///
112 /// \pre The behavior is undefined unless
113 /// `0 <= newSize`.
114 void resize(int newSize) const;
115
116 // ACCESSORS
117
118 /// Invoke the specified `accessor` on the array element at the specified
119 /// `index`. The supplied `accessor` must be a callable type that can be
120 /// called as if it had the following signature:
121 /// @code
122 /// int accessor(const ELEMENT& refOrValue);
123 /// @endcode
124 /// Return the value from the invocation of `accessor`.
125 ///
126 /// \pre The behavior is undefined unless `0 <= index < size()`.
127 template <class t_ACCESSOR>
128 int accessElement(t_ACCESSOR& accessor, int index) const;
129
130 /// Return the element type category of the wrapped array.
132
133 /// Return a pointer to the vtable.
134 const ArrayVtable *vtable() const;
135
136 /// Return a pointer to the wrapped array.
137 void *objectAddress() const;
138
139 /// Return the number of elements in the wrapped array.
140 bsl::size_t size() const;
141};
142
143 // ===================
144 // class ArrayConstRef
145 // ===================
146
148 // DATA
149 const void *d_object_p;
150 const ArrayConstVtable *d_vtable_p;
151
152 public:
153 // CREATORS
154
155 /// Create `ArrayConstRef` object that type erases the supplied `array`
156 /// object.
157 template <class t_ARRAY>
158 explicit ArrayConstRef(const t_ARRAY& array,
159 typename bsl::enable_if<
160 IsArray<t_ARRAY>::value>::type * = 0);
161
162 /// Create `ArrayConstRef` object.
163 /// \pre The behaviour is undefined unless
164 /// `objectAddress` and `vtable` point to the valid objects.
166
167 /// Create `ArrayConstRef` object that wraps the same object as the
168 /// specified `object`.
169 ArrayConstRef(const ArrayRef& object); // IMPLICIT
170
171 // ACCESSORS
172
173 /// Invoke the specified `accessor` on the array element at the specified
174 /// `index`. The supplied `accessor` must be a callable type that can be
175 /// called as if it had the following signature:
176 /// @code
177 /// int accessor(const ELEMENT& ref);
178 /// @endcode
179 /// Return the value from the invocation of `accessor`.
180 ///
181 /// \pre The behavior is undefined unless `0 <= index < size()`.
182 template <class t_ACCESSOR>
183 int accessElement(t_ACCESSOR& accessor, int index) const;
184
185 /// Return the element type category of the wrapped array.
187
188 /// Return a pointer to the vtable.
189 const ArrayConstVtable *vtable() const;
190
191 /// Return a pointer to the wrapped array.
192 const void *objectAddress() const;
193
194 /// Return the number of elements in the wrapped array.
195 bsl::size_t size() const;
196
197 /// Return the `bdlat_TypeName::xsdName` value for the underlying object.
198 const char *xsdName(int format) const;
199};
200
201// ============================================================================
202// INLINE DEFINITIONS
203// ============================================================================
204
205 // --------------
206 // class ArrayRef
207 // --------------
208
209// CREATORS
210template <class t_ARRAY>
211inline
212ArrayRef::ArrayRef(t_ARRAY& array,
213 typename bsl::enable_if<
215: d_object_p(&array)
216, d_vtable_p(ArrayVtableUtil::getVtable<t_ARRAY>())
217{
218}
219
220inline
221ArrayRef::ArrayRef(void *objectAddress, const ArrayVtable *vtable)
222: d_object_p(objectAddress)
223, d_vtable_p(vtable)
224{
225}
226
227// MANIPULATORS
228template <class t_MANIPULATOR>
229inline
230int ArrayRef::manipulateElement(t_MANIPULATOR& manipulator, int index) const
231{
233 manipulatorRef(manipulator);
234 return d_vtable_p->d_manipulateElement_fp(d_object_p,
235 manipulatorRef,
236 index);
237}
238
239inline
240void ArrayRef::reset() const
241{
242 return d_vtable_p->d_reset_fp(d_object_p);
243}
244
245inline
246void ArrayRef::resize(int newSize) const
247{
248 return d_vtable_p->d_resize_fp(d_object_p, newSize);
249}
250
251// ACCESSORS
252template <class t_ACCESSOR>
253inline
254int ArrayRef::accessElement(t_ACCESSOR& accessor, int index) const
255{
257 accessorRef(accessor);
258 return d_vtable_p->d_const.d_accessElement_fp(d_object_p,
259 accessorRef,
260 index);
261}
262
263inline
268
269inline
271{
272 return d_vtable_p;
273}
274
275inline
277{
278 return d_object_p;
279}
280
281inline
282bsl::size_t ArrayRef::size() const
283{
284 return d_vtable_p->d_const.d_size_fp(d_object_p);
285}
286
287// FREE FUNCTIONS
288// Customization-point functions (`bdlat_ArrayFunctions`)
289
290template <class t_MANIPULATOR>
291inline
293 t_MANIPULATOR& manipulator,
294 int index)
295{
296 return array->manipulateElement(manipulator, index);
297}
298
299inline
300void bdlat_arrayResize(ArrayRef *array, int newSize)
301{
302 return array->resize(newSize);
303}
304
305template <class t_ACCESSOR>
306inline
308 t_ACCESSOR& accessor,
309 int index)
310{
311 return array.accessElement(accessor, index);
312}
313
314inline
315bsl::size_t bdlat_arraySize(const ArrayRef& array)
316{
317 return array.size();
318}
319
320inline
322{
323 ref->reset();
324}
325
326 // -------------------
327 // class ArrayConstRef
328 // -------------------
329
330// CREATORS
331template <class t_ARRAY>
332inline
333ArrayConstRef::ArrayConstRef(const t_ARRAY& array,
334 typename bsl::enable_if<
336: d_object_p(&array)
337, d_vtable_p(ArrayVtableUtil::getConstVtable<t_ARRAY>())
338{
339}
340
341inline
342ArrayConstRef::ArrayConstRef(const void *objectAddress,
343 const ArrayConstVtable *vtable)
344: d_object_p(objectAddress)
345, d_vtable_p(vtable)
346{
347}
348
349inline
351: d_object_p(object.objectAddress())
352, d_vtable_p(&object.vtable()->d_const)
353{
354}
355
356// ACCESSORS
357template <class t_ACCESSOR>
358inline
359int ArrayConstRef::accessElement(t_ACCESSOR& accessor, int index) const
360{
362 accessorRef(accessor);
363 return d_vtable_p->d_accessElement_fp(d_object_p, accessorRef, index);
364}
365
366inline
371
372inline
374{
375 return d_vtable_p;
376}
377
378inline
380{
381 return d_object_p;
382}
383
384inline
385bsl::size_t ArrayConstRef::size() const
386{
387 return d_vtable_p->d_size_fp(d_object_p);
388}
389
390inline
391const char *ArrayConstRef::xsdName(int format) const
392{
393 return d_vtable_p->d_xsdName_fp(d_object_p, format);
394}
395
396// FREE FUNCTIONS
397// Customization-point functions (`bdlat_ArrayFunctions`)
398
399template <class t_ACCESSOR>
400inline
402 t_ACCESSOR& accessor,
403 int index)
404{
405 return array.accessElement(accessor, index);
406}
407
408inline
409bsl::size_t bdlat_arraySize(const ArrayConstRef& array)
410{
411 return array.size();
412}
413
414inline
415const char *bdlat_TypeName_xsdName(const ArrayConstRef& ref, int format)
416{
417 return ref.xsdName(format);
418}
419
420} // close package namespace
421
423
424template <>
425struct IsArray<bdlar::ArrayRef> : bsl::true_type {
426};
427
428template <>
429struct IsArray<bdlar::ArrayConstRef> : bsl::true_type {
430};
431
432template <>
433struct ElementType<bdlar::ArrayRef> {
435};
436
437template <>
438struct ElementType<bdlar::ArrayConstRef> {
440};
441
442} // close namespace bdlat_ArrayFunctions
443
444
445#endif
446
447// ----------------------------------------------------------------------------
448// Copyright 2024 Bloomberg Finance L.P.
449//
450// Licensed under the Apache License, Version 2.0 (the "License");
451// you may not use this file except in compliance with the License.
452// You may obtain a copy of the License at
453//
454// http://www.apache.org/licenses/LICENSE-2.0
455//
456// Unless required by applicable law or agreed to in writing, software
457// distributed under the License is distributed on an "AS IS" BASIS,
458// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
459// See the License for the specific language governing permissions and
460// limitations under the License.
461// ----------------------------- END-OF-FILE ----------------------------------
462
463/** @} */
464/** @} */
465/** @} */
Definition bdlar_anyref.h:247
Definition bdlar_anyref.h:85
Definition bdlar_arrayref.h:147
bsl::size_t size() const
Return the number of elements in the wrapped array.
Definition bdlar_arrayref.h:385
ArrayConstRef(const t_ARRAY &array, typename bsl::enable_if< IsArray< t_ARRAY >::value >::type *=0)
Definition bdlar_arrayref.h:333
bdlat_TypeCategory::Value elementCategory() const
Return the element type category of the wrapped array.
Definition bdlar_arrayref.h:367
const ArrayConstVtable * vtable() const
Return a pointer to the vtable.
Definition bdlar_arrayref.h:373
const void * objectAddress() const
Return a pointer to the wrapped array.
Definition bdlar_arrayref.h:379
const char * xsdName(int format) const
Return the bdlat_TypeName::xsdName value for the underlying object.
Definition bdlar_arrayref.h:391
int accessElement(t_ACCESSOR &accessor, int index) const
Definition bdlar_arrayref.h:359
Definition bdlar_arrayref.h:72
void reset() const
Reset the referred object to the default value.
Definition bdlar_arrayref.h:240
void * objectAddress() const
Return a pointer to the wrapped array.
Definition bdlar_arrayref.h:276
void resize(int newSize) const
Definition bdlar_arrayref.h:246
int manipulateElement(t_MANIPULATOR &manipulator, int index) const
Definition bdlar_arrayref.h:230
bsl::size_t size() const
Return the number of elements in the wrapped array.
Definition bdlar_arrayref.h:282
const ArrayVtable * vtable() const
Return a pointer to the vtable.
Definition bdlar_arrayref.h:270
bdlat_TypeCategory::Value elementCategory() const
Return the element type category of the wrapped array.
Definition bdlar_arrayref.h:264
ArrayRef(t_ARRAY &array, typename bsl::enable_if< IsArray< t_ARRAY >::value >::type *=0)
Create ArrayRef object that type erases the supplied array object.
Definition bdlar_arrayref.h:212
int accessElement(t_ACCESSOR &accessor, int index) const
Definition bdlar_arrayref.h:254
Definition bdlar_arrayvtableutil.h:66
Definition bdlar_dynamictyperef.h:128
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlar_accessorref.h:59
void bdlat_arrayResize(ArrayRef *array, int newSize)
Definition bdlar_arrayref.h:300
int bdlat_arrayAccessElement(const ArrayRef &array, t_ACCESSOR &accessor, int index)
Definition bdlar_arrayref.h:307
const char * bdlat_TypeName_xsdName(const AnyConstRef &ref, int format)
Definition bdlar_anyref.h:716
void bdlat_valueTypeReset(AnyRef *ref)
Definition bdlar_anyref.h:550
int bdlat_arrayManipulateElement(ArrayRef *array, t_MANIPULATOR &manipulator, int index)
Definition bdlar_arrayref.h:292
bsl::size_t bdlat_arraySize(const ArrayRef &array)
Definition bdlar_arrayref.h:315
Definition bdlar_arrayref.h:422
Definition bdlar_arrayvtable.h:71
AccessElement * d_accessElement_fp
Definition bdlar_arrayvtable.h:82
ElementCategory * d_elementCategory_fp
Definition bdlar_arrayvtable.h:81
XsdName * d_xsdName_fp
Definition bdlar_arrayvtable.h:84
Size * d_size_fp
Definition bdlar_arrayvtable.h:83
Definition bdlar_arrayvtable.h:96
ArrayConstVtable d_const
Definition bdlar_arrayvtable.h:105
Reset * d_reset_fp
Definition bdlar_arrayvtable.h:107
Resize * d_resize_fp
Definition bdlar_arrayvtable.h:108
ManipulateElement * d_manipulateElement_fp
Definition bdlar_arrayvtable.h:106
Definition bdlar_typecategory.h:77
bdlar::AnyConstRef Type
Definition bdlar_arrayref.h:439
bdlar::AnyRef Type
Definition bdlar_arrayref.h:434
Definition bdlat_arrayfunctions.h:756
Definition bdlat_arrayfunctions.h:762
Value
Definition bdlat_typecategory.h:1046
Definition bslmf_enableif.h:530