|
| 1 | +// Copyright (c) Darrell Wright |
| 2 | +// |
| 3 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | +// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | +// |
| 6 | +// Official repository: https://github.com/beached/header_libraries |
| 7 | +// |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <daw/daw_bit_cast.h> |
| 12 | +#include <daw/daw_move.h> |
| 13 | + |
| 14 | +#include <functional> |
| 15 | + |
| 16 | + |
| 17 | +#if defined( DAW_CX_BIT_CAST ) |
| 18 | +#define DAW_CX_IF_BIT_CAST constexpr |
| 19 | +#else |
| 20 | +#define DAW_CX_IF_BIT_CAST |
| 21 | +#endif |
| 22 | + |
| 23 | +namespace daw { |
| 24 | + /** |
| 25 | + * @brief Invokes a visitor on a reinterpretation of the provided value, allowing |
| 26 | + * modification of the value via the provided visitor, and optionally |
| 27 | + * returning a value from the visitor's result. |
| 28 | + * |
| 29 | + * @tparam VisitedT The type to reinterpreted `value` as when invoking the visitor. |
| 30 | + * @tparam OrigT The original type of the `value` being modified or accessed. |
| 31 | + * @tparam Visitor The invocable type that will be applied to the value. |
| 32 | + * |
| 33 | + * @param value A reference to the value being visited and potentially modified. |
| 34 | + * @param visitor A invocable that processes the value once reinterpreted as |
| 35 | + * type `VisitedT`. |
| 36 | + * |
| 37 | + * @return The result of invoking the visitor if the visitor returns a non-void |
| 38 | + * value; otherwise, nothing is returned. |
| 39 | + * |
| 40 | + * @details The function performs a bit cast to reinterpret `value` as type |
| 41 | + * `VisitedT`. It then applies the visitor to this reinterpreted value. |
| 42 | + * After the visitor processes the value, the function updates the |
| 43 | + * original value with the potentially modified value. If the visitor |
| 44 | + * returns a result, it is also returned by this function. |
| 45 | + */ |
| 46 | + template<typename VisitedT, typename OrigT, typename Visitor> |
| 47 | +DAW_CX_IF_BIT_CAST auto visit_as( OrigT &value, Visitor &&visitor ) { |
| 48 | + auto tmp = daw::bit_cast<VisitedT>( value ); |
| 49 | + if constexpr( std::is_void_v<std::invoke_result_t<Visitor, VisitedT &>> ) { |
| 50 | + std::invoke( DAW_FWD( visitor ), tmp ); |
| 51 | + value = daw::bit_cast<OrigT>( tmp ); |
| 52 | + } else { |
| 53 | + auto result = std::invoke( DAW_FWD( visitor ), tmp ); |
| 54 | + value = daw::bit_cast<OrigT>( tmp ); |
| 55 | + return result; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + /** |
| 61 | + * @brief Invokes a visitor on a reinterpretation of the provided value. |
| 62 | + * |
| 63 | + * @tparam VisitedT The type to reinterpret `value` as when invoking the visitor. |
| 64 | + * @tparam OrigT The original type of the `value`. |
| 65 | + * @tparam Visitor The callable type that will be applied to the reinterpreted value. |
| 66 | + * |
| 67 | + * @param value A constant reference to the value being reinterpreted and visited. |
| 68 | + * @param visitor A callable object that operates on the reinterpreted value. |
| 69 | + * |
| 70 | + * @return The result of invoking the visitor with the reinterpreted value. |
| 71 | + * |
| 72 | + * @details This function performs a bit-cast of `value` to the type `VisitedT` and |
| 73 | + * then applies the visitor to the reinterpreted value. The result of the |
| 74 | + * visitor is returned if it produces a value. |
| 75 | + */ |
| 76 | + template<typename VisitedT, typename OrigT, typename Visitor> |
| 77 | + DAW_CX_IF_BIT_CAST auto visit_as( OrigT const &value, Visitor &&visitor ) { |
| 78 | + auto const tmp = daw::bit_cast<VisitedT>( value ); |
| 79 | + return std::invoke( DAW_FWD( visitor ), tmp ); |
| 80 | + } |
| 81 | +} // namespace daw |
0 commit comments