paleolimbot commented on code in PR #365:
URL: https://github.com/apache/arrow-adbc/pull/365#discussion_r1095070991


##########
r/adbcdrivermanager/src/radbc.cc:
##########
@@ -0,0 +1,427 @@
+// 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.
+
+#define R_NO_REMAP
+#include <R.h>
+#include <Rinternals.h>
+
+#include <adbc.h>
+#include "adbc_driver_manager.h"
+
+#include "radbc.h"
+
+static const char* adbc_error_message(AdbcError* error) {
+  if (error->message == nullptr) {
+    return "";
+  } else {
+    return error->message;
+  }
+}
+
+static void adbc_error_warn(int code, AdbcError* error, const char* context) {
+  if (code != ADBC_STATUS_OK) {
+    Rf_warning("<%s> %s", context, adbc_error_message(error));
+  }
+}
+
+static void adbc_error_stop(int code, AdbcError* error, const char* context) {
+  if (code != ADBC_STATUS_OK) {
+    Rf_error("<%s> %s", context, adbc_error_message(error));
+  }
+}
+
+static void finalize_database_xptr(SEXP database_xptr) {
+  auto database = 
reinterpret_cast<AdbcDatabase*>(R_ExternalPtrAddr(database_xptr));
+  if (database == nullptr) {
+    return;
+  }
+
+  if (database->private_data != nullptr) {
+    AdbcError error;
+    int status = AdbcDatabaseRelease(database, &error);
+    adbc_error_warn(status, &error, "finalize_database_xptr()");
+  }
+
+  adbc_xptr_default_finalize<AdbcDatabase>(database_xptr);
+}
+
+extern "C" SEXP RAdbcLoadDriver(SEXP driver_name_sexp, SEXP entrypoint_sexp) {

Review Comment:
   The crux of this package is the external pointer...in particular, the 
external pointer's "tag" (which is the `environment` that can be used to 
get/set arbitrary R metadata) and "prot" (which is a reference to the parent 
object that keeps the parent object from being garbage collected while the 
child object is still in scope). It's not so much that I was trying to avoid 
cpp11...it's more that cpp11 doesn't currently offer a lot of helpers for the 
humble external pointer.
   
   I'm happy to switch to cpp11; however, I'd prefer to do it because there is 
code that is difficult to understand or error-prone (feel free to point out 
examples!).



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to