BDE 4.39.x 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 ///
160 /// \note Note that the pointer to the `data` is just copied bitwise. Also
161 /// note that the memory pointed by `data` is never accessed by a
162 /// `DatumUdt` object.
163 DatumUdt(void *data, int type);
164
165 DatumUdt(const DatumUdt& origin) = default;
166 ~DatumUdt() = default;
167
168 // MANIPULATORS
169
170 DatumUdt& operator=(const DatumUdt& rhs) = default;
171
172 // ACCESSORS
173
174 /// Return the pointer to the user-defined object.
175 void *data() const;
176
177 /// Return the type of the user-defined object.
178 int type() const;
179
180 /// Write the value of this object to the specified output `stream` in a
181 /// human-readable format, and return a reference to the modifyable
182 /// `stream`. Optionally specify an initial indentation `level`, whose
183 /// absolute value is incremented recursively for nested objects. If
184 /// `level` is specified, optionally specify `spacesPerLevel`, whose
185 /// absolute value indicates the number of spaces per indentation level
186 /// for this and all of its nested objects. If `level` is negative,
187 /// suppress indentation of the first line. If `spacesPerLevel` is
188 /// negative, format the entire output on one line, suppressing all but
189 /// the initial indentation (as governed by `level`). If `stream` is not valid on entry, this operation has no effect.
190 ///
191 /// \note Note that this
192 /// human-readable format is not fully specified, and can change without
193 /// notice.
194 bsl::ostream& print(bsl::ostream& stream,
195 int level = 0,
196 int spacesPerLevel = 4) const;
197};
198
199// FREE OPERATORS
200
201/// Return `true` if the specified `lhs` and `rhs` have the same value, and
202/// `false` otherwise. Two `DatumUdt` objects have the same value if they
203/// have the same data and type values.
204bool operator==(const DatumUdt& lhs, const DatumUdt& rhs);
205
206/// Return `true` if the specified `lhs` and `rhs` have different values,
207/// and `false` otherwise. Two `DatumUdt` objects have different values if
208/// they have different data or type values.
209bool operator!=(const DatumUdt& lhs, const DatumUdt& rhs);
210
211/// Return `true` if value of the specified `lhs` is less than value of the
212/// specified `rhs` and `false` otherwise. Value of `lhs` is less than
213/// value of `rhs`, if data value of `lhs` is less than data value of `rhs`,
214/// or they have the same data value and type value of `lhs` is less than
215/// type value of `rhs`.
216bool operator<(const DatumUdt& lhs, const DatumUdt& rhs);
217
218/// Return `true` if value of the specified `lhs` is less than or equal to
219/// value of the specified `rhs` and `false` otherwise. Value of `lhs` is
220/// less than or equal to value of `rhs`, if data value of `lhs` is less
221/// than or equal to data value of `rhs`, or they have the same data value
222/// and type value of `lhs` is less than or equal to type value of `rhs`.
223bool operator<=(const DatumUdt& lhs, const DatumUdt& rhs);
224
225/// Return `true` if value of the specified `lhs` is greater than value of
226/// the specified `rhs` and `false` otherwise. Value of `lhs` is greater
227/// than value of `rhs`, if data value of `lhs` is greater than data value
228/// of `rhs`, or they have the same data value and type value of `lhs` is
229/// greater than type value of `rhs`.
230bool operator>(const DatumUdt& lhs, const DatumUdt& rhs);
231
232/// Return `true` if value of the specified `lhs` is greater than or equal
233/// to value of the specified `rhs` and `false` otherwise. Value of `lhs`
234/// is greater than or equal to value of `rhs`, if data value of `lhs` is
235/// greater than or equal to data value of `rhs`, or they have the same data
236/// value and type value of `lhs` is greater than or equal to type value of
237/// `rhs`.
238bool operator>=(const DatumUdt& lhs, const DatumUdt& rhs);
239
240/// Write the value of the specified `rhs` object to the specified output
241/// `stream` in a single-line format, and return a reference to the
242/// modifyable `stream`. If `stream` is not valid on entry, this operation has no effect.
243///
244/// \note Note that this human-readable format is not fully
245/// specified, can change without notice, and is logically equivalent to:
246/// @code
247/// print(stream, 0, -1);
248/// @endcode
249/// Single line output format for the `DatumUdt` object is shown below:
250/// @code
251/// user-defined(<address>,type)
252/// @endcode
253/// (where `address` is hex value of the pointer to the user-define object
254/// and `type` is the type of the user-defined object)
255bsl::ostream& operator<<(bsl::ostream& stream, const DatumUdt& rhs);
256
257// ============================================================================
258// INLINE DEFINITIONS
259// ============================================================================
260
261 // --------------
262 // class DatumUdt
263 // --------------
264
265// CREATORS
266inline
267DatumUdt::DatumUdt(void *data, int type)
268: d_data_p(data)
269, d_type(type)
270{
271}
272
273// ACCESSORS
274inline
275void *DatumUdt::data() const
276{
277 return d_data_p;
278}
279
280inline
281int DatumUdt::type() const
282{
283 return d_type;
284}
285
286} // close package namespace
287
288// FREE OPERATORS
289inline
290bool bdld::operator==(const DatumUdt& lhs, const DatumUdt& rhs)
291{
292 return (lhs.data() == rhs.data() &&
293 lhs.type() == rhs.type());
294}
295
296inline
297bool bdld::operator!=(const DatumUdt& lhs, const DatumUdt& rhs)
298{
299 return (lhs.data() != rhs.data() ||
300 lhs.type() != rhs.type());
301}
302
303inline
304bool bdld::operator<(const DatumUdt& lhs, const DatumUdt& rhs)
305{
306 return (lhs.data() < rhs.data() ||
307 (lhs.data() == rhs.data() && lhs.type() < rhs.type()));
308}
309
310inline
311bool bdld::operator<=(const DatumUdt& lhs, const DatumUdt& rhs)
312{
313 return (lhs == rhs || lhs < rhs);
314}
315
316inline
317bool bdld::operator>(const DatumUdt& lhs, const DatumUdt& rhs)
318{
319 return !(lhs <= rhs);
320}
321
322inline
323bool bdld::operator>=(const DatumUdt& lhs, const DatumUdt& rhs)
324{
325 return !(lhs < rhs);
326}
327
328inline
329bsl::ostream& bdld::operator<<(bsl::ostream& stream, const DatumUdt& rhs)
330{
331 return rhs.print(stream, 0 , -1);
332}
333
334
335
336#endif
337
338// ----------------------------------------------------------------------------
339// Copyright 2015 Bloomberg Finance L.P.
340//
341// Licensed under the Apache License, Version 2.0 (the "License");
342// you may not use this file except in compliance with the License.
343// You may obtain a copy of the License at
344//
345// http://www.apache.org/licenses/LICENSE-2.0
346//
347// Unless required by applicable law or agreed to in writing, software
348// distributed under the License is distributed on an "AS IS" BASIS,
349// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
350// See the License for the specific language governing permissions and
351// limitations under the License.
352// ----------------------------- END-OF-FILE ----------------------------------
353
354
355/** @} */
356/** @} */
357/** @} */
Definition bdld_datumudt.h:144
DatumUdt(const DatumUdt &origin)=default
~DatumUdt()=default
DatumUdt(void *data, int type)
Definition bdld_datumudt.h:267
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:275
int type() const
Return the type of the user-defined object.
Definition bdld_datumudt.h:281
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)
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