Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
paleolimbot committed Dec 18, 2023
1 parent bb9cd60 commit e5a4629
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions inst/include/cpp11/as.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,21 @@ template <typename T>
enable_if_integral<T, T> as_cpp(SEXP from) {
if (Rf_isInteger(from)) {
if (Rf_xlength(from) == 1) {
return INTEGER_ELT(from, 0);
return static_cast<T>(INTEGER_ELT(from, 0));
}
} else if (Rf_isReal(from)) {
if (Rf_xlength(from) == 1) {
if (ISNA(REAL_ELT(from, 0))) {
return NA_INTEGER;
}
double value = REAL_ELT(from, 0);
if (is_convertible_without_loss_to_integer(value)) {
return value;
if (ISNA(value) && sizeof(T) >= sizeof(int)) {
return static_cast<T>(NA_INTEGER);
} else if (!ISNA(value) && is_convertible_without_loss_to_integer(value)) {
return static_cast<T>(value);
}
}
} else if (Rf_isLogical(from)) {
if (Rf_xlength(from) == 1) {
if (LOGICAL_ELT(from, 0) == NA_LOGICAL) {
return NA_INTEGER;
if (LOGICAL_ELT(from, 0) == NA_LOGICAL && sizeof(T) >= sizeof(int)) {
return static_cast<T>(NA_INTEGER);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion inst/include/cpp11/r_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class r_string {
r_string(SEXP data) : data_(data) {}
r_string(const char* data) : data_(safe[Rf_mkCharCE](data, CE_UTF8)) {}
r_string(const std::string& data)
: data_(safe[Rf_mkCharLenCE](data.c_str(), data.size(), CE_UTF8)) {}
: data_(safe[Rf_mkCharLenCE](data.c_str(), static_cast<int>(data.size()), CE_UTF8)) {}

operator SEXP() const { return data_; }
operator sexp() const { return data_; }
Expand Down
2 changes: 1 addition & 1 deletion inst/include/cpp11/sexp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class sexp {

operator SEXP() const { return data_; }
operator double() const { return REAL_ELT(data_, 0); }
operator size_t() const { return REAL_ELT(data_, 0); }
operator size_t() const { return static_cast<size_t>(REAL_ELT(data_, 0)); }
operator bool() const { return LOGICAL_ELT(data_, 0); }
SEXP data() const { return data_; }
};
Expand Down

0 comments on commit e5a4629

Please sign in to comment.