BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdld_datumintmapbuilder.h
Go to the documentation of this file.
1/// @file bdld_datumintmapbuilder.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdld_datumintmapbuilder.h -*-C++-*-
8#ifndef INCLUDED_BDLD_DATUMINTMAPBUILDER
9#define INCLUDED_BDLD_DATUMINTMAPBUILDER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$ $CSID$")
13
14/// @defgroup bdld_datumintmapbuilder bdld_datumintmapbuilder
15/// @brief Provide a utility to build a `Datum` object holding an int-map.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdld
19/// @{
20/// @addtogroup bdld_datumintmapbuilder
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdld_datumintmapbuilder-purpose"> Purpose</a>
25/// * <a href="#bdld_datumintmapbuilder-classes"> Classes </a>
26/// * <a href="#bdld_datumintmapbuilder-description"> Description </a>
27/// * <a href="#bdld_datumintmapbuilder-usage"> Usage </a>
28/// * <a href="#bdld_datumintmapbuilder-example-1-basic-syntax"> Example 1: Basic Syntax </a>
29///
30/// # Purpose {#bdld_datumintmapbuilder-purpose}
31/// Provide a utility to build a `Datum` object holding an int-map.
32///
33/// # Classes {#bdld_datumintmapbuilder-classes}
34///
35/// - bdld::DatumIntMapBuilder: utility to build `Datum` objects holding int-maps
36///
37/// @see bdld_datum
38///
39/// # Description {#bdld_datumintmapbuilder-description}
40/// This component defines a mechanism, `bdld::DatumIntMapBuilder`,
41/// used to populate a `Datum` int-map value in an exception-safe manner. In
42/// addition to providing exception safety, a `DatumIntMapBuilder` is
43/// particularly useful when the size of the int-map to be constructed is not
44/// known in advance. The user can append elements to the datum int-map as
45/// needed, and when there are no more elements to append the user calls
46/// `commit` or `sortAndCommit` and ownership of the populated `Datum` object is
47/// transferred to the caller. After calling `commit` or `sortAndCommit`, no
48/// additional elements can be appended to the `Datum` int-map value. Note that
49/// `sortAndCommit` method will sort the populated int-map (by keys) and tag the
50/// resulting `Datum` int-map value as sorted. Also note that the user can
51/// insert elements in a (ascending) sorted order and tag the int-map as sorted.
52/// The behavior is undefined if unsorted int-map is tagged sorted.
53///
54/// ## Usage {#bdld_datumintmapbuilder-usage}
55///
56///
57/// This section illustrates intended use of this component.
58///
59/// ### Example 1: Basic Syntax {#bdld_datumintmapbuilder-example-1-basic-syntax}
60///
61///
62/// Suppose we need a data map for some UX data. The keys of the map are 32-bit
63/// integers; and the values in that map can be different types. The following
64/// code illustrates how to use `bdld::DatumIntMapBuilder` to create such map
65/// easily.
66///
67/// First, we need data to fill our int-map:
68/// @code
69/// bslma::TestAllocator ta("test", veryVeryVerbose);
70///
71/// DatumIntMapEntry formData[] = {
72/// DatumIntMapEntry(1, Datum::createStringRef("Bart", &ta)),
73/// DatumIntMapEntry(2, Datum::createStringRef("Simpson", &ta)),
74/// DatumIntMapEntry(3, Datum::createStringRef("male", &ta)),
75/// DatumIntMapEntry(4, Datum::createInteger(10))
76/// };
77///
78/// const size_t DATA_SIZE = sizeof(formData) / sizeof(DatumIntMapEntry);
79/// @endcode
80/// Next, we create an object of `DatumIntMapBuilder` class with initial
81/// capacity sufficient for storing all our data:
82/// @code
83/// DatumIntMapBuilder builder(DATA_SIZE, &ta);
84/// @endcode
85/// Then, we load our builder with these data:
86/// @code
87/// for (size_t i = 0; i < DATA_SIZE; ++i) {
88/// builder.pushBack(formData[i].key(), formData[i].value());
89/// }
90/// @endcode
91/// Next, we adopt the int-map, held by our builder, by newly created `Datum`
92/// object:
93/// @code
94/// Datum form = builder.commit();
95/// @endcode
96/// Now, we can check that all data have been correctly added to the int-map at
97/// the required order:
98/// @code
99/// assert(true == form.isIntMap());
100/// assert(DATA_SIZE == form.theIntMap().size());
101///
102/// assert(1 == form.theIntMap()[0].key());
103/// assert(true == form.theIntMap()[0].value().isString());
104/// assert("Bart" == form.theIntMap()[0].value().theString());
105///
106/// assert(2 == form.theIntMap()[1].key());
107/// assert(true == form.theIntMap()[1].value().isString());
108/// assert("Simpson" == form.theIntMap()[1].value().theString());
109///
110/// assert(3 == form.theIntMap()[2].key());
111/// assert(true == form.theIntMap()[2].value().isString());
112/// assert("male" == form.theIntMap()[2].value().theString());
113///
114/// assert(4 == form.theIntMap()[3].key());
115/// assert(true == form.theIntMap()[3].value().isInteger());
116/// assert(10 == form.theIntMap()[3].value().theInteger());
117/// @endcode
118/// Finally, we destroy the `Datum` object to release all allocated memory
119/// correctly:
120/// @code
121/// Datum::destroy(form, &ta);
122/// assert(0 == ta.numBytesInUse());
123/// @endcode
124/// @}
125/** @} */
126/** @} */
127
128/** @addtogroup bdl
129 * @{
130 */
131/** @addtogroup bdld
132 * @{
133 */
134/** @addtogroup bdld_datumintmapbuilder
135 * @{
136 */
137
138#include <bdlscm_version.h>
139
140#include <bdld_datum.h>
141
142#include <bslma_bslallocator.h>
144
146
147#include <bsls_types.h>
148
149
150namespace bdld {
151
152 // ========================
153 // class DatumIntMapBuilder
154 // ========================
155
156/// This `class` provides a mechanism to build a `Datum` object having an
157/// int-map value in an exception-safe manner. See @ref bdld_datum for more
158/// information about integer maps.
159///
160/// See @ref bdld_datumintmapbuilder
162
163 public:
164 // TYPES
165
166 /// `SizeType` is an alias for a unsigned integral value, representing
167 /// the capacity or size of a datum int-map.
169
171
172 private:
173 // DATA
174 DatumMutableIntMapRef d_mapping; // mutable access to the datum int-map
175
176 SizeType d_capacity; // capacity of the datum int-map
177
178 bool d_sorted; // underlying int-map is sorted
179
180 allocator_type d_allocator; // allocator for memory
181
182 private:
183 // NOT IMPLEMENTED
185 DatumIntMapBuilder& operator=(const DatumIntMapBuilder&);
186
187 public:
188 // TRAITS
191
192 // CREATORS
193
195 explicit DatumIntMapBuilder(const allocator_type& allocator);
196 /// Create a `DatumIntMapBuilder` object that will administer the
197 /// process of building a `Datum` int-map. Optionally specify an
198 /// `initialCapacity` for the int-map. If `initialCapacity` is not
199 /// supplied, the initial capacity of the int-map is 0. Optionally
200 /// specify an `allocator` (e.g., the address of a `bslma::Allocator`
201 /// object) to supply memory; otherwise, the default allocator is used.
203 SizeType initialCapacity,
204 const allocator_type& allocator = allocator_type());
205
206 /// Destroy this object. If this object is holding a datum int-map that
207 /// has not been adopted, then the datum int-map is disposed after
208 /// destroying each of its elements.
210
211 // MANIPULATORS
212
213 /// Append the specified array `entries` having the specified `size` to
214 /// the `Datum` int-map being build by this object. The behavior is
215 /// undefined unless `0 != entries && 0 != size` and each element in
216 /// `entries` that needs dynamic memory is allocated with the same
217 /// allocator that was used to construct this object. The behavior is
218 /// undefined if `commit` or `sortAndCommit` has already been called on
219 /// this object.
220 void append(const DatumIntMapEntry *entries, SizeType size);
221
222 /// Return a `Datum` int-map value holding the elements supplied to
223 /// `pushBack` or `append`. The caller is responsible for releasing the
224 /// resources of the returned `Datum` object. Calling this method
225 /// indicates that the caller is finished building the `Datum` int-map
226 /// and no further values shall be appended. The behavior is undefined
227 /// if any method of this object, other than its destructor, is called
228 /// after `commit` invocation.
230
231 /// Append the entry with the specified `key` and the specified `value`
232 /// to the `Datum` int-map being build by this object. The behavior is
233 /// undefined if `value` needs dynamic memory and was allocated using a
234 /// different allocator than the one used to construct this object. The
235 /// behavior is also undefined if `commit` or `sortAndCommit` has
236 /// already been called on this object.
237 void pushBack(int key, const Datum& value);
238
239 /// Mark the Datum int-map being built by this object as sorted if the
240 /// specified `value` is `true` and mark it unsorted otherwise. This
241 /// function does not sort the int-map entries, or mark them to be
242 /// sorted later; the function should be used to indicate if the entries
243 /// are being appended in sorted order. The behavior is undefined if
244 /// `commit` or `sortAndCommit` has already been called on this object.
245 /// The behavior is also undefined if the int-map being constructed is
246 /// marked sorted, but the entries are not appended in sorted order.
247 /// Note also that the int-map being constructed is marked unsorted by
248 /// default.
249 void setSorted(bool value);
250
251 /// Return a `Datum` int-map value holding the elements supplied to
252 /// `pushBack` or `append` sorted by their keys. The caller is
253 /// responsible for releasing the resources of the returned `Datum`
254 /// object. Calling this method indicates that the caller is finished
255 /// building the `Datum` int-map and no further values shall be
256 /// appended. The behavior is undefined if any method of this object,
257 /// other than its destructor, is called after `sortAndCommit`
258 /// invocation.
260
261 // ACCESSORS
262
263 /// Return the capacity of the held `Datum` int-map. The behavior is
264 /// undefined if `commit` or `sortAndCommit` has already been called on
265 /// this object. Note that similar to the capacity of a `vector`, the
266 /// returned capacity has no bearing on the value of the `Datum` being
267 /// constructed, but does indicate at which point additional memory will
268 /// be required to grow the `Datum` int-map being built.
269 SizeType capacity() const;
270
271 /// Return the allocator used by this object to supply memory. Note
272 /// that if no allocator was supplied at construction the default
273 /// allocator in effect at construction is used.
275
276 /// Return the size of the held `Datum` int-map. The behavior is
277 /// undefined if `commit` or `sortAndCommit` has already been called on
278 /// this object.
279 SizeType size() const;
280};
281
282// ============================================================================
283// INLINE DEFINITIONS
284// ============================================================================
285
286 // ------------------------
287 // class DatumIntMapBuilder
288 // ------------------------
289
290// ACCESSORS
291inline
293{
294 return d_capacity;
295}
296
297inline
302
303inline
305{
306 if (d_capacity) {
307 return *d_mapping.size(); // RETURN
308 }
309 return 0;
310}
311
312} // close package namespace
313
314
315#endif
316
317// ----------------------------------------------------------------------------
318// Copyright 2017 Bloomberg Finance L.P.
319//
320// Licensed under the Apache License, Version 2.0 (the "License");
321// you may not use this file except in compliance with the License.
322// You may obtain a copy of the License at
323//
324// http://www.apache.org/licenses/LICENSE-2.0
325//
326// Unless required by applicable law or agreed to in writing, software
327// distributed under the License is distributed on an "AS IS" BASIS,
328// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
329// See the License for the specific language governing permissions and
330// limitations under the License.
331// ----------------------------- END-OF-FILE ----------------------------------
332
333/** @} */
334/** @} */
335/** @} */
Definition bdld_datumintmapbuilder.h:161
allocator_type get_allocator() const
Definition bdld_datumintmapbuilder.h:298
SizeType size() const
Definition bdld_datumintmapbuilder.h:304
BSLMF_NESTED_TRAIT_DECLARATION(DatumIntMapBuilder, bslma::UsesBslmaAllocator)
void pushBack(int key, const Datum &value)
bsls::Types::size_type SizeType
Definition bdld_datumintmapbuilder.h:168
SizeType capacity() const
Definition bdld_datumintmapbuilder.h:292
void append(const DatumIntMapEntry *entries, SizeType size)
void setSorted(bool value)
DatumIntMapBuilder(const allocator_type &allocator)
bsl::allocator< char > allocator_type
Definition bdld_datumintmapbuilder.h:170
DatumIntMapBuilder(SizeType initialCapacity, const allocator_type &allocator=allocator_type())
Definition bdld_datum.h:2628
Definition bdld_datum.h:2302
SizeType * size() const
Definition bdld_datum.h:5402
Definition bdld_datum.h:787
Definition bslma_bslallocator.h:580
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdld_datum.h:730
Definition bslma_usesbslmaallocator.h:343
std::size_t size_type
Definition bsls_types.h:124