From 17b0e310ccb1b48b49c026664a6f1f3a6e982df3 Mon Sep 17 00:00:00 2001 From: Dmitry Chigarev Date: Mon, 12 Oct 2020 14:27:53 +0300 Subject: [PATCH] Get rid of code duplication at DecimalScalar Signed-off-by: Dmitry Chigarev --- cpp/src/arrow/scalar.h | 25 ++++++++++++------------- cpp/src/arrow/type.cc | 12 +++++------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/cpp/src/arrow/scalar.h b/cpp/src/arrow/scalar.h index a6ec9d3afefde..bb6500d419ba3 100644 --- a/cpp/src/arrow/scalar.h +++ b/cpp/src/arrow/scalar.h @@ -36,6 +36,7 @@ #include "arrow/type_traits.h" #include "arrow/util/compare.h" #include "arrow/util/decimal.h" +#include "arrow/util/decimal_type_traits.h" #include "arrow/util/string_view.h" #include "arrow/util/visibility.h" @@ -336,26 +337,24 @@ struct ARROW_EXPORT DurationScalar : public TemporalScalar { using TemporalScalar::TemporalScalar; }; -struct ARROW_EXPORT Decimal128Scalar : public Scalar { +template +struct BaseDecimalScalar : public Scalar { using Scalar::Scalar; - using TypeClass = Decimal128Type; - using ValueType = Decimal128; + using TypeClass = typename DecimalTypeTraits::TypeClass; + using ValueType = typename DecimalTypeTraits::ValueType; - Decimal128Scalar(Decimal128 value, std::shared_ptr type) + BaseDecimalScalar(ValueType value, std::shared_ptr type) : Scalar(std::move(type), true), value(value) {} - Decimal128 value; + ValueType value; }; -struct ARROW_EXPORT Decimal256Scalar : public Scalar { - using Scalar::Scalar; - using TypeClass = Decimal256Type; - using ValueType = Decimal256; - - Decimal256Scalar(Decimal256 value, std::shared_ptr type) - : Scalar(std::move(type), true), value(value) {} +struct ARROW_EXPORT Decimal128Scalar : public BaseDecimalScalar<128> { + using BaseDecimalScalar<128>::BaseDecimalScalar; +}; - Decimal256 value; +struct ARROW_EXPORT Decimal256Scalar : public BaseDecimalScalar<256> { + using BaseDecimalScalar<256>::BaseDecimalScalar; }; struct ARROW_EXPORT BaseListScalar : public Scalar { diff --git a/cpp/src/arrow/type.cc b/cpp/src/arrow/type.cc index a64394aa93b30..0b092b43a569a 100644 --- a/cpp/src/arrow/type.cc +++ b/cpp/src/arrow/type.cc @@ -2149,15 +2149,13 @@ std::shared_ptr decimal(int32_t precision, int32_t scale) { : decimal256(precision, scale); } -#define DECIMAL_BUILDER_DECL(width) \ -std::shared_ptr decimal##width(int32_t precision, int32_t scale) { \ - return std::make_shared(precision, scale); \ +std::shared_ptr decimal128(int32_t precision, int32_t scale) { + return std::make_shared(precision, scale); } -DECIMAL_BUILDER_DECL(128) -DECIMAL_BUILDER_DECL(256) - -#undef DECIMAL_BUILDER_DECL +std::shared_ptr decimal256(int32_t precision, int32_t scale) { + return std::make_shared(precision, scale); +} template std::string BaseDecimalType::ToString() const {