This is an automated email from the ASF dual-hosted git repository.

sgilmore pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new c5fdc8e06c GH-38369: [MATLAB]  Create utility functions for 
simplifying management of `Proxy` instances for `Array`s (#46907)
c5fdc8e06c is described below

commit c5fdc8e06c629142f6de6923976d3eeab359cf47
Author: Sarah Gilmore <[email protected]>
AuthorDate: Thu Jun 26 10:44:56 2025 -0400

    GH-38369: [MATLAB]  Create utility functions for simplifying management of 
`Proxy` instances for `Array`s (#46907)
    
    ### Rationale for this change
    
    This is a follow up to 
https://github.com/apache/arrow/pull/38357#discussion_r1365908049.
    
    In the C++ code for the MATLAB interface, we frequently `wrap`/`unwrap` 
Arrow types, like `arrow::Array`, into/from a corresponding `libmexclass` 
`Proxy` objects. This currently takes several lines of code, leading to lots of 
code duplication and introduces room for subtle implementation errors.
    
    It would be helpful to abstract away some of the steps involved in `Proxy` 
creation/management into a set of helper utilities that we can reuse in the C++ 
code for the MATLAB interface.
    
    ### What changes are included in this PR?
    
    1. Added a new utility function that wraps a 
`std::shared_ptr<arrow::Array>` in a 
`std::shared_ptr<arrow::matlab::array::proxy::Array>`.  Its function signature 
is
    
    ```c++
    arrow::Result<std::shared_ptr<arrow::matlab::array::proxy::Array>> 
wrap(const std::shared_ptr<arrow::Array>& array);`
    ````
    2. Added a new utility function that wraps a 
`std::shared_ptr<arrow::Array>` in a 
`std::shared_ptr<arrow::matlab::array::proxy::Array>` and adds the proxy to the 
`libmexclass::proxy::ProxyManager`. Its function signature is
    
    ```c++
    arrow::Result<std::shared_ptr<arrow::matlab::array::proxy::Array>> 
wrap_and_manage(const std::shared_ptr<arrow::Array>& array);
    ```
    
    3. Added a new utility function that wraps a 
`std::shared_ptr<arrow::DataType>` in a 
`std::shared_ptr<arrow::matlab::type::proxy::Type>`. Its function signature is
    
    ```c++
    arrow::Result<std::shared_ptr<arrow::matlab::type::proxy::Type>> wrap(const 
std::shared_ptr<arrow::DataType>& type);
    ```
    
    4. Added a new utility function that wraps a 
`std::shared_ptr<arrow::DataType>` in a 
`std::shared_ptr<arrow::matlab::type::proxy::DataType>` and adds the proxy to 
the `libmexclass::proxy::ProxyManager`. It's function signature is
    
    ```c++
    arrow::Result<std::shared_ptr<arrow::matlab::type::proxy::Type>> 
wrap_and_manage(const std::shared_ptr<arrow::DataType>& type);
    ```
    
    5. Deleted `arrow::matlab::array::proxy::wrap` and 
`arrow::matlab::type::proxy::wrap` functions.
    
    ### Are these changes tested?
    
    Tested via the existing MATLAB test classes.
    
    ### Are there any user-facing changes?
    
    No.
    
    * GitHub Issue: #38369
    
    Lead-authored-by: Sarah Gilmore <[email protected]>
    Co-authored-by: Sarah Gilmore <[email protected]>
    Co-authored-by: Kevin Gurney <[email protected]>
    Signed-off-by: Sarah Gilmore <[email protected]>
---
 matlab/src/cpp/arrow/matlab/array/proxy/array.cc   |  35 +---
 .../cpp/arrow/matlab/array/proxy/chunked_array.cc  |  35 +---
 .../src/cpp/arrow/matlab/array/proxy/list_array.cc |  18 +-
 .../cpp/arrow/matlab/array/proxy/struct_array.cc   |  33 +---
 matlab/src/cpp/arrow/matlab/array/proxy/wrap.cc    |  94 ---------
 matlab/src/cpp/arrow/matlab/array/proxy/wrap.h     |  30 ---
 .../src/cpp/arrow/matlab/c/proxy/array_importer.cc |  20 +-
 matlab/src/cpp/arrow/matlab/proxy/wrap.cc          | 220 +++++++++++++++++++++
 matlab/src/cpp/arrow/matlab/proxy/wrap.h           |  54 +++++
 .../cpp/arrow/matlab/tabular/proxy/record_batch.cc |  37 +---
 matlab/src/cpp/arrow/matlab/tabular/proxy/table.cc |   1 -
 matlab/src/cpp/arrow/matlab/type/proxy/field.cc    |  18 +-
 .../src/cpp/arrow/matlab/type/proxy/list_type.cc   |  22 +--
 matlab/src/cpp/arrow/matlab/type/proxy/wrap.cc     |  96 ---------
 matlab/src/cpp/arrow/matlab/type/proxy/wrap.h      |  29 ---
 .../src/matlab/+arrow/+c/+internal/ArrayImporter.m |   6 +-
 matlab/src/matlab/+arrow/+tabular/RecordBatch.m    |   8 +-
 matlab/src/matlab/+arrow/+type/ListType.m          |   6 +-
 matlab/tools/cmake/BuildMatlabArrowInterface.cmake |   5 +-
 19 files changed, 330 insertions(+), 437 deletions(-)

diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/array.cc 
b/matlab/src/cpp/arrow/matlab/array/proxy/array.cc
index 1eb6de74fe..2fa82d27e3 100644
--- a/matlab/src/cpp/arrow/matlab/array/proxy/array.cc
+++ b/matlab/src/cpp/arrow/matlab/array/proxy/array.cc
@@ -19,11 +19,10 @@
 #include "arrow/util/utf8.h"
 
 #include "arrow/matlab/array/proxy/array.h"
-#include "arrow/matlab/array/proxy/wrap.h"
 #include "arrow/matlab/bit/unpack.h"
 #include "arrow/matlab/error/error.h"
 #include "arrow/matlab/index/validate.h"
-#include "arrow/matlab/type/proxy/wrap.h"
+#include "arrow/matlab/proxy/wrap.h"
 #include "arrow/pretty_print.h"
 #include "arrow/type_traits.h"
 
@@ -110,20 +109,9 @@ void Array::getValid(libmexclass::proxy::method::Context& 
context) {
 }
 
 void Array::getType(libmexclass::proxy::method::Context& context) {
-  namespace mda = ::matlab::data;
-
-  mda::ArrayFactory factory;
-
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto type_proxy, 
type::proxy::wrap(array->type()),
-                                      context, 
error::ARRAY_FAILED_TO_CREATE_TYPE_PROXY);
-
-  const auto type_id = static_cast<int32_t>(type_proxy->unwrap()->id());
-  const auto proxy_id = 
libmexclass::proxy::ProxyManager::manageProxy(type_proxy);
-
-  mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", 
"TypeID"});
-  output[0]["ProxyID"] = factory.createScalar(proxy_id);
-  output[0]["TypeID"] = factory.createScalar(type_id);
-  context.outputs[0] = output;
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(
+      context.outputs[0], 
arrow::matlab::proxy::wrap_and_manage(array->type()), context,
+      error::ARRAY_FAILED_TO_CREATE_TYPE_PROXY);
 }
 
 void Array::isEqual(libmexclass::proxy::method::Context& context) {
@@ -167,18 +155,9 @@ void Array::slice(libmexclass::proxy::method::Context& 
context) {
                                       context, 
error::ARRAY_SLICE_NEGATIVE_LENGTH);
 
   auto sliced_array = array->Slice(offset, length);
-  const auto type_id = static_cast<int32_t>(sliced_array->type_id());
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto sliced_array_proxy,
-                                      array::proxy::wrap(sliced_array), 
context,
-                                      
error::ARRAY_SLICE_FAILED_TO_CREATE_ARRAY_PROXY);
-
-  const auto proxy_id = 
libmexclass::proxy::ProxyManager::manageProxy(sliced_array_proxy);
-
-  mda::ArrayFactory factory;
-  mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", 
"TypeID"});
-  output[0]["ProxyID"] = factory.createScalar(proxy_id);
-  output[0]["TypeID"] = factory.createScalar(type_id);
-  context.outputs[0] = output;
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(
+      context.outputs[0], arrow::matlab::proxy::wrap_and_manage(sliced_array), 
context,
+      error::ARRAY_SLICE_FAILED_TO_CREATE_ARRAY_PROXY);
 }
 
 void Array::exportToC(libmexclass::proxy::method::Context& context) {
diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/chunked_array.cc 
b/matlab/src/cpp/arrow/matlab/array/proxy/chunked_array.cc
index a23d4744dd..00410f83e3 100644
--- a/matlab/src/cpp/arrow/matlab/array/proxy/chunked_array.cc
+++ b/matlab/src/cpp/arrow/matlab/array/proxy/chunked_array.cc
@@ -19,9 +19,8 @@
 
 #include "arrow/matlab/array/proxy/array.h"
 #include "arrow/matlab/array/proxy/chunked_array.h"
-#include "arrow/matlab/array/proxy/wrap.h"
 #include "arrow/matlab/error/error.h"
-#include "arrow/matlab/type/proxy/wrap.h"
+#include "arrow/matlab/proxy/wrap.h"
 
 #include "libmexclass/proxy/ProxyManager.h"
 
@@ -126,35 +125,15 @@ void 
ChunkedArray::getChunk(libmexclass::proxy::method::Context& context) {
   }
 
   const auto array = chunked_array->chunk(index);
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto array_proxy,
-                                      
arrow::matlab::array::proxy::wrap(array), context,
-                                      error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
-
-  const auto array_proxy_id = 
libmexclass::proxy::ProxyManager::manageProxy(array_proxy);
-  const auto type_id = static_cast<int64_t>(array->type_id());
-
-  mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", 
"TypeID"});
-  output[0]["ProxyID"] = factory.createScalar(array_proxy_id);
-  output[0]["TypeID"] = factory.createScalar(type_id);
-  context.outputs[0] = output;
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(context.outputs[0],
+                                      
arrow::matlab::proxy::wrap_and_manage(array),
+                                      context, 
error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
 }
 
 void ChunkedArray::getType(libmexclass::proxy::method::Context& context) {
-  namespace mda = ::matlab::data;
-
-  mda::ArrayFactory factory;
-
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto type_proxy,
-                                      
type::proxy::wrap(chunked_array->type()), context,
-                                      
error::ARRAY_FAILED_TO_CREATE_TYPE_PROXY);
-
-  const auto proxy_id = 
libmexclass::proxy::ProxyManager::manageProxy(type_proxy);
-  const auto type_id = static_cast<int32_t>(type_proxy->unwrap()->id());
-
-  mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", 
"TypeID"});
-  output[0]["ProxyID"] = factory.createScalar(proxy_id);
-  output[0]["TypeID"] = factory.createScalar(type_id);
-  context.outputs[0] = output;
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(
+      context.outputs[0], 
arrow::matlab::proxy::wrap_and_manage(chunked_array->type()),
+      context, error::ARRAY_FAILED_TO_CREATE_TYPE_PROXY);
 }
 
 void ChunkedArray::isEqual(libmexclass::proxy::method::Context& context) {
diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/list_array.cc 
b/matlab/src/cpp/arrow/matlab/array/proxy/list_array.cc
index 9b8a587e56..36b9a6a332 100644
--- a/matlab/src/cpp/arrow/matlab/array/proxy/list_array.cc
+++ b/matlab/src/cpp/arrow/matlab/array/proxy/list_array.cc
@@ -17,9 +17,10 @@
 
 #include "arrow/matlab/array/proxy/list_array.h"
 #include "arrow/matlab/array/proxy/numeric_array.h"
-#include "arrow/matlab/array/proxy/wrap.h"
 #include "arrow/matlab/array/validation_mode.h"
 #include "arrow/matlab/error/error.h"
+#include "arrow/matlab/proxy/wrap.h"
+
 #include "libmexclass/proxy/ProxyManager.h"
 
 namespace arrow::matlab::array::proxy {
@@ -78,20 +79,9 @@ void 
ListArray::getValues(libmexclass::proxy::method::Context& context) {
   auto list_array = std::static_pointer_cast<arrow::ListArray>(array);
   auto value_array = list_array->values();
 
-  // Wrap the array within a proxy object if possible.
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto value_array_proxy, 
proxy::wrap(value_array),
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(context.outputs[0],
+                                      
arrow::matlab::proxy::wrap_and_manage(value_array),
                                       context, 
error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
-  const auto value_array_proxy_id = 
ProxyManager::manageProxy(value_array_proxy);
-  const auto type_id = value_array->type_id();
-
-  // Return a struct with two fields: ProxyID and TypeID. The MATLAB
-  // layer will use these values to construct the appropriate MATLAB
-  // arrow.array.Array subclass.
-  mda::ArrayFactory factory;
-  mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", 
"TypeID"});
-  output[0]["ProxyID"] = factory.createScalar(value_array_proxy_id);
-  output[0]["TypeID"] = factory.createScalar(static_cast<int32_t>(type_id));
-  context.outputs[0] = output;
 }
 
 void ListArray::getOffsets(libmexclass::proxy::method::Context& context) {
diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/struct_array.cc 
b/matlab/src/cpp/arrow/matlab/array/proxy/struct_array.cc
index e70c70950e..18e4d7a6a0 100644
--- a/matlab/src/cpp/arrow/matlab/array/proxy/struct_array.cc
+++ b/matlab/src/cpp/arrow/matlab/array/proxy/struct_array.cc
@@ -16,11 +16,10 @@
 // under the License.
 
 #include "arrow/matlab/array/proxy/struct_array.h"
-#include "arrow/matlab/array/proxy/wrap.h"
 #include "arrow/matlab/bit/pack.h"
 #include "arrow/matlab/error/error.h"
 #include "arrow/matlab/index/validate.h"
-
+#include "arrow/matlab/proxy/wrap.h"
 #include "arrow/util/utf8.h"
 
 #include "libmexclass/proxy/ProxyManager.h"
@@ -114,21 +113,9 @@ void 
StructArray::getFieldByIndex(libmexclass::proxy::method::Context& context)
   const int32_t index = matlab_index - 1;
 
   auto field_array = struct_array->field(index);
-
-  // Wrap the array within a proxy object if possible.
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto field_array_proxy, 
proxy::wrap(field_array),
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(context.outputs[0],
+                                      
arrow::matlab::proxy::wrap_and_manage(field_array),
                                       context, 
error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
-  const auto field_array_proxy_id = 
ProxyManager::manageProxy(field_array_proxy);
-  const auto type_id = field_array->type_id();
-
-  // Return a struct with two fields: ProxyID and TypeID. The MATLAB
-  // layer will use these values to construct the appropriate MATLAB
-  // arrow.array.Array subclass.
-  mda::ArrayFactory factory;
-  mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", 
"TypeID"});
-  output[0]["ProxyID"] = factory.createScalar(field_array_proxy_id);
-  output[0]["TypeID"] = factory.createScalar(static_cast<int32_t>(type_id));
-  context.outputs[0] = output;
 }
 
 void StructArray::getFieldByName(libmexclass::proxy::method::Context& context) 
{
@@ -154,19 +141,9 @@ void 
StructArray::getFieldByName(libmexclass::proxy::method::Context& context) {
   }
 
   // Wrap the array within a proxy object if possible.
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto field_array_proxy, 
proxy::wrap(field_array),
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(context.outputs[0],
+                                      
arrow::matlab::proxy::wrap_and_manage(field_array),
                                       context, 
error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
-  const auto field_array_proxy_id = 
ProxyManager::manageProxy(field_array_proxy);
-  const auto type_id = field_array->type_id();
-
-  // Return a struct with two fields: ProxyID and TypeID. The MATLAB
-  // layer will use these values to construct the appropriate MATLAB
-  // arrow.array.Array subclass.
-  mda::ArrayFactory factory;
-  mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", 
"TypeID"});
-  output[0]["ProxyID"] = factory.createScalar(field_array_proxy_id);
-  output[0]["TypeID"] = factory.createScalar(static_cast<int32_t>(type_id));
-  context.outputs[0] = output;
 }
 
 void StructArray::getFieldNames(libmexclass::proxy::method::Context& context) {
diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/wrap.cc 
b/matlab/src/cpp/arrow/matlab/array/proxy/wrap.cc
deleted file mode 100644
index 3e38306906..0000000000
--- a/matlab/src/cpp/arrow/matlab/array/proxy/wrap.cc
+++ /dev/null
@@ -1,94 +0,0 @@
-// 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 "arrow/matlab/array/proxy/wrap.h"
-#include "arrow/matlab/array/proxy/array.h"
-#include "arrow/matlab/array/proxy/boolean_array.h"
-#include "arrow/matlab/array/proxy/list_array.h"
-#include "arrow/matlab/array/proxy/numeric_array.h"
-#include "arrow/matlab/array/proxy/string_array.h"
-#include "arrow/matlab/array/proxy/struct_array.h"
-
-namespace arrow::matlab::array::proxy {
-
-arrow::Result<std::shared_ptr<proxy::Array>> wrap(
-    const std::shared_ptr<arrow::Array>& array) {
-  using ID = arrow::Type::type;
-  switch (array->type_id()) {
-    case ID::BOOL:
-      return std::make_shared<proxy::BooleanArray>(
-          std::static_pointer_cast<arrow::BooleanArray>(array));
-    case ID::UINT8:
-      return std::make_shared<proxy::NumericArray<arrow::UInt8Type>>(
-          std::static_pointer_cast<arrow::UInt8Array>(array));
-    case ID::UINT16:
-      return std::make_shared<proxy::NumericArray<arrow::UInt16Type>>(
-          std::static_pointer_cast<arrow::UInt16Array>(array));
-    case ID::UINT32:
-      return std::make_shared<proxy::NumericArray<arrow::UInt32Type>>(
-          std::static_pointer_cast<arrow::UInt32Array>(array));
-    case ID::UINT64:
-      return std::make_shared<proxy::NumericArray<arrow::UInt64Type>>(
-          std::static_pointer_cast<arrow::UInt64Array>(array));
-    case ID::INT8:
-      return std::make_shared<proxy::NumericArray<arrow::Int8Type>>(
-          std::static_pointer_cast<arrow::Int8Array>(array));
-    case ID::INT16:
-      return std::make_shared<proxy::NumericArray<arrow::Int16Type>>(
-          std::static_pointer_cast<arrow::Int16Array>(array));
-    case ID::INT32:
-      return std::make_shared<proxy::NumericArray<arrow::Int32Type>>(
-          std::static_pointer_cast<arrow::Int32Array>(array));
-    case ID::INT64:
-      return std::make_shared<proxy::NumericArray<arrow::Int64Type>>(
-          std::static_pointer_cast<arrow::Int64Array>(array));
-    case ID::FLOAT:
-      return std::make_shared<proxy::NumericArray<arrow::FloatType>>(
-          std::static_pointer_cast<arrow::FloatArray>(array));
-    case ID::DOUBLE:
-      return std::make_shared<proxy::NumericArray<arrow::DoubleType>>(
-          std::static_pointer_cast<arrow::DoubleArray>(array));
-    case ID::TIMESTAMP:
-      return std::make_shared<proxy::NumericArray<arrow::TimestampType>>(
-          std::static_pointer_cast<arrow::TimestampArray>(array));
-    case ID::TIME32:
-      return std::make_shared<proxy::NumericArray<arrow::Time32Type>>(
-          std::static_pointer_cast<arrow::Time32Array>(array));
-    case ID::TIME64:
-      return std::make_shared<proxy::NumericArray<arrow::Time64Type>>(
-          std::static_pointer_cast<arrow::Time64Array>(array));
-    case ID::DATE32:
-      return std::make_shared<proxy::NumericArray<arrow::Date32Type>>(
-          std::static_pointer_cast<arrow::Date32Array>(array));
-    case ID::DATE64:
-      return std::make_shared<proxy::NumericArray<arrow::Date64Type>>(
-          std::static_pointer_cast<arrow::Date64Array>(array));
-    case ID::STRING:
-      return std::make_shared<proxy::StringArray>(
-          std::static_pointer_cast<arrow::StringArray>(array));
-    case ID::LIST:
-      return std::make_shared<proxy::ListArray>(
-          std::static_pointer_cast<arrow::ListArray>(array));
-    case ID::STRUCT:
-      return std::make_shared<proxy::StructArray>(
-          std::static_pointer_cast<arrow::StructArray>(array));
-    default:
-      return arrow::Status::NotImplemented("Unsupported DataType: " +
-                                           array->type()->ToString());
-  }
-}
-}  // namespace arrow::matlab::array::proxy
diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/wrap.h 
b/matlab/src/cpp/arrow/matlab/array/proxy/wrap.h
deleted file mode 100644
index 4555990c00..0000000000
--- a/matlab/src/cpp/arrow/matlab/array/proxy/wrap.h
+++ /dev/null
@@ -1,30 +0,0 @@
-// 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.
-
-#pragma once
-
-#include "arrow/array.h"
-#include "arrow/result.h"
-
-#include "arrow/matlab/array/proxy/array.h"
-
-namespace arrow::matlab::array::proxy {
-
-arrow::Result<std::shared_ptr<proxy::Array>> wrap(
-    const std::shared_ptr<arrow::Array>& array);
-
-}
diff --git a/matlab/src/cpp/arrow/matlab/c/proxy/array_importer.cc 
b/matlab/src/cpp/arrow/matlab/c/proxy/array_importer.cc
index b6f68332d1..e4b7e55183 100644
--- a/matlab/src/cpp/arrow/matlab/c/proxy/array_importer.cc
+++ b/matlab/src/cpp/arrow/matlab/c/proxy/array_importer.cc
@@ -18,11 +18,9 @@
 #include "arrow/array.h"
 #include "arrow/c/bridge.h"
 
-#include "arrow/matlab/array/proxy/wrap.h"
 #include "arrow/matlab/c/proxy/array_importer.h"
 #include "arrow/matlab/error/error.h"
-
-#include "libmexclass/proxy/ProxyManager.h"
+#include "arrow/matlab/proxy/wrap.h"
 
 namespace arrow::matlab::c::proxy {
 
@@ -35,7 +33,6 @@ libmexclass::proxy::MakeResult ArrayImporter::make(
 
 void ArrayImporter::import(libmexclass::proxy::method::Context& context) {
   namespace mda = ::matlab::data;
-  using namespace libmexclass::proxy;
 
   mda::StructArray args = context.inputs[0];
   const mda::TypedArray<uint64_t> arrow_array_address_mda = 
args[0]["ArrowArrayAddress"];
@@ -52,18 +49,9 @@ void 
ArrayImporter::import(libmexclass::proxy::method::Context& context) {
                                       arrow::ImportArray(arrow_array, 
arrow_schema),
                                       context, error::C_IMPORT_FAILED);
 
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto array_proxy,
-                                      
arrow::matlab::array::proxy::wrap(array), context,
-                                      error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
-
-  mda::ArrayFactory factory;
-  const auto array_proxy_id = ProxyManager::manageProxy(array_proxy);
-  const auto array_proxy_id_mda = factory.createScalar(array_proxy_id);
-  const auto array_type_id_mda =
-      factory.createScalar(static_cast<int32_t>(array->type_id()));
-
-  context.outputs[0] = array_proxy_id_mda;
-  context.outputs[1] = array_type_id_mda;
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(context.outputs[0],
+                                      
arrow::matlab::proxy::wrap_and_manage(array),
+                                      context, 
error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
 }
 
 }  // namespace arrow::matlab::c::proxy
diff --git a/matlab/src/cpp/arrow/matlab/proxy/wrap.cc 
b/matlab/src/cpp/arrow/matlab/proxy/wrap.cc
new file mode 100644
index 0000000000..c31ee5f487
--- /dev/null
+++ b/matlab/src/cpp/arrow/matlab/proxy/wrap.cc
@@ -0,0 +1,220 @@
+// 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 "arrow/matlab/proxy/wrap.h"
+#include "arrow/matlab/array/proxy/boolean_array.h"
+#include "arrow/matlab/array/proxy/list_array.h"
+#include "arrow/matlab/array/proxy/numeric_array.h"
+#include "arrow/matlab/array/proxy/string_array.h"
+#include "arrow/matlab/array/proxy/struct_array.h"
+#include "arrow/matlab/type/proxy/date32_type.h"
+#include "arrow/matlab/type/proxy/date64_type.h"
+#include "arrow/matlab/type/proxy/list_type.h"
+#include "arrow/matlab/type/proxy/primitive_ctype.h"
+#include "arrow/matlab/type/proxy/string_type.h"
+#include "arrow/matlab/type/proxy/struct_type.h"
+#include "arrow/matlab/type/proxy/time32_type.h"
+#include "arrow/matlab/type/proxy/time64_type.h"
+
+#include "libmexclass/proxy/ProxyManager.h"
+
+namespace arrow::matlab::proxy {
+
+namespace {
+template <typename T>
+struct ProxyTraits {};
+
+// Using MATLAB_PROXY_TRAITS_DEF enables the wrap functions defined
+// below to construct appropriate Proxy objects. The arguments to the macro
+// are used to automatically define a ProxyTraits struct which follows a "type
+// traits pattern".
+#define MATLAB_PROXY_TRAITS_DEF(ArrowType_, ArrayProxyName_, TypeProxyName_) \
+  template <>                                                                \
+  struct ProxyTraits<ArrowType_> {                                           \
+    using ArrayProxy = arrow::matlab::array::proxy::ArrayProxyName_;         \
+    using TypeProxy = arrow::matlab::type::proxy::TypeProxyName_;            \
+  };
+
+MATLAB_PROXY_TRAITS_DEF(arrow::BooleanType, BooleanArray, PrimitiveCType<bool>)
+MATLAB_PROXY_TRAITS_DEF(arrow::Int8Type, NumericArray<arrow::Int8Type>,
+                        PrimitiveCType<int8_t>)
+MATLAB_PROXY_TRAITS_DEF(arrow::Int16Type, NumericArray<arrow::Int16Type>,
+                        PrimitiveCType<int16_t>)
+MATLAB_PROXY_TRAITS_DEF(arrow::Int32Type, NumericArray<arrow::Int32Type>,
+                        PrimitiveCType<int32_t>)
+MATLAB_PROXY_TRAITS_DEF(arrow::Int64Type, NumericArray<arrow::Int64Type>,
+                        PrimitiveCType<int64_t>)
+MATLAB_PROXY_TRAITS_DEF(arrow::UInt8Type, NumericArray<arrow::UInt8Type>,
+                        PrimitiveCType<uint8_t>)
+MATLAB_PROXY_TRAITS_DEF(arrow::UInt16Type, NumericArray<arrow::UInt16Type>,
+                        PrimitiveCType<uint16_t>)
+MATLAB_PROXY_TRAITS_DEF(arrow::UInt32Type, NumericArray<arrow::UInt32Type>,
+                        PrimitiveCType<uint32_t>)
+MATLAB_PROXY_TRAITS_DEF(arrow::UInt64Type, NumericArray<arrow::UInt64Type>,
+                        PrimitiveCType<uint64_t>)
+MATLAB_PROXY_TRAITS_DEF(arrow::FloatType, NumericArray<arrow::FloatType>,
+                        PrimitiveCType<float>)
+MATLAB_PROXY_TRAITS_DEF(arrow::DoubleType, NumericArray<arrow::DoubleType>,
+                        PrimitiveCType<double>)
+MATLAB_PROXY_TRAITS_DEF(arrow::Time32Type, NumericArray<arrow::Time32Type>, 
Time32Type)
+MATLAB_PROXY_TRAITS_DEF(arrow::Time64Type, NumericArray<arrow::Time64Type>, 
Time64Type)
+MATLAB_PROXY_TRAITS_DEF(arrow::Date32Type, NumericArray<arrow::Date32Type>, 
Date32Type)
+MATLAB_PROXY_TRAITS_DEF(arrow::Date64Type, NumericArray<arrow::Date64Type>, 
Date64Type)
+MATLAB_PROXY_TRAITS_DEF(arrow::TimestampType, 
NumericArray<arrow::TimestampType>,
+                        TimestampType)
+MATLAB_PROXY_TRAITS_DEF(arrow::StringType, StringArray, StringType)
+MATLAB_PROXY_TRAITS_DEF(arrow::ListType, ListArray, ListType)
+MATLAB_PROXY_TRAITS_DEF(arrow::StructType, StructArray, StructType)
+
+#undef MATLAB_PROXY_TRAITS_DEF
+
+template <typename ArrowType>
+std::shared_ptr<typename ProxyTraits<ArrowType>::ArrayProxy> make_proxy(
+    const std::shared_ptr<arrow::Array>& array) {
+  using ArrowArrayType = typename arrow::TypeTraits<ArrowType>::ArrayType;
+  using ArrayProxy = typename ProxyTraits<ArrowType>::ArrayProxy;
+  return 
std::make_shared<ArrayProxy>(std::static_pointer_cast<ArrowArrayType>(array));
+}
+
+template <typename ArrowType>
+std::shared_ptr<typename ProxyTraits<ArrowType>::TypeProxy> make_proxy(
+    const std::shared_ptr<arrow::DataType>& datatype) {
+  using TypeProxy = typename ProxyTraits<ArrowType>::TypeProxy;
+  return 
std::make_shared<TypeProxy>(std::static_pointer_cast<ArrowType>(datatype));
+}
+
+arrow::Type::type get_type_id(const std::shared_ptr<arrow::Array>& array) {
+  return array->type_id();
+}
+
+arrow::Type::type get_type_id(const std::shared_ptr<arrow::DataType>& 
datatype) {
+  return datatype->id();
+}
+
+std::string get_type_string(const std::shared_ptr<arrow::Array>& array) {
+  return array->type()->ToString();
+}
+
+std::string get_type_string(const std::shared_ptr<arrow::DataType>& datatype) {
+  return datatype->ToString();
+}
+
+struct ArrayProxyWrapper {
+  using InputType = arrow::Array;
+  using OutputType = arrow::matlab::array::proxy::Array;
+
+  template <typename ArrowType>
+  std::shared_ptr<OutputType> wrap(const std::shared_ptr<InputType>& input) 
const {
+    return make_proxy<ArrowType>(input);
+  }
+};
+
+struct TypeProxyWrapper {
+  using InputType = arrow::DataType;
+  using OutputType = arrow::matlab::type::proxy::Type;
+
+  template <typename ArrowType>
+  std::shared_ptr<OutputType> wrap(const std::shared_ptr<InputType>& input) 
const {
+    return make_proxy<ArrowType>(input);
+  }
+};
+
+template <typename Wrapper>
+arrow::Result<std::shared_ptr<typename Wrapper::OutputType>> wrap(
+    const std::shared_ptr<typename Wrapper::InputType>& input, const Wrapper& 
wrapper) {
+  using ID = arrow::Type::type;
+  switch (get_type_id(input)) {
+    case ID::BOOL:
+      return wrapper.template wrap<arrow::BooleanType>(input);
+    case ID::UINT8:
+      return wrapper.template wrap<arrow::UInt8Type>(input);
+    case ID::UINT16:
+      return wrapper.template wrap<arrow::UInt16Type>(input);
+    case ID::UINT32:
+      return wrapper.template wrap<arrow::UInt32Type>(input);
+    case ID::UINT64:
+      return wrapper.template wrap<arrow::UInt64Type>(input);
+    case ID::INT8:
+      return wrapper.template wrap<arrow::Int8Type>(input);
+    case ID::INT16:
+      return wrapper.template wrap<arrow::Int16Type>(input);
+    case ID::INT32:
+      return wrapper.template wrap<arrow::Int32Type>(input);
+    case ID::INT64:
+      return wrapper.template wrap<arrow::Int64Type>(input);
+    case ID::FLOAT:
+      return wrapper.template wrap<arrow::FloatType>(input);
+    case ID::DOUBLE:
+      return wrapper.template wrap<arrow::DoubleType>(input);
+    case ID::TIMESTAMP:
+      return wrapper.template wrap<arrow::TimestampType>(input);
+    case ID::TIME32:
+      return wrapper.template wrap<arrow::Time32Type>(input);
+    case ID::TIME64:
+      return wrapper.template wrap<arrow::Time64Type>(input);
+    case ID::DATE32:
+      return wrapper.template wrap<arrow::Date32Type>(input);
+    case ID::DATE64:
+      return wrapper.template wrap<arrow::Date64Type>(input);
+    case ID::STRING:
+      return wrapper.template wrap<arrow::StringType>(input);
+    case ID::LIST:
+      return wrapper.template wrap<arrow::ListType>(input);
+    case ID::STRUCT:
+      return wrapper.template wrap<arrow::StructType>(input);
+    default:
+      return arrow::Status::NotImplemented("Unsupported DataType: " +
+                                           get_type_string(input));
+  }
+}
+
+::matlab::data::StructArray manage_proxy(
+    const std::shared_ptr<libmexclass::proxy::Proxy>& proxy,
+    const arrow::Type::type type_id) {
+  namespace mda = ::matlab::data;
+  mda::ArrayFactory factory;
+  const auto proxy_id = libmexclass::proxy::ProxyManager::manageProxy(proxy);
+  mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", 
"TypeID"});
+  output[0]["ProxyID"] = factory.createScalar(proxy_id);
+  output[0]["TypeID"] = factory.createScalar(static_cast<int32_t>(type_id));
+  return output;
+}
+
+}  // anonymous namespace
+
+arrow::Result<std::shared_ptr<arrow::matlab::array::proxy::Array>> wrap(
+    const std::shared_ptr<arrow::Array>& array) {
+  return wrap(array, ArrayProxyWrapper{});
+}
+
+arrow::Result<::matlab::data::StructArray> wrap_and_manage(
+    const std::shared_ptr<arrow::Array>& array) {
+  ARROW_ASSIGN_OR_RAISE(auto proxy, wrap(array));
+  return manage_proxy(proxy, array->type_id());
+}
+
+arrow::Result<std::shared_ptr<arrow::matlab::type::proxy::Type>> wrap(
+    const std::shared_ptr<arrow::DataType>& datatype) {
+  return wrap(datatype, TypeProxyWrapper{});
+}
+
+arrow::Result<::matlab::data::StructArray> wrap_and_manage(
+    const std::shared_ptr<arrow::DataType>& datatype) {
+  ARROW_ASSIGN_OR_RAISE(auto proxy, wrap(datatype));
+  return manage_proxy(proxy, datatype->id());
+}
+}  // namespace arrow::matlab::proxy
diff --git a/matlab/src/cpp/arrow/matlab/proxy/wrap.h 
b/matlab/src/cpp/arrow/matlab/proxy/wrap.h
new file mode 100644
index 0000000000..59c84eb0e1
--- /dev/null
+++ b/matlab/src/cpp/arrow/matlab/proxy/wrap.h
@@ -0,0 +1,54 @@
+// 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.
+
+#pragma once
+
+#include "MatlabDataArray.hpp"
+#include "arrow/array.h"
+#include "arrow/matlab/array/proxy/array.h"
+#include "arrow/matlab/type/proxy/type.h"
+#include "arrow/result.h"
+
+namespace arrow::matlab::proxy {
+
+/// \brief Wraps an array within a proxy::Array.
+///
+/// \return arrow::result<std::shared_ptr<arrow::matlab::array::proxy::Array>>
+arrow::Result<std::shared_ptr<arrow::matlab::array::proxy::Array>> wrap(
+    const std::shared_ptr<arrow::Array>& array);
+
+/// \brief Wraps an array within a proxy::Array and adds the Proxy to the 
ProxyManager.
+///
+/// \return arrow::Result<mda::StructArray>. The mda::StructArray has two 
fields: ProxyID
+/// (uint64) and TypeID (int32).
+arrow::Result<::matlab::data::StructArray> wrap_and_manage(
+    const std::shared_ptr<arrow::Array>& array);
+
+/// \brief Wraps a DataType within a proxy::DataType.
+///
+/// \return arrow::result<std::shared_ptr<arrow::matlab::type::proxy::Type>>
+arrow::Result<std::shared_ptr<arrow::matlab::type::proxy::Type>> wrap(
+    const std::shared_ptr<arrow::DataType>& datatype);
+
+/// \brief Wraps a DataType within a proxy::DataType and adds the proxy to the
+/// ProxyManager.
+///
+/// \return arrow::Result<mda::StructArray>. The mda::StructArray has two 
fields: ProxyID
+/// (uint64) and TypeID (int32).
+arrow::Result<::matlab::data::StructArray> wrap_and_manage(
+    const std::shared_ptr<arrow::DataType>& datatype);
+}  // namespace arrow::matlab::proxy
diff --git a/matlab/src/cpp/arrow/matlab/tabular/proxy/record_batch.cc 
b/matlab/src/cpp/arrow/matlab/tabular/proxy/record_batch.cc
index f3cee25a3a..acf2421654 100644
--- a/matlab/src/cpp/arrow/matlab/tabular/proxy/record_batch.cc
+++ b/matlab/src/cpp/arrow/matlab/tabular/proxy/record_batch.cc
@@ -15,13 +15,12 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#include "arrow/matlab/tabular/proxy/record_batch.h"
 #include "arrow/c/bridge.h"
 #include "arrow/matlab/array/proxy/array.h"
-#include "arrow/matlab/array/proxy/wrap.h"
-
 #include "arrow/matlab/error/error.h"
+#include "arrow/matlab/proxy/wrap.h"
 #include "arrow/matlab/tabular/get_row_as_string.h"
-#include "arrow/matlab/tabular/proxy/record_batch.h"
 #include "arrow/matlab/tabular/proxy/schema.h"
 #include "arrow/type.h"
 #include "arrow/util/utf8.h"
@@ -159,8 +158,6 @@ void 
RecordBatch::getColumnNames(libmexclass::proxy::method::Context& context) {
 
 void RecordBatch::getColumnByIndex(libmexclass::proxy::method::Context& 
context) {
   namespace mda = ::matlab::data;
-  using namespace libmexclass::proxy;
-  mda::ArrayFactory factory;
 
   mda::StructArray args = context.inputs[0];
   const mda::TypedArray<int32_t> index_mda = args[0]["Index"];
@@ -182,23 +179,13 @@ void 
RecordBatch::getColumnByIndex(libmexclass::proxy::method::Context& context)
   }
 
   const auto array = record_batch->column(index);
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto array_proxy,
-                                      
arrow::matlab::array::proxy::wrap(array), context,
-                                      error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
-
-  const auto array_proxy_id = ProxyManager::manageProxy(array_proxy);
-  const auto array_proxy_id_mda = factory.createScalar(array_proxy_id);
-  const auto array_type_id_mda =
-      factory.createScalar(static_cast<int32_t>(array->type_id()));
-
-  context.outputs[0] = array_proxy_id_mda;
-  context.outputs[1] = array_type_id_mda;
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(context.outputs[0],
+                                      
arrow::matlab::proxy::wrap_and_manage(array),
+                                      context, 
error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
 }
 
 void RecordBatch::getColumnByName(libmexclass::proxy::method::Context& 
context) {
   namespace mda = ::matlab::data;
-  using namespace libmexclass::proxy;
-  mda::ArrayFactory factory;
 
   mda::StructArray args = context.inputs[0];
   const mda::StringArray name_mda = args[0]["Name"];
@@ -213,17 +200,9 @@ void 
RecordBatch::getColumnByName(libmexclass::proxy::method::Context& context)
                                       
error::ARROW_TABULAR_SCHEMA_AMBIGUOUS_FIELD_NAME);
 
   const auto array = record_batch->GetColumnByName(name);
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto array_proxy,
-                                      
arrow::matlab::array::proxy::wrap(array), context,
-                                      error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
-
-  const auto array_proxy_id = ProxyManager::manageProxy(array_proxy);
-  const auto array_proxy_id_mda = factory.createScalar(array_proxy_id);
-  const auto array_type_id_mda =
-      factory.createScalar(static_cast<int32_t>(array->type_id()));
-
-  context.outputs[0] = array_proxy_id_mda;
-  context.outputs[1] = array_type_id_mda;
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(context.outputs[0],
+                                      
arrow::matlab::proxy::wrap_and_manage(array),
+                                      context, 
error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);
 }
 
 void RecordBatch::getSchema(libmexclass::proxy::method::Context& context) {
diff --git a/matlab/src/cpp/arrow/matlab/tabular/proxy/table.cc 
b/matlab/src/cpp/arrow/matlab/tabular/proxy/table.cc
index ff7de96e34..a237edea64 100644
--- a/matlab/src/cpp/arrow/matlab/tabular/proxy/table.cc
+++ b/matlab/src/cpp/arrow/matlab/tabular/proxy/table.cc
@@ -20,7 +20,6 @@
 #include "arrow/matlab/array/proxy/array.h"
 
 #include "arrow/matlab/array/proxy/chunked_array.h"
-#include "arrow/matlab/array/proxy/wrap.h"
 
 #include "arrow/matlab/error/error.h"
 #include "arrow/matlab/tabular/get_row_as_string.h"
diff --git a/matlab/src/cpp/arrow/matlab/type/proxy/field.cc 
b/matlab/src/cpp/arrow/matlab/type/proxy/field.cc
index 96de037010..33c883b770 100644
--- a/matlab/src/cpp/arrow/matlab/type/proxy/field.cc
+++ b/matlab/src/cpp/arrow/matlab/type/proxy/field.cc
@@ -18,12 +18,11 @@
 #include "arrow/util/utf8.h"
 
 #include "arrow/matlab/error/error.h"
+#include "arrow/matlab/proxy/wrap.h"
 #include "arrow/matlab/type/proxy/field.h"
-
 #include "arrow/matlab/type/proxy/primitive_ctype.h"
 #include "arrow/matlab/type/proxy/string_type.h"
 #include "arrow/matlab/type/proxy/timestamp_type.h"
-#include "arrow/matlab/type/proxy/wrap.h"
 
 #include "libmexclass/proxy/ProxyManager.h"
 
@@ -49,19 +48,10 @@ void Field::getName(libmexclass::proxy::method::Context& 
context) {
 }
 
 void Field::getType(libmexclass::proxy::method::Context& context) {
-  namespace mda = ::matlab::data;
-
   const auto& datatype = field->type();
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto proxy, type::proxy::wrap(datatype), 
context,
-                                      
error::FIELD_FAILED_TO_CREATE_TYPE_PROXY);
-  const auto proxy_id = libmexclass::proxy::ProxyManager::manageProxy(proxy);
-  const auto type_id = static_cast<int32_t>(datatype->id());
-
-  mda::ArrayFactory factory;
-  mda::StructArray output = factory.createStructArray({1, 1}, {"ProxyID", 
"TypeID"});
-  output[0]["ProxyID"] = factory.createScalar(proxy_id);
-  output[0]["TypeID"] = factory.createScalar(type_id);
-  context.outputs[0] = output;
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(context.outputs[0],
+                                      
arrow::matlab::proxy::wrap_and_manage(datatype),
+                                      context, 
error::FIELD_FAILED_TO_CREATE_TYPE_PROXY);
 }
 
 libmexclass::proxy::MakeResult Field::make(
diff --git a/matlab/src/cpp/arrow/matlab/type/proxy/list_type.cc 
b/matlab/src/cpp/arrow/matlab/type/proxy/list_type.cc
index d2a9b08590..bbae7f4b94 100644
--- a/matlab/src/cpp/arrow/matlab/type/proxy/list_type.cc
+++ b/matlab/src/cpp/arrow/matlab/type/proxy/list_type.cc
@@ -17,7 +17,8 @@
 
 #include "arrow/matlab/type/proxy/list_type.h"
 #include "arrow/matlab/error/error.h"
-#include "arrow/matlab/type/proxy/wrap.h"
+#include "arrow/matlab/proxy/wrap.h"
+
 #include "libmexclass/proxy/ProxyManager.h"
 
 namespace arrow::matlab::type::proxy {
@@ -28,25 +29,12 @@ ListType::ListType(std::shared_ptr<arrow::ListType> 
list_type)
 }
 
 void ListType::getValueType(libmexclass::proxy::method::Context& context) {
-  namespace mda = ::matlab::data;
-  mda::ArrayFactory factory;
-
   const auto list_type = std::static_pointer_cast<arrow::ListType>(data_type);
   const auto value_type = list_type->value_type();
-  const auto value_type_id = static_cast<int32_t>(value_type->id());
-
-  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto value_type_proxy,
-                                      type::proxy::wrap(value_type), context,
-                                      
error::LIST_TYPE_FAILED_TO_CREATE_VALUE_TYPE_PROXY);
-  const auto value_type_proxy_id =
-      libmexclass::proxy::ProxyManager::manageProxy(value_type_proxy);
-
-  mda::StructArray output =
-      factory.createStructArray({1, 1}, {"ValueTypeProxyID", "ValueTypeID"});
-  output[0]["ValueTypeProxyID"] = factory.createScalar(value_type_proxy_id);
-  output[0]["ValueTypeID"] = factory.createScalar(value_type_id);
 
-  context.outputs[0] = output;
+  MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(
+      context.outputs[0], arrow::matlab::proxy::wrap_and_manage(value_type), 
context,
+      error::LIST_TYPE_FAILED_TO_CREATE_VALUE_TYPE_PROXY);
 }
 
 libmexclass::proxy::MakeResult ListType::make(
diff --git a/matlab/src/cpp/arrow/matlab/type/proxy/wrap.cc 
b/matlab/src/cpp/arrow/matlab/type/proxy/wrap.cc
deleted file mode 100644
index bc2921908c..0000000000
--- a/matlab/src/cpp/arrow/matlab/type/proxy/wrap.cc
+++ /dev/null
@@ -1,96 +0,0 @@
-// 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 "arrow/matlab/type/proxy/wrap.h"
-
-#include "arrow/matlab/type/proxy/date32_type.h"
-#include "arrow/matlab/type/proxy/date64_type.h"
-#include "arrow/matlab/type/proxy/list_type.h"
-#include "arrow/matlab/type/proxy/primitive_ctype.h"
-#include "arrow/matlab/type/proxy/string_type.h"
-#include "arrow/matlab/type/proxy/struct_type.h"
-#include "arrow/matlab/type/proxy/time32_type.h"
-#include "arrow/matlab/type/proxy/time64_type.h"
-#include "arrow/matlab/type/proxy/timestamp_type.h"
-
-namespace arrow::matlab::type::proxy {
-
-arrow::Result<std::shared_ptr<type::proxy::Type>> wrap(
-    const std::shared_ptr<arrow::DataType>& type) {
-  using ID = arrow::Type::type;
-  switch (type->id()) {
-    case ID::BOOL:
-      return std::make_shared<PrimitiveCType<bool>>(
-          std::static_pointer_cast<arrow::BooleanType>(type));
-    case ID::UINT8:
-      return std::make_shared<PrimitiveCType<uint8_t>>(
-          std::static_pointer_cast<arrow::UInt8Type>(type));
-    case ID::UINT16:
-      return std::make_shared<PrimitiveCType<uint16_t>>(
-          std::static_pointer_cast<arrow::UInt16Type>(type));
-    case ID::UINT32:
-      return std::make_shared<PrimitiveCType<uint32_t>>(
-          std::static_pointer_cast<arrow::UInt32Type>(type));
-    case ID::UINT64:
-      return std::make_shared<PrimitiveCType<uint64_t>>(
-          std::static_pointer_cast<arrow::UInt64Type>(type));
-    case ID::INT8:
-      return std::make_shared<PrimitiveCType<int8_t>>(
-          std::static_pointer_cast<arrow::Int8Type>(type));
-    case ID::INT16:
-      return std::make_shared<PrimitiveCType<int16_t>>(
-          std::static_pointer_cast<arrow::Int16Type>(type));
-    case ID::INT32:
-      return std::make_shared<PrimitiveCType<int32_t>>(
-          std::static_pointer_cast<arrow::Int32Type>(type));
-    case ID::INT64:
-      return std::make_shared<PrimitiveCType<int64_t>>(
-          std::static_pointer_cast<arrow::Int64Type>(type));
-    case ID::FLOAT:
-      return std::make_shared<PrimitiveCType<float>>(
-          std::static_pointer_cast<arrow::FloatType>(type));
-    case ID::DOUBLE:
-      return std::make_shared<PrimitiveCType<double>>(
-          std::static_pointer_cast<arrow::DoubleType>(type));
-    case ID::TIMESTAMP:
-      return std::make_shared<TimestampType>(
-          std::static_pointer_cast<arrow::TimestampType>(type));
-    case ID::TIME32:
-      return std::make_shared<Time32Type>(
-          std::static_pointer_cast<arrow::Time32Type>(type));
-    case ID::TIME64:
-      return std::make_shared<Time64Type>(
-          std::static_pointer_cast<arrow::Time64Type>(type));
-    case ID::DATE32:
-      return std::make_shared<Date32Type>(
-          std::static_pointer_cast<arrow::Date32Type>(type));
-    case ID::DATE64:
-      return std::make_shared<Date64Type>(
-          std::static_pointer_cast<arrow::Date64Type>(type));
-    case ID::STRING:
-      return std::make_shared<StringType>(
-          std::static_pointer_cast<arrow::StringType>(type));
-    case ID::LIST:
-      return 
std::make_shared<ListType>(std::static_pointer_cast<arrow::ListType>(type));
-    case ID::STRUCT:
-      return std::make_shared<StructType>(
-          std::static_pointer_cast<arrow::StructType>(type));
-    default:
-      return arrow::Status::NotImplemented("Unsupported DataType: " + 
type->ToString());
-  }
-}
-}  // namespace arrow::matlab::type::proxy
diff --git a/matlab/src/cpp/arrow/matlab/type/proxy/wrap.h 
b/matlab/src/cpp/arrow/matlab/type/proxy/wrap.h
deleted file mode 100644
index 7d433b3e49..0000000000
--- a/matlab/src/cpp/arrow/matlab/type/proxy/wrap.h
+++ /dev/null
@@ -1,29 +0,0 @@
-// 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.
-
-#pragma once
-
-#include "arrow/result.h"
-#include "arrow/type.h"
-
-#include "arrow/matlab/type/proxy/type.h"
-
-namespace arrow::matlab::type::proxy {
-
-arrow::Result<std::shared_ptr<Type>> wrap(const 
std::shared_ptr<arrow::DataType>& type);
-
-}
diff --git a/matlab/src/matlab/+arrow/+c/+internal/ArrayImporter.m 
b/matlab/src/matlab/+arrow/+c/+internal/ArrayImporter.m
index 3f2f7445b3..1b230a536b 100644
--- a/matlab/src/matlab/+arrow/+c/+internal/ArrayImporter.m
+++ b/matlab/src/matlab/+arrow/+c/+internal/ArrayImporter.m
@@ -38,9 +38,9 @@ classdef ArrayImporter < matlab.mixin.Scalar
                 ArrowArrayAddress=cArray.Address,...
                 ArrowSchemaAddress=cSchema.Address...
             );
-            [proxyID, typeID] = obj.Proxy.import(args);
-            traits = arrow.type.traits.traits(arrow.type.ID(typeID));
-            proxy = libmexclass.proxy.Proxy(Name=traits.ArrayProxyClassName, 
ID=proxyID);
+            proxyInfo = obj.Proxy.import(args);
+            traits = arrow.type.traits.traits(arrow.type.ID(proxyInfo.TypeID));
+            proxy = libmexclass.proxy.Proxy(Name=traits.ArrayProxyClassName, 
ID=proxyInfo.ProxyID);
             array = traits.ArrayConstructor(proxy);
         end
 
diff --git a/matlab/src/matlab/+arrow/+tabular/RecordBatch.m 
b/matlab/src/matlab/+arrow/+tabular/RecordBatch.m
index da5c1fc1c3..5c66c66c26 100644
--- a/matlab/src/matlab/+arrow/+tabular/RecordBatch.m
+++ b/matlab/src/matlab/+arrow/+tabular/RecordBatch.m
@@ -65,14 +65,14 @@ classdef RecordBatch < matlab.mixin.CustomDisplay & ...
 
             if isnumeric(idx)
                 args = struct(Index=idx);
-                [proxyID, typeID] = obj.Proxy.getColumnByIndex(args);
+                proxyInfo = obj.Proxy.getColumnByIndex(args);
             else
                 args = struct(Name=idx);
-                [proxyID, typeID] = obj.Proxy.getColumnByName(args);
+                proxyInfo = obj.Proxy.getColumnByName(args);
             end
             
-            traits = arrow.type.traits.traits(arrow.type.ID(typeID));
-            proxy = libmexclass.proxy.Proxy(Name=traits.ArrayProxyClassName, 
ID=proxyID);
+            traits = arrow.type.traits.traits(arrow.type.ID(proxyInfo.TypeID));
+            proxy = libmexclass.proxy.Proxy(Name=traits.ArrayProxyClassName, 
ID=proxyInfo.ProxyID);
             arrowArray = traits.ArrayConstructor(proxy);
         end
 
diff --git a/matlab/src/matlab/+arrow/+type/ListType.m 
b/matlab/src/matlab/+arrow/+type/ListType.m
index 4166cfd5a8..0133508ea4 100644
--- a/matlab/src/matlab/+arrow/+type/ListType.m
+++ b/matlab/src/matlab/+arrow/+type/ListType.m
@@ -41,9 +41,9 @@ classdef ListType < arrow.type.Type
 
     methods
         function valueType = get.ValueType(obj)
-            valueTypeStruct = obj.Proxy.getValueType();
-            traits = 
arrow.type.traits.traits(arrow.type.ID(valueTypeStruct.ValueTypeID));
-            proxy = libmexclass.proxy.Proxy(Name=traits.TypeProxyClassName, 
ID=valueTypeStruct.ValueTypeProxyID);
+            proxyInfo = obj.Proxy.getValueType();
+            traits = arrow.type.traits.traits(arrow.type.ID(proxyInfo.TypeID));
+            proxy = libmexclass.proxy.Proxy(Name=traits.TypeProxyClassName, 
ID=proxyInfo.ProxyID);
             valueType = traits.TypeConstructor(proxy);
         end
     end
diff --git a/matlab/tools/cmake/BuildMatlabArrowInterface.cmake 
b/matlab/tools/cmake/BuildMatlabArrowInterface.cmake
index 8dd34f3a61..8cc7b78f7c 100644
--- a/matlab/tools/cmake/BuildMatlabArrowInterface.cmake
+++ b/matlab/tools/cmake/BuildMatlabArrowInterface.cmake
@@ -49,7 +49,6 @@ set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_SOURCES 
"${CMAKE_SOURCE_DIR}/src/cpp/a
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/array/proxy/struct_array.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/array/proxy/list_array.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/array/proxy/chunked_array.cc"
-                                                  
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/array/proxy/wrap.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/tabular/proxy/record_batch.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/tabular/proxy/table.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/tabular/proxy/schema.cc"
@@ -69,7 +68,6 @@ set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_SOURCES 
"${CMAKE_SOURCE_DIR}/src/cpp/a
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/struct_type.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/list_type.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/field.cc"
-                                                  
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/wrap.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/feather/proxy/writer.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/feather/proxy/reader.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/csv/proxy/table_writer.cc"
@@ -84,7 +82,8 @@ set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_SOURCES 
"${CMAKE_SOURCE_DIR}/src/cpp/a
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_writer.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_writer.cc"
                                                   
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_stream_reader.cc"
-                                                  
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_stream_writer.cc")
+                                                  
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_stream_writer.cc"
+                                                  
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/proxy/wrap.cc")
 
 
 set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_FACTORY_INCLUDE_DIR 
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/proxy")

Reply via email to