2121namespace rds2cpp {
2222
2323template <class Source_ >
24- IntegerVector parse_integer_body (Source_&);
24+ std::unique_ptr< IntegerVector> parse_integer_body (Source_&);
2525
2626template <class Source_ >
27- DoubleVector parse_double_body (Source_& src);
27+ std::unique_ptr< DoubleVector> parse_double_body (Source_& src);
2828
2929template <class Source_ >
3030std::unique_ptr<RObject> parse_object (Source_&, SharedParseInfo&);
3131
3232template <class Source_ >
33- PairList parse_pairlist_body (Source_&, const Header&, SharedParseInfo&);
33+ std::unique_ptr< PairList> parse_pairlist_body (Source_&, const Header&, SharedParseInfo&);
3434
3535namespace altrep_internal {
3636
3737template <class Vector_ , class Source_ >
38- Vector_ parse_numeric_compact_seq (Source_& src) try {
38+ std::unique_ptr< Vector_> parse_numeric_compact_seq (Source_& src) try {
3939 auto header = parse_header (src);
4040 if (header[3 ] != static_cast <unsigned char >(SEXPType::REAL )) {
4141 throw std::runtime_error (" expected compact_seq to store sequence information in doubles" );
4242 }
4343
4444 auto info = parse_double_body (src);
45- const auto & ranges = info. data ;
45+ const auto & ranges = info-> data ;
4646 if (ranges.size () != 3 ) {
4747 throw std::runtime_error (" expected compact_seq's sequence information to be of length 3" );
4848 }
@@ -69,9 +69,9 @@ Vector_ parse_numeric_compact_seq(Source_& src) try {
6969 }
7070 }
7171
72- Vector_ output (len);
72+ auto output = std::make_unique<Vector_> (len);
7373 for (I<decltype (len)> i = 0 ; i < len; ++i, start += step) {
74- output. data [i] = start;
74+ output-> data [i] = start;
7575 }
7676
7777 auto terminator = parse_header (src);
@@ -82,20 +82,19 @@ Vector_ parse_numeric_compact_seq(Source_& src) try {
8282 return output;
8383} catch (std::exception& e) {
8484 throw traceback (" failed to parse compact numeric ALTREP" , e);
85- return Vector_ ();
85+ return std::unique_ptr< Vector_> ();
8686}
8787
88- template <class Vector , class Source_ >
89- Vector parse_attribute_wrapper (Source_& src, SharedParseInfo& shared) try {
88+ template <class Vector_ , class Source_ >
89+ std::unique_ptr<Vector_> parse_attribute_wrapper (Source_& src, SharedParseInfo& shared) try {
9090 auto plist_header = parse_header (src);
9191 if (plist_header[3 ] != static_cast <unsigned char >(SEXPType::LIST )) {
9292 throw std::runtime_error (" expected pairlist in wrap_* ALTREP's payload" );
9393 }
9494
9595 // First pairlist element is a CONS cell where the first value is the wrapped integer vector.
96-
9796 auto contents = parse_object (src, shared);
98- if (contents->type () != Vector ::vector_sexp_type) {
97+ if (contents->type () != Vector_ ::vector_sexp_type) {
9998 throw std::runtime_error (" incorrectly typed contents in wrap_* ALTREP's payload" );
10099 }
101100
@@ -106,45 +105,45 @@ Vector parse_attribute_wrapper(Source_& src, SharedParseInfo& shared) try {
106105 }
107106
108107 auto metadata = parse_integer_body (src);
109- if (metadata. data .size () != 2 ) {
108+ if (metadata-> data .size () != 2 ) {
110109 throw std::runtime_error (" wrap_* ALTREP's metadata should be a length-2 integer vector" );
111110 }
112111
113112 // Now we can finally get the attributes, which makes up the rest of the pairlist.
114- auto coerced = static_cast <Vector *>(contents.get ( ));
113+ std::unique_ptr<Vector_> output ( static_cast <Vector_ *>(contents.release () ));
115114 auto attrheader = parse_header (src);
116115 if (attrheader[3 ] == static_cast <unsigned >(SEXPType::LIST )) {
117- parse_attributes_body (src, attrheader, coerced ->attributes , shared);
116+ parse_attributes_body (src, attrheader, output ->attributes , shared);
118117 } else if (attrheader[3 ] != static_cast <unsigned >(SEXPType::NILVALUE_ )) {
119118 throw std::runtime_error (" wrap_* ALTREP's attributes should be a pairlist or NULL" );
120119 }
121120
122- return Vector ( std::move (*coerced)) ;
121+ return output ;
123122} catch (std::exception& e) {
124123 throw traceback (" failed to parse attribute-wrapped ALTREP" , e);
125- return Vector ();
124+ return std::unique_ptr<Vector_> ();
126125}
127126
128127template <class Source_ >
129- StringVector parse_deferred_string (Source_& src, SharedParseInfo& shared) try {
128+ std::unique_ptr< StringVector> parse_deferred_string (Source_& src, SharedParseInfo& shared) try {
130129 auto plist_header = parse_header (src);
131130 if (plist_header[3 ] != static_cast <unsigned char >(SEXPType::LIST )) {
132131 throw std::runtime_error (" expected pairlist in deferred_string ALTREP's payload" );
133132 }
134133
135134 // First pairlist element is a CONS cell where the first value is the thing to be converted.
136135 auto contents = parse_object (src, shared);
137- StringVector output;
136+ std::unique_ptr< StringVector> output;
138137
139138 if (contents->type () == SEXPType::INT ){
140139 auto cast = static_cast <IntegerVector*>(contents.get ());
141140 const auto n = cast->data .size ();
142- output = StringVector (n);
141+ output = std::make_unique< StringVector> (n);
143142
144143 for (I<decltype (n)> i = 0 ; i < n; ++i) {
145144 if (cast->data [i] != std::numeric_limits<std::int32_t >::min ()) { // see altrep.c.
146- output. data [i].value = std::to_string (cast->data [i]);
147- output. data [i].encoding = StringEncoding::ASCII ;
145+ output-> data [i].value = std::to_string (cast->data [i]);
146+ output-> data [i].encoding = StringEncoding::ASCII ;
148147 }
149148 }
150149
@@ -153,15 +152,15 @@ StringVector parse_deferred_string(Source_& src, SharedParseInfo& shared) try {
153152 converter.precision (std::numeric_limits<double >::max_digits10);
154153 auto cast = static_cast <DoubleVector*>(contents.get ());
155154 const bool lw = (little_endian () ? 0 : 1 ); // see arithmetic.c.
156- output = StringVector (cast->data .size ());
155+ output = std::make_unique< StringVector> (cast->data .size ());
157156
158157 const auto datalen = cast->data .size ();
159158 for (I<decltype (datalen)> i = 0 ; i < datalen; ++i) {
160- output. data [i].encoding = StringEncoding::ASCII ;
159+ output-> data [i].encoding = StringEncoding::ASCII ;
161160
162161 if (std::isfinite (cast->data [i])) {
163162 converter << cast->data [i];
164- output. data [i].value = converter.str ();
163+ output-> data [i].value = converter.str ();
165164 converter.str (std::string ());
166165
167166 } else if (std::isnan (cast->data [i])) {
@@ -177,14 +176,14 @@ StringVector parse_deferred_string(Source_& src, SharedParseInfo& shared) try {
177176 if (payload == 1954 ) {
178177 // Missing values are represented by the default unset value of String::value.
179178 } else {
180- output. data [i].value = " NaN" ;
179+ output-> data [i].value = " NaN" ;
181180 }
182181
183182 } else if (std::isinf (cast->data [i])) {
184183 if (cast->data [i] > 0 ) {
185- output. data [i].value = " Inf" ;
184+ output-> data [i].value = " Inf" ;
186185 } else {
187- output. data [i].value = " -Inf" ;
186+ output-> data [i].value = " -Inf" ;
188187 }
189188 }
190189 }
@@ -200,7 +199,7 @@ StringVector parse_deferred_string(Source_& src, SharedParseInfo& shared) try {
200199 }
201200
202201 auto metadata = parse_integer_body (src);
203- if (metadata. data .size () != 1 ) {
202+ if (metadata-> data .size () != 1 ) {
204203 throw std::runtime_error (" deferred_string ALTREP's metadata should be a length-1 integer vector" );
205204 }
206205
@@ -213,7 +212,7 @@ StringVector parse_deferred_string(Source_& src, SharedParseInfo& shared) try {
213212 return output;
214213} catch (std::exception& e) {
215214 throw traceback (" failed to parse deferred string ALTREP" , e);
216- return StringVector ();
215+ return std::make_unique< StringVector> ();
217216}
218217
219218}
@@ -226,24 +225,20 @@ std::unique_ptr<RObject> parse_altrep_body(Source_& src, SharedParseInfo& shared
226225 }
227226
228227 auto plist = parse_pairlist_body (src, header, shared);
229- if (plist. data .size () < 1 || plist. data [0 ].value ->type () != SEXPType::SYM ) {
228+ if (plist-> data .size () < 1 || plist-> data [0 ].value ->type () != SEXPType::SYM ) {
230229 throw std::runtime_error (" expected type specification symbol in the ALTREP description" );
231230 }
232231
233232 std::unique_ptr<RObject> output;
234- auto pointerize_ = [&](auto x) -> void {
235- pointerize (output, std::move (x));
236- };
237-
238- auto sdx = static_cast <SymbolIndex*>(plist.data [0 ].value .get ());
233+ auto sdx = static_cast <SymbolIndex*>(plist->data [0 ].value .get ());
239234 const auto & symb = shared.symbols [sdx->index ];
240235
241236 if (symb.name == " wrap_integer" ) {
242- pointerize_ ( altrep_internal::parse_attribute_wrapper<IntegerVector>(src, shared) );
237+ output = altrep_internal::parse_attribute_wrapper<IntegerVector>(src, shared);
243238 } else if (symb.name == " compact_intseq" ) {
244- pointerize_ ( altrep_internal::parse_numeric_compact_seq<IntegerVector>(src) );
239+ output = altrep_internal::parse_numeric_compact_seq<IntegerVector>(src);
245240 } else if (symb.name == " deferred_string" ) {
246- pointerize_ ( altrep_internal::parse_deferred_string (src, shared) );
241+ output = altrep_internal::parse_deferred_string (src, shared);
247242 } else {
248243 throw std::runtime_error (" unrecognized ALTREP type '" + symb.name + " '" );
249244 }
0 commit comments