Skip to content

Commit 033ab9f

Browse files
using struct input_parameter<const std::shared_ptr<T>& instead of the heavier Exporter class
1 parent df4eb30 commit 033ab9f

7 files changed

Lines changed: 63 additions & 48 deletions

File tree

r/src/RcppExports.cpp

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/src/arrow_types.h

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,54 @@ struct NoDelete {
3737
inline void operator()(T* ptr){};
3838
};
3939

40+
namespace arrow {
41+
namespace r {
42+
struct symbols {
43+
static SEXP units;
44+
static SEXP xp;
45+
};
46+
}
47+
}
48+
4049
namespace Rcpp {
50+
namespace internal {
51+
52+
template <typename Pointer>
53+
Pointer r6_to_smart_pointer(SEXP self) {
54+
return reinterpret_cast<Pointer>(EXTPTR_PTR(Rf_findVarInFrame(self, arrow::r::symbols::xp)));
55+
}
56+
57+
}
58+
59+
template <typename T>
60+
class ConstReferenceSmartPtrInputParameter {
61+
public:
62+
using const_reference = const T&;
63+
64+
ConstReferenceSmartPtrInputParameter(SEXP self) :
65+
ptr(internal::r6_to_smart_pointer<const T*>(self))
66+
{}
67+
68+
inline operator const_reference(){ return *ptr; }
69+
70+
private:
71+
const T* ptr;
72+
};
73+
4174
namespace traits {
4275

76+
template <typename T>
77+
struct input_parameter<const std::shared_ptr<T>&> {
78+
typedef typename Rcpp::ConstReferenceSmartPtrInputParameter<std::shared_ptr<T>> type ;
79+
} ;
80+
4381
struct wrap_type_shared_ptr_tag {};
4482

4583
template <typename T>
4684
struct wrap_type_traits<std::shared_ptr<T>> {
4785
using wrap_category = wrap_type_shared_ptr_tag;
4886
};
4987

50-
template <typename T>
51-
class Exporter<std::shared_ptr<T>>;
52-
5388
} // namespace traits
5489
namespace internal {
5590

@@ -69,26 +104,6 @@ RCPP_EXPOSED_ENUM_NODECL(arrow::StatusCode)
69104
RCPP_EXPOSED_ENUM_NODECL(arrow::io::FileMode::type)
70105

71106
namespace Rcpp {
72-
namespace traits {
73-
74-
template <typename T>
75-
class Exporter<std::shared_ptr<T>> {
76-
public:
77-
Exporter(SEXP self) : xp(extract_xp(self)) {}
78-
79-
inline std::shared_ptr<T> get() { return *Rcpp::XPtr<std::shared_ptr<T>>(xp); }
80-
81-
private:
82-
SEXP xp;
83-
84-
SEXP extract_xp(SEXP self) {
85-
static SEXP symb_xp = Rf_install(".:xp:.");
86-
return Rf_findVarInFrame(self, symb_xp);
87-
}
88-
};
89-
90-
} // namespace traits
91-
92107
namespace internal {
93108

94109
template <typename T>
@@ -150,9 +165,5 @@ class RBuffer : public MutableBuffer {
150165
Vec vec_;
151166
};
152167

153-
struct symbols {
154-
static SEXP units;
155-
};
156-
157168
} // namespace r
158169
} // namespace arrow

r/src/datatype.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ std::shared_ptr<arrow::DataType> Time64__initialize(arrow::TimeUnit::type unit)
107107
// [[Rcpp::export]]
108108
SEXP list__(SEXP x) {
109109
if (Rf_inherits(x, "arrow::Field")) {
110-
return wrap(arrow::list(Rcpp::as<std::shared_ptr<arrow::Field>>(x)));
110+
Rcpp::ConstReferenceSmartPtrInputParameter<std::shared_ptr<arrow::Field>> field(x);
111+
return wrap(arrow::list(field));
111112
}
112113

113114
if (Rf_inherits(x, "arrow::DataType")) {
114-
return wrap(arrow::list(Rcpp::as<std::shared_ptr<arrow::DataType>>(x)));
115+
Rcpp::ConstReferenceSmartPtrInputParameter<std::shared_ptr<arrow::DataType>> type(x);
116+
return wrap(arrow::list(type));
115117
}
116118

117119
stop("incompatible");
@@ -122,7 +124,8 @@ template <typename T>
122124
std::vector<std::shared_ptr<T>> List_to_shared_ptr_vector(List x) {
123125
std::vector<std::shared_ptr<T>> vec;
124126
for (SEXP element : x) {
125-
vec.push_back(as<std::shared_ptr<T>>(element));
127+
Rcpp::ConstReferenceSmartPtrInputParameter<std::shared_ptr<T>> ptr(element);
128+
vec.push_back(ptr);
126129
}
127130
return vec;
128131
}

r/src/field.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ std::string Field__ToString(const std::shared_ptr<arrow::Field>& type) {
3434
}
3535

3636
// [[Rcpp::export]]
37-
std::string Field__name(std::shared_ptr<arrow::Field> type) { return type->name(); }
37+
std::string Field__name(const std::shared_ptr<arrow::Field>& type) { return type->name(); }
3838

3939
// [[Rcpp::export]]
40-
bool Field__nullable(std::shared_ptr<arrow::Field> type) { return type->nullable(); }
40+
bool Field__nullable(const std::shared_ptr<arrow::Field>& type) { return type->nullable(); }

r/src/memorypool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ std::shared_ptr<arrow::MemoryPool> MemoryPool__default() {
2626
}
2727

2828
// [[Rcpp::export]]
29-
int MemoryPool__bytes_allocated(std::shared_ptr<arrow::MemoryPool> pool) {
29+
int MemoryPool__bytes_allocated(const std::shared_ptr<arrow::MemoryPool>& pool) {
3030
return pool->bytes_allocated();
3131
}
3232

3333
// [[Rcpp::export]]
34-
int MemoryPool__max_memory(std::shared_ptr<arrow::MemoryPool> pool) {
34+
int MemoryPool__max_memory(const std::shared_ptr<arrow::MemoryPool>& pool) {
3535
return pool->max_memory();
3636
}

r/src/recordbatch.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ CharacterVector RecordBatch__names(const std::shared_ptr<arrow::RecordBatch>& ba
111111

112112
// [[Rcpp::export]]
113113
std::shared_ptr<arrow::RecordBatch> RecordBatch__Slice1(
114-
std::shared_ptr<arrow::RecordBatch>& self, int offset) {
114+
const std::shared_ptr<arrow::RecordBatch>& self, int offset) {
115115
return self->Slice(offset);
116116
}
117117

118118
// [[Rcpp::export]]
119119
std::shared_ptr<arrow::RecordBatch> RecordBatch__Slice2(
120-
std::shared_ptr<arrow::RecordBatch>& self, int offset, int length) {
120+
const std::shared_ptr<arrow::RecordBatch>& self, int offset, int length) {
121121
return self->Slice(offset, length);
122122
}

r/src/symbols.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
namespace arrow {
2121
namespace r {
2222
SEXP symbols::units = Rf_install("units");
23+
SEXP symbols::xp = Rf_install(".:xp:.");
2324
}
2425
} // namespace arrow

0 commit comments

Comments
 (0)