wjones127 commented on a change in pull request #166:
URL: https://github.com/apache/arrow-cookbook/pull/166#discussion_r829290778
##########
File path: cpp/code/basic_arrow.cc
##########
@@ -63,3 +66,54 @@ arrow::Status ReturnNotOk() {
TEST(BasicArrow, ReturnNotOkNoMacro) { ASSERT_OK(ReturnNotOkMacro()); }
TEST(BasicArrow, ReturnNotOk) { ASSERT_OK(ReturnNotOk()); }
+
+class TypeCountVisitor : public arrow::TypeVisitor {
+ public:
+ uint64_t nested_count;
+ uint64_t non_nested_count;
+
+ template <typename T>
+ arrow::enable_if_not_nested<T, arrow::Status> Visit(const T&) {
Review comment:
I think I must be doing this wrong, since I am getting NotImplemented
for int8:
https://github.com/apache/arrow-cookbook/runs/5588880880?check_suite_focus=true#step:6:94
Any idea on why?
##########
File path: cpp/code/basic_arrow.cc
##########
@@ -63,3 +66,54 @@ arrow::Status ReturnNotOk() {
TEST(BasicArrow, ReturnNotOkNoMacro) { ASSERT_OK(ReturnNotOkMacro()); }
TEST(BasicArrow, ReturnNotOk) { ASSERT_OK(ReturnNotOk()); }
+
+class TypeCountVisitor : public arrow::TypeVisitor {
+ public:
+ uint64_t nested_count;
+ uint64_t non_nested_count;
+
+ template <typename T>
+ arrow::enable_if_not_nested<T, arrow::Status> Visit(const T&) {
Review comment:
Is it using the default implementation from the parent class instead?
##########
File path: cpp/source/basic.rst
##########
@@ -48,3 +48,29 @@ boilerplate for you. It will run the contained expression
and check the resulti
.. recipe:: ../code/basic_arrow.cc ReturnNotOk
:caption: Using ARROW_RETURN_NOT_OK to check the status
:dedent: 2
+
+
+Using the Visitor Pattern
+=========================
+
+Arrow classes like DataType, Scalar, and Array have specialized subclasses for
+each Arrow type. In order to work with them, use the visitor pattern. These
+types provide an Accept method and have associated Visitor classes.
+
+As an example, below we implement a :cpp:class:`arrow::TypeVisitor` that counts
+the number of primitive and nested types.
+
+To implement a TypeVisitor we have to implement a Visit method for every
possible
+DataType we wish to handle. Fortunately, we can often use templates and type
+traits to make this less verbose.
+
+.. literalinclude:: ../code/basic_arrow.cc
+ :language: cpp
+ :linenos:
+ :start-at: class TypeCountVisitor
+ :end-at: }; // TypeCountVisitor
+ :caption: TypeVisitor that counts nested and non-nested types
Review comment:
Agreed. I'd like to find a better example, but haven't yet thought of
one that wasn't too long. But as a first step I'd like to at least figure out
how to use our API.
##########
File path: cpp/code/basic_arrow.cc
##########
@@ -63,3 +66,54 @@ arrow::Status ReturnNotOk() {
TEST(BasicArrow, ReturnNotOkNoMacro) { ASSERT_OK(ReturnNotOkMacro()); }
TEST(BasicArrow, ReturnNotOk) { ASSERT_OK(ReturnNotOk()); }
+
+class TypeCountVisitor : public arrow::TypeVisitor {
+ public:
+ uint64_t nested_count;
+ uint64_t non_nested_count;
+
+ template <typename T>
+ arrow::enable_if_not_nested<T, arrow::Status> Visit(const T&) {
Review comment:
Ah so if I understand right, if you subclass `TypeVisitor` you can't use
templates to implement the overriding methods.
##########
File path: cpp/source/basic.rst
##########
@@ -48,3 +48,29 @@ boilerplate for you. It will run the contained expression
and check the resulti
.. recipe:: ../code/basic_arrow.cc ReturnNotOk
:caption: Using ARROW_RETURN_NOT_OK to check the status
:dedent: 2
+
+
+Using the Visitor Pattern
+=========================
+
+Arrow classes like DataType, Scalar, and Array have specialized subclasses for
+each Arrow type. In order to work with them, use the visitor pattern. These
+types provide an Accept method and have associated Visitor classes.
+
+As an example, below we implement a :cpp:class:`arrow::TypeVisitor` that counts
+the number of primitive and nested types.
+
+To implement a TypeVisitor we have to implement a Visit method for every
possible
+DataType we wish to handle. Fortunately, we can often use templates and type
+traits to make this less verbose.
+
+.. literalinclude:: ../code/basic_arrow.cc
+ :language: cpp
+ :linenos:
+ :start-at: class TypeCountVisitor
+ :end-at: }; // TypeCountVisitor
+ :caption: TypeVisitor that counts nested and non-nested types
Review comment:
IMO one of the most common uses of the visitors would probably be to
implement a conversion to third-party types. So something like converting a
`std::vector<arrow::Scalar>` to `rapidjson::Document`. But those things aren't
something that could be copied and pasted as-is, since the conversion logic
obviously varies a lot. So maybe this is better for the user guide after-all?
(I partly wanted to do it here since we have a nice build and test setup.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]