BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_managedattribute.h
Go to the documentation of this file.
1/// @file ball_managedattribute.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_managedattribute.h -*-C++-*-
8#ifndef INCLUDED_BALL_MANAGEDATTRIBUTE
9#define INCLUDED_BALL_MANAGEDATTRIBUTE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_managedattribute ball_managedattribute
15/// @brief Provide a wrapper for `ball::Attribute` with managed name storage.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_managedattribute
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_managedattribute-purpose"> Purpose</a>
25/// * <a href="#ball_managedattribute-classes"> Classes </a>
26/// * <a href="#ball_managedattribute-description"> Description </a>
27/// * <a href="#ball_managedattribute-usage"> Usage </a>
28/// * <a href="#ball_managedattribute-example-1-basic-properties-of-ball-managedattribute"> Example 1: Basic Properties of ball::ManagedAttribute </a>
29///
30/// # Purpose {#ball_managedattribute-purpose}
31/// Provide a wrapper for `ball::Attribute` with managed name storage.
32///
33/// # Classes {#ball_managedattribute-classes}
34///
35/// - ball::ManagedAttribute: wrapper for `ball::Attribute` with managed storage
36///
37/// @see ball_attribute
38///
39/// # Description {#ball_managedattribute-description}
40/// This component implements a wrapper for `ball::Attribute`,
41/// `ball::ManagedAttribute`, that manages the lifetime of the attribute name.
42/// Note that `ball::Attribute` does *not* manage the lifetime of its name (see
43/// @ref ball_attribute ).
44///
45/// ## Usage {#ball_managedattribute-usage}
46///
47///
48/// In this section we show intended usage of this component.
49///
50/// ### Example 1: Basic Properties of ball::ManagedAttribute {#ball_managedattribute-example-1-basic-properties-of-ball-managedattribute}
51///
52///
53/// This example creates `ball::ManagedAttribute` objects and shows basic
54/// properties of those objects:
55/// @code
56/// ball::ManagedAttribute p1("uuid", 4044457);
57/// ball::ManagedAttribute p2("name", "Bloomberg");
58///
59/// assert("uuid" == p1.key());
60/// assert("name" == p2.key());
61///
62/// assert(true == p1.value().is<int>());
63/// assert(4044457 == p1.value().the<int>());
64/// assert(true == p2.value().is<bsl::string>());
65/// assert("Bloomberg" == p2.value().the<bsl::string>());
66/// @endcode
67/// Finally, we show that `ball::ManagedAttribute` manages the storage for the
68/// attribute name after construction:
69/// @code
70/// char buffer[] = "Hello";
71/// ball::ManagedAttribute p3(buffer, 1);
72/// bsl::strcpy(buffer, "World");
73/// assert("Hello" == p3.key()));
74/// @endcode
75/// @}
76/** @} */
77/** @} */
78
79/** @addtogroup bal
80 * @{
81 */
82/** @addtogroup ball
83 * @{
84 */
85/** @addtogroup ball_managedattribute
86 * @{
87 */
88
89#include <balscm_version.h>
90
91#include <ball_attribute.h>
92
93#include <bdlb_variant.h>
94
95#include <bslma_allocator.h>
96#include <bslma_bslallocator.h>
98
100
101#include <bsls_keyword.h>
102#include <bsls_types.h>
103
104#include <bsl_string.h>
105
106
107namespace ball {
108
109 // ======================
110 // class ManagedAttribute
111 // ======================
112
113/// A `ball::ManagedAttribute` object contains a `ball::Attribute` object
114/// and provides storage for the attribute's name.
115///
116/// See @ref ball_managedattribute
118
119 private:
120 // DATA
121 bsl::string d_name; // storage for attribute's name
122 Attribute d_attribute; // attribute
123
124 // FRIENDS
125 friend bool operator==(const ManagedAttribute&, const ManagedAttribute&);
126 friend bool operator!=(const ManagedAttribute&, const ManagedAttribute&);
127 friend bsl::ostream& operator<<(bsl::ostream&, const ManagedAttribute&);
128
129 public:
130 // TYPES
132
133 // TRAITS
136
137 // CLASS METHODS
138
139 /// Return a hash value calculated from the specified `attribute` using
140 /// the specified `size` as the number of slots. The hash value is
141 /// guaranteed to be in the range `[0 .. size - 1]`. The behavior is
142 /// undefined unless `0 < size`.
143 static int hash(const ManagedAttribute& attribute, int size);
144
145 // CREATORS
146
147 /// Create a `ManagedAttribute` object having the value of the specified
148 /// `attribute`. Optionally specify an `allocator` (e.g., the address
149 /// of a `bslma::Allocator` object) to supply memory; otherwise, the
150 /// default allocator is used.
151 explicit ManagedAttribute(
152 const Attribute& attribute,
153 const allocator_type& allocator = allocator_type());
154
155 /// Create a `ManagedAttribute` object having the specified `name` and
156 /// string `value`. Optionally specify an `allocator` (e.g., the
157 /// address of a `bslma::Allocator` object) to supply memory; otherwise,
158 /// the default allocator is used.
160 const bsl::string_view& value,
161 const allocator_type& allocator = allocator_type());
162
163 /// Create a `ManagedAttribute` object having the specified `name` and
164 /// the C-style `value` string. Optionally specify an `allocator`
165 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
166 /// otherwise, the default allocator is used.
168 const char *value,
169 const allocator_type& allocator = allocator_type());
170
172 int value,
173 const allocator_type& allocator = allocator_type());
175 long value,
176 const allocator_type& allocator = allocator_type());
178 long long value,
179 const allocator_type& allocator = allocator_type());
181 unsigned int value,
182 const allocator_type& allocator = allocator_type());
184 unsigned long value,
185 const allocator_type& allocator = allocator_type());
186 /// Create a `ManagedAttribute` object having the specified `name` and
187 /// `value`. Optionally specify an `allocator` (e.g., the address of a
188 /// `bslma::Allocator` object) to supply memory; otherwise, the default
189 /// allocator is used.
191 unsigned long long value,
192 const allocator_type& allocator = allocator_type());
193
194 /// Create a `ManagedAttribute` object having the specified `name` and
195 /// the pointer to the specified `value` of cv-qualified `void` type.
196 /// Optionally specify an `allocator` (e.g., the address of a
197 /// `bslma::Allocator` object) to supply memory; otherwise, the default
198 /// allocator is used.
200 const void *value,
201 const allocator_type& allocator = allocator_type());
202
203 /// Create a `ManagedAttribute` object having the specified `name` and
204 /// `value`. Optionally specify an `allocator` (e.g., the address of a
205 /// `bslma::Allocator` object) to supply memory; otherwise, the default
206 /// allocator is used.
208 const Attribute::Value& value,
209 const allocator_type& allocator = allocator_type());
210
211 /// Create a `ManagedAttribute` object having the same value as the
212 /// specified `original` object. Optionally specify an `allocator`
213 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
214 /// otherwise, the default allocator is used.
215 ManagedAttribute(const ManagedAttribute& original,
216 const allocator_type& allocator = allocator_type());
217
218 /// Destroy this object.
219 ~ManagedAttribute() = default;
220
221 // MANIPULATORS
222
223 /// Assign to this object the value of the specified `rhs` object, and
224 /// return a non-`const` reference to this object.
226
227 /// Set the attribute name of this object to the specified `name`.
228 void setName(const bsl::string_view& name);
229
230 void setValue(int value);
231 void setValue(long value);
232 void setValue(long long value);
233 void setValue(unsigned int value);
234 void setValue(unsigned long value);
235 void setValue(unsigned long long value);
236 void setValue(const bsl::string_view& value);
237 void setValue(const Attribute::Value& value);
238 void setValue(const char *value);
239 /// Set the attribute value of this object to the specified `value`.
240 void setValue(const void *value);
241
242 // ACCESSORS
243
244 /// Return a `const` reference to the attribute of this object.
245 const Attribute& attribute() const;
246
247 /// Return the allocator used by this object to supply memory. Note
248 /// that if no allocator was supplied at construction the default
249 /// allocator in effect at construction is used.
251
252 /// Format this object to the specified output `stream` at the (absolute
253 /// value of) the optionally specified indentation `level` and return a
254 /// reference to `stream`. If `level` is specified, optionally specify
255 /// `spacesPerLevel`, the number of spaces per indentation level for
256 /// this and all of its nested objects. If `level` is negative,
257 /// suppress indentation of the first line. If `spacesPerLevel` is
258 /// negative, format the entire output on one line, suppressing all but
259 /// the initial indentation (as governed by `level`). If `stream` is
260 /// not valid on entry, this operation has no effect.
261 bsl::ostream& print(bsl::ostream& stream,
262 int level = 0,
263 int spacesPerLevel = 4) const;
264
265 /// Return a `const` reference to the attribute name of this object.
266 const bsl::string& key() const;
267
268 /// Return the attribute name of this object. Note that this accessor
269 /// should not be used to get the attribute name if the name string
270 /// contains embedded zeros.
271 ///
272 /// @deprecated Use @ref key() instead.
273 const char *name() const;
274
275 /// Return a `const` reference to the attribute value of this object.
276 const Attribute::Value& value() const;
277};
278
279// FREE OPERATORS
280
281/// Return `true` if the specified `lhs` and `rhs` objects have the same
282/// value, and `false` otherwise. Two `ManagedAttribute` objects have the
283/// same value if they have the same name, same attribute value type, and
284/// the same attribute value.
285bool operator==(const ManagedAttribute& lhs, const ManagedAttribute& rhs);
286bool operator==(const ManagedAttribute& lhs, const Attribute& rhs);
287bool operator==(const Attribute& lhs, const ManagedAttribute& rhs);
288
289/// Return `true` if the specified `lhs` and `rhs` objects do not have the
290/// same value, and `false` otherwise. Two `ManagedAttribute` objects do
291/// not have the same value if any of their respective names, attribute
292/// value types, or attribute values differ.
293bool operator!=(const ManagedAttribute& lhs, const ManagedAttribute& rhs);
294bool operator!=(const ManagedAttribute& lhs, const Attribute& rhs);
295bool operator!=(const Attribute& lhs, const ManagedAttribute& rhs);
296
297/// Write the value of the specified `attribute` to the specified `output`
298/// stream. Return the specified `output` stream.
299bsl::ostream& operator<<(bsl::ostream& output,
300 const ManagedAttribute& attribute);
301
302// ============================================================================
303// INLINE DEFINITIONS
304// ============================================================================
305
306 // ----------------------
307 // class ManagedAttribute
308 // ----------------------
309
310//CLASS METHODS
311inline
312int ManagedAttribute::hash(const ManagedAttribute& attribute, int size)
313{
314 return Attribute::hash(attribute.d_attribute, size);
315}
316
317// CREATORS
318inline
320 const allocator_type& allocator)
321: d_name(attribute.name(), allocator)
322, d_attribute(d_name.c_str(), attribute.value(), allocator)
323{
324}
325
326inline
328 const bsl::string_view& value,
329 const allocator_type& allocator)
330: d_name(name, allocator)
331, d_attribute(d_name.c_str(), value, allocator)
332{
333}
334
335inline
337 const char *value,
338 const allocator_type& allocator)
339: d_name(name, allocator)
340, d_attribute(d_name.c_str(), value, allocator)
341{
342}
343
344inline
346 int value,
347 const allocator_type& allocator)
348: d_name(name, allocator)
349, d_attribute(d_name.c_str(), value, allocator)
350{
351}
352
353inline
355 long value,
356 const allocator_type& allocator)
357: d_name(name, allocator)
358, d_attribute(d_name.c_str(), value, allocator)
359{
360}
361
362inline
364 long long value,
365 const allocator_type& allocator)
366: d_name(name, allocator)
367, d_attribute(d_name.c_str(), value, allocator)
368{
369}
370
371inline
373 unsigned int value,
374 const allocator_type& allocator)
375: d_name(name, allocator)
376, d_attribute(d_name.c_str(), value, allocator)
377{
378}
379
380inline
382 unsigned long value,
383 const allocator_type& allocator)
384: d_name(name, allocator)
385, d_attribute(d_name.c_str(), value, allocator)
386{
387}
388
389inline
391 unsigned long long value,
392 const allocator_type& allocator)
393: d_name(name, allocator)
394, d_attribute(d_name.c_str(), value, allocator)
395{
396}
397
398inline
400 const void *value,
401 const allocator_type& allocator)
402: d_name(name, allocator)
403, d_attribute(d_name.c_str(), value, allocator)
404{
405}
406
407inline
409 const Attribute::Value& value,
410 const allocator_type& allocator)
411: d_name(name, allocator)
412, d_attribute(d_name.c_str(), value, allocator)
413{
414}
415
416inline
418 const allocator_type& allocator)
419: d_name(original.d_name, allocator)
420, d_attribute(d_name.c_str(), original.d_attribute.value(), allocator)
421{
422}
423
424// MANIPULATORS
425inline
427{
428 d_name = rhs.d_name;
429 d_attribute.setName(d_name.c_str());
430 d_attribute.setValue(rhs.d_attribute.value());
431 return *this;
432}
433
434inline
436{
437 d_name.assign(name);
438 d_attribute.setName(d_name.c_str());
439}
440
441inline
443{
444 d_attribute.setValue(value);
445}
446
447inline
449{
450 d_attribute.setValue(value);
451}
452
453inline
454void ManagedAttribute::setValue(long long value)
455{
456 d_attribute.setValue(value);
457}
458
459inline
460void ManagedAttribute::setValue(unsigned int value)
461{
462 d_attribute.setValue(value);
463}
464
465inline
466void ManagedAttribute::setValue(unsigned long value)
467{
468 d_attribute.setValue(value);
469}
470
471inline
472void ManagedAttribute::setValue(unsigned long long value)
473{
474 d_attribute.setValue(value);
475}
476
477inline
479{
480 d_attribute.setValue(value);
481}
482
483inline
485{
486 d_attribute.setValue(value);
487}
488
489inline
490void ManagedAttribute::setValue(const char *value)
491{
492 d_attribute.setValue(value);
493}
494
495inline
496void ManagedAttribute::setValue(const void *value)
497{
498 d_attribute.setValue(value);
499}
500
501// ACCESSORS
502inline
504{
505 return d_attribute;
506}
507
508inline
510{
511 return d_name;
512}
513
514inline
515const char *ManagedAttribute::name() const
516{
517 return d_attribute.name();
518}
519
520inline
522{
523 return d_attribute.value();
524}
525
526 // Aspects
527
528inline
533
534} // close package namespace
535
536// FREE OPERATORS
537inline
538bool ball::operator==(const ManagedAttribute& lhs,
539 const ManagedAttribute& rhs)
540{
541 return (lhs.d_name == rhs.d_name
542 && lhs.d_attribute.value() == rhs.d_attribute.value());
543}
544
545inline
546bool ball::operator==(const ManagedAttribute& lhs,
547 const Attribute& rhs)
548{
549 return (lhs.key() == rhs.name() && lhs.value() == rhs.value());
550}
551
552inline
553bool ball::operator==(const Attribute& lhs,
554 const ManagedAttribute& rhs)
555{
556 return (lhs.name() == rhs.key() && lhs.value() == rhs.value());
557}
558
559inline
560bool ball::operator!=(const ManagedAttribute& lhs,
561 const ManagedAttribute& rhs)
562{
563 return !(lhs == rhs);
564}
565
566inline
567bool ball::operator!=(const ManagedAttribute& lhs,
568 const Attribute& rhs)
569{
570 return !(lhs == rhs);
571}
572
573inline
574bool ball::operator!=(const Attribute& lhs,
575 const ManagedAttribute& rhs)
576{
577 return !(lhs == rhs);
578}
579
580
581
582#endif
583
584// ----------------------------------------------------------------------------
585// Copyright 2020 Bloomberg Finance L.P.
586//
587// Licensed under the Apache License, Version 2.0 (the "License");
588// you may not use this file except in compliance with the License.
589// You may obtain a copy of the License at
590//
591// http://www.apache.org/licenses/LICENSE-2.0
592//
593// Unless required by applicable law or agreed to in writing, software
594// distributed under the License is distributed on an "AS IS" BASIS,
595// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
596// See the License for the specific language governing permissions and
597// limitations under the License.
598// ----------------------------- END-OF-FILE ----------------------------------
599
600/** @} */
601/** @} */
602/** @} */
Definition ball_attribute.h:198
void setName(const char *name)
Definition ball_attribute.h:558
void setValue(const Value &value)
Definition ball_attribute.h:565
const char * name() const
Return the name of this object.
Definition ball_attribute.h:643
static int hash(const Attribute &attribute, int size)
const Value & value() const
Definition ball_attribute.h:649
Definition ball_managedattribute.h:117
const Attribute & attribute() const
Return a const reference to the attribute of this object.
Definition ball_managedattribute.h:503
bsl::allocator< char > allocator_type
Definition ball_managedattribute.h:131
const Attribute::Value & value() const
Return a const reference to the attribute value of this object.
Definition ball_managedattribute.h:521
friend bool operator==(const ManagedAttribute &, const ManagedAttribute &)
BSLMF_NESTED_TRAIT_DECLARATION(ManagedAttribute, bslma::UsesBslmaAllocator)
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
allocator_type get_allocator() const
Definition ball_managedattribute.h:529
friend bsl::ostream & operator<<(bsl::ostream &, const ManagedAttribute &)
static int hash(const ManagedAttribute &attribute, int size)
Definition ball_managedattribute.h:312
ManagedAttribute & operator=(const ManagedAttribute &rhs)
Definition ball_managedattribute.h:426
void setValue(int value)
Definition ball_managedattribute.h:442
const char * name() const
Definition ball_managedattribute.h:515
ManagedAttribute(const Attribute &attribute, const allocator_type &allocator=allocator_type())
Definition ball_managedattribute.h:319
const bsl::string & key() const
Return a const reference to the attribute name of this object.
Definition ball_managedattribute.h:509
~ManagedAttribute()=default
Destroy this object.
void setName(const bsl::string_view &name)
Set the attribute name of this object to the specified name.
Definition ball_managedattribute.h:435
friend bool operator!=(const ManagedAttribute &, const ManagedAttribute &)
Definition bslma_bslallocator.h:580
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
const CHAR_TYPE * c_str() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6705
basic_string & assign(const basic_string &replacement)
Definition bslstl_string.h:5716
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Return the allocator used by this string to supply memory.
Definition bslstl_string.h:6723
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition ball_administration.h:214
bsl::ostream & operator<<(bsl::ostream &output, const Attribute &attribute)
bool operator!=(const Attribute &lhs, const Attribute &rhs)
bool operator==(const Attribute &lhs, const Attribute &rhs)
Definition bslma_usesbslmaallocator.h:343