BDE 4.14.0 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
182 /// `size`. The behavior is undefined unless `0 != data || 0 == size`.
183 /// Note that the memory pointed by `data` is never accessed by a
184 /// `DatumBinaryRef` object.
185 DatumBinaryRef(const void *data, SizeType size);
186
187 DatumBinaryRef(const DatumBinaryRef& origin) = default;
188 ~DatumBinaryRef() = default;
189
190 // MANIPULATORS
191
193
194 // ACCESSORS
195
196 /// Return the pointer to the non-modifiable binary data.
197 const void *data() const;
198
199 /// Return the size of the binary data.
200 SizeType size() const;
201
202 /// Write the value of this object to the specified output `stream` in a
203 /// human-readable format, and return a reference to the modifyable
204 /// `stream`. Optionally specify an initial indentation `level`, whose
205 /// absolute value is incremented recursively for nested objects. If
206 /// `level` is specified, optionally specify `spacesPerLevel`, whose
207 /// absolute value indicates the number of spaces per indentation level
208 /// for this and all of its nested objects. If `level` is negative,
209 /// suppress indentation of the first line. If `spacesPerLevel` is
210 /// negative, format the entire output on one line, suppressing all but
211 /// the initial indentation (as governed by `level`). If `stream` is
212 /// not valid on entry, this operation has no effect. Note that this
213 /// human-readable format is not fully specified, and can change without
214 /// notice.
215 bsl::ostream& print(bsl::ostream& stream,
216 int level = 0,
217 int spacesPerLevel = 4) const;
218};
219
220// FREE OPERATORS
221
222/// Return `true` if the specified `lhs` and `rhs` have the same value, and
223/// `false` otherwise. Two `DatumBinaryRef` objects have the same value if
224/// they refer to arrays of bytes of the same size and having the same
225/// content.
226bool operator==(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
227
228/// Return `true` if the specified `lhs` and `rhs` have different values,
229/// and `true` otherwise. Two `DatumBinaryRef` objects have different
230/// values if they refer to arrays of bytes of different sizes or having a
231/// different content.
232bool operator!=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
233
234/// Return `true` if the specified `lhs` compares smaller than the specified
235/// `rhs`, and `false` otherwise. A byte comparision is performed using
236/// memcpy.
237bool operator<(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
238
239/// Return `true` if the specified `lhs` compares larger than the specified
240/// `rhs`, and `false` otherwise. A byte comparision is performed using
241/// memcpy.
242bool operator>(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
243
244/// Return `true` if the specified `lhs` compares smaller than or equal to
245/// the specified `rhs`, and `false` otherwise. A byte comparision is
246/// performed using memcpy.
247bool operator<=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
248
249/// Return `true` if the specified `lhs` compares larger than or equal to
250/// the specified `rhs`, and `false` otherwise. A byte comparision is
251/// performed using memcpy.
252bool operator>=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs);
253
254/// Write the value of the specified `rhs` object to the specified output
255/// `stream` in a single-line format, and return a reference to the
256/// modifyable `stream`. If `stream` is not valid on entry, this operation
257/// has no effect. Note that this human-readable format is not fully
258/// specified, can change without notice, and is logically equivalent to:
259/// @code
260/// print(stream, 0, -1);
261/// @endcode
262bsl::ostream& operator<<(bsl::ostream& stream, const DatumBinaryRef& rhs);
263
264// ============================================================================
265// INLINE DEFINITIONS
266// ============================================================================
267
268 // --------------------
269 // class DatumBinaryRef
270 // --------------------
271
272// CREATORS
273inline
275: d_data_p(0)
276, d_size(0)
277{
278}
279
280inline
282: d_data_p(data)
283, d_size(size)
284{
285 BSLS_ASSERT(0 != data || 0 == size);
286}
287
288// ACCESSORS
289inline
290const void *DatumBinaryRef::data() const
291{
292 return d_data_p;
293}
294
295inline
297{
298 return d_size;
299}
300
301} // close package namespace
302
303// FREE OPERATORS
304inline
305bool bdld::operator==(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
306{
307 return (lhs.size() == rhs.size()) &&
308 (lhs.size() == 0 || !bsl::memcmp(lhs.data(), rhs.data(), rhs.size()));
309}
310
311
312inline
313bool bdld::operator!=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
314{
315 return !(lhs == rhs);
316}
317
318inline
319bool bdld::operator<(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
320{
321 const size_t minSize = bsl::min(lhs.size(), rhs.size());
322 const int cmp = minSize
323 ? bsl::memcmp(lhs.data(), rhs.data(), minSize)
324 : 0;
325
326 return cmp == 0 ? lhs.size() < rhs.size() : cmp < 0;
327}
328
329inline
330bool bdld::operator>(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
331{
332 const size_t minSize = bsl::min(lhs.size(), rhs.size());
333 const int cmp = minSize
334 ? bsl::memcmp(lhs.data(), rhs.data(), minSize)
335 : 0;
336
337 return cmp == 0 ? lhs.size() > rhs.size() : cmp > 0;
338}
339
340inline
341bool bdld::operator<=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
342{
343 return !(lhs > rhs);
344}
345
346inline
347bool bdld::operator>=(const DatumBinaryRef& lhs, const DatumBinaryRef& rhs)
348{
349 return !(lhs < rhs);
350}
351
352inline
353bsl::ostream& bdld::operator<<(bsl::ostream& stream, const DatumBinaryRef& rhs)
354{
355 return rhs.print(stream, 0 , -1);
356}
357
358
359
360#endif
361
362// ----------------------------------------------------------------------------
363// Copyright 2015 Bloomberg Finance L.P.
364//
365// Licensed under the Apache License, Version 2.0 (the "License");
366// you may not use this file except in compliance with the License.
367// You may obtain a copy of the License at
368//
369// http://www.apache.org/licenses/LICENSE-2.0
370//
371// Unless required by applicable law or agreed to in writing, software
372// distributed under the License is distributed on an "AS IS" BASIS,
373// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
374// See the License for the specific language governing permissions and
375// limitations under the License.
376// ----------------------------- END-OF-FILE ----------------------------------
377
378
379/** @} */
380/** @} */
381/** @} */
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:290
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:274
SizeType size() const
Return the size of the binary data.
Definition bdld_datumbinaryref.h:296
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:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdld_datum.h:730
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)
Definition bdlb_printmethods.h:306
Definition bslmf_istriviallycopyable.h:329
std::size_t size_type
Definition bsls_types.h:124