BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdld_datummapowningkeysbuilder.h
Go to the documentation of this file.
1/// @file bdld_datummapowningkeysbuilder.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdld_datummapowningkeysbuilder.h -*-C++-*-
8#ifndef INCLUDED_BDLD_DATUMMAPOWNINGKEYSBUILDER
9#define INCLUDED_BDLD_DATUMMAPOWNINGKEYSBUILDER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$ $CSID$")
13
14/// @defgroup bdld_datummapowningkeysbuilder bdld_datummapowningkeysbuilder
15/// @brief Provide a utility to build a `Datum` object holding a map.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdld
19/// @{
20/// @addtogroup bdld_datummapowningkeysbuilder
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdld_datummapowningkeysbuilder-purpose"> Purpose</a>
25/// * <a href="#bdld_datummapowningkeysbuilder-classes"> Classes </a>
26/// * <a href="#bdld_datummapowningkeysbuilder-description"> Description </a>
27/// * <a href="#bdld_datummapowningkeysbuilder-usage"> Usage </a>
28/// * <a href="#bdld_datummapowningkeysbuilder-example-1-basic-syntax"> Example 1: Basic Syntax </a>
29///
30/// # Purpose {#bdld_datummapowningkeysbuilder-purpose}
31/// Provide a utility to build a `Datum` object holding a map.
32///
33/// # Classes {#bdld_datummapowningkeysbuilder-classes}
34///
35/// - bdld::DatumMapOwningKeysBuilder: utility to build a `Datum` map value
36///
37/// @see bdld_datum, bdld_datummapbuilder
38///
39/// # Description {#bdld_datummapowningkeysbuilder-description}
40/// This component defines a mechanism,
41/// `bdld::DatumMapOwningKeysBuilder`, used to populate a `Datum` map value in
42/// an exception-safe manner. In addition to providing exception safety, a
43/// `DatumMapOwningKeysBuilder` is particularly useful when the size of the map
44/// to be constructed is not known in advance. The user can append elements to
45/// the datum map as needed, and when there are no more elements to append the
46/// user calls `commit` or `sortAndCommit` and ownership of the populated
47/// `Datum` object is transferred to the caller. After calling `commit` or
48/// `sortAndCommit`, no additional elements can be appended to the `Datum` map
49/// value. Note that `sortAndCommit` method will sort the populated map (by
50/// keys) and tag the resulting `Datum` map value as sorted. Also note that the
51/// user can insert elements in a (ascending) sorted order and tag the map as
52/// sorted. The behaviour is undefined if unsorted map is tagged sorted.
53///
54/// The only difference between this component and @ref bdld_datummapbuilder is
55/// that this component makes a copy of the map entries keys and the resulting
56/// `Datum` object owns memory for the map entries keys.
57///
58/// ## Usage {#bdld_datummapowningkeysbuilder-usage}
59///
60///
61/// This section illustrates intended use of this component.
62///
63/// ### Example 1: Basic Syntax {#bdld_datummapowningkeysbuilder-example-1-basic-syntax}
64///
65///
66/// Suppose we need a map for some personal data. The values in that map can be
67/// different types and keys must be protected from destruction as this map
68/// object can be passed out of scope. The following code illustrates how to
69/// use `bdld::DatumMapOwningKeysBuilder` to create such map easily.
70///
71/// First, we need data to fill our map:
72/// @code
73/// bslma::TestAllocator ta("test", veryVeryVerbose);
74///
75/// bsl::string firstName = "firstName";
76/// bsl::string lastName = "lastName";
77/// bsl::string gender = "gender";
78/// bsl::string age = "age";
79///
80/// DatumMapEntry bartData[] = {
81/// DatumMapEntry(firstName, Datum::createStringRef("Bart", &ta)),
82/// DatumMapEntry(lastName, Datum::createStringRef("Simpson", &ta)),
83/// DatumMapEntry(gender, Datum::createStringRef("male", &ta)),
84/// DatumMapEntry(age, Datum::createInteger(10))
85/// };
86///
87/// const size_t DATA_SIZE = sizeof(bartData) / sizeof(DatumMapEntry);
88/// const size_t KEYS_SIZE = firstName.length()
89/// + lastName.length()
90/// + gender.length()
91/// + age.length();
92/// @endcode
93/// Next, we create an object of `DatumMapOwningKeysBuilder` class with initial
94/// capacity sufficient for storing all our data:
95/// @code
96/// DatumMapOwningKeysBuilder builder(DATA_SIZE, KEYS_SIZE, &ta);
97/// @endcode
98/// Then, we load our builder with these data:
99/// @code
100/// for (size_t i = 0; i < DATA_SIZE; ++i) {
101/// builder.pushBack(bartData[i].key(), bartData[i].value());
102/// }
103/// @endcode
104/// Next, we adopt the map, held by our builder, by newly created `Datum`
105/// object:
106/// @code
107/// Datum bart = builder.commit();
108/// @endcode
109/// Now, we can check that all data have been correctly added to the map at the
110/// required order:
111/// @code
112/// assert(true == bart.isMap());
113/// assert(DATA_SIZE == bart.theMap().size());
114///
115/// assert("firstName" == bart.theMap()[0].key());
116/// assert(true == bart.theMap()[0].value().isString());
117/// assert("Bart" == bart.theMap()[0].value().theString());
118///
119/// assert("lastName" == bart.theMap()[1].key());
120/// assert(true == bart.theMap()[1].value().isString());
121/// assert("Simpson" == bart.theMap()[1].value().theString());
122///
123/// assert("gender" == bart.theMap()[2].key());
124/// assert(true == bart.theMap()[2].value().isString());
125/// assert("male" == bart.theMap()[2].value().theString());
126///
127/// assert("age" == bart.theMap()[3].key());
128/// assert(true == bart.theMap()[3].value().isInteger());
129/// assert(10 == bart.theMap()[3].value().theInteger());
130/// @endcode
131/// Finally, we destroy the `Datum` object to release all allocated memory
132/// correctly:
133/// @code
134/// Datum::destroy(bart, &ta);
135/// assert(0 == ta.numBytesInUse());
136/// @endcode
137/// @}
138/** @} */
139/** @} */
140
141/** @addtogroup bdl
142 * @{
143 */
144/** @addtogroup bdld
145 * @{
146 */
147/** @addtogroup bdld_datummapowningkeysbuilder
148 * @{
149 */
150
151#include <bdlscm_version.h>
152
153#include <bdld_datum.h>
154
155#include <bslma_allocator.h>
156#include <bslma_bslallocator.h>
158
160
161#include <bsls_types.h>
162
163
164namespace bdld {
165
166 // ===============================
167 // class DatumMapOwningKeysBuilder
168 // ===============================
169
170/// This `class` provides a mechanism to build a `Datum` object having a map
171/// (owning keys) value in an exception-safe manner.
172///
173/// See @ref bdld_datummapowningkeysbuilder
175
176 public:
177 // TYPES
178
179 /// `SizeType` is an alias for a signed value, representing the capacity,
180 /// *keys-capacity*, size or *keys-size* of a datum-key-owning map.
182
184
185 private:
186 // DATA
187 DatumMutableMapOwningKeysRef d_mapping; // mutable access to the
188 // datum-key-owning map
189
190 SizeType d_capacity; // capacity of the
191 // datum-key-owning map
192
193 SizeType d_keysCapacity; // keys-capacity of the
194 // datum-key-owning map (in
195 // bytes)
196
197 bool d_sorted; // underlying map is sorted
198 // or not
199
200 allocator_type d_allocator; // allocator
201
202 private:
203 // NOT IMPLEMENTED
206
207 public:
208 // TRAITS
209
210 /// `DatumMapOwningKeysBuilder` is allocator-aware.
213
214 // CREATORS
215
216 /// Create a `DatumMapOwningKeysBuilder` object that will administer the
217 /// process of building a `Datum` map (owning keys) using the specified
218 /// `allocator` (e.g., the address of a `bslma::Allocator` object) to supply memory.
219 ///
220 /// \note Note that no memory is allocated until `append` or
221 /// `pushBack` methods are called on this object.
223
224 /// Create a `DatumMapBuilder` object managing the ownership of `Datum`
225 /// map (owning keys) having the specified `initialCapacity` and
226 /// `initialKeysCapacity` (in bytes) using the specified
227 /// `allocator` (e.g., the address of a `bslma::Allocator` object) to
228 /// supply memory.
230 SizeType initialKeysCapacity,
232
233 /// Destroy this object. If this object is holding a datum-key-owning
234 /// map that has not been adopted, then the datum-key-owning map is
235 /// disposed after destroying each of its elements.
237
238 // MANIPULATORS
239
240 /// Append the specified array `entries` having the specified `size` to
241 /// the `Datum` map (owning keys) being build by this object.
242 ///
243 /// \pre The behavior is undefined unless and `0 != entries && 0 != size` and
244 /// each element in `entries` that needs dynamic memory, is allocated
245 /// with the same allocator that was used to construct this object.
246 ///
247 /// \pre The behavior is undefined if `commit` or `sortAndCommit` has already
248 /// been called on this object.
249 void append(const DatumMapEntry *entries, SizeType size);
250
251 /// Return a `Datum` map (owning keys) value holding the elements
252 /// supplied to `pushBack` or `append`. The caller is responsible for
253 /// releasing the resources of the returned `Datum` object. Calling
254 /// this method indicates that the caller is finished building the
255 /// `Datum` map (owning keys) and no further values shall be appended.
256 ///
257 /// \pre The behavior is undefined if any method of this object, other than
258 /// its destructor, is called after `commit` invocation.
260
261 /// Append the entry with the specified `key` and the specified `value`
262 /// to the `Datum` map being build by this object.
263 ///
264 /// \pre The behavior is undefined if `value` needs dynamic memory and was allocated using a
265 /// different allocator than the one used to construct this object. The
266 /// behavior is also undefined if `commit` or `sortAndCommit` has
267 /// already been called on this object.
268 void pushBack(const bslstl::StringRef& key, const Datum& value);
269
270 /// Mark the Datum map (owning keys) being built by this object as
271 /// sorted if the specified `value` is `true` and mark it unsorted
272 /// otherwise. This function does not sort the map entries, or mark
273 /// them to be sorted later; the function should be used to indicate if
274 /// the entries are being appended in sorted order.
275 ///
276 /// \pre The behavior is undefined if `commit` or `sortAndCommit` has already been called on
277 /// this object. The behavior is also undefined if the map being
278 /// constructed is marked sorted, but the entries are not appended in
279 /// sorted order. Note also that the map being constructed is marked
280 /// unsorted by default.
281 void setSorted(bool value);
282
283 /// Return a `Datum` map (owning keys) value holding the elements
284 /// supplied to `pushBack` or `append` sorted by their keys. The caller
285 /// is responsible for releasing the resources of the returned `Datum`
286 /// object. Calling this method indicates that the caller is finished
287 /// building the `Datum` map (owning keys) and no further values shall be appended.
288 ///
289 /// \pre The behavior is undefined if any method of this
290 /// object, other than its destructor, is called after `sortAndCommit`
291 /// invocation.
293
294 // ACCESSORS
295
296 /// Return the capacity of the held `Datum` map (owning keys).
297 ///
298 /// \pre The behavior is undefined if `commit` or `sortAndCommit` has already been called on this object.
299 ///
300 /// \note Note that similar to the capacity of a
301 /// `vector`, the returned capacity has no bearing on the value of the
302 /// `Datum` being constructed, but does indicate at which point
303 /// additional memory will be required to grow the `Datum` map being
304 /// built.
305 SizeType capacity() const;
306
307 /// Return the keys-capacity of the held `Datum` map (owning keys).
308 ///
309 /// \pre The behavior is undefined if `commit` or `sortAndCommit` has already been called on this object.
310 ///
311 /// \note Note that similar to the capacity of a
312 /// `vector`, the returned capacity has no bearing on the value of the
313 /// `Datum` being constructed, but does indicate at which point
314 /// additional memory will be required to grow the `Datum` map being
315 /// built.
316 SizeType keysCapacity() const;
317
318 /// Return the size of the held `Datum` map (owning keys).
319 ///
320 /// \pre The behavior is undefined if `commit` or `sortAndCommit` has already been called
321 /// on this object.
322 SizeType size() const;
323
324 // Aspects
325
326 /// Return `get_allocator().mechanism()`.
327 ///
328 /// @deprecated Use @ref get_allocator() instead.
330
331 /// Return the allocator used by this object to supply memory.
332 ///
333 /// \note Note that if no allocator was supplied at construction the default
334 /// allocator in effect at construction is used.
336};
337
338// ============================================================================
339// INLINE DEFINITIONS
340// ============================================================================
341
342 // -------------------------------
343 // class DatumMapOwningKeysBuilder
344 // -------------------------------
345
346// ACCESSORS
347inline
352
353inline
359
360inline
362{
363 if (d_capacity) {
364 return *d_mapping.size(); // RETURN
365 }
366 return 0;
367}
368
369 // Aspects
370
371inline
376
377inline
380{
381 return d_allocator;
382}
383
384} // close package namespace
385
386
387#endif
388
389// ----------------------------------------------------------------------------
390// Copyright 2020 Bloomberg Finance L.P.
391//
392// Licensed under the Apache License, Version 2.0 (the "License");
393// you may not use this file except in compliance with the License.
394// You may obtain a copy of the License at
395//
396// http://www.apache.org/licenses/LICENSE-2.0
397//
398// Unless required by applicable law or agreed to in writing, software
399// distributed under the License is distributed on an "AS IS" BASIS,
400// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
401// See the License for the specific language governing permissions and
402// limitations under the License.
403// ----------------------------- END-OF-FILE ----------------------------------
404
405/** @} */
406/** @} */
407/** @} */
Definition bdld_datum.h:3007
Definition bdld_datummapowningkeysbuilder.h:174
bslma::Allocator * allocator() const
Definition bdld_datummapowningkeysbuilder.h:372
Datum::SizeType SizeType
Definition bdld_datummapowningkeysbuilder.h:181
allocator_type get_allocator() const
Definition bdld_datummapowningkeysbuilder.h:379
SizeType size() const
Definition bdld_datummapowningkeysbuilder.h:361
void pushBack(const bslstl::StringRef &key, const Datum &value)
BSLMF_NESTED_TRAIT_DECLARATION(DatumMapOwningKeysBuilder, bslma::UsesBslmaAllocator)
DatumMapOwningKeysBuilder is allocator-aware.
void append(const DatumMapEntry *entries, SizeType size)
bsl::allocator< char > allocator_type
Definition bdld_datummapowningkeysbuilder.h:183
SizeType keysCapacity() const
Definition bdld_datummapowningkeysbuilder.h:354
DatumMapOwningKeysBuilder(SizeType initialCapacity, SizeType initialKeysCapacity, const allocator_type &allocator)
DatumMapOwningKeysBuilder(const allocator_type &allocator)
SizeType capacity() const
Definition bdld_datummapowningkeysbuilder.h:348
Definition bdld_datum.h:2474
SizeType * size() const
Definition bdld_datum.h:5715
Definition bdld_datum.h:799
bsls::Types::size_type SizeType
Definition bdld_datum.h:1478
Definition bslma_bslallocator.h:588
BloombergLP::bslma::Allocator * mechanism() const
Definition bslma_bslallocator.h:1146
Definition bslma_allocator.h:545
Definition bslstl_stringref.h:374
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdld_datum.h:740
Definition bslma_usesbslmaallocator.h:344