BDE 4.39.x 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]`.
142 ///
143 /// \pre The behavior is undefined unless `0 < size`.
144 static int hash(const ManagedAttribute& attribute, int size);
145
146 // CREATORS
147
148 /// Create a `ManagedAttribute` object having the value of the specified
149 /// `attribute`. Optionally specify an `allocator` (e.g., the address
150 /// of a `bslma::Allocator` object) to supply memory; otherwise, the
151 /// default allocator is used.
152 explicit ManagedAttribute(
153 const Attribute& attribute,
154 const allocator_type& allocator = allocator_type());
155
156 /// Create a `ManagedAttribute` object having the specified `name` and
157 /// string `value`. Optionally specify an `allocator` (e.g., the
158 /// address of a `bslma::Allocator` object) to supply memory; otherwise,
159 /// the default allocator is used.
161 const bsl::string_view& value,
162 const allocator_type& allocator = allocator_type());
163
164 /// Create a `ManagedAttribute` object having the specified `name` and
165 /// the C-style `value` string. Optionally specify an `allocator`
166 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
167 /// otherwise, the default allocator is used.
169 const char *value,
170 const allocator_type& allocator = allocator_type());
171
172 /// Create a `ManagedAttribute` object having the specified `name` and
173 /// `value`. Optionally specify an `allocator` (e.g., the address of a
174 /// `bslma::Allocator` object) to supply memory; otherwise, the default
175 /// allocator is used.
177 int value,
178 const allocator_type& allocator = allocator_type());
180 long value,
181 const allocator_type& allocator = allocator_type());
183 long long value,
184 const allocator_type& allocator = allocator_type());
186 unsigned int value,
187 const allocator_type& allocator = allocator_type());
189 unsigned long value,
190 const allocator_type& allocator = allocator_type());
192 unsigned long long value,
193 const allocator_type& allocator = allocator_type());
194
195 /// Create a `ManagedAttribute` object having the specified `name` and
196 /// the pointer to the specified `value` of cv-qualified `void` type.
197 /// Optionally specify an `allocator` (e.g., the address of a
198 /// `bslma::Allocator` object) to supply memory; otherwise, the default
199 /// allocator is used.
201 const void *value,
202 const allocator_type& allocator = allocator_type());
203
204 /// Create a `ManagedAttribute` object having the specified `name` and
205 /// `value`. Optionally specify an `allocator` (e.g., the address of a
206 /// `bslma::Allocator` object) to supply memory; otherwise, the default
207 /// allocator is used.
209 const Attribute::Value& value,
210 const allocator_type& allocator = allocator_type());
211
212 /// Create a `ManagedAttribute` object having the same value as the
213 /// specified `original` object. Optionally specify an `allocator`
214 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
215 /// otherwise, the default allocator is used.
216 ManagedAttribute(const ManagedAttribute& original,
217 const allocator_type& allocator = allocator_type());
218
219 /// Destroy this object.
220 ~ManagedAttribute() = default;
221
222 // MANIPULATORS
223
224 /// Assign to this object the value of the specified `rhs` object, and
225 /// return a non-`const` reference to this object.
227
228 /// Set the attribute name of this object to the specified `name`.
229 void setName(const bsl::string_view& name);
230
231 /// Set the attribute value of this object to the specified `value`.
232 void setValue(int value);
233 void setValue(long value);
234 void setValue(long long value);
235 void setValue(unsigned int value);
236 void setValue(unsigned long value);
237 void setValue(unsigned long long value);
238 void setValue(const bsl::string_view& value);
239 void setValue(const Attribute::Value& value);
240 void setValue(const char *value);
241 void setValue(const void *value);
242
243 // ACCESSORS
244
245 /// Return a `const` reference to the attribute of this object.
246 const Attribute& attribute() const;
247
248 /// Return the allocator used by this object to supply memory.
249 ///
250 /// \note Note that if no allocator was supplied at construction the default
251 /// allocator in effect at construction is used.
253
254 /// Format this object to the specified output `stream` at the (absolute
255 /// value of) the optionally specified indentation `level` and return a
256 /// reference to `stream`. If `level` is specified, optionally specify
257 /// `spacesPerLevel`, the number of spaces per indentation level for
258 /// this and all of its nested objects. If `level` is negative,
259 /// suppress indentation of the first line. If `spacesPerLevel` is
260 /// negative, format the entire output on one line, suppressing all but
261 /// the initial indentation (as governed by `level`). If `stream` is
262 /// not valid on entry, this operation has no effect.
263 bsl::ostream& print(bsl::ostream& stream,
264 int level = 0,
265 int spacesPerLevel = 4) const;
266
267 /// Return a `const` reference to the attribute name of this object.
268 const bsl::string& key() const;
269
270 /// Return the attribute name of this object.
271 /// \note Note that this accessor
272 /// should not be used to get the attribute name if the name string
273 /// contains embedded zeros.
274 ///
275 /// @deprecated Use @ref key() instead.
276 const char *name() const;
277
278 /// Return a `const` reference to the attribute value of this object.
279 const Attribute::Value& value() const;
280};
281
282// FREE OPERATORS
283
284/// Return `true` if the specified `lhs` and `rhs` objects have the same
285/// value, and `false` otherwise. Two `ManagedAttribute` objects have the
286/// same value if they have the same name, same attribute value type, and
287/// the same attribute value.
288bool operator==(const ManagedAttribute& lhs, const ManagedAttribute& rhs);
289bool operator==(const ManagedAttribute& lhs, const Attribute& rhs);
290bool operator==(const Attribute& lhs, const ManagedAttribute& rhs);
291
292/// Return `true` if the specified `lhs` and `rhs` objects do not have the
293/// same value, and `false` otherwise. Two `ManagedAttribute` objects do
294/// not have the same value if any of their respective names, attribute
295/// value types, or attribute values differ.
296bool operator!=(const ManagedAttribute& lhs, const ManagedAttribute& rhs);
297bool operator!=(const ManagedAttribute& lhs, const Attribute& rhs);
298bool operator!=(const Attribute& lhs, const ManagedAttribute& rhs);
299
300/// Write the value of the specified `attribute` to the specified `output`
301/// stream. Return the specified `output` stream.
302bsl::ostream& operator<<(bsl::ostream& output,
303 const ManagedAttribute& attribute);
304
305// ============================================================================
306// INLINE DEFINITIONS
307// ============================================================================
308
309 // ----------------------
310 // class ManagedAttribute
311 // ----------------------
312
313//CLASS METHODS
314inline
315int ManagedAttribute::hash(const ManagedAttribute& attribute, int size)
316{
317 return Attribute::hash(attribute.d_attribute, size);
318}
319
320// CREATORS
321inline
323 const allocator_type& allocator)
324: d_name(attribute.name(), allocator)
325, d_attribute(d_name.c_str(), attribute.value(), allocator)
326{
327}
328
329inline
331 const bsl::string_view& value,
332 const allocator_type& allocator)
333: d_name(name, allocator)
334, d_attribute(d_name.c_str(), value, allocator)
335{
336}
337
338inline
340 const char *value,
341 const allocator_type& allocator)
342: d_name(name, allocator)
343, d_attribute(d_name.c_str(), value, allocator)
344{
345}
346
347inline
349 int value,
350 const allocator_type& allocator)
351: d_name(name, allocator)
352, d_attribute(d_name.c_str(), value, allocator)
353{
354}
355
356inline
358 long value,
359 const allocator_type& allocator)
360: d_name(name, allocator)
361, d_attribute(d_name.c_str(), value, allocator)
362{
363}
364
365inline
367 long long value,
368 const allocator_type& allocator)
369: d_name(name, allocator)
370, d_attribute(d_name.c_str(), value, allocator)
371{
372}
373
374inline
376 unsigned int value,
377 const allocator_type& allocator)
378: d_name(name, allocator)
379, d_attribute(d_name.c_str(), value, allocator)
380{
381}
382
383inline
385 unsigned long value,
386 const allocator_type& allocator)
387: d_name(name, allocator)
388, d_attribute(d_name.c_str(), value, allocator)
389{
390}
391
392inline
394 unsigned long long value,
395 const allocator_type& allocator)
396: d_name(name, allocator)
397, d_attribute(d_name.c_str(), value, allocator)
398{
399}
400
401inline
403 const void *value,
404 const allocator_type& allocator)
405: d_name(name, allocator)
406, d_attribute(d_name.c_str(), value, allocator)
407{
408}
409
410inline
412 const Attribute::Value& value,
413 const allocator_type& allocator)
414: d_name(name, allocator)
415, d_attribute(d_name.c_str(), value, allocator)
416{
417}
418
419inline
421 const allocator_type& allocator)
422: d_name(original.d_name, allocator)
423, d_attribute(d_name.c_str(), original.d_attribute.value(), allocator)
424{
425}
426
427// MANIPULATORS
428inline
430{
431 d_name = rhs.d_name;
432 d_attribute.setName(d_name.c_str());
433 d_attribute.setValue(rhs.d_attribute.value());
434 return *this;
435}
436
437inline
439{
440 d_name.assign(name);
441 d_attribute.setName(d_name.c_str());
442}
443
444inline
446{
447 d_attribute.setValue(value);
448}
449
450inline
452{
453 d_attribute.setValue(value);
454}
455
456inline
457void ManagedAttribute::setValue(long long value)
458{
459 d_attribute.setValue(value);
460}
461
462inline
463void ManagedAttribute::setValue(unsigned int value)
464{
465 d_attribute.setValue(value);
466}
467
468inline
469void ManagedAttribute::setValue(unsigned long value)
470{
471 d_attribute.setValue(value);
472}
473
474inline
475void ManagedAttribute::setValue(unsigned long long value)
476{
477 d_attribute.setValue(value);
478}
479
480inline
482{
483 d_attribute.setValue(value);
484}
485
486inline
488{
489 d_attribute.setValue(value);
490}
491
492inline
493void ManagedAttribute::setValue(const char *value)
494{
495 d_attribute.setValue(value);
496}
497
498inline
499void ManagedAttribute::setValue(const void *value)
500{
501 d_attribute.setValue(value);
502}
503
504// ACCESSORS
505inline
507{
508 return d_attribute;
509}
510
511inline
513{
514 return d_name;
515}
516
517inline
518const char *ManagedAttribute::name() const
519{
520 return d_attribute.name();
521}
522
523inline
525{
526 return d_attribute.value();
527}
528
529 // Aspects
530
531inline
536
537} // close package namespace
538
539// FREE OPERATORS
540inline
541bool ball::operator==(const ManagedAttribute& lhs,
542 const ManagedAttribute& rhs)
543{
544 return (lhs.d_name == rhs.d_name
545 && lhs.d_attribute.value() == rhs.d_attribute.value());
546}
547
548inline
549bool ball::operator==(const ManagedAttribute& lhs,
550 const Attribute& rhs)
551{
552 return (lhs.key() == rhs.name() && lhs.value() == rhs.value());
553}
554
555inline
556bool ball::operator==(const Attribute& lhs,
557 const ManagedAttribute& rhs)
558{
559 return (lhs.name() == rhs.key() && lhs.value() == rhs.value());
560}
561
562inline
563bool ball::operator!=(const ManagedAttribute& lhs,
564 const ManagedAttribute& rhs)
565{
566 return !(lhs == rhs);
567}
568
569inline
570bool ball::operator!=(const ManagedAttribute& lhs,
571 const Attribute& rhs)
572{
573 return !(lhs == rhs);
574}
575
576inline
577bool ball::operator!=(const Attribute& lhs,
578 const ManagedAttribute& rhs)
579{
580 return !(lhs == rhs);
581}
582
583
584
585#endif
586
587// ----------------------------------------------------------------------------
588// Copyright 2020 Bloomberg Finance L.P.
589//
590// Licensed under the Apache License, Version 2.0 (the "License");
591// you may not use this file except in compliance with the License.
592// You may obtain a copy of the License at
593//
594// http://www.apache.org/licenses/LICENSE-2.0
595//
596// Unless required by applicable law or agreed to in writing, software
597// distributed under the License is distributed on an "AS IS" BASIS,
598// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
599// See the License for the specific language governing permissions and
600// limitations under the License.
601// ----------------------------- END-OF-FILE ----------------------------------
602
603/** @} */
604/** @} */
605/** @} */
Definition ball_attribute.h:199
void setName(const char *name)
Definition ball_attribute.h:558
void setValue(const Value &value)
Set the attribute value of this object to the specified 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:651
Definition ball_managedattribute.h:117
const Attribute & attribute() const
Return a const reference to the attribute of this object.
Definition ball_managedattribute.h:506
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:524
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:532
friend bsl::ostream & operator<<(bsl::ostream &, const ManagedAttribute &)
static int hash(const ManagedAttribute &attribute, int size)
Definition ball_managedattribute.h:315
ManagedAttribute & operator=(const ManagedAttribute &rhs)
Definition ball_managedattribute.h:429
void setValue(int value)
Set the attribute value of this object to the specified value.
Definition ball_managedattribute.h:445
const char * name() const
Definition ball_managedattribute.h:518
ManagedAttribute(const Attribute &attribute, const allocator_type &allocator=allocator_type())
Definition ball_managedattribute.h:322
const bsl::string & key() const
Return a const reference to the attribute name of this object.
Definition ball_managedattribute.h:512
~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:438
friend bool operator!=(const ManagedAttribute &, const ManagedAttribute &)
Definition bslma_bslallocator.h:588
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
const CHAR_TYPE * c_str() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:7405
basic_string & assign(const basic_string &replacement)
Definition bslstl_string.h:6347
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Return the allocator used by this string to supply memory.
Definition bslstl_string.h:7423
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
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)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Definition bslma_usesbslmaallocator.h:344