From c5e3c6ee3e291a5dfc6670160677f4962d526dc4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 25 Nov 2010 14:54:24 +0200 Subject: Add the {parser,serializer}_maps functions to set polymorphic maps Use that in the generated aggregate classes in C++/Hybrid instead of the individual setters since their names are unknown for included and imported schemas. --- xsde/cxx/parser/elements.hxx | 105 +++++++++++++++++++++++++++++++++++--- xsde/cxx/parser/parser-header.cxx | 29 ++++++++++- xsde/cxx/parser/parser-inline.cxx | 79 +++++++++++++++++++++++++--- 3 files changed, 196 insertions(+), 17 deletions(-) (limited to 'xsde/cxx/parser') diff --git a/xsde/cxx/parser/elements.hxx b/xsde/cxx/parser/elements.hxx index 497577f..9857989 100644 --- a/xsde/cxx/parser/elements.hxx +++ b/xsde/cxx/parser/elements.hxx @@ -323,20 +323,44 @@ namespace CXX // struct ParticleParamDecl: Traversal::Element, Context { - ParticleParamDecl (Context& c, Boolean& first, Boolean name_arg) - : Context (c), first_ (first), name_arg_ (name_arg) + ParticleParamDecl (Context& c, + Boolean& first, + Boolean name_arg, + Boolean poly) + : Context (c), + first_ (first), + name_arg_ (name_arg), + poly_ (poly), + result_ (0) + { + } + + ParticleParamDecl (Context& c, Boolean* result, Boolean poly) + : Context (c), first_ (name_arg_), poly_ (poly), result_ (result) { } virtual Void traverse (SemanticGraph::Element& e) { + if (poly_ && anonymous (e.type ())) + return; + + if (result_ != 0) + { + *result_ = true; + return; + } + if (!first_) os << "," << endl; else first_ = false; - os << fq_name (e.type ()) << "&"; + if (poly_) + os << parser_map << "&"; + else + os << fq_name (e.type ()) << "&"; if (name_arg_) os << " " << ename (e); @@ -347,18 +371,34 @@ namespace CXX private: Boolean& first_; Boolean name_arg_; + Boolean poly_; + Boolean* result_; }; struct AttributeParamDecl: Traversal::Attribute, Context { AttributeParamDecl (Context& c, Boolean& first, Boolean name_arg) - : Context (c), first_ (first), name_arg_ (name_arg) + : Context (c), + first_ (first), + name_arg_ (name_arg), + result_ (0) + { + } + + AttributeParamDecl (Context& c, Boolean* result) + : Context (c), first_ (name_arg_), result_ (result) { } virtual Void traverse (Type& a) { + if (result_ != 0) + { + *result_ = true; + return; + } + if (!first_) os << "," << endl; else @@ -375,18 +415,38 @@ namespace CXX private: Boolean& first_; Boolean name_arg_; + Boolean* result_; }; struct ParserParamDecl : Traversal::Complex, Traversal::List, Context { - ParserParamDecl (Context& c, Boolean name_arg) + ParserParamDecl (Context& c, Boolean name_arg, Boolean poly) : Context (c), - particle_ (c, first_, name_arg), + particle_ (c, first_, name_arg, poly), attribute_ (c, first_, name_arg), first_ (true), - name_arg_ (name_arg) + name_arg_ (name_arg), + poly_ (poly), + result_ (0) + { + inherits_ >> *this; + + contains_compositor_ >> compositor_ >> contains_particle_; + contains_particle_ >> particle_; + contains_particle_ >> compositor_; + + if (!poly_) + names_ >> attribute_; + } + + ParserParamDecl (Context& c, Boolean* result, Boolean poly) + : Context (c), + particle_ (c, result, poly), + attribute_ (c, result), + poly_ (poly), + result_ (result) { inherits_ >> *this; @@ -394,7 +454,8 @@ namespace CXX contains_particle_ >> particle_; contains_particle_ >> compositor_; - names_ >> attribute_; + if (!poly_) + names_ >> attribute_; } virtual Void @@ -412,6 +473,15 @@ namespace CXX virtual Void traverse (SemanticGraph::List& l) { + if (poly_) + return; + + if (result_ != 0) + { + *result_ = true; + return; + } + if (!first_) os << "," << endl; else @@ -438,6 +508,25 @@ namespace CXX Boolean first_; Boolean name_arg_; + Boolean poly_; + Boolean* result_; + }; + + struct ParserParamTest + { + ParserParamTest (Context& c, Boolean& result, Boolean poly) + : impl_ (c, &result, poly) + { + } + + Void + traverse (SemanticGraph::Complex& c) + { + impl_.traverse (c); + } + + private: + ParserParamDecl impl_; }; // diff --git a/xsde/cxx/parser/parser-header.cxx b/xsde/cxx/parser/parser-header.cxx index 6e7dfe2..bec77fe 100644 --- a/xsde/cxx/parser/parser-header.cxx +++ b/xsde/cxx/parser/parser-header.cxx @@ -824,17 +824,44 @@ namespace CXX os << "// Parser construction API." << endl << "//" << endl; + // parsers () + // os << "void" << endl << "parsers ("; { - ParserParamDecl decl (*this, false); + ParserParamDecl decl (*this, false, false); decl.traverse (c); } os << ");" << endl; + // parser_maps () + // + if (poly_code && he) + { + Boolean r (false); + ParserParamTest test (*this, r, true); + test.traverse (c); + + // Have potentially polymorphic elements. + // + if (r) + { + os << "void" << endl + << "parser_maps ("; + + { + ParserParamDecl decl (*this, false, true); + decl.traverse (c); + } + + os << ");" + << endl; + } + } + if (ha) { os << "// Individual attribute parsers." << endl diff --git a/xsde/cxx/parser/parser-inline.cxx b/xsde/cxx/parser/parser-inline.cxx index f0d9e7b..2d02b3e 100644 --- a/xsde/cxx/parser/parser-inline.cxx +++ b/xsde/cxx/parser/parser-inline.cxx @@ -376,16 +376,22 @@ namespace CXX // struct ParticleMemberSet: Traversal::Element, Context { - ParticleMemberSet (Context& c) - : Context (c) + ParticleMemberSet (Context& c, Boolean poly) + : Context (c), poly_ (poly) { } virtual Void traverse (SemanticGraph::Element& e) { - os << "this->" << emember (e) << " = &" << ename (e) << ";"; + if (poly_) + os << "this->" << emember_map (e) << " = &" << ename (e) << ";"; + else + os << "this->" << emember (e) << " = &" << ename (e) << ";"; } + + private: + Boolean poly_; }; struct AttributeMemberSet: Traversal::Attribute, Context @@ -406,8 +412,8 @@ namespace CXX Traversal::List, Context { - BaseMemberSet (Context& c) - : Context (c) + BaseMemberSet (Context& c, Boolean poly) + : Context (c), poly_ (poly) { inherits_ >> *this; } @@ -427,6 +433,9 @@ namespace CXX virtual Void traverse (SemanticGraph::List& l) { + if (poly_) + return; + String const& name (ename (l)); String item (unclash (name, "item")); @@ -435,6 +444,7 @@ namespace CXX private: Traversal::Inherits inherits_; + Boolean poly_; }; // @@ -533,9 +543,11 @@ namespace CXX : Context (c), particle_accessor_ (c), attribute_accessor_ (c), - base_set_ (c), - particle_set_ (c), + base_set_ (c, false), + particle_set_ (c, false), attribute_set_ (c), + base_set_poly_ (c, true), + particle_set_poly_ (c, true), particle_ (c) { // Accessor. @@ -559,6 +571,16 @@ namespace CXX contains_particle_set_ >> particle_set_; names_attribute_set_ >> attribute_set_; + + // Member set polymorphic. + // + inherits_base_set_poly_ >> base_set_poly_; + base_set_poly_ >> contains_compositor_set_poly_; + + contains_compositor_set_poly_ >> compositor_set_poly_; + compositor_set_poly_ >> contains_particle_set_poly_; + contains_particle_set_poly_ >> compositor_set_poly_; + contains_particle_set_poly_ >> particle_set_poly_; } virtual Void @@ -611,7 +633,7 @@ namespace CXX << "parsers ("; { - ParserParamDecl decl (*this, true); + ParserParamDecl decl (*this, true, false); decl.traverse (c); } @@ -627,6 +649,37 @@ namespace CXX contains_compositor (c, contains_compositor_set_); os << "}"; + + // parser_maps () + // + if (poly_code && he) + { + Boolean r (false); + ParserParamTest test (*this, r, true); + test.traverse (c); + + // Have potentially polymorphic elements. + // + if (r) + { + os << inl + << "void " << name << "::" << endl + << "parser_maps ("; + + { + ParserParamDecl decl (*this, true, true); + decl.traverse (c); + } + + os << ")" + << "{"; + + inherits (c, inherits_base_set_poly_); + contains_compositor (c, contains_compositor_set_poly_); + + os << "}"; + } + } } String fq_base; @@ -832,6 +885,16 @@ namespace CXX // // + BaseMemberSet base_set_poly_; + Traversal::Inherits inherits_base_set_poly_; + + Traversal::Compositor compositor_set_poly_; + ParticleMemberSet particle_set_poly_; + Traversal::ContainsCompositor contains_compositor_set_poly_; + Traversal::ContainsParticle contains_particle_set_poly_; + + // + // Particle particle_; }; } -- cgit v1.1