Skip to content

Commit 43c9ad6

Browse files
authored
Handle inheritance for S4 objects (#425)
1 parent 6d4c8e0 commit 43c9ad6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/xml2_utils.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,31 @@ enum NodeType {
1515
nodeset = 3,
1616
};
1717

18+
19+
inline int inherits3(SEXP x, const char *name) {
20+
SEXP expr = PROTECT(Rf_lang3(Rf_install("inherits"), x, Rf_mkString(name)));
21+
SEXP result = PROTECT(Rf_eval(expr, R_GlobalEnv));
22+
23+
int out = LOGICAL(result)[0];
24+
UNPROTECT(2);
25+
return out;
26+
}
27+
1828
inline const NodeType getNodeType(SEXP x) {
29+
30+
// for fhircrackr
31+
if (IS_S4_OBJECT(x)) {
32+
if (inherits3(x, "xml_node")) {
33+
return(NodeType::node);
34+
} else if (inherits3(x, "xml_nodeset")) {
35+
return(NodeType::nodeset);
36+
} else if (inherits3(x, "xml_missing")) {
37+
return(NodeType::missing);
38+
} else {
39+
Rf_error("Unexpected node type");
40+
}
41+
}
42+
1943
if (Rf_inherits(x, "xml_node")) {
2044
return(NodeType::node);
2145
} else if (Rf_inherits(x, "xml_nodeset")) {

0 commit comments

Comments
 (0)