Skip to content

Commit c31d05a

Browse files
committed
Fix push_back with named arguments
It should now work much more consistently than previously Fixes #86
1 parent f94de80 commit c31d05a

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# cpp11 (development version)
22

3+
* `push_back()` now works more consistently with named arguments (#86)
4+
35
# cpp11 0.2.0
46

57
## New features

cpp11test/src/test-list.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,64 @@ context("list-C++") {
4747
expect_true(fifth[2] == 'c');
4848
}
4949

50+
using namespace cpp11::literals;
51+
test_that("unnamed_list.push_back(unnamed_arg)") {
52+
cpp11::writable::list x(1);
53+
x.push_back(cpp11::writable::integers(2));
54+
55+
expect_true(x.names() == R_NilValue);
56+
}
57+
58+
test_that("unnamed_list.push_back(named_arg)") {
59+
cpp11::writable::list x(1);
60+
x.push_back("bar"_nm = 2);
61+
62+
cpp11::strings nms(x.names());
63+
64+
expect_true(nms.size() == 2);
65+
expect_true(nms[0] == "");
66+
expect_true(nms[1] == "bar");
67+
}
68+
69+
test_that("named_list.push_back(unnamed_arg)") {
70+
cpp11::writable::list x({"foo"_nm = 1});
71+
x.push_back(cpp11::writable::integers(2));
72+
73+
cpp11::strings nms(x.names());
74+
75+
expect_true(nms.size() == 2);
76+
expect_true(nms[0] == "foo");
77+
expect_true(nms[1] == "");
78+
}
79+
80+
test_that("named_list.push_back(named_arg)") {
81+
cpp11::writable::list x({"foo"_nm = 1});
82+
x.push_back({"bar"_nm = 2});
83+
84+
cpp11::strings nms(x.names());
85+
86+
expect_true(nms.size() == 2);
87+
expect_true(nms[0] == "foo");
88+
expect_true(nms[1] == "bar");
89+
}
90+
91+
test_that("empty_list.push_back(unnamed_arg)") {
92+
cpp11::writable::list x;
93+
x.push_back(cpp11::writable::integers(2));
94+
95+
expect_true(x.names() == R_NilValue);
96+
}
97+
98+
test_that("empty_list.push_back(named_arg)") {
99+
cpp11::writable::list x;
100+
x.push_back({"bar"_nm = 2});
101+
102+
cpp11::strings nms(x.names());
103+
104+
expect_true(nms.size() == 1);
105+
expect_true(nms[0] == "bar");
106+
}
107+
50108
test_that("attribute setting works") {
51109
cpp11::writable::list x(
52110
{cpp11::writable::doubles({1, 2, 3}), cpp11::writable::strings({"x", "y", "z"})});

inst/include/cpp11/r_vector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ class r_vector : public cpp11::r_vector<T> {
330330

331331
using cpp11::r_vector<T>::cbegin;
332332
using cpp11::r_vector<T>::cend;
333+
using cpp11::r_vector<T>::size;
333334

334335
iterator find(const r_string& name) const;
335336

inst/include/cpp11/strings.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,12 @@ typedef r_vector<r_string> strings;
172172
template <typename T>
173173
inline void r_vector<T>::push_back(const named_arg& value) {
174174
push_back(value.value());
175+
if (Rf_xlength(names()) == 0) {
176+
cpp11::writable::strings new_nms(size());
177+
names() = new_nms;
178+
}
175179
cpp11::writable::strings nms(names());
176-
nms.push_back(value.name());
180+
nms[size() - 1] = value.name();
177181
}
178182

179183
} // namespace writable

0 commit comments

Comments
 (0)