Skip to content

Commit

Permalink
Handle inheritance for S4 objects
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Nov 30, 2023
1 parent 6d4c8e0 commit dcad635
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/xml2_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,31 @@ enum NodeType {
nodeset = 3,
};


inline int inherits3(SEXP x, const char *name) {
SEXP expr = PROTECT(Rf_lang3(Rf_install("inherits"), x, Rf_mkString(name)));
SEXP result = PROTECT(Rf_eval(expr, R_GlobalEnv));

int out = LOGICAL(result)[0];
UNPROTECT(2);
return out;
}

inline const NodeType getNodeType(SEXP x) {

// for fhircrackr
if (IS_S4_OBJECT(x)) {
if (inherits3(x, "xml_node")) {
return(NodeType::node);
} else if (inherits3(x, "xml_nodeset")) {
return(NodeType::nodeset);
} else if (inherits3(x, "xml_missing")) {
return(NodeType::missing);
} else {
Rf_error("Unexpected node type");
}
}

if (Rf_inherits(x, "xml_node")) {
return(NodeType::node);
} else if (Rf_inherits(x, "xml_nodeset")) {
Expand Down

0 comments on commit dcad635

Please sign in to comment.