BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlt_iso8601utilconfiguration.h
Go to the documentation of this file.
1/// @file bdlt_iso8601utilconfiguration.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_iso8601utilconfiguration.h -*-C++-*-
8#ifndef INCLUDED_BDLT_ISO8601UTILCONFIGURATION
9#define INCLUDED_BDLT_ISO8601UTILCONFIGURATION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_iso8601utilconfiguration bdlt_iso8601utilconfiguration
15/// @brief Provide an attribute class to configure ISO 8601 string generation.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_iso8601utilconfiguration
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_iso8601utilconfiguration-purpose"> Purpose</a>
25/// * <a href="#bdlt_iso8601utilconfiguration-classes"> Classes </a>
26/// * <a href="#bdlt_iso8601utilconfiguration-description"> Description </a>
27/// * <a href="#bdlt_iso8601utilconfiguration-attributes"> Attributes </a>
28/// * <a href="#bdlt_iso8601utilconfiguration-default-configuration"> Default Configuration </a>
29/// * <a href="#bdlt_iso8601utilconfiguration-usage"> Usage </a>
30/// * <a href="#bdlt_iso8601utilconfiguration-example-1-configuring-iso-8601-string-generation"> Example 1: Configuring ISO 8601 String Generation </a>
31/// * <a href="#bdlt_iso8601utilconfiguration-example-2-setting-the-process-wide-default-configuration"> Example 2: Setting the Process-Wide Default Configuration </a>
32///
33/// # Purpose {#bdlt_iso8601utilconfiguration-purpose}
34/// Provide an attribute class to configure ISO 8601 string generation.
35///
36/// # Classes {#bdlt_iso8601utilconfiguration-classes}
37///
38/// - bdlt::Iso8601UtilConfiguration: configuration for ISO 8601 strings
39///
40/// @see bdlt_iso8601util, bdlt_iso8601utilparseconfiguration
41///
42/// # Description {#bdlt_iso8601utilconfiguration-description}
43/// This component provides an unconstrained (value-semantic)
44/// attribute class, `bdlt::Iso8601UtilConfiguration`, that may be used to
45/// configure various aspects of generated ISO 8601 strings.
46///
47/// ## Attributes {#bdlt_iso8601utilconfiguration-attributes}
48///
49///
50/// @code
51/// Name Type Default
52/// ------------------------- ---- -------
53/// fractionalSecondPrecision int 3
54/// omitColonInZoneDesignator bool false
55/// useCommaForDecimalSign bool false
56/// useZAbbreviationForUtc bool false
57/// @endcode
58/// * `fractionalSecondPrecision`: number of digits used to represent
59/// fractional seconds; must be in the range `0 .. 6`.
60/// * `omitColonInZoneDesignator`: `true` if `:` should be omitted from zone
61/// designators.
62/// * `useCommaForDecimalSign`: `true` if `,` should be used as the decimal
63/// sign in fractional seconds; otherwise, `.` should be used.
64/// * `useZAbbreviationForUtc`: `true` if `Z` should be used for the zone
65/// designator instead of `+00:00` (specific to UTC).
66///
67/// ## Default Configuration {#bdlt_iso8601utilconfiguration-default-configuration}
68///
69///
70/// This component also provides a (process-wide) default configuration that may
71/// be set and retrieved via the `setDefaultConfiguration` and
72/// `defaultConfiguration` class methods, respectively. See Usage Example 2 for
73/// further details.
74///
75/// ## Usage {#bdlt_iso8601utilconfiguration-usage}
76///
77///
78/// This section illustrates intended use of this component.
79///
80/// ### Example 1: Configuring ISO 8601 String Generation {#bdlt_iso8601utilconfiguration-example-1-configuring-iso-8601-string-generation}
81///
82///
83/// This example demonstrates creation of a `bdlt::Iso8601UtilConfiguration`
84/// object that may be used to influence the format of the output produced by a
85/// hypothetical utility, `my::Iso8601Util`, that generates and parses ISO 8601
86/// strings for `bdlt` vocabulary types (see @ref bdlt_iso8601util , which provides
87/// just such functionality). In particular, suppose that given a sample
88/// `bdlt::TimeTz` object:
89/// @code
90/// const bdlt::TimeTz timeTz(bdlt::Time(8, 59, 59, 123), 240);
91/// @endcode
92/// `my::Iso8601Util` produces, by default, the following (valid) ISO 8601
93/// string:
94/// @code
95/// 08:59:59.123+04:00
96/// @endcode
97/// However, we would like to produce the following (also valid) ISO 8601 string
98/// instead:
99/// @code
100/// 08:59:59,123+0400
101/// @endcode
102/// `bdlt::Iso8601UtilConfiguration` can be used to obtain the desired result
103/// assuming that `my::Iso8601Util` uses `bdlt::Iso8601UtilConfiguration` to
104/// affect the format of generated strings in this fashion (e.g., again see
105/// @ref bdlt_iso8601util ).
106///
107/// First, we construct a `bdlt::Iso8601UtilConfiguration` object that has the
108/// default value:
109/// @code
110/// bdlt::Iso8601UtilConfiguration configuration;
111/// assert( configuration.fractionalSecondPrecision() == 3);
112/// assert(!configuration.omitColonInZoneDesignator());
113/// assert(!configuration.useCommaForDecimalSign());
114/// assert(!configuration.useZAbbreviationForUtc());
115/// @endcode
116/// Then, we modify `configuration` to indicate that we want to use `,` as the
117/// decimal sign (in fractional seconds):
118/// @code
119/// configuration.setUseCommaForDecimalSign(true);
120/// assert( configuration.fractionalSecondPrecision() == 3);
121/// assert(!configuration.omitColonInZoneDesignator());
122/// assert( configuration.useCommaForDecimalSign());
123/// assert(!configuration.useZAbbreviationForUtc());
124/// @endcode
125/// Finally, we modify `configuration` to indicate that we want to omit the `:`
126/// in zone designators:
127/// @code
128/// configuration.setOmitColonInZoneDesignator(true);
129/// assert( configuration.fractionalSecondPrecision() == 3);
130/// assert( configuration.omitColonInZoneDesignator());
131/// assert( configuration.useCommaForDecimalSign());
132/// assert(!configuration.useZAbbreviationForUtc());
133/// @endcode
134/// Our `configuration` object can now be supplied to `my::Iso8601Util` to
135/// produce the desired result.
136///
137/// ### Example 2: Setting the Process-Wide Default Configuration {#bdlt_iso8601utilconfiguration-example-2-setting-the-process-wide-default-configuration}
138///
139///
140/// This example demonstrates how to establish the process-wide default
141/// configuration.
142///
143/// First, we retrieve the default configuration in effect at process start-up
144/// and note that it has the default-constructed value:
145/// @code
146/// bdlt::Iso8601UtilConfiguration configuration =
147/// bdlt::Iso8601UtilConfiguration::defaultConfiguration();
148/// assert(bdlt::Iso8601UtilConfiguration() == configuration);
149/// assert( configuration.fractionalSecondPrecision() == 3);
150/// assert(!configuration.omitColonInZoneDesignator());
151/// assert(!configuration.useCommaForDecimalSign());
152/// assert(!configuration.useZAbbreviationForUtc());
153/// @endcode
154/// Next, we modify `configuration` to indicate that we want to output `Z` when
155/// the zone designator is UTC (i.e., instead of `+00:00`):
156/// @code
157/// configuration.setUseZAbbreviationForUtc(true);
158/// assert( configuration.fractionalSecondPrecision() == 3);
159/// assert(!configuration.omitColonInZoneDesignator());
160/// assert(!configuration.useCommaForDecimalSign());
161/// assert( configuration.useZAbbreviationForUtc());
162/// @endcode
163/// Then, we modify `configuration` to display milliseconds:
164/// @code
165/// configuration.setFractionalSecondPrecision(6);
166/// assert( configuration.fractionalSecondPrecision() == 6);
167/// assert(!configuration.omitColonInZoneDesignator());
168/// assert(!configuration.useCommaForDecimalSign());
169/// assert( configuration.useZAbbreviationForUtc());
170/// @endcode
171/// Now, we set the default configuration to the value of our `configuration`
172/// object:
173/// @code
174/// bdlt::Iso8601UtilConfiguration::setDefaultConfiguration(configuration);
175/// @endcode
176/// Finally, we verify that the default configuration was updated as expected:
177/// @code
178/// const bdlt::Iso8601UtilConfiguration newConfiguration =
179/// bdlt::Iso8601UtilConfiguration::defaultConfiguration();
180/// assert( newConfiguration.fractionalSecondPrecision() == 6);
181/// assert(!newConfiguration.omitColonInZoneDesignator());
182/// assert(!newConfiguration.useCommaForDecimalSign());
183/// assert( newConfiguration.useZAbbreviationForUtc());
184/// @endcode
185/// Note that the expected usage is that the process-wide configuration will be
186/// established *once*, early in `main`, and not changed throughout the lifetime
187/// of a process.
188/// @}
189/** @} */
190/** @} */
191
192/** @addtogroup bdl
193 * @{
194 */
195/** @addtogroup bdlt
196 * @{
197 */
198/** @addtogroup bdlt_iso8601utilconfiguration
199 * @{
200 */
201
202#include <bdlscm_version.h>
203
204#include <bsls_assert.h>
206#include <bsls_review.h>
207
208#include <bsl_iosfwd.h>
209
210
211namespace bdlt {
212
213 // ==============================
214 // class Iso8601UtilConfiguration
215 // ==============================
216
217/// This unconstrained (value-semantic) attribute class characterizes how to
218/// configure certain behavior in `Iso8601Util` functions. Currently, only
219/// the `generate` and `generateRaw` methods of that utility are affected by
220/// `Iso8601UtilConfiguration` settings. See the Attributes section under
221/// @DESCRIPTION in the component-level documentation for information on the
222/// class attributes.
223///
224/// See @ref bdlt_iso8601utilconfiguration
226
227 private:
228 // PRIVATE TYPES
229
230 /// This enumeration denotes the distinct bits that define the values of
231 /// each of the four configuration attributes.
232 enum {
233
234 k_FRACTIONAL_SECOND_PRECISION_MASK = 0x07,
235 k_OMIT_COLON_IN_ZONE_DESIGNATOR_BIT = 0x08,
236 k_USE_COMMA_FOR_DECIMAL_SIGN_BIT = 0x10,
237 k_USE_Z_ABBREVIATION_FOR_UTC_BIT = 0x20
238 };
239
240 // CLASS DATA
241 static bsls::AtomicOperations::AtomicTypes::Int
242 s_defaultConfiguration; // process-wide configuration
243
244 // DATA
245 int d_configurationMask; // bitmask defining configuration
246
247 // FRIENDS
252
253 private:
254 // PRIVATE CREATORS
255
256 /// Create an `Iso8601UtilConfiguration` object having the value
257 /// indicated by the specified `configurationMask`. The behavior is
258 /// undefined unless `configurationMask` represents a valid
259 /// `Iso8601UtilConfiguration` value.
260 explicit Iso8601UtilConfiguration(int configurationMask);
261
262 public:
263 // CLASS METHODS
264
265 /// Return the value of the process-wide `Iso8601UtilConfiguration` that
266 /// is currently in effect.
268
269 /// Set the value of the process-wide `Iso8601UtilConfiguration` to the
270 /// specified `configuration`. Note that the expected usage is that the
271 /// process-wide configuration will be established *once*, early in
272 /// `main`, and not changed throughout the lifetime of a process.
273 static void setDefaultConfiguration(
274 const Iso8601UtilConfiguration& configuration);
275
276 // CREATORS
277
278 /// Create an `Iso8601UtilConfiguration` object having the (default)
279 /// attribute values:
280 /// @code
281 /// fractionalSecondPrecision() == 3
282 /// omitColonInZoneDesignator() == false
283 /// useCommaForDecimalSign() == false
284 /// useZAbbreviationForUtc() == false
285 /// @endcode
287
288 /// Create an `Iso8601UtilConfiguration` object having the value of the
289 /// specified `original` configuration.
291
292 /// Destroy this object.
294
295 // MANIPULATORS
296
297 /// Assign to this object the value of the specified `rhs`
298 /// configuration, and return a reference providing modifiable access to
299 /// this object.
301
302 /// Set the `fractionalSecondPrecision` attribute of this object to the
303 /// specified `value`. The behavior is undefined unless `0 <= value`
304 /// and `6 >= value`.
306
307 /// Set the `omitColonInZoneDesignator` attribute of this object to the
308 /// specified `value`.
310
311 /// Set the `useCommaForDecimalSign` attribute of this object to the
312 /// specified `value`.
314
315 /// Set the `useZAbbreviationForUtc` attribute of this object to the
316 /// specified `value`.
318
319 // ACCESSORS
320
321 /// Return the value of the `fractionalSecondPrecision` attribute of
322 /// this object.
323 int fractionalSecondPrecision() const;
324
325 /// Return the value of the `omitColonInZoneDesignator` attribute of
326 /// this object.
327 bool omitColonInZoneDesignator() const;
328
329 /// Return the value of the `useCommaForDecimalSign` attribute of this
330 /// object.
331 bool useCommaForDecimalSign() const;
332
333 /// Return the value of the `useZAbbreviationForUtc` attribute of this
334 /// object.
335 bool useZAbbreviationForUtc() const;
336
337 // Aspects
338
339 /// Write the value of this object to the specified output `stream` in a
340 /// human-readable format, and return a reference to `stream`.
341 /// Optionally specify an initial indentation `level`, whose absolute
342 /// value is incremented recursively for nested objects. If `level` is
343 /// specified, optionally specify `spacesPerLevel`, whose absolute value
344 /// indicates the number of spaces per indentation level for this and
345 /// all of its nested objects. If `level` is negative, suppress
346 /// indentation of the first line. If `spacesPerLevel` is negative,
347 /// format the entire output on one line, suppressing all but the
348 /// initial indentation (as governed by `level`). If `stream` is not
349 /// valid on entry, this operation has no effect. Note that this
350 /// human-readable format is not fully specified, and can change without
351 /// notice.
352 bsl::ostream& print(bsl::ostream& stream,
353 int level = 0,
354 int spacesPerLevel = 4) const;
355};
356
357// FREE OPERATORS
358
359/// Return `true` if the specified `lhs` and `rhs` objects have the same
360/// value, and `false` otherwise. Two `Iso8601UtilConfiguration` objects
361/// have the same value if each of their `fractionalSecondPrecision`,
362/// `omitColonInZoneDesignator`, `useCommaForDecimalSign`, and
363/// `useZAbbreviationForUtc` attributes (respectively) have the same value.
364bool operator==(const Iso8601UtilConfiguration& lhs,
365 const Iso8601UtilConfiguration& rhs);
366
367/// Return `true` if the specified `lhs` and `rhs` objects do not have the
368/// same value, and `false` otherwise. Two `Iso8601UtilConfiguration`
369/// objects do not have the same value if any of their
370/// `fractionalSecondPrecision`, `omitColonInZoneDesignator`,
371/// `useCommaForDecimalSign`, or `useZAbbreviationForUtc` attributes
372/// (respectively) do not have the same value.
373bool operator!=(const Iso8601UtilConfiguration& lhs,
374 const Iso8601UtilConfiguration& rhs);
375
376/// Write the value of the specified `object` to the specified output
377/// `stream` in a single-line format, and return a reference to `stream`.
378/// If `stream` is not valid on entry, this operation has no effect. Note
379/// that this human-readable format is not fully specified and can change
380/// without notice. Also note that this method has the same behavior as
381/// `object.print(stream, 0, -1)`, but with the attribute names elided.
382bsl::ostream& operator<<(bsl::ostream& stream,
383 const Iso8601UtilConfiguration& object);
384
385// ============================================================================
386// INLINE DEFINITIONS
387// ============================================================================
388
389 // ------------------------------
390 // class Iso8601UtilConfiguration
391 // ------------------------------
392
393// PRIVATE CREATORS
394inline
396: d_configurationMask(configurationMask)
397{
398 BSLS_ASSERT(0 == (configurationMask
399 & ~(k_FRACTIONAL_SECOND_PRECISION_MASK
400 | k_OMIT_COLON_IN_ZONE_DESIGNATOR_BIT
401 | k_USE_COMMA_FOR_DECIMAL_SIGN_BIT
402 | k_USE_Z_ABBREVIATION_FOR_UTC_BIT)));
403}
404
405// CLASS METHODS
406inline
407Iso8601UtilConfiguration Iso8601UtilConfiguration::defaultConfiguration()
408{
410 bsls::AtomicOperations::getIntRelaxed(&s_defaultConfiguration));
411}
412
413inline
414void Iso8601UtilConfiguration::setDefaultConfiguration(
415 const Iso8601UtilConfiguration& configuration)
416{
417 bsls::AtomicOperations::setIntRelaxed(&s_defaultConfiguration,
418 configuration.d_configurationMask);
419}
420
421// CREATORS
422inline
423Iso8601UtilConfiguration::Iso8601UtilConfiguration()
424: d_configurationMask(3)
425{
426}
427
428inline
430 const Iso8601UtilConfiguration& original)
431: d_configurationMask(original.d_configurationMask)
432{
433}
434
435inline
437{
438 BSLS_ASSERT(0 == (d_configurationMask
439 & ~(k_FRACTIONAL_SECOND_PRECISION_MASK
440 | k_OMIT_COLON_IN_ZONE_DESIGNATOR_BIT
441 | k_USE_COMMA_FOR_DECIMAL_SIGN_BIT
442 | k_USE_Z_ABBREVIATION_FOR_UTC_BIT)));
443}
444
445// MANIPULATORS
446inline
448 const Iso8601UtilConfiguration& rhs)
449{
450 d_configurationMask = rhs.d_configurationMask;
451
452 return *this;
453}
454
455// ACCESSORS
456inline
458{
459 return d_configurationMask & k_FRACTIONAL_SECOND_PRECISION_MASK;
460}
461
462inline
464{
465 return d_configurationMask & k_OMIT_COLON_IN_ZONE_DESIGNATOR_BIT;
466}
467
468inline
470{
471 return d_configurationMask & k_USE_COMMA_FOR_DECIMAL_SIGN_BIT;
472}
473
474inline
476{
477 return d_configurationMask & k_USE_Z_ABBREVIATION_FOR_UTC_BIT;
478}
479
480} // close package namespace
481
482// FREE OPERATORS
483inline
484bool bdlt::operator==(const Iso8601UtilConfiguration& lhs,
485 const Iso8601UtilConfiguration& rhs)
486{
487 return lhs.d_configurationMask == rhs.d_configurationMask;
488}
489
490inline
491bool bdlt::operator!=(const Iso8601UtilConfiguration& lhs,
492 const Iso8601UtilConfiguration& rhs)
493{
494 return lhs.d_configurationMask != rhs.d_configurationMask;
495}
496
497
498
499#endif
500
501// ----------------------------------------------------------------------------
502// Copyright 2016 Bloomberg Finance L.P.
503//
504// Licensed under the Apache License, Version 2.0 (the "License");
505// you may not use this file except in compliance with the License.
506// You may obtain a copy of the License at
507//
508// http://www.apache.org/licenses/LICENSE-2.0
509//
510// Unless required by applicable law or agreed to in writing, software
511// distributed under the License is distributed on an "AS IS" BASIS,
512// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
513// See the License for the specific language governing permissions and
514// limitations under the License.
515// ----------------------------- END-OF-FILE ----------------------------------
516
517/** @} */
518/** @} */
519/** @} */
Definition bdlt_iso8601utilconfiguration.h:225
int fractionalSecondPrecision() const
Definition bdlt_iso8601utilconfiguration.h:457
friend bool operator!=(const Iso8601UtilConfiguration &, const Iso8601UtilConfiguration &)
bool omitColonInZoneDesignator() const
Definition bdlt_iso8601utilconfiguration.h:463
bool useCommaForDecimalSign() const
Definition bdlt_iso8601utilconfiguration.h:469
Iso8601UtilConfiguration()
Definition bdlt_iso8601utilconfiguration.h:423
void setFractionalSecondPrecision(int value)
bool useZAbbreviationForUtc() const
Definition bdlt_iso8601utilconfiguration.h:475
void setUseZAbbreviationForUtc(bool value)
void setOmitColonInZoneDesignator(bool value)
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
static Iso8601UtilConfiguration defaultConfiguration()
Definition bdlt_iso8601utilconfiguration.h:407
static void setDefaultConfiguration(const Iso8601UtilConfiguration &configuration)
Definition bdlt_iso8601utilconfiguration.h:414
~Iso8601UtilConfiguration()
Destroy this object.
Definition bdlt_iso8601utilconfiguration.h:436
void setUseCommaForDecimalSign(bool value)
friend bool operator==(const Iso8601UtilConfiguration &, const Iso8601UtilConfiguration &)
Iso8601UtilConfiguration & operator=(const Iso8601UtilConfiguration &rhs)
Definition bdlt_iso8601utilconfiguration.h:447
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicisma30360.h:112
bool operator==(const Calendar &lhs, const Calendar &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const Calendar &calendar)
bool operator!=(const Calendar &lhs, const Calendar &rhs)
static void setIntRelaxed(AtomicTypes::Int *atomicInt, int value)
Definition bsls_atomicoperations.h:1552
static int getIntRelaxed(AtomicTypes::Int const *atomicInt)
Definition bsls_atomicoperations.h:1534