Skip to content
2 changes: 2 additions & 0 deletions cpp/src/arrow/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "arrow/tensor.h" // IYWU pragma: export
#include "arrow/type.h" // IYWU pragma: export
#include "arrow/util/key_value_metadata.h" // IWYU pragma: export
#include "arrow/visit_array_inline.h" // IYWU pragma: export
#include "arrow/visit_scalar_inline.h" // IYWU pragma: export
#include "arrow/visitor.h" // IYWU pragma: export

/// \brief Top-level namespace for Apache Arrow C++ API
Expand Down
35 changes: 35 additions & 0 deletions cpp/src/arrow/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,22 @@ TYPE_ID_TRAIT(EXTENSION, ExtensionType)
// Per-type type traits
//

/// \addtogroup type-traits
/// \brief Base template for type traits of Arrow data types
/// Type traits provide various information about a type at compile time, such
/// as the associated ArrayType, BuilderType, and ScalarType. Not all types
/// provide all information.
/// \tparam T An Arrow data type
template <typename T>
struct TypeTraits {};

/// \brief Base template for type traits of C++ types
/// \tparam T A standard C++ type
template <typename T>
struct CTypeTraits {};

/// \addtogroup type-traits
/// @{
template <>
struct TypeTraits<NullType> {
using ArrayType = NullArray;
Expand All @@ -115,7 +125,9 @@ struct TypeTraits<BooleanType> {
constexpr static bool is_parameter_free = true;
static inline std::shared_ptr<DataType> type_singleton() { return boolean(); }
};
/// @}

/// \addtogroup c-type-traits
template <>
struct CTypeTraits<bool> : public TypeTraits<BooleanType> {
using ArrowType = BooleanType;
Expand Down Expand Up @@ -162,6 +174,8 @@ PRIMITIVE_TYPE_TRAITS_DEF(double, Double, float64)
#undef PRIMITIVE_TYPE_TRAITS_DEF
#undef PRIMITIVE_TYPE_TRAITS_DEF_

/// \addtogroup type-traits
/// @{
template <>
struct TypeTraits<Date64Type> {
using ArrayType = Date64Array;
Expand Down Expand Up @@ -367,6 +381,10 @@ struct TypeTraits<LargeStringType> {
static inline std::shared_ptr<DataType> type_singleton() { return large_utf8(); }
};

/// @}

/// \addtogroup c-type-traits
/// @{
template <>
struct CTypeTraits<std::string> : public TypeTraits<StringType> {
using ArrowType = StringType;
Expand All @@ -383,7 +401,10 @@ struct CTypeTraits<DayTimeIntervalType::DayMilliseconds>
: public TypeTraits<DayTimeIntervalType> {
using ArrowType = DayTimeIntervalType;
};
/// @}

/// \addtogroup type-traits
/// @{
template <>
struct TypeTraits<ListType> {
using ArrayType = ListArray;
Expand Down Expand Up @@ -426,7 +447,9 @@ struct TypeTraits<FixedSizeListType> {
using ScalarType = FixedSizeListScalar;
constexpr static bool is_parameter_free = false;
};
/// @}

/// \addtogroup c-type-traits
template <typename CType>
struct CTypeTraits<std::vector<CType>> : public TypeTraits<ListType> {
using ArrowType = ListType;
Expand All @@ -436,6 +459,8 @@ struct CTypeTraits<std::vector<CType>> : public TypeTraits<ListType> {
}
};

/// \addtogroup type-traits
/// @{
template <>
struct TypeTraits<StructType> {
using ArrayType = StructArray;
Expand Down Expand Up @@ -473,6 +498,7 @@ struct TypeTraits<ExtensionType> {
using ScalarType = ExtensionScalar;
constexpr static bool is_parameter_free = false;
};
/// @}

namespace internal {

Expand All @@ -490,6 +516,9 @@ using void_t = typename make_void<Ts...>::type;
// Useful type predicates
//

/// \addtogroup type-predicates
/// @{

// only in C++14
template <bool B, typename T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
Expand Down Expand Up @@ -813,6 +842,10 @@ template <typename T, typename R = void>
using enable_if_physical_floating_point =
enable_if_t<is_physical_floating_type<T>::value, R>;

/// @}

/// \addtogroup runtime-type-predicates
/// @{
static inline bool is_integer(Type::type type_id) {
switch (type_id) {
case Type::UINT8:
Expand Down Expand Up @@ -1057,4 +1090,6 @@ static inline int offset_bit_width(Type::type type_id) {
return 0;
}

/// @}

} // namespace arrow
17 changes: 17 additions & 0 deletions cpp/src/arrow/visit_array_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ namespace arrow {
internal::checked_cast<const typename TypeTraits<TYPE_CLASS##Type>::ArrayType&>( \
array));

/// \brief Apply the visitors Visit() method specialized to the array type
///
/// \tparam VISITOR Visitor type that implements Visit() for all array types.
/// \return Status
///
/// A visitor is a type that implements specialized logic for each Arrow type.
/// Example usage:
///
/// ```
/// class ExampleVisitor {
/// arrow::Status Visit(arrow::NumericArray<Int32Type> arr) { ... }
/// arrow::Status Visit(arrow::NumericArray<Int64Type> arr) { ... }
/// ...
/// }
/// ExampleVisitor visitor;
/// VisitArrayInline(some_array, &visitor);
/// ```
template <typename VISITOR>
inline Status VisitArrayInline(const Array& array, VISITOR* visitor) {
switch (array.type_id()) {
Expand Down
17 changes: 17 additions & 0 deletions cpp/src/arrow/visit_scalar_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ namespace arrow {
case TYPE_CLASS##Type::type_id: \
return visitor->Visit(internal::checked_cast<const TYPE_CLASS##Scalar&>(scalar));

/// \brief Apply the visitors Visit() method specialized to the scalar type
///
/// \tparam VISITOR Visitor type that implements Visit() for all scalar types.
/// \return Status
///
/// A visitor is a type that implements specialized logic for each Arrow type.
/// Example usage:
///
/// ```
/// class ExampleVisitor {
/// arrow::Status Visit(arrow::Int32Scalar arr) { ... }
/// arrow::Status Visit(arrow::Int64Scalar arr) { ... }
/// ...
/// }
/// ExampleVisitor visitor;
/// VisitScalarInline(some_scalar, &visitor);
/// ```
template <typename VISITOR>
inline Status VisitScalarInline(const Scalar& scalar, VISITOR* visitor) {
switch (scalar.type->id()) {
Expand Down
22 changes: 21 additions & 1 deletion cpp/src/arrow/visit_type_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ namespace arrow {
case TYPE_CLASS##Type::type_id: \
return visitor->Visit(internal::checked_cast<const TYPE_CLASS##Type&>(type));

/// \brief Calls `visitor` with the corresponding concrete type class
///
/// \tparam VISITOR Visitor type that implements Visit() for all Arrow types.
/// \return Status
///
/// A visitor is a type that implements specialized logic for each Arrow type.
/// Example usage:
///
/// ```
/// class ExampleVisitor {
/// arrow::Status Visit(const arrow::Int32Type& type) { ... }
/// arrow::Status Visit(const arrow::Int64Type& type) { ... }
/// ...
/// }
/// ExampleVisitor visitor;
/// VisitTypeInline(some_type, &visitor);
/// ```
template <typename VISITOR>
inline Status VisitTypeInline(const DataType& type, VISITOR* visitor) {
switch (type.id()) {
Expand All @@ -46,7 +63,10 @@ inline Status VisitTypeInline(const DataType& type, VISITOR* visitor) {
return visitor->Visit(concrete_ptr); \
}

// Calls `visitor` with a nullptr of the corresponding concrete type class
/// \brief Calls `visitor` with a nullptr of the corresponding concrete type class
///
/// \tparam VISITOR Visitor type that implements Visit() for all Arrow types.
/// \return Status
template <typename VISITOR>
inline Status VisitTypeIdInline(Type::type id, VISITOR* visitor) {
switch (id) {
Expand Down
129 changes: 129 additions & 0 deletions docs/source/cpp/api/utilities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,132 @@ Compression

.. doxygenclass:: arrow::util::Decompressor
:members:

Visitors
========

.. doxygenfunction:: arrow::VisitTypeInline
:project: arrow_cpp

.. doxygenfunction:: arrow::VisitTypeIdInline
:project: arrow_cpp

.. doxygenfunction:: arrow::VisitScalarInline
:project: arrow_cpp

.. doxygenfunction:: arrow::VisitArrayInline
:project: arrow_cpp


.. _type-traits:

Type Traits
===========

These types provide relationships between Arrow types at compile time. :cpp:type:`TypeTraits`
maps Arrow DataTypes to other types, and :cpp:type:`CTypeTraits ` maps C types to
Arrow types.

TypeTraits
----------

Each specialized type defines the following associated types:

.. cpp:type:: TypeTraits::ArrayType

Corresponding :doc:`Arrow array type </cpp/api/array.rst>`

.. cpp:type:: TypeTraits::BuilderType

Corresponding :doc:`array builder type </cpp/api/builders.rst>`

.. cpp:type:: TypeTraits::ScalarType

Corresponding :doc:`Arrow scalar type </cpp/api/scalar.rst>`

.. cpp:var:: TypeTraits::is_parameter_free

Whether the type has any type parameters, such as field types in nested types
or scale and precision in decimal types.


In addition, the following are defined for many but not all of the types:

.. cpp:type:: TypeTraits::CType

Corresponding C type. For example, ``int64_t`` for ``Int64Array``.

.. cpp:type:: TypeTraits::TensorType

Corresponding :doc:`Arrow tensor type </cpp/api/tensor.rst>`

.. cpp:function:: static inline constexpr int64_t bytes_required(int64_t elements)

Return the number of bytes required for given number of elements. Defined for
types with a fixed size.

.. cpp:function:: static inline std::shared_ptr<DataType> TypeTraits::type_singleton()

For types where is_parameter_free is true, returns an instance of the data type.


.. doxygengroup:: type-traits
:content-only:
:members:
:undoc-members:

CTypeTraits
-----------

Each specialized type defines the following associated types:

.. cpp:type:: CTypeTraits::ArrowType

Corresponding :doc:`Arrow type </cpp/api/datatype.rst>`

.. doxygengroup:: c-type-traits
:content-only:
:members:
:undoc-members:


.. _type-predicates-api:

Type Predicates
---------------

Type predicates that can be used with templates. Predicates of the form ``is_XXX``
resolve to constant boolean values, while predicates of the form ``enable_if_XXX``
resolve to the second type parameter ``R`` if the first parameter ``T`` passes
the test.

Example usage:

.. code-block:: cpp

template<typename TypeClass>
arrow::enable_if_number<TypeClass, RETURN_TYPE> MyFunction(const TypeClass& type) {
..
}

template<typename ArrayType, typename TypeClass=ArrayType::TypeClass>
arrow::enable_if_number<TypeClass, RETURN_TYPE> MyFunction(const ArrayType& array) {
..
}


.. doxygengroup:: type-predicates
:content-only:
:members:
:undoc-members:


Runtime Type Predicates
-----------------------

Type predicates that can be applied at runtime.

.. doxygengroup:: runtime-type-predicates
:content-only:
:members:
:undoc-members:
14 changes: 0 additions & 14 deletions docs/source/cpp/conventions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,3 @@ For example::
.. seealso::
:doc:`API reference for error reporting <api/support>`

Visitor Pattern
---------------

Several types, including :class:`arrow::DataType`, :class:`arrow::Scalar`, and
:class:`arrow::Array`, have sub-types specialized to their corresponding Arrow
types. For example, for the Arrow boolean type there is :class:`arrow::BooleanType`,
:class:`arrow::BooleanScalar`, and :class:`arrow::BooleanArray`. In order to
process these entities, you may need to write write logic that specializes based
on the particular Arrow type. In these cases, use the
`visitor pattern <https://en.wikipedia.org/wiki/Visitor_pattern>`_.

Arrow provides abstract visitor classes (:class:`arrow::TypeVisitor`,
:class:`arrow::ScalarVisitor`, :class:`arrow::ArrayVisitor`) and an ``Accept()``
method on each of the corresponding base types (e.g. :func:`arrow::Array::Accept`).
Loading