BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdld_datumbinaryref.h
Go to the documentation of this file.
1/// @file bdld_datumbinaryref.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdld_datumbinaryref.h -*-C++-*-
8#ifndef INCLUDED_BDLD_DATUMBINARYREF
9#define INCLUDED_BDLD_DATUMBINARYREF
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$ $CSID$")
13
14/// @defgroup bdld_datumbinaryref bdld_datumbinaryref
15/// @brief Provide a type to represent binary data and its size.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdld
19/// @{
20/// @addtogroup bdld_datumbinaryref
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdld_datumbinaryref-purpose"> Purpose</a>
25/// * <a href="#bdld_datumbinaryref-classes"> Classes </a>
26/// * <a href="#bdld_datumbinaryref-description"> Description </a>
27/// * <a href="#bdld_datumbinaryref-usage"> Usage </a>
28/// * <a href="#bdld_datumbinaryref-example-1-basic-datumbinaryref-usage"> Example 1: Basic DatumBinaryRef usage </a>
29///
30/// # Purpose {#bdld_datumbinaryref-purpose}
31/// Provide a type to represent binary data and its size.
32///
33/// # Classes {#bdld_datumbinaryref-classes}
34///
35/// - bdld::DatumBinaryRef: a type representing binary data and its size
36///
37/// @see bdld_datum, bdld_datumerror, bdld_datumudt
38///
39/// # Description {#bdld_datumbinaryref-description}
40/// This component implements a class, `bdld::DatumBinaryRef`, that
41/// represents a pointer to a non-modifiable binary data (as a `void *`) and an
42/// integral size value. Note, that `bdld::DatumBinaryRef` is nether a
43/// value-semantic type, nor is it an in-core value-semantic type (see
44/// @ref bsldoc_glossary ). The `bdld::DatumBinaryRef` notion of value is expressed
45/// by its equality-operator - two `bdld::DatumBinaryRef` compare equal if the
46/// binary data they refer to is identical (both by size and by content).
47/// Accessors inside `Datum` class that need to return a binary data, return an
48/// instance of `bdld::DatumBinaryRef`.
49///
50/// ## Usage {#bdld_datumbinaryref-usage}
51///
52///
53/// This section illustrates intended use of this component.
54///
55/// ### Example 1: Basic DatumBinaryRef usage {#bdld_datumbinaryref-example-1-basic-datumbinaryref-usage}
56///
57///
58/// Suppose we have three functions. Data are obtained in the first one (with
59/// memory allocation), processed in the second one and released (with memory
60/// deallocation) in the third one. The following code illustrates how to use
61/// `bdld::DatumBinaryRef` to pass information about memory storage between
62/// them.
63///
64/// First, we write all three functions:
65/// @code
66/// // Allocate array of the specified `size` and initialize it with some
67/// // values.
68/// bdld::DatumBinaryRef obtainData(size_t size)
69/// {
70/// if (0 == size) {
71/// return bdld::DatumBinaryRef(); // RETURN
72/// }
73/// int *buffer = new int[size];
74/// for (size_t i = 0; i < size; ++i) {
75/// buffer[i] = static_cast<int>(i);
76/// }
77/// return bdld::DatumBinaryRef(static_cast<void *>(buffer), size);
78/// }
79///
80/// /// Process data, held by the specified `binaryData` object.
81/// int processData(const bdld::DatumBinaryRef& binaryData)
82/// {
83/// ostringstream out;
84/// binaryData.print(out);
85///
86/// if (binaryData == bdld::DatumBinaryRef()) {
87/// return 0; // RETURN
88/// }
89///
90/// int result = 0;
91/// const int *array = static_cast<const int *>(binaryData.data());
92/// for (size_t i = 0; i < binaryData.size(); ++i) {
93/// result += array[i];
94/// }
95/// return result;
96/// }
97///
98/// /// Release memory, held by the specified `binaryData` object.
99/// void releaseData(const bdld::DatumBinaryRef& binaryData)
100/// {
101/// const int *array = static_cast<const int *>(binaryData.data());
102/// delete [] array;
103/// }
104/// @endcode
105/// Next, we call first one to obtain data:
106/// @code
107/// bdld::DatumBinaryRef binaryData = obtainData(5);
108/// @endcode
109/// Then we verify the results of second one's call:
110/// @code
111/// assert(10 == processData(binaryData));
112/// @endcode
113/// Finally, we release allocated memory:
114/// @code
115/// releaseData(binaryData);
116/// @endcode
117/// @}
118/** @} */
119/** @} */
120
121/** @addtogroup bdl
122 * @{
123 */
124/** @addtogroup bdld
125 * @{
126 */
127/** @addtogroup bdld_datumbinaryref
128 * @{
129 */
130
131#include <bdlscm_version.h>
132
133#include <bdlb_printmethods.h>
134
137
138#include <bsls_assert.h>
139#include <bsls_review.h>
140#include <bsls_types.h>
141
142#include <bsl_algorithm.h>
143#include <bsl_cstring.h>
144#include <bsl_iosfwd.h>
145
146
147
148namespace bdld {
149 // ====================
150 // class DatumBinaryRef
151 // ====================
152
153/// This class provides a type to represent a pointer to non-modifiable
154/// binary data and an integral size value.
155///
156/// See @ref bdld_datumbinaryref
158
159 public:
160 // TYPES
161
162 /// `SizeType` is an alias for an unsigned integral value, representing
163 /// the length of the binary array.
165
166 private:
167 // DATA
168 const void *d_data_p; // pointer to the binary data (not owned)
169 SizeType d_size; // size of the binary data
170
171 public:
172 // TRAITS
175
176 // CREATORS
177
178 /// Create a `DatumBinaryRef` object having zero data pointer and size.
180
181 /// Create a `DatumBinaryRef` object having the specified `data` and `size`.
182 ///
183 /// \pre The behavior is undefined unless `0 != data || 0 == size`.
184 ///
185 /// \note Note that the memory pointed by `data` is never accessed by a
186 /// `DatumBinaryRef` object.
187 DatumBinaryRef(const void *data, SizeType size);
188
189 DatumBinaryRef(const DatumBinaryRef& origin) = default;
190 ~DatumBinaryRef() = default;
191
192 // MANIPULATORS
193
195
196 // ACCESSORS
197
198 /// Return the pointer to the non-modifiable binary data.
199 const void *data() const;
200
201 /// Return the size of the binary data.
202 SizeType size() const;
203
204 /// Write the value of this object to the specified output `stream` in a
205 /// human-readable format, and return a reference to the modifyable
206 /// `stream`. Optionally specify an initial indentation `level`, whose
207 /// absolute value is incremented recursively for nested objects. If
208 /// `level` is specified, optionally specify `spacesPerLevel`, whose
209 /// absolute value indicates the number of spaces per indentation level
210 /// for this and all of its nested objects. If `level` is negative,
211 /// suppress indentation of the first line. If `spacesPerLevel` is
212 /// negative, format the entire output on one line, suppressing all but
213 /// the initial indentation (as governed by `level`). If `stream` is not valid on entry, this operation has no effect.
214 ///
215 /// \note Note that this
216 /// human-readable format is not fully specified, and can change without
217 /// notice.
218 bsl::ostream& print(bsl::ostream& stream,
219 int level = 0,
220 int spacesPerLevel = 4) const;
221};
222
223// FREE OPERATORS
224
225/// Return `true` if the specified `lhs` and `rhs` have the same value, and
226/// `false` otherwise. Two `DatumBinaryRef` objects have the same value if
227/// they refer to arrays of bytes of the same size and having the same
228/// content.
229bool operator==(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
230
231/// Return `true` if the specified `lhs` and `rhs` have different values,
232/// and `true` otherwise. Two `DatumBinaryRef` objects have different
233/// values if they refer to arrays of bytes of different sizes or having a
234/// different content.
235bool operator!=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
236
237/// Return `true` if the specified `lhs` compares smaller than the specified
238/// `rhs`, and `false` otherwise. A byte comparision is performed using
239/// memcpy.
240bool operator<(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
241
242/// Return `true` if the specified `lhs` compares larger than the specified
243/// `rhs`, and `false` otherwise. A byte comparision is performed using
244/// memcpy.
245bool operator>(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
246
247/// Return `true` if the specified `lhs` compares smaller than or equal to
248/// the specified `rhs`, and `false` otherwise. A byte comparision is
249/// performed using memcpy.
250bool operator<=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
251
252/// Return `true` if the specified `lhs` compares larger than or equal to
253/// the specified `rhs`, and `false` otherwise. A byte comparision is
254/// performed using memcpy.
255bool operator>=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
256
257/// Write the value of the specified `rhs` object to the specified output
258/// `stream` in a single-line format, and return a reference to the
259/// modifyable `stream`. If `stream` is not valid on entry, this operation has no effect.
260///
261/// \note Note that this human-readable format is not fully
262/// specified, can change without notice, and is logically equivalent to:
263/// @code
264/// print(stream, 0, -1);
265/// @endcode
266bsl::ostream& operator<<(bsl::ostream& stream, const DatumBinaryRef& rhs);
267
268// ============================================================================
269// INLINE DEFINITIONS
270// ============================================================================
271
272 // --------------------
273 // class DatumBinaryRef
274 // --------------------
275
276// CREATORS
277inline
279: d_data_p(0)
280, d_size(0)
281{
282}
283
284inline
286: d_data_p(data)
287, d_size(size)
288{
289 BSLS_ASSERT(0 != data || 0 == size);
290}
291
292// ACCESSORS
293inline
294const void *DatumBinaryRef::data() const
295{
296 return d_data_p;
297}
298
299inline
301{
302 return d_size;
303}
304
305} // close package namespace
306
307// FREE OPERATORS
308inline
309bool bdld::operator==(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
310{
311 return (lhs.size() == rhs.size()) &&
312 (lhs.size() == 0 || !bsl::memcmp(lhs.data(), rhs.data(), rhs.size()));
313}
314
315
316inline
317bool bdld::operator!=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
318{
319 return !(lhs == rhs);
320}
321
322inline
323bool bdld::operator<(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
324{
325 const size_t minSize = bsl::min(lhs.size(), rhs.size());
326 const int cmp = minSize
327 ? bsl::memcmp(lhs.data(), rhs.data(), minSize)
328 : 0;
329
330 return cmp == 0 ? lhs.size() < rhs.size() : cmp < 0;
331}
332
333inline
334bool bdld::operator>(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
335{
336 const size_t minSize = bsl::min(lhs.size(), rhs.size());
337 const int cmp = minSize
338 ? bsl::memcmp(lhs.data(), rhs.data(), minSize)
339 : 0;
340
341 return cmp == 0 ? lhs.size() > rhs.size() : cmp > 0;
342}
343
344inline
345bool bdld::operator<=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
346{
347 return !(lhs > rhs);
348}
349
350inline
351bool bdld::operator>=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
352{
353 return !(lhs < rhs);
354}
355
356inline
357bsl::ostream& bdld::operator<<(bsl::ostream& stream, const DatumBinaryRef& rhs)
358{
359 return rhs.print(stream, 0 , -1);
360}
361
362
363
364#endif
365
366// ----------------------------------------------------------------------------
367// Copyright 2015 Bloomberg Finance L.P.
368//
369// Licensed under the Apache License, Version 2.0 (the "License");
370// you may not use this file except in compliance with the License.
371// You may obtain a copy of the License at
372//
373// http://www.apache.org/licenses/LICENSE-2.0
374//
375// Unless required by applicable law or agreed to in writing, software
376// distributed under the License is distributed on an "AS IS" BASIS,
377// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
378// See the License for the specific language governing permissions and
379// limitations under the License.
380// ----------------------------- END-OF-FILE ----------------------------------
381
382
383/** @} */
384/** @} */
385/** @} */
Definition bdld_datumbinaryref.h:157
bsls::Types::size_type SizeType
Definition bdld_datumbinaryref.h:164
const void * data() const
Return the pointer to the non-modifiable binary data.
Definition bdld_datumbinaryref.h:294
BSLMF_NESTED_TRAIT_DECLARATION(DatumBinaryRef, bdlb::HasPrintMethod)
~DatumBinaryRef()=default
DatumBinaryRef(const DatumBinaryRef &origin)=default
DatumBinaryRef & operator=(const DatumBinaryRef &rhs)=default
DatumBinaryRef()
Create a DatumBinaryRef object having zero data pointer and size.
Definition bdld_datumbinaryref.h:278
SizeType size() const
Return the size of the binary data.
Definition bdld_datumbinaryref.h:300
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
BSLMF_NESTED_TRAIT_DECLARATION(DatumBinaryRef, bsl::is_trivially_copyable)
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdld_datum.h:740
bsl::ostream & operator<<(bsl::ostream &stream, const Datum &rhs)
bool operator<=(const DatumBinaryRef &lhs, const DatumBinaryRef &rhs)
bool operator<(const DatumBinaryRef &lhs, const DatumBinaryRef &rhs)
bool operator>=(const DatumBinaryRef &lhs, const DatumBinaryRef &rhs)
bool operator==(const Datum &lhs, const Datum &rhs)
bool operator>(const DatumBinaryRef &lhs, const DatumBinaryRef &rhs)
bool operator!=(const Datum &lhs, const Datum &rhs)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Definition bdlb_printmethods.h:306
Definition bslmf_istriviallycopyable.h:324
std::size_t size_type
Definition bsls_types.h:126