BDE 4.14.0 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
219 /// supply memory. Note that no memory is allocated until `append` or
220 /// `pushBack` methods are called on this object.
222
223 /// Create a `DatumMapBuilder` object managing the ownership of `Datum`
224 /// map (owning keys) having the specified `initialCapacity` and
225 /// `initialKeysCapacity` (in bytes) using the specified
226 /// `allocator` (e.g., the address of a `bslma::Allocator` object) to
227 /// supply memory.
229 SizeType initialKeysCapacity,
231
232 /// Destroy this object. If this object is holding a datum-key-owning
233 /// map that has not been adopted, then the datum-key-owning map is
234 /// disposed after destroying each of its elements.
236
237 // MANIPULATORS
238
239 /// Append the specified array `entries` having the specified `size` to
240 /// the `Datum` map (owning keys) being build by this object. The
241 /// behavior is undefined unless and `0 != entries && 0 != size` and
242 /// each element in `entries` that needs dynamic memory, is allocated
243 /// with the same allocator that was used to construct this object. The
244 /// behavior is undefined if `commit` or `sortAndCommit` has already
245 /// been called on this object.
246 void append(const DatumMapEntry *entries, SizeType size);
247
248 /// Return a `Datum` map (owning keys) value holding the elements
249 /// supplied to `pushBack` or `append`. The caller is responsible for
250 /// releasing the resources of the returned `Datum` object. Calling
251 /// this method indicates that the caller is finished building the
252 /// `Datum` map (owning keys) and no further values shall be appended.
253 /// The behavior is undefined if any method of this object, other than
254 /// its destructor, is called after `commit` invocation.
256
257 /// Append the entry with the specified `key` and the specified `value`
258 /// to the `Datum` map being build by this object. The behavior is
259 /// undefined if `value` needs dynamic memory and was allocated using a
260 /// different allocator than the one used to construct this object. The
261 /// behavior is also undefined if `commit` or `sortAndCommit` has
262 /// already been called on this object.
263 void pushBack(const bslstl::StringRef& key, const Datum& value);
264
265 /// Mark the Datum map (owning keys) being built by this object as
266 /// sorted if the specified `value` is `true` and mark it unsorted
267 /// otherwise. This function does not sort the map entries, or mark
268 /// them to be sorted later; the function should be used to indicate if
269 /// the entries are being appended in sorted order. The behavior is
270 /// undefined if `commit` or `sortAndCommit` has already been called on
271 /// this object. The behavior is also undefined if the map being
272 /// constructed is marked sorted, but the entries are not appended in
273 /// sorted order. Note also that the map being constructed is marked
274 /// unsorted by default.
275 void setSorted(bool value);
276
277 /// Return a `Datum` map (owning keys) value holding the elements
278 /// supplied to `pushBack` or `append` sorted by their keys. The caller
279 /// is responsible for releasing the resources of the returned `Datum`
280 /// object. Calling this method indicates that the caller is finished
281 /// building the `Datum` map (owning keys) and no further values shall
282 /// be appended. The behavior is undefined if any method of this
283 /// object, other than its destructor, is called after `sortAndCommit`
284 /// invocation.
286
287 // ACCESSORS
288
289 /// Return the capacity of the held `Datum` map (owning keys). The
290 /// behavior is undefined if `commit` or `sortAndCommit` has already
291 /// been called on this object. Note that similar to the capacity of a
292 /// `vector`, the returned capacity has no bearing on the value of the
293 /// `Datum` being constructed, but does indicate at which point
294 /// additional memory will be required to grow the `Datum` map being
295 /// built.
296 SizeType capacity() const;
297
298 /// Return the keys-capacity of the held `Datum` map (owning keys). The
299 /// behavior is undefined if `commit` or `sortAndCommit` has already
300 /// been called on this object. 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 keysCapacity() const;
306
307 /// Return the size of the held `Datum` map (owning keys). The behavior
308 /// is undefined if `commit` or `sortAndCommit` has already been called
309 /// on this object.
310 SizeType size() const;
311
312 // Aspects
313
314 /// @deprecated Use @ref get_allocator() instead.
315 ///
316 /// Return `get_allocator().mechanism()`.
318
319 /// Return the allocator used by this object to supply memory. Note
320 /// that if no allocator was supplied at construction the default
321 /// allocator in effect at construction is used.
323};
324
325// ============================================================================
326// INLINE DEFINITIONS
327// ============================================================================
328
329 // -------------------------------
330 // class DatumMapOwningKeysBuilder
331 // -------------------------------
332
333// ACCESSORS
334inline
339
340inline
346
347inline
349{
350 if (d_capacity) {
351 return *d_mapping.size(); // RETURN
352 }
353 return 0;
354}
355
356 // Aspects
357
358inline
363
364inline
367{
368 return d_allocator;
369}
370
371} // close package namespace
372
373
374#endif
375
376// ----------------------------------------------------------------------------
377// Copyright 2020 Bloomberg Finance L.P.
378//
379// Licensed under the Apache License, Version 2.0 (the "License");
380// you may not use this file except in compliance with the License.
381// You may obtain a copy of the License at
382//
383// http://www.apache.org/licenses/LICENSE-2.0
384//
385// Unless required by applicable law or agreed to in writing, software
386// distributed under the License is distributed on an "AS IS" BASIS,
387// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
388// See the License for the specific language governing permissions and
389// limitations under the License.
390// ----------------------------- END-OF-FILE ----------------------------------
391
392/** @} */
393/** @} */
394/** @} */
Definition bdld_datum.h:2892
Definition bdld_datummapowningkeysbuilder.h:174
bslma::Allocator * allocator() const
Definition bdld_datummapowningkeysbuilder.h:359
Datum::SizeType SizeType
Definition bdld_datummapowningkeysbuilder.h:181
allocator_type get_allocator() const
Definition bdld_datummapowningkeysbuilder.h:366
SizeType size() const
Definition bdld_datummapowningkeysbuilder.h:348
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:341
DatumMapOwningKeysBuilder(SizeType initialCapacity, SizeType initialKeysCapacity, const allocator_type &allocator)
DatumMapOwningKeysBuilder(const allocator_type &allocator)
SizeType capacity() const
Definition bdld_datummapowningkeysbuilder.h:335
Definition bdld_datum.h:2376
SizeType * size() const
Definition bdld_datum.h:5518
Definition bdld_datum.h:787
bsls::Types::size_type SizeType
Definition bdld_datum.h:1462
Definition bslma_bslallocator.h:580
BloombergLP::bslma::Allocator * mechanism() const
Definition bslma_bslallocator.h:1126
Definition bslma_allocator.h:457
Definition bslstl_stringref.h:372
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdld_datum.h:730
Definition bslma_usesbslmaallocator.h:343