BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlde_base64encoderoptions.h
Go to the documentation of this file.
1/// @file bdlde_base64encoderoptions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlde_base64encoderoptions.h -*-C++-*-
8#ifndef INCLUDED_BDLDE_BASE64ENCODEROPTIONS
9#define INCLUDED_BDLDE_BASE64ENCODEROPTIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT_RCSID(bdlde_base64encoderoptions_h,"$Id$ $CSID$")
14
15/// @defgroup bdlde_base64encoderoptions bdlde_base64encoderoptions
16/// @brief Provide a value-semantic attribute class for encoder options.
17/// @addtogroup bdl
18/// @{
19/// @addtogroup bdlde
20/// @{
21/// @addtogroup bdlde_base64encoderoptions
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bdlde_base64encoderoptions-purpose"> Purpose</a>
26/// * <a href="#bdlde_base64encoderoptions-classes"> Classes </a>
27/// * <a href="#bdlde_base64encoderoptions-description"> Description </a>
28/// * <a href="#bdlde_base64encoderoptions-usage"> Usage </a>
29/// * <a href="#bdlde_base64encoderoptions-example-1-basic-usage"> Example 1: Basic Usage </a>
30/// * <a href="#bdlde_base64encoderoptions-example-2"> Example 2: </a>
31/// * <a href="#bdlde_base64encoderoptions-example-3"> Example 3: </a>
32/// * <a href="#bdlde_base64encoderoptions-example-4"> Example 4: </a>
33///
34/// # Purpose {#bdlde_base64encoderoptions-purpose}
35/// Provide a value-semantic attribute class for encoder options.
36///
37/// # Classes {#bdlde_base64encoderoptions-classes}
38///
39/// - bdlde::Base64EncoderOptions: options for encoder
40///
41/// @see bdlde_base64decoderoptions, bdlde_base64encoder,
42/// bdlde_base64decoder, bdlde_base64alphabet
43///
44/// # Description {#bdlde_base64encoderoptions-description}
45/// This component provides a value-semantic attribute class for
46/// specifying options for `bdlde::Base64Encoder`.
47///
48/// This `class` supports default-generated copy construction and copy
49/// assignment, but the constructor is private. To create an object one must
50/// call one of the class methods, which will return a newly-constructed object
51/// by value. Specialized class methods are provided to create objects
52/// configured for the `mime`, `urlSafe`, and `standard` configurations.
53///
54/// Other configurations may be obtained by specifying arguments to the `custom`
55/// class method, or by calling the setters after the object is created.
56///
57/// ## Usage {#bdlde_base64encoderoptions-usage}
58///
59///
60/// This section illustrates intended use of this component.
61///
62/// ### Example 1: Basic Usage {#bdlde_base64encoderoptions-example-1-basic-usage}
63///
64///
65/// Suppose we want a `Base64EncoderOptions` object configured for MIME
66/// encoding, meaning `maxLineLength == 76`, `alphabet == e_BASIC`, and
67/// `isPadded == true`.
68///
69/// First, it turns out that those are the default values of the attributes, so
70/// all we have to do is default construct an object, and we're done.
71/// @code
72/// const bdlde::Base64EncoderOptions& mimeOptions =
73/// bdlde::Base64EncoderOptions::mime();
74/// @endcode
75/// Then, we check the attributes:
76/// @code
77/// assert(mimeOptions.maxLineLength() == 76);
78/// assert(mimeOptions.alphabet() == bdlde::Base64Alphabet::e_BASIC);
79/// assert(mimeOptions.isPadded() == true);
80/// @endcode
81/// Now, we stream the object:
82/// @code
83/// mimeOptions.print(cout);
84/// @endcode
85/// Finally, we observe the output:
86/// @code
87/// [
88/// maxLineLength = 76
89/// alphabet = BASIC
90/// isPadded = true
91/// ]
92/// @endcode
93///
94/// ### Example 2: {#bdlde_base64encoderoptions-example-2}
95///
96///
97/// Suppose we want a `Base64EncoderOptions` object configured for translating
98/// URL's. That would mean a `maxLineLength == 0`, `alphabet == e_URL`, and
99/// `isPadded == false`.
100///
101/// First, the class method `urlSafe` returns an object configured exactly that
102/// way, so we simply call it:
103/// @code
104/// const bdlde::Base64EncoderOptions& urlOptions =
105/// bdlde::Base64EncoderOptions::urlSafe();
106/// @endcode
107/// Then, we check the attributes:
108/// @code
109/// assert(urlOptions.maxLineLength() == 0);
110/// assert(urlOptions.alphabet() == bdlde::Base64Alphabet::e_URL);
111/// assert(urlOptions.isPadded() == false);
112/// @endcode
113/// Now, we stream the object:
114/// @code
115/// urlOptions.print(cout);
116/// @endcode
117/// Finally, we observe the output:
118/// @code
119/// [
120/// maxLineLength = 0
121/// alphabet = URL
122/// isPadded = false
123/// ]
124/// @endcode
125///
126/// ### Example 3: {#bdlde_base64encoderoptions-example-3}
127///
128///
129/// Suppose we want an options object configured for standard Base64:
130///
131/// First, we can simply call the `standard` class method:
132/// @code
133/// const bdlde::Base64EncoderOptions& standardOptions =
134/// bdlde::Base64EncoderOptions::standard();
135/// @endcode
136/// Then, we check the attributes:
137/// @code
138/// assert(standardOptions.maxLineLength() == 0);
139/// assert(standardOptions.alphabet() == bdlde::Base64Alphabet::e_BASIC);
140/// assert(standardOptions.isPadded() == true);
141/// @endcode
142/// Now, we stream the object:
143/// @code
144/// standardOptions.print(cout);
145/// @endcode
146/// Finally, we observe the output:
147/// @code
148/// [
149/// maxLineLength = 0
150/// alphabet = BASIC
151/// isPadded = true
152/// ]
153/// @endcode
154///
155/// ### Example 4: {#bdlde_base64encoderoptions-example-4}
156///
157///
158/// Suppose we want a really strangely configured options object with
159/// `maxLineLength == 200`, `alphabet == e_URL`, and padding.
160///
161/// First, we can simply call the `custom` class method:
162/// @code
163/// const bdlde::Base64EncoderOptions& customOptions =
164/// bdlde::Base64EncoderOptions::custom(200,
165/// bdlde::Base64Alphabet::e_URL,
166/// true);
167/// @endcode
168/// Then, we check the attributes:
169/// @code
170/// assert(customOptions.maxLineLength() == 200);
171/// assert(customOptions.alphabet() == bdlde::Base64Alphabet::e_URL);
172/// assert(customOptions.isPadded() == true);
173/// @endcode
174/// Now, we stream the object:
175/// @code
176/// cout << customOptions << endl;
177/// @endcode
178/// Finally, we observe the output:
179/// @code
180/// [ maxLineLength = 200 alphabet = URL isPadded = true ]
181/// @endcode
182/// @}
183/** @} */
184/** @} */
185
186/** @addtogroup bdl
187 * @{
188 */
189/** @addtogroup bdlde
190 * @{
191 */
192/** @addtogroup bdlde_base64encoderoptions
193 * @{
194 */
195
196#include <bdlde_base64alphabet.h>
197
199
200#include <bsls_assert.h>
201
202#include <bsl_iosfwd.h>
203
204
205namespace bdlde {
206
207 // ==========================
208 // class Base64EncoderOptions
209 // ==========================
210
211/// This `class` stores the configuration of a `Base64Encoder`.
212///
213/// See @ref bdlde_base64encoderoptions
215
216 // DATA
217 int d_maxLineLength; // the max line length of output
218 // between CRLF's. A value of
219 // 0 means no CRLF's will be
220 // added
221
222 Base64Alphabet::Enum d_alphabet; // the alphabet to be used --
223 // basic or url
224
225 bool d_isPadded; // is the output to be padded with
226 // '='s
227
228 public:
229 // PUBLIC TYPES
231
232 private:
233 // PRIVATE CREATORS
234
235 /// Create a `Base64EncoderOptions` object having the specified
236 /// `maxLineLength, `alphabet', and `isPadded` attribute values. The
237 /// behavior is unless `0 <= maxLineLength` and `alphabet` is a defined
238 /// value of `Base64Alphabet::Enum`.
241 bool padded);
242
243 public:
244 // CLASS METHODS
245
246 /// Return a `Base64EncoderOptions` object having the specified
247 /// `maxLineLength, alphabet, and `isPadded' attribute values. The
248 /// behavior is unless `0 <= maxLineLength` and 'alphabet is a defined
249 /// value of `Base64Alphabet::Enum`.
250 static
253 bool padded);
254
255 /// Return a `Base64EncoderOptions` object having the attributes
256 /// `maxLineLength == 76`, `alphabet == Base64Alphabet::e_BASIC`, and
257 /// `isPadded == true`. This conforms to RFC 2045.
258 static
260
261 /// Return a `Base64EncoderOptions` object having the attributes
262 /// `maxLineLength == 0`, `alphabet == Base64Alphabet::e_BASIC`, and
263 /// `isPadded == false`. If `padded` is not specified, it defaults to
264 /// `true`. This conforms to RFC 4648 section 4.
265 static
266 Base64EncoderOptions standard(bool padded = true);
267
268 /// Return a `Base64EncoderOptions` object having the attributes
269 /// `maxLineLength == 0`, `alphabet == Base64Alphabet::e_URL`, and
270 /// the specified `padded`. If `padded` is not specified, it defaults
271 /// to `false`. This conforms to RFC 4648 section 5.
272 static
273 Base64EncoderOptions urlSafe(bool padded = false);
274
275 // CREATORS
276
277 /// Default copy constructor.
279
280 /// Destroy this object.
282
283 // MANIPULATORS
284
285 /// Default operator=().
287
288 /// Set the `alphabet` attribute to the specified `value`. The behavior
289 /// is undefined unless `value` is either `e_BASIC` or `e_UTL`.
291
292 /// Set the `isPadded` attribute to the specified `value`.
293 void setIsPadded(bool value);
294
295 /// Set the `maxLineLength` attribute to the specified `value`. The
296 /// behavior is undefined unless `0 <= value`.
297 void setMaxLineLength(int value);
298
299 // ACCESSORS
300
301 /// Format this object to the specified output `stream` at the
302 /// optionally specified indentation `level` and return a reference to
303 /// the modifiable `stream`. If `level` is specified, optionally
304 /// specify `spacesPerLevel`, the number of spaces per indentation level
305 /// for this and all of its nested objects. Each line is indented by
306 /// the absolute value of `level * spacesPerLevel`. If `level` is
307 /// negative, suppress indentation of the first line. If
308 /// `spacesPerLevel` is negative, suppress line breaks and format the
309 /// entire output on one line. If `stream` is initially invalid, this
310 /// operation has no effect. Note that a trailing newline is provided
311 /// in multiline mode only.
312 bsl::ostream& print(bsl::ostream& stream,
313 int level = 0,
314 int spacesPerLevel = 4) const;
315
316 /// Return the value of the `alphabet` attribute.
318
319 /// Return the value of the `isPadded` attribute.
320 bool isPadded() const;
321
322 /// Return the value of the `maxLineLength` attribute.
323 int maxLineLength() const;
324};
325
326// FREE OPERATORS
327
328/// Return `true` if the specified `lhs` and `rhs` attribute objects have
329/// the same value, and `false` otherwise. Two attribute objects have the
330/// same value if each respective attribute has the same value.
331bool operator==(const Base64EncoderOptions& lhs,
332 const Base64EncoderOptions& rhs);
333
334/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
335/// have the same value, and `false` otherwise. Two attribute objects do
336/// not have the same value if one or more respective attributes differ in
337/// values.
338bool operator!=(const Base64EncoderOptions& lhs,
339 const Base64EncoderOptions& rhs);
340
341/// Format the specified `rhs` to the specified output `stream` and
342/// return a reference to the modifiable `stream`.
343bsl::ostream& operator<<(bsl::ostream& stream,
344 const Base64EncoderOptions& rhs);
345
346} // close package namespace
347
348
349// TRAITS
350namespace bsl {
351
352template <>
355
356
357} // close namespace bsl
358
359// ============================================================================
360// INLINE FUNCTION DEFINITIONS
361// ============================================================================
362
363
364namespace bdlde {
365
366 // --------------------------
367 // class Base64EncoderOptions
368 // --------------------------
369
370// PRIVATE CREATORS
371inline
372Base64EncoderOptions::Base64EncoderOptions(int maxLineLength,
373 Base64Alphabet::Enum alphabet,
374 bool padded)
375: d_maxLineLength(maxLineLength)
376, d_alphabet(alphabet)
377, d_isPadded(padded)
378{
379 BSLS_ASSERT(0 <= maxLineLength);
380 BSLS_ASSERT(Base64Alphabet::e_BASIC == alphabet ||
381 Base64Alphabet::e_URL == alphabet);
382}
383
384// CLASS METHODS
385inline
386Base64EncoderOptions Base64EncoderOptions::custom(
387 int maxLineLength,
388 Base64Alphabet::Enum alphabet,
389 bool padded)
390{
391 return Base64EncoderOptions(maxLineLength, alphabet, padded);
392}
393
394inline
395Base64EncoderOptions Base64EncoderOptions::mime()
396{
397 return Base64EncoderOptions(k_MIME_MAX_LINE_LENGTH,
398 Base64Alphabet::e_BASIC,
399 true);
400}
401
402inline
403Base64EncoderOptions Base64EncoderOptions::standard(bool padded)
404{
405 return Base64EncoderOptions(0, Base64Alphabet::e_BASIC, padded);
406}
407
408inline
409Base64EncoderOptions Base64EncoderOptions::urlSafe(bool padded)
410{
411 return Base64EncoderOptions(0, Base64Alphabet::e_URL, padded);
412}
413
414// MANIPULATORS
415inline
416void Base64EncoderOptions::setAlphabet(Base64Alphabet::Enum value)
417{
418 BSLS_ASSERT(Base64Alphabet::e_BASIC == value ||
419 Base64Alphabet::e_URL == value);
420
421 d_alphabet = value;
422}
423
424inline
425void Base64EncoderOptions::setIsPadded(bool value)
426{
427 d_isPadded = value;
428}
429
430inline
431void Base64EncoderOptions::setMaxLineLength(int value)
432{
433 BSLS_ASSERT(0 <= value);
434
435 d_maxLineLength = value;
436}
437
438// ACCESSORS
439inline
440Base64Alphabet::Enum Base64EncoderOptions::alphabet() const
441{
442 return d_alphabet;
443}
444
445inline
446bool Base64EncoderOptions::isPadded() const
447{
448 return d_isPadded;
449}
450
451inline
452int Base64EncoderOptions::maxLineLength() const
453{
454 return d_maxLineLength;
455}
456
457} // close package namespace
458
459
460#endif
461
462// ----------------------------------------------------------------------------
463// Copyright 2022 Bloomberg Finance L.P.
464//
465// Licensed under the Apache License, Version 2.0 (the "License");
466// you may not use this file except in compliance with the License.
467// You may obtain a copy of the License at
468//
469// http://www.apache.org/licenses/LICENSE-2.0
470//
471// Unless required by applicable law or agreed to in writing, software
472// distributed under the License is distributed on an "AS IS" BASIS,
473// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
474// See the License for the specific language governing permissions and
475// limitations under the License.
476// ----------------------------- END-OF-FILE ----------------------------------
477
478/** @} */
479/** @} */
480/** @} */
Definition bdlde_base64encoderoptions.h:214
static Base64EncoderOptions custom(int maxLineLength, Base64Alphabet::Enum alphabet, bool padded)
Definition bdlde_base64encoderoptions.h:386
~Base64EncoderOptions()=default
Destroy this object.
static Base64EncoderOptions standard(bool padded=true)
Definition bdlde_base64encoderoptions.h:403
bool isPadded() const
Return the value of the isPadded attribute.
Definition bdlde_base64encoderoptions.h:446
int maxLineLength() const
Return the value of the maxLineLength attribute.
Definition bdlde_base64encoderoptions.h:452
@ k_MIME_MAX_LINE_LENGTH
Definition bdlde_base64encoderoptions.h:230
Base64EncoderOptions & operator=(const Base64EncoderOptions &)=default
Default operator=().
static Base64EncoderOptions urlSafe(bool padded=false)
Definition bdlde_base64encoderoptions.h:409
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
void setIsPadded(bool value)
Set the isPadded attribute to the specified value.
Definition bdlde_base64encoderoptions.h:425
void setAlphabet(Base64Alphabet::Enum value)
Definition bdlde_base64encoderoptions.h:416
static Base64EncoderOptions mime()
Definition bdlde_base64encoderoptions.h:395
void setMaxLineLength(int value)
Definition bdlde_base64encoderoptions.h:431
Base64EncoderOptions(const Base64EncoderOptions &)=default
Default copy constructor.
Base64Alphabet::Enum alphabet() const
Return the value of the alphabet attribute.
Definition bdlde_base64encoderoptions.h:440
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT_RCSID(tag, str)
Definition bsls_ident.h:260
#define BSLS_IDENT_PRAGMA_ONCE
Definition bsls_ident.h:310
Definition bdlde_base64alphabet.h:118
bsl::ostream & operator<<(bsl::ostream &stream, Base64Alphabet::Enum value)
Definition bdlb_printmethods.h:283
Enum
Definition bdlde_base64alphabet.h:135
Definition bslmf_istriviallycopyable.h:329