Skip to content

Commit 7623751

Browse files
committed
Added utility for easy registration of new symbols when writing files.
1 parent a6c1c45 commit 7623751

3 files changed

Lines changed: 47 additions & 32 deletions

File tree

examples/src/write_dgCMatrix.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,27 @@ int main () {
1313

1414
auto ivec = std::make_unique<rds2cpp::IntegerVector>();
1515
ivec->data = std::vector<int32_t>{ 6, 8, 0, 3, 5, 6, 0, 1, 3, 7 };
16-
obj.attributes.emplace_back(file_info.symbols.size(), std::move(ivec));
17-
file_info.symbols.emplace_back("i", rds2cpp::StringEncoding::UTF8);
16+
obj.attributes.emplace_back(rds2cpp::register_symbol("i", rds2cpp::StringEncoding::UTF8, file_info.symbols), std::move(ivec));
1817

1918
auto pvec = std::make_unique<rds2cpp::IntegerVector>();
2019
pvec->data = std::vector<int32_t>{ 0, 0, 2, 3, 4, 5, 6, 8, 8, 8, 10 };
21-
obj.attributes.emplace_back(file_info.symbols.size(), std::move(pvec));
22-
file_info.symbols.emplace_back("p", rds2cpp::StringEncoding::UTF8);
20+
obj.attributes.emplace_back(rds2cpp::register_symbol("p", rds2cpp::StringEncoding::UTF8, file_info.symbols), std::move(pvec));
2321

2422
auto xvec = std::make_unique<rds2cpp::DoubleVector>();
2523
xvec->data = std::vector<double>{ 0.96, -0.34, 0.82, -2, -0.072, 0.39, 0.16, 0.36, -1.5, -0.047 };
26-
obj.attributes.emplace_back(file_info.symbols.size(), std::move(xvec));
27-
file_info.symbols.emplace_back("x", rds2cpp::StringEncoding::UTF8);
24+
obj.attributes.emplace_back(rds2cpp::register_symbol("x", rds2cpp::StringEncoding::UTF8, file_info.symbols), std::move(xvec));
2825

2926
auto dims = std::make_unique<rds2cpp::IntegerVector>();
3027
dims->data = std::vector<int32_t>{ 10, 10 };
31-
obj.attributes.emplace_back(file_info.symbols.size(), std::move(dims));
32-
file_info.symbols.emplace_back("Dim", rds2cpp::StringEncoding::UTF8);
28+
obj.attributes.emplace_back(rds2cpp::register_symbol("Dim", rds2cpp::StringEncoding::UTF8, file_info.symbols), std::move(dims));
3329

3430
auto dimnames = std::make_unique<rds2cpp::GenericVector>();
3531
dimnames->data.emplace_back(new rds2cpp::Null);
3632
dimnames->data.emplace_back(new rds2cpp::Null);
37-
obj.attributes.emplace_back(file_info.symbols.size(), std::move(dimnames));
38-
file_info.symbols.emplace_back("Dimnames", rds2cpp::StringEncoding::UTF8);
33+
obj.attributes.emplace_back(rds2cpp::register_symbol("Dimnames", rds2cpp::StringEncoding::UTF8, file_info.symbols), std::move(dimnames));
3934

4035
auto factors = std::make_unique<rds2cpp::GenericVector>();
41-
obj.attributes.emplace_back(file_info.symbols.size(), std::move(factors));
42-
file_info.symbols.emplace_back("factors", rds2cpp::StringEncoding::UTF8);
36+
obj.attributes.emplace_back(rds2cpp::register_symbol("factors", rds2cpp::StringEncoding::UTF8, file_info.symbols), std::move(factors));
4337

4438
rds2cpp::write_rds(file_info, "my_matrix.rds", {});
4539
return 0;

include/rds2cpp/RObject.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ struct SymbolIndex final : public RObject {
102102
std::size_t index = - 1;
103103
};
104104

105+
/**
106+
* @param name Name of the symbol, see `Symbol::name`.
107+
* @param name Encoding of the symbol, see `Symbol::encoding`.
108+
* @param symbols Vector containing the global set of symbols, typically `RdsFile::symbols` or `RdaFile::symbols`.
109+
*
110+
* @return A new symbol is added to `symbols` and a `SymbolIndex` is returned that points to the new entry of `symbols`.
111+
*
112+
* This is a convenient helper to register new symbols in `RdsFile` prior to calling `write_rds()` (or to `RdaFile` before `write_rda()`).
113+
* It returns a `SymbolIndex` that can be used in various fields like `LanguageArgument::name`, `PairListElement::tag`, `EnvironmentVariable::name`, etc.
114+
*/
115+
inline SymbolIndex register_symbol(std::string name, StringEncoding encoding, std::vector<Symbol>& symbols) {
116+
auto idx = symbols.size();
117+
symbols.emplace_back(std::move(name), encoding);
118+
return SymbolIndex(idx);
119+
}
120+
105121
/**
106122
* @cond
107123
*/

tests/src/write.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@
77
#include <string>
88
#include <cstddef>
99

10-
template<class RdxFile_>
11-
static rds2cpp::SymbolIndex prepare_symbol(std::string x, rds2cpp::StringEncoding enc, RdxFile_& globals) {
12-
rds2cpp::SymbolIndex sidx(globals.symbols.size());
13-
globals.symbols.emplace_back(x, enc);
14-
return sidx;
15-
}
16-
1710
template<class RdxFile_>
1811
std::unique_ptr<rds2cpp::RObject> unconvert(const Rcpp::RObject& x, RdxFile_& globals);
1912

@@ -22,8 +15,10 @@ void add_attributes(const Rcpp::RObject& x, RdsObject_* y, RdxFile_& globals) {
2215
const auto& attr_names = x.attributeNames();
2316
auto& attr_dest = y->attributes;
2417
for (const auto& attr : attr_names) {
25-
auto sidx = prepare_symbol(attr, rds2cpp::StringEncoding::ASCII, globals); // just assume the encoding, I guess.
26-
attr_dest.emplace_back(std::move(sidx), unconvert(x.attr(attr), globals));
18+
attr_dest.emplace_back(
19+
rds2cpp::register_symbol(attr, rds2cpp::StringEncoding::ASCII, globals.symbols), // just assume the encoding, I guess.
20+
unconvert(x.attr(attr), globals)
21+
);
2722
}
2823
}
2924

@@ -33,8 +28,10 @@ void add_attributes_except(const Rcpp::RObject& x, RdsObject_* y, RdxFile_& glob
3328
auto& attr_dest = y->attributes;
3429
for (const auto& attr : attr_names) {
3530
if (excluded.find(attr) == excluded.end()) {
36-
auto sidx = prepare_symbol(attr, rds2cpp::StringEncoding::ASCII, globals); // just assume the encoding, I guess.
37-
attr_dest.emplace_back(std::move(sidx), unconvert(x.attr(attr), globals));
31+
attr_dest.emplace_back(
32+
rds2cpp::register_symbol(attr, rds2cpp::StringEncoding::ASCII, globals.symbols), // just assume the encoding, I guess.
33+
unconvert(x.attr(attr), globals)
34+
);
3835
}
3936
}
4037
}
@@ -133,8 +130,10 @@ std::unique_ptr<rds2cpp::RObject> unconvert(const Rcpp::RObject& x, RdxFile_& gl
133130
if (curname == "") {
134131
ptr->data.emplace_back(std::move(unc));
135132
} else {
136-
auto sidx = prepare_symbol(std::move(curname), rds2cpp::StringEncoding::ASCII, globals);
137-
ptr->data.emplace_back(std::move(sidx), std::move(unc));
133+
ptr->data.emplace_back(
134+
rds2cpp::register_symbol(std::move(curname), rds2cpp::StringEncoding::ASCII, globals.symbols),
135+
std::move(unc)
136+
);
138137
}
139138
}
140139
add_attributes(x, ptr, globals);
@@ -168,8 +167,10 @@ std::unique_ptr<rds2cpp::RObject> unconvert(const Rcpp::RObject& x, RdxFile_& gl
168167
Rcpp::CharacterVector names = vec.attr("names");
169168
const std::size_t n = vec.size();
170169
for (std::size_t i = 0; i < n; ++i) {
171-
auto sidx = prepare_symbol(Rcpp::String(names[i]).get_cstring(), rds2cpp::StringEncoding::UTF8, globals);
172-
latest.variables.emplace_back(std::move(sidx), unconvert(vec[i], globals));
170+
latest.variables.emplace_back(
171+
rds2cpp::register_symbol(Rcpp::String(names[i]).get_cstring(), rds2cpp::StringEncoding::UTF8, globals.symbols),
172+
unconvert(vec[i], globals)
173+
);
173174
}
174175

175176
add_attributes_except(x, &latest, globals, { "pretend-to-be-an-environment", "environment-index", "environment-parent", "environment-locked", "names" });
@@ -201,7 +202,7 @@ std::unique_ptr<rds2cpp::RObject> unconvert(const Rcpp::RObject& x, RdxFile_& gl
201202
auto ptr = new rds2cpp::LanguageObject;
202203
output.reset(ptr);
203204
Rcpp::CharacterVector name(vec[0]);
204-
ptr->function = prepare_symbol(Rcpp::String(name[0]).get_cstring(), rds2cpp::StringEncoding::UTF8, globals);
205+
ptr->function = rds2cpp::register_symbol(Rcpp::String(name[0]).get_cstring(), rds2cpp::StringEncoding::UTF8, globals.symbols);
205206

206207
Rcpp::List arguments(vec[1]);
207208
Rcpp::CharacterVector argnames;
@@ -213,8 +214,10 @@ std::unique_ptr<rds2cpp::RObject> unconvert(const Rcpp::RObject& x, RdxFile_& gl
213214
for (std::size_t a = 0; a < num_args; ++a) {
214215
auto unc = unconvert(arguments[a], globals);
215216
if (argnames.size()) {
216-
auto sidx = prepare_symbol(Rcpp::String(argnames[a]).get_cstring(), rds2cpp::StringEncoding::UTF8, globals);
217-
ptr->arguments.emplace_back(std::move(sidx), std::move(unc));
217+
ptr->arguments.emplace_back(
218+
rds2cpp::register_symbol(Rcpp::String(argnames[a]).get_cstring(), rds2cpp::StringEncoding::UTF8, globals.symbols),
219+
std::move(unc)
220+
);
218221
} else {
219222
ptr->arguments.emplace_back(std::move(unc));
220223
}
@@ -267,8 +270,10 @@ std::unique_ptr<rds2cpp::RObject> unconvert(const Rcpp::RObject& x, RdxFile_& gl
267270
const auto& names = obj.attributeNames();
268271
for (const auto& n : names) {
269272
if (n != "class") {
270-
auto sidx = prepare_symbol(n, rds2cpp::StringEncoding::ASCII, globals); // again, just assuming the encoding.
271-
ptr->attributes.emplace_back(std::move(sidx), unconvert(obj.slot(n), globals));
273+
ptr->attributes.emplace_back(
274+
rds2cpp::register_symbol(n, rds2cpp::StringEncoding::ASCII, globals.symbols), // again, just assuming the encoding.
275+
unconvert(obj.slot(n), globals)
276+
);
272277
}
273278
}
274279

0 commit comments

Comments
 (0)