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