Reference proxy for decimal values in array layouts.
Reference proxy for decimal values in array layouts. This class provides a reference-like interface for accessing and modifying decimal values stored in array layouts. It acts as a proxy that forwards operations to the underlying layout while maintaining value semantics similar to built-in references.
decimal_array<int64_t> arr = ...;
auto ref = arr[0];
ref = decimal<int64_t>(12345, 2);
double val = static_cast<double>(ref);
int64_t storage = ref.storage();
#pragma once
#if defined(__cpp_lib_format)
# include <format>
# include <ostream>
#endif
{
template <class L>
class decimal_reference
{
public:
using reference =
typename L::inner_reference;
using size_type = typename L::size_type;
using difference_type = std::ptrdiff_t;
constexpr explicit operator float() const
constexpr explicit operator double() const
constexpr explicit operator long double() const
[[nodiscard]] explicit operator std::string() const
[[nodiscard]]
constexpr value_type::integer_type
storage()
const;
[[nodiscard]]
constexpr int scale()
const;
private:
L* p_layout = nullptr;
};
template <typename L>
: p_layout(layout)
, m_index(index)
{
}
template <typename L>
{
p_layout->assign(std::forward<value_type>(rhs), m_index);
return *this;
}
template <typename L>
{
p_layout->assign(rhs, m_index);
return *this;
}
template <typename L>
{
this->operator=(rhs.value());
return *this;
}
template <typename L>
{
this->operator=(rhs.value());
return *this;
}
template <typename L>
{
return value() == rhs;
}
template <typename L>
{
return value() <=> rhs;
}
template <typename L>
{
return static_cast<const L*>(p_layout)->value(m_index);
}
template <typename L>
{
return value().storage();
}
template <typename L>
{
return value().scale();
}
template <typename L>
constexpr decimal_reference<L>::operator float() const
{
return static_cast<float>(value());
}
template <typename L>
constexpr decimal_reference<L>::operator double() const
{
return static_cast<double>(value());
}
template <typename L>
constexpr decimal_reference<L>::operator long double() const
{
return static_cast<long double>(value());
}
}
#if defined(__cpp_lib_format)
template <typename L>
struct std::formatter<
sparrow::decimal_reference<L>>
{
constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}
auto format(const sparrow::decimal_reference<L>& ref, std::format_context& ctx) const
{
const auto& value = ref.
value();
return std::format_to(ctx.out(), "{}", value);
}
};
template <typename L>
{
os << std::format("{}", value);
return os;
}
#endif
constexpr int scale() const
Gets the scale of the decimal.
constexpr bool operator==(const value_type &rhs) const
Equality comparison with decimal value.
constexpr decimal_reference(L *layout, size_type index) noexcept
Constructs a decimal reference for the given layout and index.
constexpr value_type::integer_type storage() const
Gets the raw storage value of the decimal.
constexpr self_type & operator=(self_type &&rhs) noexcept
Move assignment from another decimal reference.
typename L::inner_value_type value_type
decimal_reference< L > self_type
constexpr const_reference value() const
Gets the referenced decimal value.
typename L::inner_reference reference
constexpr auto operator<=>(const value_type &rhs) const
Three-way comparison with decimal value.
typename L::size_type size_type
typename L::inner_const_reference const_reference
constexpr bool is_int_placeholder_v
std::ostream & operator<<(std::ostream &os, const sparrow::nullval_t &)