source: rrlib_rtti_conversion/tConversionOption.h @ 2:e67df930e58f

Last change on this file since 2:e67df930e58f was 0:d316daefc472, checked in by Max Reichardt <mreichardt@…>, 2 years ago

Initial commit

File size: 9.5 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/rtti_conversion/tCompiledConversionOperation.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2016-07-15
27 *
28 * \brief   Contains tConversionOption
29 *
30 * \b tConversionOption
31 *
32 * One concrete option of conversion provided by a registered conversion operation (that may provide multiple).
33 * Used for selection of possibly a sequence of conversions and for compiling them.
34 *
35 */
36//----------------------------------------------------------------------
37#ifndef __rrlib__rtti_conversion__tConversionOption_h__
38#define __rrlib__rtti_conversion__tConversionOption_h__
39
40//----------------------------------------------------------------------
41// External includes (system with <>, local with "")
42//----------------------------------------------------------------------
43
44//----------------------------------------------------------------------
45// Internal includes with ""
46//----------------------------------------------------------------------
47
48//----------------------------------------------------------------------
49// Namespace declaration
50//----------------------------------------------------------------------
51namespace rrlib
52{
53namespace rtti
54{
55namespace conversion
56{
57
58//----------------------------------------------------------------------
59// Forward declarations / typedefs / enums
60//----------------------------------------------------------------------
61
62struct tCurrentConversionOperation;
63
64/*!
65 * Type of conversion option
66 */
67enum class tConversionOptionType
68{
69  /*!
70   * Empty/invalid conversion option
71   */
72  NONE,
73
74  /*!
75   * Conversion is performed with a standard tConversionFunction with no reference on the source object.
76   *
77   * Computational overhead:
78   * - Single/final operation: a function pointer call + copying to the destination object.
79   * - First operation in sequence: a function pointer call + creating and copying to intermediate object on the stack (expensive for destination types which allocate memory internally)
80   */
81  STANDARD_CONVERSION_FUNCTION,
82
83  // The options below reference the source object -  so it must either be const and available as long as destination object is used - or the data (finally) needs to be copied
84
85  /*!
86   * Destination data is available as reference to source object - with a fixed offset.
87   *
88   * Computational overhead (preferred option):
89   * - Single/final operation: Simple memcpy for destination for types that support bitwise copy (may even be optimized away).
90   *                           Otherwise, this will result in a virtual function call for copying to the destination object.
91   * - First operation in sequence: optimized away
92   */
93  CONST_OFFSET_REFERENCE_TO_SOURCE_OBJECT,
94
95  /*!
96   * Destination data is available as reference to source object - with a variable offset (e.g. a std::vector element).
97   * Offset of destination type in source objects is determined via tGetDestinationReferenceFunction.
98   *
99   * Computational overhead (good option for expensively copied objects):
100   * - Single/final operation: a standard conversion function (overhead see above)
101   * - First operation in sequence: a function pointer call (without copying)
102   */
103  VARIABLE_OFFSET_REFERENCE_TO_SOURCE_OBJECT,
104
105  /*!
106   * Conversion is performed with a standard tConversionFunction, however, destination object references/wraps the source object.
107   *
108   * Computational overhead (in some cases good option for expensively copied objects in a sequence):
109   * - Single operation: a standard conversion function (overhead see above)
110   * - Chaining: a function pointer call (creating wrapper; to make sense this should require less allocation/copying than STANDARD_CONVERSION_FUNCTION)
111   */
112  RESULT_REFERENCES_SOURCE_OBJECT
113};
114
115//----------------------------------------------------------------------
116// Class declaration
117//----------------------------------------------------------------------
118//! Conversion option
119/*!
120 * One concrete option of conversion provided by a registered conversion operation (that may provide multiple).
121 * Used for selection of possibly a sequence of conversions and for compiling them.
122 */
123struct tConversionOption
124{
125  /*!
126   * Function pointer for conversion operation (used by any tConversionOptionType except of CONST_OFFSET_REFERENCE_TO_SOURCE_OBJECT)
127   *
128   * \param source_object Source object to convert
129   * \param destination_object Destination object to copy result to.
130   * \param operation Provides access to current conversion operation (e.g. flags and parameters). Any operation that does not write to destination_object must call Continue() on this object.
131   */
132  typedef void (*tConversionFunction)(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation);
133
134  /*!
135   * Function pointer for obtaining variable offset to source object (VARIABLE_OFFSET_REFERENCE_TO_SOURCE_OBJECT)
136   *
137   * \param source_object Source object
138   * \param operation Provides access to current conversion operation (e.g. flags and parameters). (Continue() must not be called)
139   * \return Destination object (references source object)
140   */
141  typedef tTypedConstPointer(*tGetDestinationReferenceFunction)(const tTypedConstPointer& source_object, const tCurrentConversionOperation& operation);
142
143  /*! Source and destination types */
144  rrlib::rtti::tType source_type, destination_type;
145
146  /*! Type of conversion option - determines how variables below are filled */
147  enum tConversionOptionType type;
148
149  // Conversion function if first operation in sequence
150  union
151  {
152    /*! Contains offset of destination type data in source type data when type is CONST_OFFSET_REFERENCE_TO_SOURCE_OBJECT */
153    size_t const_offset_reference_to_source_object;
154
155    /*! Function pointer for first conversion operation when type is not CONST_OFFSET_REFERENCE_TO_SOURCE_OBJECT */
156    tConversionFunction first_conversion_function;
157  };
158
159  // Additional operation
160  union
161  {
162    /*! Contains final conversion function when type is STANDARD_CONVERSION_FUNCTION and RESULT_REFERENCES_SOURCE_OBJECT */
163    tConversionFunction final_conversion_function;
164
165    /*! Function pointer for obtaining variable offset to source object when type is VARIABLE_OFFSET_REFERENCE_TO_SOURCE_OBJECT */
166    tGetDestinationReferenceFunction destination_reference_function;
167  };
168
169  /*!
170   * Constructor for STANDARD_CONVERSION_FUNCTION and RESULT_REFERENCES_SOURCE_OBJECT
171   */
172  constexpr tConversionOption(const rrlib::rtti::tType& source_type, const rrlib::rtti::tType& destination_type, bool destination_references_source, tConversionFunction first_conversion_function, tConversionFunction final_conversion_function) :
173    source_type(source_type),
174    destination_type(destination_type),
175    type(destination_references_source ? tConversionOptionType::RESULT_REFERENCES_SOURCE_OBJECT : tConversionOptionType::STANDARD_CONVERSION_FUNCTION),
176    first_conversion_function(first_conversion_function),
177    final_conversion_function(final_conversion_function)
178  {}
179
180  /*!
181   * Constructor for CONST_OFFSET_REFERENCE_TO_SOURCE_OBJECT
182   */
183  constexpr tConversionOption(const rrlib::rtti::tType& source_type, const rrlib::rtti::tType& destination_type, size_t const_offset_reference_to_source_object) :
184    source_type(source_type),
185    destination_type(destination_type),
186    type(tConversionOptionType::STANDARD_CONVERSION_FUNCTION),
187    const_offset_reference_to_source_object(const_offset_reference_to_source_object),
188    final_conversion_function(nullptr)
189  {}
190
191  /*!
192   * Constructor for VARIABLE_OFFSET_REFERENCE_TO_SOURCE_OBJECT
193   */
194  constexpr tConversionOption(const rrlib::rtti::tType& source_type, const rrlib::rtti::tType& destination_type, tConversionFunction first_conversion_function, tGetDestinationReferenceFunction destination_reference_function) :
195    source_type(source_type),
196    destination_type(destination_type),
197    type(tConversionOptionType::STANDARD_CONVERSION_FUNCTION),
198    first_conversion_function(first_conversion_function),
199    destination_reference_function(destination_reference_function)
200  {}
201
202  /*!
203   * Constructor for NONE
204   */
205  constexpr tConversionOption() :
206    source_type(),
207    destination_type(),
208    type(tConversionOptionType::NONE),
209    const_offset_reference_to_source_object(0),
210    final_conversion_function(nullptr)
211  {}
212};
213
214/*!
215 * Conversion option for static_cast (includes information whether cast is implicit)
216 */
217struct tConversionOptionStaticCast
218{
219  /*! Conversion option with main data on cast operation */
220  tConversionOption conversion_option;
221
222  /*! True if this is a implicit cast */
223  bool implicit;
224};
225
226//----------------------------------------------------------------------
227// End of namespace declaration
228//----------------------------------------------------------------------
229}
230}
231}
232
233
234#endif
Note: See TracBrowser for help on using the repository browser.