BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdld_datumudt.h
Go to the documentation of this file.
1/// @file bdld_datumudt.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdld_datumudt.h -*-C++-*-
8#ifndef INCLUDED_BDLD_DATUMUDT
9#define INCLUDED_BDLD_DATUMUDT
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$ $CSID$")
13
14/// @defgroup bdld_datumudt bdld_datumudt
15/// @brief Provide a type to represent a user-defined type.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdld
19/// @{
20/// @addtogroup bdld_datumudt
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdld_datumudt-purpose"> Purpose</a>
25/// * <a href="#bdld_datumudt-classes"> Classes </a>
26/// * <a href="#bdld_datumudt-description"> Description </a>
27/// * <a href="#bdld_datumudt-attributes"> Attributes </a>
28/// * <a href="#bdld_datumudt-usage"> Usage </a>
29/// * <a href="#bdld_datumudt-example-1-basic-datumudt-usage"> Example 1: Basic DatumUdt usage </a>
30///
31/// # Purpose {#bdld_datumudt-purpose}
32/// Provide a type to represent a user-defined type.
33///
34/// # Classes {#bdld_datumudt-classes}
35///
36/// - bdld::DatumUdt: a *value-semantic* type representing user-defined type
37///
38/// @see bdld_datum, bdld_datumerror, bdld_datumbinaryref
39///
40/// # Description {#bdld_datumudt-description}
41/// This component provides a single, simply-constrained (in-core
42/// value-semantic) attribute class, `bdld::DatumUdt`, that is used to extend
43/// the set of possible types that can be held by `Datum` objects.
44///
45/// ## Attributes {#bdld_datumudt-attributes}
46///
47///
48///
49/// | Name | Type | Simple Constraints
50/// | ------- | -------- | ------------------
51/// | data_p | void * | none
52/// | type | int | [0 .. 65355]
53///
54/// * `data_p`: pointer to an external object/memory.
55/// * `type`: user type associated with the pointer.
56///
57/// This component provides a way to extend the set of data types that are
58/// natively supported by the `Datum` mechanism by allowing the client associate
59/// an opaque pointer to user data with the data type information. Note that the
60/// `bdld::DatumUdt` does not access the pointer nor it interpret the type
61/// information supplied at construction time. The client code is responsible
62/// for interpretation of the type information and casting the pointer back to
63/// the pointer to the actual type.
64///
65/// Accessors inside `Datum` class that need to return a user-defined object,
66/// return an instance of `DatumUdt`.
67///
68/// ## Usage {#bdld_datumudt-usage}
69///
70///
71/// This section illustrates intended use of this component.
72///
73/// ### Example 1: Basic DatumUdt usage {#bdld_datumudt-example-1-basic-datumudt-usage}
74///
75///
76/// Imagine we are using `Datum` within an expression evaluation subsystem.
77/// Within that subsystem, along with the set of types defined by
78/// `Datum::DataType` we also need to hold `Sequence` and `Choice` types within
79/// `Datum` values (which are not natively represented by `Datum`). First, we
80/// define the set of types used by our subsystem that are an extension to the
81/// types in `DatumType`:
82/// @code
83/// struct Sequence {
84/// struct Sequence *d_next_p;
85/// int d_value;
86/// };
87///
88/// enum ExtraExpressionTypes {
89/// e_SEQUENCE = 5,
90/// e_CHOICE = 6
91/// };
92/// @endcode
93/// Notice that the numeric values will be provided as the `type` attribute to
94/// `DatumUdt`.
95///
96/// Then we create a `Sequence` object, and create a `DatumUdt` to hold it (note
97/// that we've created the object on the stack for clarity):
98/// @code
99/// Sequence sequence;
100/// {
101/// const bdld::DatumUdt udt(&sequence, e_SEQUENCE);
102/// assert(e_SEQUENCE == udt.type());
103/// assert(&sequence == udt.data());
104/// }
105/// @endcode
106/// @}
107/** @} */
108/** @} */
109
110/** @addtogroup bdl
111 * @{
112 */
113/** @addtogroup bdld
114 * @{
115 */
116/** @addtogroup bdld_datumudt
117 * @{
118 */
119
120#include <bdlscm_version.h>
121
122#include <bdlb_printmethods.h>
123
126
127#include <bsls_assert.h>
128
129#include <bsl_string.h>
130#include <bsl_iosfwd.h>
131
132
133
134namespace bdld {
135 // ==============
136 // class DatumUdt
137 // ==============
138
139/// This class provides a type to represent a pointer to an object of some
140/// user-defined type and an integer value denoting what actual type is
141/// represented.
142///
143/// See @ref bdld_datumudt
144class DatumUdt {
145
146 private:
147 // DATA
148 void *d_data_p; // pointer to user-defined object (not owned)
149 int d_type; // type of user-defined object
150
151 public:
152 // TRAITS
155
156 // CREATORS
157
158 /// Create a `DatumUdt` object having the specified `data` and `type`.
159 /// Note that the pointer to the `data` is just copied bitwise. Also
160 /// note that the memory pointed by `data` is never accessed by a
161 /// `DatumUdt` object.
162 DatumUdt(void *data, int type);
163
164 DatumUdt(const DatumUdt& origin) = default;
165 ~DatumUdt() = default;
166
167 // MANIPULATORS
168
169 DatumUdt& operator=(const DatumUdt& rhs) = default;
170
171 // ACCESSORS
172
173 /// Return the pointer to the user-defined object.
174 void *data() const;
175
176 /// Return the type of the user-defined object.
177 int type() const;
178
179 /// Write the value of this object to the specified output `stream` in a
180 /// human-readable format, and return a reference to the modifyable
181 /// `stream`. Optionally specify an initial indentation `level`, whose
182 /// absolute value is incremented recursively for nested objects. If
183 /// `level` is specified, optionally specify `spacesPerLevel`, whose
184 /// absolute value indicates the number of spaces per indentation level
185 /// for this and all of its nested objects. If `level` is negative,
186 /// suppress indentation of the first line. If `spacesPerLevel` is
187 /// negative, format the entire output on one line, suppressing all but
188 /// the initial indentation (as governed by `level`). If `stream` is
189 /// not valid on entry, this operation has no effect. Note that this
190 /// human-readable format is not fully specified, and can change without
191 /// notice.
192 bsl::ostream& print(bsl::ostream& stream,
193 int level = 0,
194 int spacesPerLevel = 4) const;
195};
196
197// FREE OPERATORS
198
199/// Return `true` if the specified `lhs` and `rhs` have the same value, and
200/// `false` otherwise. Two `DatumUdt` objects have the same value if they
201/// have the same data and type values.
202bool operator==(const DatumUdt& lhs, const DatumUdt& rhs);
203
204/// Return `true` if the specified `lhs` and `rhs` have different values,
205/// and `false` otherwise. Two `DatumUdt` objects have different values if
206/// they have different data or type values.
207bool operator!=(const DatumUdt& lhs, const DatumUdt& rhs);
208
209/// Return `true` if value of the specified `lhs` is less than value of the
210/// specified `rhs` and `false` otherwise. Value of `lhs` is less than
211/// value of `rhs`, if data value of `lhs` is less than data value of `rhs`,
212/// or they have the same data value and type value of `lhs` is less than
213/// type value of `rhs`.
214bool operator<(const DatumUdt& lhs, const DatumUdt& rhs);
215
216/// Return `true` if value of the specified `lhs` is less than or equal to
217/// value of the specified `rhs` and `false` otherwise. Value of `lhs` is
218/// less than or equal to value of `rhs`, if data value of `lhs` is less
219/// than or equal to data value of `rhs`, or they have the same data value
220/// and type value of `lhs` is less than or equal to type value of `rhs`.
221bool operator<=(const DatumUdt& lhs, const DatumUdt& rhs);
222
223/// Return `true` if value of the specified `lhs` is greater than value of
224/// the specified `rhs` and `false` otherwise. Value of `lhs` is greater
225/// than value of `rhs`, if data value of `lhs` is greater than data value
226/// of `rhs`, or they have the same data value and type value of `lhs` is
227/// greater than type value of `rhs`.
228bool operator>(const DatumUdt& lhs, const DatumUdt& rhs);
229
230/// Return `true` if value of the specified `lhs` is greater than or equal
231/// to value of the specified `rhs` and `false` otherwise. Value of `lhs`
232/// is greater than or equal to value of `rhs`, if data value of `lhs` is
233/// greater than or equal to data value of `rhs`, or they have the same data
234/// value and type value of `lhs` is greater than or equal to type value of
235/// `rhs`.
236bool operator>=(const DatumUdt& lhs, const DatumUdt& rhs);
237
238/// Write the value of the specified `rhs` object to the specified output
239/// `stream` in a single-line format, and return a reference to the
240/// modifyable `stream`. If `stream` is not valid on entry, this operation
241/// has no effect. Note that this human-readable format is not fully
242/// specified, can change without notice, and is logically equivalent to:
243/// @code
244/// print(stream, 0, -1);
245/// @endcode
246/// Single line output format for the `DatumUdt` object is shown below:
247/// @code
248/// user-defined(<address>,type)
249/// @endcode
250/// (where `address` is hex value of the pointer to the user-define object
251/// and `type` is the type of the user-defined object)
252bsl::ostream& operator<<(bsl::ostream& stream, const DatumUdt& rhs);
253
254// ============================================================================
255// INLINE DEFINITIONS
256// ============================================================================
257
258 // --------------
259 // class DatumUdt
260 // --------------
261
262// CREATORS
263inline
264DatumUdt::DatumUdt(void *data, int type)
265: d_data_p(data)
266, d_type(type)
267{
268}
269
270// ACCESSORS
271inline
272void *DatumUdt::data() const
273{
274 return d_data_p;
275}
276
277inline
278int DatumUdt::type() const
279{
280 return d_type;
281}
282
283} // close package namespace
284
285// FREE OPERATORS
286inline
287bool bdld::operator==(const DatumUdt& lhs, const DatumUdt& rhs)
288{
289 return (lhs.data() == rhs.data() &&
290 lhs.type() == rhs.type());
291}
292
293inline
294bool bdld::operator!=(const DatumUdt& lhs, const DatumUdt& rhs)
295{
296 return (lhs.data() != rhs.data() ||
297 lhs.type() != rhs.type());
298}
299
300inline
301bool bdld::operator<(const DatumUdt& lhs, const DatumUdt& rhs)
302{
303 return (lhs.data() < rhs.data() ||
304 (lhs.data() == rhs.data() && lhs.type() < rhs.type()));
305}
306
307inline
308bool bdld::operator<=(const DatumUdt& lhs, const DatumUdt& rhs)
309{
310 return (lhs == rhs || lhs < rhs);
311}
312
313inline
314bool bdld::operator>(const DatumUdt& lhs, const DatumUdt& rhs)
315{
316 return !(lhs <= rhs);
317}
318
319inline
320bool bdld::operator>=(const DatumUdt& lhs, const DatumUdt& rhs)
321{
322 return !(lhs < rhs);
323}
324
325inline
326bsl::ostream& bdld::operator<<(bsl::ostream& stream, const DatumUdt& rhs)
327{
328 return rhs.print(stream, 0 , -1);
329}
330
331
332
333#endif
334
335// ----------------------------------------------------------------------------
336// Copyright 2015 Bloomberg Finance L.P.
337//
338// Licensed under the Apache License, Version 2.0 (the "License");
339// you may not use this file except in compliance with the License.
340// You may obtain a copy of the License at
341//
342// http://www.apache.org/licenses/LICENSE-2.0
343//
344// Unless required by applicable law or agreed to in writing, software
345// distributed under the License is distributed on an "AS IS" BASIS,
346// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
347// See the License for the specific language governing permissions and
348// limitations under the License.
349// ----------------------------- END-OF-FILE ----------------------------------
350
351
352/** @} */
353/** @} */
354/** @} */
Definition bdld_datumudt.h:144
DatumUdt(const DatumUdt &origin)=default
~DatumUdt()=default
DatumUdt(void *data, int type)
Definition bdld_datumudt.h:264
BSLMF_NESTED_TRAIT_DECLARATION(DatumUdt, bdlb::HasPrintMethod)
DatumUdt & operator=(const DatumUdt &rhs)=default
void * data() const
Return the pointer to the user-defined object.
Definition bdld_datumudt.h:272
int type() const
Return the type of the user-defined object.
Definition bdld_datumudt.h:278
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
BSLMF_NESTED_TRAIT_DECLARATION(DatumUdt, bsl::is_trivially_copyable)
#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