paleolimbot commented on code in PR #406:
URL: https://github.com/apache/arrow-nanoarrow/pull/406#discussion_r1534360446


##########
examples/cmake-scenarios/src/library.cpp:
##########
@@ -0,0 +1,39 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "nanoarrow/nanoarrow.h"
+#include "nanoarrow/nanoarrow.hpp"
+
+#include "library.hpp"
+
+std::optional<std::pair<std::unique_ptr<ArrowArray>, 
std::unique_ptr<ArrowSchema>>>
+make_simple_array() {
+  nanoarrow::UniqueArray tmp_array;
+  nanoarrow::UniqueSchema tmp_schema;
+  auto result = make_simple_array(tmp_array.get(), tmp_schema.get());
+  if (result != 0) {
+    if (tmp_array.get()->release) tmp_array.get()->release(tmp_array.get());
+    if (tmp_schema.get()->release) tmp_schema.get()->release(tmp_schema.get());
+    return std::nullopt;
+  }
+  auto ret_array = std::make_unique<ArrowArray>();

Review Comment:
   This is all new, so take it with a grain of salt, but usually there are an 
internal set of helpers that make heavy use of nanoarrow library calls. These 
helpers should maybe look like:
   
   ```c
   ArrowErrorCode MakeInt32(ArrowSchema* schema) {
     nanoarrow::UniqueSchema tmp;
     NANOARROW_RETURN_NOT_OK(ArrowSchemaInitFromType(tmp.get(), 
NANOARROW_TYPE_INT32));
     ArrowSchemaMove(tmp.get(), schema);
     return NANOARROW_OK;
   }
   ```
   
   (e.g., return `ArrowErrorCode` so that you can use 
`NANOARROW_RETURN_NOT_OK()` and use the `Unique` helpers to ensure you can 
return/throw wherever you want)
   
   Then there comes a point where you're at the interface with another library, 
where in C++, you probably don't want a pointer output argument and you have 
some other error handling schema.
   
   ```cpp
   nanarrow::UniqueSchema MakeInt32Outer() {
     nanoarrow::UniqueSchema out;
     ArrowErrorCode code = MakeInt32(out.get());
     if (code != NANOARROW_OK) {
       // report the error via whatever scheme the application is using
     }
   
     return out;
   }
   ```
   
   This is all open to interpretation and opinions, of course.
   
   > Does the tmp_array.move(...) call not transfer the the release function 
pointer from tmp_array to ret_array? I assumed that it would transfer all the 
members of the underlying ArrowArray, not just the data buffer pointers.
   
   It transfers everything; however, unless you give it very specific 
instructions, there's no guarantee that the `std::unique_ptr<ArrowArray>` will 
actually call the release callback when it is deleted.



-- 
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]

Reply via email to