BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlma_memoryblockdescriptor.h
Go to the documentation of this file.
1/// @file bdlma_memoryblockdescriptor.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlma_memoryblockdescriptor.h -*-C++-*-
8#ifndef INCLUDED_BDLMA_MEMORYBLOCKDESCRIPTOR
9#define INCLUDED_BDLMA_MEMORYBLOCKDESCRIPTOR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlma_memoryblockdescriptor bdlma_memoryblockdescriptor
15/// @brief Provide a class describing a block of memory.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlma
19/// @{
20/// @addtogroup bdlma_memoryblockdescriptor
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlma_memoryblockdescriptor-purpose"> Purpose</a>
25/// * <a href="#bdlma_memoryblockdescriptor-classes"> Classes </a>
26/// * <a href="#bdlma_memoryblockdescriptor-description"> Description </a>
27/// * <a href="#bdlma_memoryblockdescriptor-usage"> Usage </a>
28/// * <a href="#bdlma_memoryblockdescriptor-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdlma_memoryblockdescriptor-purpose}
31/// Provide a class describing a block of memory.
32///
33/// # Classes {#bdlma_memoryblockdescriptor-classes}
34///
35/// - bdlma::MemoryBlockDescriptor: describes a block of memory
36///
37/// @see
38///
39/// # Description {#bdlma_memoryblockdescriptor-description}
40/// This component defines an in-core value-semantic class for
41/// describing a block of memory, namely `bdlma::MemoryBlockDescriptor`. Each
42/// descriptor object contains the address of the block of memory and the size
43/// of the block. The distinguished "null" descriptor contains an address and a
44/// size that are both 0.
45///
46/// ## Usage {#bdlma_memoryblockdescriptor-usage}
47///
48///
49/// This section illustrates intended use of this component.
50///
51/// ### Example 1: Basic Usage {#bdlma_memoryblockdescriptor-example-1-basic-usage}
52///
53///
54/// This example demonstrates how to create and test the state of a
55/// `bdlma::MemoryBlockDescriptor`.
56/// @code
57/// char buffer[100];
58///
59/// bdlma::MemoryBlockDescriptor a(buffer, sizeof buffer);
60/// assert(!a.isNull());
61/// assert(buffer == a.address());
62/// assert(sizeof buffer == a.size());
63///
64/// bdlma::MemoryBlockDescriptor b;
65/// assert( b.isNull());
66/// assert(0 == b.address());
67/// assert(0 == b.size());
68/// assert(a != b);
69///
70/// b = a;
71/// assert(!b.isNull());
72/// assert(buffer == b.address());
73/// assert(sizeof buffer == b.size());
74/// assert(a == b);
75///
76/// @endcode
77/// @}
78/** @} */
79/** @} */
80
81/** @addtogroup bdl
82 * @{
83 */
84/** @addtogroup bdlma
85 * @{
86 */
87/** @addtogroup bdlma_memoryblockdescriptor
88 * @{
89 */
90
91#include <bdlscm_version.h>
92
93#include <bsls_assert.h>
94#include <bsls_review.h>
95#include <bsls_types.h>
96
97
98namespace bdlma {
99
100 // ===========================
101 // class MemoryBlockDescriptor
102 // ===========================
103
104/// This is an in-core value-semantic class describing the size and address
105/// of a block of memory. A null block descriptor is defined as a
106/// descriptor having an address and size of 0.
107///
108/// \pre The behavior is undefined for a descriptor whose address is 0, but whose size if not also 0.
109///
110/// See @ref bdlma_memoryblockdescriptor
112
113 public:
114 // PUBLIC TYPES
115 typedef bsls::Types::size_type size_type; // type for block size
116
117 private:
118 // DATA
119 void *d_address_p; // address of the memory block
120 size_type d_size; // size of the block
121
122 public:
123 // CREATORS
124
125 /// Create a memory block descriptor having an address and size of 0.
127
128 /// Create a memory block descriptor having the specified `address` and `size`.
129 ///
130 /// \pre The behavior is undefined if `address` is 0 but `size` is
131 /// not also 0.
133
134 /// Create a memory block descriptor having the same value as the
135 /// specified `original` descriptor. Two descriptors have the same
136 /// value if and only if they have the same address and size.
138
139 /// Destroy this object.
141
142 // MANIPULATORS
143
144 /// Assign to this memory block descriptor the value of the specified `rhs`
145 /// descriptor and return a reference to this modifiable descriptor.
147
148 /// Set the address and size of the memory block described by this object to the specified `address` and `size`.
149 ///
150 /// \pre The behavior is undefined if
151 /// `address' is 0 but `size` is not also 0.
153
154 // ACCESSORS
155
156 /// Return `true` if this memory block descriptor describes a null
157 /// memory block and `false` otherwise. A null memory block has an
158 /// address and size of 0.
159 bool isNull() const;
160
161 /// Return the address of the modifiable memory block described by this
162 /// object, or 0 if this is a null descriptor.
163 void *address() const;
164
165 /// Return the size of the memory block described by this object.
166 size_type size() const;
167
168 /// Format the attributes of this memory block descriptor to `stdout` in
169 /// some reasonable (single-line) format.
170 void print() const;
171};
172
173// FREE OPERATORS
174
175/// Return `true` if the specified `lhs` and `rhs` memory block descriptors
176/// have the same value and `false` otherwise. Two descriptors have the same
177/// value if and only if they have the same address and size.
178bool operator==(const MemoryBlockDescriptor& lhs,
179 const MemoryBlockDescriptor& rhs);
180
181/// Return `true` if the specified `lhs` and `rhs` memory block descriptors do
182/// not have the same value and `false` otherwise. Two descriptors differ in
183/// value if they differ in either their address or size.
184bool operator!=(const MemoryBlockDescriptor& lhs,
185 const MemoryBlockDescriptor& rhs);
186
187// ============================================================================
188// INLINE DEFINITIONS
189// ============================================================================
190
191 // ---------------------------
192 // class MemoryBlockDescriptor
193 // ---------------------------
194
195// CREATORS
196inline
198: d_address_p(0)
199, d_size(0)
200{
201}
202
203inline
205 size_type size)
206: d_address_p(address)
207, d_size(size)
208{
209 BSLS_ASSERT(address || 0 == size);
210}
211
212inline
214 const MemoryBlockDescriptor& original)
215: d_address_p(original.d_address_p)
216, d_size(original.d_size)
217{
218}
219
220// MANIPULATORS
221inline
223 const MemoryBlockDescriptor& rhs)
224{
225 d_address_p = rhs.d_address_p;
226 d_size = rhs.d_size;
227
228 return *this;
229}
230
231inline
233{
234 BSLS_ASSERT(address || 0 == size);
235
236 d_address_p = address;
237 d_size = size;
238}
239
240// ACCESSORS
241inline
243{
244 return d_address_p;
245}
246
247inline
250{
251 return d_size;
252}
253
254inline
256{
257 return 0 == d_address_p;
258}
259
260} // close package namespace
261
262// FREE OPERATORS
263inline
264bool bdlma::operator==(const MemoryBlockDescriptor& lhs,
265 const MemoryBlockDescriptor& rhs)
266{
267 return lhs.address() == rhs.address() && lhs.size() == rhs.size();
268}
269
270inline
271bool bdlma::operator!=(const MemoryBlockDescriptor& lhs,
272 const MemoryBlockDescriptor& rhs)
273{
274 return !(lhs == rhs);
275}
276
277
278
279#endif
280
281// ----------------------------------------------------------------------------
282// Copyright 2015 Bloomberg Finance L.P.
283//
284// Licensed under the Apache License, Version 2.0 (the "License");
285// you may not use this file except in compliance with the License.
286// You may obtain a copy of the License at
287//
288// http://www.apache.org/licenses/LICENSE-2.0
289//
290// Unless required by applicable law or agreed to in writing, software
291// distributed under the License is distributed on an "AS IS" BASIS,
292// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
293// See the License for the specific language governing permissions and
294// limitations under the License.
295// ----------------------------- END-OF-FILE ----------------------------------
296
297/** @} */
298/** @} */
299/** @} */
Definition bdlma_memoryblockdescriptor.h:111
size_type size() const
Return the size of the memory block described by this object.
Definition bdlma_memoryblockdescriptor.h:249
MemoryBlockDescriptor()
Create a memory block descriptor having an address and size of 0.
Definition bdlma_memoryblockdescriptor.h:197
MemoryBlockDescriptor & operator=(const MemoryBlockDescriptor &rhs)
Definition bdlma_memoryblockdescriptor.h:222
bool isNull() const
Definition bdlma_memoryblockdescriptor.h:255
void * address() const
Definition bdlma_memoryblockdescriptor.h:242
~MemoryBlockDescriptor()=default
Destroy this object.
void setAddressAndSize(void *address, size_type size)
Definition bdlma_memoryblockdescriptor.h:232
bsls::Types::size_type size_type
Definition bdlma_memoryblockdescriptor.h:115
#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 bdlma_alignedallocator.h:278
bool operator==(const MemoryBlockDescriptor &lhs, const MemoryBlockDescriptor &rhs)
bool operator!=(const MemoryBlockDescriptor &lhs, const MemoryBlockDescriptor &rhs)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
std::size_t size_type
Definition bsls_types.h:126