BDE 4.14.0 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. The behavior is undefined
107/// for a descriptor whose address is 0, but whose size if not also 0.
108///
109/// See @ref bdlma_memoryblockdescriptor
111
112 public:
113 // PUBLIC TYPES
114 typedef bsls::Types::size_type size_type; // type for block size
115
116 private:
117 // DATA
118 void *d_address_p; // address of the memory block
119 size_type d_size; // size of the block
120
121 public:
122 // CREATORS
123
124 /// Create a memory block descriptor having an address and size of 0.
126
127 /// Create a memory block descriptor having the specified `address` and
128 /// `size`. The behavior is undefined if `address` is 0 but `size` is
129 /// not also 0.
131
132 /// Create a memory block descriptor having the same value as the
133 /// specified `original` descriptor. Two descriptors have the same
134 /// value if and only if they have the same address and size.
136
137 // Destroy this object.
139
140 // MANIPULATORS
141
142 /// Assign to this memory block descriptor the value of the specified `rhs`
143 /// descriptor and return a reference to this modifiable descriptor.
145
146 /// Set the address and size of the memory block described by this object
147 /// to the specified `address` and `size`. The behavior is undefined if
148 /// `address' is 0 but `size` is not also 0.
150
151 // ACCESSORS
152
153 /// Return `true` if this memory block descriptor describes a null
154 /// memory block and `false` otherwise. A null memory block has an
155 /// address and size of 0.
156 bool isNull() const;
157
158 /// Return the address of the modifiable memory block described by this
159 /// object, or 0 if this is a null descriptor.
160 void *address() const;
161
162 /// Return the size of the memory block described by this object.
163 size_type size() const;
164
165 /// Format the attributes of this memory block descriptor to `stdout` in
166 /// some reasonable (single-line) format.
167 void print() const;
168};
169
170// FREE OPERATORS
171
172/// Return `true` if the specified `lhs` and `rhs` memory block descriptors
173/// have the same value and `false` otherwise. Two descriptors have the same
174/// value if and only if they have the same address and size.
175bool operator==(const MemoryBlockDescriptor& lhs,
176 const MemoryBlockDescriptor& rhs);
177
178/// Return `true` if the specified `lhs` and `rhs` memory block descriptors do
179/// not have the same value and `false` otherwise. Two descriptors differ in
180/// value if they differ in either their address or size.
181bool operator!=(const MemoryBlockDescriptor& lhs,
182 const MemoryBlockDescriptor& rhs);
183
184// ============================================================================
185// INLINE DEFINITIONS
186// ============================================================================
187
188 // ---------------------------
189 // class MemoryBlockDescriptor
190 // ---------------------------
191
192// CREATORS
193inline
195: d_address_p(0)
196, d_size(0)
197{
198}
199
200inline
202 size_type size)
203: d_address_p(address)
204, d_size(size)
205{
206 BSLS_ASSERT(address || 0 == size);
207}
208
209inline
211 const MemoryBlockDescriptor& original)
212: d_address_p(original.d_address_p)
213, d_size(original.d_size)
214{
215}
216
217// MANIPULATORS
218inline
220 const MemoryBlockDescriptor& rhs)
221{
222 d_address_p = rhs.d_address_p;
223 d_size = rhs.d_size;
224
225 return *this;
226}
227
228inline
230{
231 BSLS_ASSERT(address || 0 == size);
232
233 d_address_p = address;
234 d_size = size;
235}
236
237// ACCESSORS
238inline
240{
241 return d_address_p;
242}
243
244inline
247{
248 return d_size;
249}
250
251inline
253{
254 return 0 == d_address_p;
255}
256
257} // close package namespace
258
259// FREE OPERATORS
260inline
261bool bdlma::operator==(const MemoryBlockDescriptor& lhs,
262 const MemoryBlockDescriptor& rhs)
263{
264 return lhs.address() == rhs.address() && lhs.size() == rhs.size();
265}
266
267inline
268bool bdlma::operator!=(const MemoryBlockDescriptor& lhs,
269 const MemoryBlockDescriptor& rhs)
270{
271 return !(lhs == rhs);
272}
273
274
275
276#endif
277
278// ----------------------------------------------------------------------------
279// Copyright 2015 Bloomberg Finance L.P.
280//
281// Licensed under the Apache License, Version 2.0 (the "License");
282// you may not use this file except in compliance with the License.
283// You may obtain a copy of the License at
284//
285// http://www.apache.org/licenses/LICENSE-2.0
286//
287// Unless required by applicable law or agreed to in writing, software
288// distributed under the License is distributed on an "AS IS" BASIS,
289// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
290// See the License for the specific language governing permissions and
291// limitations under the License.
292// ----------------------------- END-OF-FILE ----------------------------------
293
294/** @} */
295/** @} */
296/** @} */
Definition bdlma_memoryblockdescriptor.h:110
size_type size() const
Return the size of the memory block described by this object.
Definition bdlma_memoryblockdescriptor.h:246
MemoryBlockDescriptor()
Create a memory block descriptor having an address and size of 0.
Definition bdlma_memoryblockdescriptor.h:194
MemoryBlockDescriptor & operator=(const MemoryBlockDescriptor &rhs)
Definition bdlma_memoryblockdescriptor.h:219
bool isNull() const
Definition bdlma_memoryblockdescriptor.h:252
void * address() const
Definition bdlma_memoryblockdescriptor.h:239
void setAddressAndSize(void *address, size_type size)
Definition bdlma_memoryblockdescriptor.h:229
bsls::Types::size_type size_type
Definition bdlma_memoryblockdescriptor.h:114
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlma_alignedallocator.h:276
bool operator==(const MemoryBlockDescriptor &lhs, const MemoryBlockDescriptor &rhs)
bool operator!=(const MemoryBlockDescriptor &lhs, const MemoryBlockDescriptor &rhs)
std::size_t size_type
Definition bsls_types.h:124