From dcad6353ff43a71b2a3de27b7480020dde8bb43a Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Thu, 30 Nov 2023 16:35:25 -0600 Subject: [PATCH] Handle inheritance for S4 objects --- src/xml2_utils.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/xml2_utils.h b/src/xml2_utils.h index d77b3cf7..ead3ed8e 100644 --- a/src/xml2_utils.h +++ b/src/xml2_utils.h @@ -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")) {