rds2cpp
Read and write RDS/RDA files in C++
Loading...
Searching...
No Matches
RObject.hpp
Go to the documentation of this file.
1#ifndef RDS2CPP_ROBJECT_HPP
2#define RDS2CPP_ROBJECT_HPP
3
4#include <vector>
5#include <cstdint>
6#include <cstddef>
7#include <complex>
8#include <optional>
9
10#include "SEXPType.hpp"
11#include "StringEncoding.hpp"
12#include "utils_other.hpp"
13
15
22namespace rds2cpp {
23
27struct RObject {
31 RObject() {}
32 virtual ~RObject() {}
33
34 // Rule of 5'ing.
35 RObject& operator=(const RObject& rhs) = default;
36 RObject(const RObject& rhs) = default;
37 RObject& operator=(RObject&& rhs) = default;
38 RObject(RObject&& rhs) = default;
46 virtual SEXPType type() const = 0;
47};
48
52struct Null final : public RObject {
53 SEXPType type() const { return SEXPType::NIL; }
54};
55
59struct Symbol {
63 Symbol() = default;
64
69 Symbol(std::string name, StringEncoding encoding) : name(std::move(name)), encoding(encoding) {}
70
74 std::string name;
75
79 StringEncoding encoding = StringEncoding::UTF8;
80};
81
85struct SymbolIndex final : public RObject {
89 SymbolIndex() = default;
90
94 SymbolIndex(std::size_t i) : index(i) {}
95
96
97 SEXPType type() const { return SEXPType::SYM; }
98
102 std::size_t index = - 1;
103};
104
117inline SymbolIndex register_symbol(std::string name, StringEncoding encoding, std::vector<Symbol>& symbols) {
118 auto idx = symbols.size();
119 symbols.emplace_back(std::move(name), encoding);
120 return SymbolIndex(idx);
121}
122
126struct Attribute;
134struct EnvironmentIndex final : public RObject {
138 EnvironmentIndex() = default;
139
144
150 EnvironmentIndex(std::size_t i): index(i), env_type(SEXPType::ENV) {}
151
156 SEXPType type() const { return env_type; }
157
162 std::size_t index = -1;
163
167 SEXPType env_type = SEXPType::GLOBALENV_;
168};
169
178
183 EnvironmentVariable(SymbolIndex name, std::unique_ptr<RObject> value) : name(std::move(name)), value(std::move(value)) {}
184
192 std::unique_ptr<RObject> value;
193};
194
202 bool locked = false;
203
207 bool hashed = false;
208
213 SEXPType parent_type = SEXPType::GLOBALENV_;
214
219 std::size_t parent = -1;
220
224 std::vector<EnvironmentVariable> variables;
225
229 std::vector<Attribute> attributes;
230};
231
239 std::unique_ptr<RObject> protection;
240
244 std::unique_ptr<RObject> tag;
245
249 std::vector<Attribute> attributes;
250};
251
255struct ExternalPointerIndex final : public RObject {
260
264 ExternalPointerIndex(std::size_t i) : index(i) {}
265
266
267 SEXPType type() const { return SEXPType::EXTPTR; }
268
272 std::size_t index = -1;
273};
274
278struct Attribute {
282 Attribute() = default;
283
288 Attribute(SymbolIndex name, std::unique_ptr<RObject> value) : name(std::move(name)), value(std::move(value)) {}
289
294
298 std::unique_ptr<RObject> value;
299};
300
307template<typename ElementType, SEXPType stype>
308struct AtomicVector final : public RObject {
312 AtomicVector(std::size_t n = 0) :
313 data(sanisizer::cast<I<decltype(data.size())> >(n))
314 {}
315
316 static constexpr SEXPType vector_sexp_type = stype;
321 SEXPType type() const { return stype; }
322
326 std::vector<ElementType> data;
327
331 std::vector<Attribute> attributes;
332};
333
338
343
348
353
358
362struct String {
366 String() = default;
367
373
378 std::optional<std::string> value;
379
384 StringEncoding encoding = StringEncoding::UTF8;
385};
386
390struct StringVector final : public RObject {
394 StringVector(std::size_t n = 0) : data(sanisizer::cast<I<decltype(data.size())> >(n)) {}
399 SEXPType type() const { return SEXPType::STR; }
400
404 std::vector<String> data;
405
409 std::vector<Attribute> attributes;
410};
411
415struct GenericVector final : public RObject {
419 GenericVector(std::size_t n = 0) :
420 data(sanisizer::cast<I<decltype(data.size())> >(n))
421 {}
426 SEXPType type() const { return SEXPType::VEC; }
427
431 std::vector<std::unique_ptr<RObject> > data;
432
436 std::vector<Attribute> attributes;
437};
438
446 PairListElement() = default;
447
452 PairListElement(std::unique_ptr<RObject> value) : value(std::move(value)) {}
453
459 PairListElement(SymbolIndex tag, std::unique_ptr<RObject> value) : tag(std::move(tag)), value(std::move(value)) {}
460
465 std::optional<SymbolIndex> tag;
466
470 std::unique_ptr<RObject> value;
471};
472
476struct PairList final : public RObject {
477 SEXPType type() const { return SEXPType::LIST; }
478
482 std::vector<PairListElement> data;
483
487 std::vector<Attribute> attributes;
488};
489
493struct S4Object final : public RObject {
494 SEXPType type() const { return SEXPType::S4; }
495
499 std::string class_name;
500
504 StringEncoding class_encoding = StringEncoding::UTF8;
505
509 std::string package_name;
510
514 StringEncoding package_encoding = StringEncoding::UTF8;
515
520 std::vector<Attribute> attributes;
521};
522
526struct BuiltInFunction final : public RObject {
527 SEXPType type() const { return SEXPType::BUILTIN; }
528
532 std::string name;
533};
534
542 LanguageArgument() = default;
543
549 LanguageArgument(SymbolIndex name, std::unique_ptr<RObject> value) : name(std::move(name)), value(std::move(value)) {}
550
555 LanguageArgument(std::unique_ptr<RObject> value) : value(std::move(value)) {}
556
561 std::optional<SymbolIndex> name;
562
567 std::unique_ptr<RObject> value;
568};
569
573struct LanguageObject final : public RObject {
574
575 SEXPType type() const { return SEXPType::LANG; }
576
581
585 std::vector<LanguageArgument> arguments;
586
590 std::vector<Attribute> attributes;
591};
592
596struct ExpressionVector final : public RObject {
600 ExpressionVector(std::size_t n = 0) :
601 data(sanisizer::cast<I<decltype(data.size())> >(n))
602 {}
607 SEXPType type() const { return SEXPType::EXPR; }
608
613 std::vector<std::unique_ptr<RObject> > data;
614
618 std::vector<Attribute> attributes;
619};
620
621}
622
623#endif
R data types.
String encodings.
Parse an RDS file in C++.
Definition StringEncoding.hpp:12
AtomicVector< unsigned char, SEXPType::RAW > RawVector
Raw vector.
Definition RObject.hpp:352
AtomicVector< double, SEXPType::REAL > DoubleVector
Double-precision vector.
Definition RObject.hpp:347
StringEncoding
Definition StringEncoding.hpp:17
SymbolIndex register_symbol(std::string name, StringEncoding encoding, std::vector< Symbol > &symbols)
Definition RObject.hpp:117
AtomicVector< std::int32_t, SEXPType::INT > IntegerVector
Integer vector.
Definition RObject.hpp:337
AtomicVector< std::int32_t, SEXPType::LGL > LogicalVector
Logical vector.
Definition RObject.hpp:342
SEXPType
Definition SEXPType.hpp:31
AtomicVector< std::complex< double >, SEXPType::CPLX > ComplexVector
Complex vector.
Definition RObject.hpp:357
constexpr Dest_ cast(Value_ x)
Vector of some atomic type.
Definition RObject.hpp:308
SEXPType type() const
Definition RObject.hpp:321
std::vector< ElementType > data
Definition RObject.hpp:326
std::vector< Attribute > attributes
Definition RObject.hpp:331
Attribute name and value.
Definition RObject.hpp:278
SymbolIndex name
Definition RObject.hpp:293
std::unique_ptr< RObject > value
Definition RObject.hpp:298
Attribute(SymbolIndex name, std::unique_ptr< RObject > value)
Definition RObject.hpp:288
Built-in function.
Definition RObject.hpp:526
SEXPType type() const
Definition RObject.hpp:527
std::string name
Definition RObject.hpp:532
Reference to an environment.
Definition RObject.hpp:134
std::size_t index
Definition RObject.hpp:162
SEXPType env_type
Definition RObject.hpp:167
EnvironmentIndex(SEXPType e)
Definition RObject.hpp:143
SEXPType type() const
Definition RObject.hpp:156
EnvironmentIndex(std::size_t i)
Definition RObject.hpp:150
Variable in an Environment.
Definition RObject.hpp:173
EnvironmentVariable(SymbolIndex name, std::unique_ptr< RObject > value)
Definition RObject.hpp:183
SymbolIndex name
Definition RObject.hpp:188
std::unique_ptr< RObject > value
Definition RObject.hpp:192
An R environment.
Definition RObject.hpp:198
bool hashed
Definition RObject.hpp:207
bool locked
Definition RObject.hpp:202
std::vector< Attribute > attributes
Definition RObject.hpp:229
std::vector< EnvironmentVariable > variables
Definition RObject.hpp:224
std::size_t parent
Definition RObject.hpp:219
SEXPType parent_type
Definition RObject.hpp:213
Expression vector.
Definition RObject.hpp:596
std::vector< Attribute > attributes
Definition RObject.hpp:618
std::vector< std::unique_ptr< RObject > > data
Definition RObject.hpp:613
SEXPType type() const
Definition RObject.hpp:607
Reference to an external pointer.
Definition RObject.hpp:255
SEXPType type() const
Definition RObject.hpp:267
ExternalPointerIndex(std::size_t i)
Definition RObject.hpp:264
std::size_t index
Definition RObject.hpp:272
An R external pointer.
Definition RObject.hpp:235
std::unique_ptr< RObject > tag
Definition RObject.hpp:244
std::unique_ptr< RObject > protection
Definition RObject.hpp:239
std::vector< Attribute > attributes
Definition RObject.hpp:249
Generic vector, i.e., an ordinary R list.
Definition RObject.hpp:415
SEXPType type() const
Definition RObject.hpp:426
std::vector< Attribute > attributes
Definition RObject.hpp:436
std::vector< std::unique_ptr< RObject > > data
Definition RObject.hpp:431
Function argument in a LanguageObject.
Definition RObject.hpp:538
LanguageArgument(SymbolIndex name, std::unique_ptr< RObject > value)
Definition RObject.hpp:549
LanguageArgument(std::unique_ptr< RObject > value)
Definition RObject.hpp:555
std::optional< SymbolIndex > name
Definition RObject.hpp:561
std::unique_ptr< RObject > value
Definition RObject.hpp:567
Language object, i.e., a function call.
Definition RObject.hpp:573
std::vector< LanguageArgument > arguments
Definition RObject.hpp:585
std::vector< Attribute > attributes
Definition RObject.hpp:590
SymbolIndex function
Definition RObject.hpp:580
SEXPType type() const
Definition RObject.hpp:575
R's NULL value.
Definition RObject.hpp:52
SEXPType type() const
Definition RObject.hpp:53
Element of a PairList.
Definition RObject.hpp:442
PairListElement(SymbolIndex tag, std::unique_ptr< RObject > value)
Definition RObject.hpp:459
std::optional< SymbolIndex > tag
Definition RObject.hpp:465
PairListElement(std::unique_ptr< RObject > value)
Definition RObject.hpp:452
std::unique_ptr< RObject > value
Definition RObject.hpp:470
Pairlist, i.e., a linked list.
Definition RObject.hpp:476
std::vector< PairListElement > data
Definition RObject.hpp:482
SEXPType type() const
Definition RObject.hpp:477
std::vector< Attribute > attributes
Definition RObject.hpp:487
Virtual class for all unserialized R objects.
Definition RObject.hpp:27
virtual SEXPType type() const =0
S4 object.
Definition RObject.hpp:493
StringEncoding package_encoding
Definition RObject.hpp:514
std::string class_name
Definition RObject.hpp:499
StringEncoding class_encoding
Definition RObject.hpp:504
std::string package_name
Definition RObject.hpp:509
SEXPType type() const
Definition RObject.hpp:494
std::vector< Attribute > attributes
Definition RObject.hpp:520
String vector.
Definition RObject.hpp:390
std::vector< String > data
Definition RObject.hpp:404
std::vector< Attribute > attributes
Definition RObject.hpp:409
SEXPType type() const
Definition RObject.hpp:399
Single string in a StringVector.
Definition RObject.hpp:362
StringEncoding encoding
Definition RObject.hpp:384
std::optional< std::string > value
Definition RObject.hpp:378
String(std::string value, StringEncoding encoding)
Definition RObject.hpp:372
String()=default
Reference to a language symbol.
Definition RObject.hpp:85
SEXPType type() const
Definition RObject.hpp:97
SymbolIndex(std::size_t i)
Definition RObject.hpp:94
std::size_t index
Definition RObject.hpp:102
An R symbol.
Definition RObject.hpp:59
Symbol()=default
std::string name
Definition RObject.hpp:74
Symbol(std::string name, StringEncoding encoding)
Definition RObject.hpp:69
StringEncoding encoding
Definition RObject.hpp:79