Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ Please note that the cpp11 project is released with a [Contributor Code of Condu

cpp11 would not exist without Rcpp.
Thanks to the Rcpp authors, Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey, Qiang Kou, Nathan Russell, Douglas Bates and John Chambers for their work writing and maintaining Rcpp.

## Clang format

To match GHA, use clang-format-12 to format C++ code. With systems that provide clang-format-14 or newer, you can use Docker:

```bash
docker run --rm -v "$PWD":/work -w /work ubuntu:22.04 bash -lc "\
apt-get update && apt-get install -y clang-format-12 && \
find . -name '*.cpp' -o -name '*.hpp' -o -name '*.h' | xargs -r clang-format-12 -i"
```
60 changes: 26 additions & 34 deletions inst/include/cpp11/list_of.hpp
Original file line number Diff line number Diff line change
@@ -1,57 +1,51 @@
#pragma once

#include <string> // for string, basic_string
#include <string> // for string, basic_string

#include "cpp11/R.hpp" // for R_xlen_t, SEXP, SEXPREC, LONG_VECTOR_SUPPORT
#include "cpp11/list.hpp" // for list
#include "cpp11/R.hpp" // for R_xlen_t, SEXP, SEXPREC, LONG_VECTOR_SUPPORT
#include "cpp11/list.hpp" // for list

namespace cpp11 {

template <typename T> class list_of : public list {
public:
list_of(const list &data) : list(data) {}
template <typename T>
class list_of : public list {
public:
list_of(const list& data) : list(data) {}

#ifdef LONG_VECTOR_SUPPORT
T operator[](const int pos) const {
return operator[](static_cast<R_xlen_t>(pos));
}
T operator[](const int pos) const { return operator[](static_cast<R_xlen_t>(pos)); }
#endif

T operator[](const R_xlen_t pos) const { return list::operator[](pos); }

T operator[](const char *pos) const { return list::operator[](pos); }
T operator[](const char* pos) const { return list::operator[](pos); }

T operator[](const std::string &pos) const {
return list::operator[](pos.c_str());
}
T operator[](const std::string& pos) const { return list::operator[](pos.c_str()); }
};

namespace writable {
template <typename T> class list_of : public writable::list {
public:
list_of(const list &data) : writable::list(data) {}
template <typename T>
class list_of : public writable::list {
public:
list_of(const list& data) : writable::list(data) {}
list_of(R_xlen_t n) : writable::list(n) {}

class proxy {
private:
private:
writable::list::proxy data_;

public:
proxy(const writable::list::proxy &data) : data_(data) {}
public:
proxy(const writable::list::proxy& data) : data_(data) {}

operator T() const { return static_cast<SEXP>(*this); }
operator SEXP() const { return static_cast<SEXP>(data_); }
#ifdef LONG_VECTOR_SUPPORT
typename T::proxy operator[](int pos) { return static_cast<T>(data_)[pos]; }
#endif
typename T::proxy operator[](R_xlen_t pos) {
return static_cast<T>(data_)[pos];
}
proxy operator[](const char *pos) { static_cast<T>(data_)[pos]; }
proxy operator[](const std::string &pos) {
return static_cast<T>(data_)[pos];
}
proxy &operator=(const T &rhs) {
typename T::proxy operator[](R_xlen_t pos) { return static_cast<T>(data_)[pos]; }
proxy operator[](const char* pos) { static_cast<T>(data_)[pos]; }
proxy operator[](const std::string& pos) { return static_cast<T>(data_)[pos]; }
proxy& operator=(const T& rhs) {
data_ = rhs;

return *this;
Expand All @@ -60,20 +54,18 @@ template <typename T> class list_of : public writable::list {

#ifdef LONG_VECTOR_SUPPORT
proxy operator[](int pos) {
return { writable::list::operator[](static_cast<R_xlen_t>(pos)) };
return {writable::list::operator[](static_cast<R_xlen_t>(pos))};
}
#endif

proxy operator[](R_xlen_t pos) { return writable::list::operator[](pos); }

proxy operator[](const char *pos) {
return { writable::list::operator[](pos) };
}
proxy operator[](const char* pos) { return {writable::list::operator[](pos)}; }

proxy operator[](const std::string &pos) {
proxy operator[](const std::string& pos) {
return writable::list::operator[](pos.c_str());
}
};
} // namespace writable
} // namespace writable

} // namespace cpp11
} // namespace cpp11
Loading