BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlar_symbolicconverter.h
Go to the documentation of this file.
1/// @file bdlar_symbolicconverter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlar_symbolicconverter.h -*-C++-*-
8#ifndef INCLUDED_BDLAR_SYMBOLICCONVERTER
9#define INCLUDED_BDLAR_SYMBOLICCONVERTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlar_symbolicconverter bdlar_symbolicconverter
15/// @brief Provide a utility for convert types with matching member symbols.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlar
19/// @{
20/// @addtogroup bdlar_symbolicconverter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlar_symbolicconverter-purpose"> Purpose</a>
25/// * <a href="#bdlar_symbolicconverter-classes"> Classes </a>
26/// * <a href="#bdlar_symbolicconverter-description"> Description </a>
27/// * <a href="#bdlar_symbolicconverter-usage"> Usage </a>
28/// * <a href="#bdlar_symbolicconverter-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdlar_symbolicconverter-purpose}
31/// Provide a utility for convert types with matching member symbols.
32///
33/// # Classes {#bdlar_symbolicconverter-classes}
34///
35/// - bdlar::SymbolicConverter: symbolic converter utility
36///
37/// @see bdlat_symbolicconverter
38///
39/// # Description {#bdlar_symbolicconverter-description}
40/// The `bdlar::SymbolicConverter` utility provided by this
41/// component defines a single parameterized function `convert`. The `convert`
42/// function takes two arguments: a destination and a source object. The
43/// destination and source objects may be of different types. Details about the
44/// used conversion rules can be found in the @ref bdlat_symbolicconverter
45/// component description.
46///
47/// Both `bdlat_SymbolicConverter` and `bdlar::SymbolicConverter` classes
48/// implement absolutely equivalent functionality and produce the same results.
49/// The difference in not **what** they do, but **how** they do it. While
50/// `bdlat_SymbolicConverter` uses template metaprogramming and generates a lot
51/// of template instances, `bdlar::SymbolicConverter` doesn't generate any code
52/// at compile time - all the required code is pre-compiled. It can reduce code
53/// bloat, compile time, and the load on the compiler in general, which can be
54/// benificial for large data schemas.
55///
56/// ## Usage {#bdlar_symbolicconverter-usage}
57///
58///
59/// This section illustrates intended use of this component.
60///
61/// ### Example 1: Basic Usage {#bdlar_symbolicconverter-example-1-basic-usage}
62///
63///
64/// This component can be used with types supported by the `bdlat` framework.
65/// In particular, types generated by the `bas_codegen.pl` tool can be used.
66/// For example, suppose we have the following XML schema inside a file called
67/// `xsdfile.xsd`:
68/// @code
69/// <?xml version='1.0' encoding='UTF-8'?>
70/// <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
71/// xmlns:bdem='http://bloomberg.com/schemas/bdem'
72/// elementFormDefault='unqualified'>
73///
74/// <xs:complexType name='Employee'>
75/// <xs:sequence>
76/// <xs:element name='Name' type='string'/>
77/// <xs:element name='Dept' type='string'/>
78/// <xs:element name='Age' type='int'/>
79/// <xs:element name='Salary' type='float'/>
80/// </xs:sequence>
81/// </xs:complexType>
82///
83/// <xs:complexType name='Trainee'>
84/// <xs:sequence>
85/// <xs:element name='Name' type='string'/>
86/// <xs:element name='Dept' type='string'/>
87/// <xs:element name='Age' type='int'/>
88/// </xs:sequence>
89/// </xs:complexType>
90///
91/// </xs:schema>
92/// @endcode
93/// Using the `bas_codegen.pl` tool, we can generate C++ classes for this
94/// schema:
95/// @code
96/// $ bas_codegen.pl -g h -g cpp -p test xsdfile.xsd
97/// @endcode
98/// This tool will generate the header and implementation files for the
99/// @ref test_employee and @ref test_trainee components in the current directory.
100///
101/// Now suppose we want to create a `hireTrainee` function, that converts a
102/// trainee to an employee. Such a function could be written as follows:
103/// @code
104/// #include <test_employee.h>
105/// #include <test_trainee.h>
106///
107/// #include <bdlar_symbolicconverter.h>
108///
109/// using namespace BloombergLP;
110///
111/// int hireTrainee(test::Employee *result,
112/// const test::Trainee& trainee,
113/// float salary)
114/// {
115/// int retCode = bdlar::SymbolicConverter::convert(result, trainee);
116///
117/// result->salary() = salary;
118///
119/// return retCode;
120/// }
121/// @endcode
122/// The `hireTrainee` function can be used as follows:
123/// @code
124/// void usageExample()
125/// {
126/// test::Trainee trainee;
127///
128/// trainee.name() = "Bob";
129/// trainee.dept() = "RnD";
130/// trainee.age() = 24;
131///
132/// test::Employee employee;
133///
134/// int result = hireTrainee(&employee, trainee, 20000.00f);
135///
136/// assert(0 == result);
137/// assert("Bob" == employee.name());
138/// assert("RnD" == employee.dept());
139/// assert(24 == employee.age());
140/// assert(20000.00f == employee.salary());
141/// }
142/// @endcode
143/// @}
144/** @} */
145/** @} */
146
147/** @addtogroup bdl
148 * @{
149 */
150/** @addtogroup bdlar
151 * @{
152 */
153/** @addtogroup bdlar_symbolicconverter
154 * @{
155 */
156
157#include <bslscm_version.h>
158
159#include <bdlar_refutil.h>
160
161#include <bsl_ostream.h>
162
163
164namespace bdlar {
165
166 // ========================
167 // struct SymbolicConverter
168 // ========================
169
170/// This utility contains a single `convert` function that converts a value
171/// from one type to another compatible type.
172///
173/// See @ref bdlar_symbolicconverter
175 // CLASS METHODS
176
177 /// Convert the value of the specified `rhs` object to the specified
178 /// (modifiable) `lhs` object. Optionally specify an `errorStream` to
179 /// print error messages. Return 0 on success and a non-zero value
180 /// otherwise. The supported conversions are described in the
181 /// @ref bdlat_symbolicconverter component-level documentation.
182 template <class LHS_TYPE, class RHS_TYPE>
183 static int convert(LHS_TYPE *lhs,
184 const RHS_TYPE& rhs,
185 bsl::ostream& errorStream);
186 template <class LHS_TYPE, class RHS_TYPE>
187 static int convert(LHS_TYPE *lhs, const RHS_TYPE& rhs);
188 static int convert(AnyRef lhs, AnyConstRef rhs, bsl::ostream& errorStream);
189 static int convert(AnyRef lhs, AnyConstRef rhs);
190};
191
192// ============================================================================
193// INLINE DEFINITIONS
194// ============================================================================
195
196 // ------------------------
197 // struct SymbolicConverter
198 // ------------------------
199
200template <class LHS_TYPE, class RHS_TYPE>
201inline
203 const RHS_TYPE& rhs,
204 bsl::ostream& errorStream)
205{
208 errorStream);
209}
210
211template <class LHS_TYPE, class RHS_TYPE>
212inline
213int SymbolicConverter::convert(LHS_TYPE *lhs, const RHS_TYPE& rhs)
214{
215 bsl::ostream nullStream(0);
216 return convert(lhs, rhs, nullStream);
217}
218
219inline
221{
222 bsl::ostream nullStream(0);
223 return convert(lhs, rhs, nullStream);
224}
225
226} // close package namespace
227
228
229#endif
230
231// ----------------------------------------------------------------------------
232// Copyright 2025 Bloomberg Finance L.P.
233//
234// Licensed under the Apache License, Version 2.0 (the "License");
235// you may not use this file except in compliance with the License.
236// You may obtain a copy of the License at
237//
238// http://www.apache.org/licenses/LICENSE-2.0
239//
240// Unless required by applicable law or agreed to in writing, software
241// distributed under the License is distributed on an "AS IS" BASIS,
242// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
243// See the License for the specific language governing permissions and
244// limitations under the License.
245// ----------------------------- END-OF-FILE ----------------------------------
246
247/** @} */
248/** @} */
249/** @} */
Definition bdlar_anyref.h:247
Definition bdlar_anyref.h:85
static bsl::enable_if<!IsDynamic< t_TYPE >::value, AnyConstRef >::type makeAnyConstRef(const t_TYPE &object)
Make AnyConstRef to the specified object.
Definition bdlar_refutil.h:194
static bsl::enable_if<!IsDynamic< t_TYPE >::value, AnyRef >::type makeAnyRef(t_TYPE &object)
Make AnyRef to the specified object.
Definition bdlar_refutil.h:209
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlar_accessorref.h:59
Definition bdlar_symbolicconverter.h:174
static int convert(LHS_TYPE *lhs, const RHS_TYPE &rhs, bsl::ostream &errorStream)
Definition bdlar_symbolicconverter.h:202
static int convert(AnyRef lhs, AnyConstRef rhs, bsl::ostream &errorStream)