From 209f07b8e9b8705814df22012e22856ec07f1592 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 8 Oct 2010 16:00:33 +0200 Subject: Factor out common facets handling code in serializer --- xsde/cxx/parser/parser-source.cxx | 8 +- xsde/cxx/serializer/elements.cxx | 36 +++++++ xsde/cxx/serializer/elements.hxx | 4 + xsde/cxx/serializer/serializer-header.cxx | 39 ++------ xsde/cxx/serializer/serializer-inline.cxx | 150 +++++++++++++----------------- xsde/cxx/serializer/serializer-source.cxx | 6 +- 6 files changed, 119 insertions(+), 124 deletions(-) (limited to 'xsde/cxx') diff --git a/xsde/cxx/parser/parser-source.cxx b/xsde/cxx/parser/parser-source.cxx index 521f810..54d5a05 100644 --- a/xsde/cxx/parser/parser-source.cxx +++ b/xsde/cxx/parser/parser-source.cxx @@ -36,14 +36,14 @@ namespace CXX base.inherits_p () && base_ret == ret_type (base.inherits ().base ())); - Boolean facets (false); // Whether we need to set facets. + Boolean enum_facets (false); // Whether we need to set enum facets. if (validation) { - StringBasedType t (facets); + StringBasedType t (enum_facets); t.dispatch (e); } - if (facets || same || ret == L"void" || poly_code || + if (enum_facets || same || ret == L"void" || poly_code || (tiein && !(base_same || base_ret == L"void"))) { os << "// " << name << endl @@ -155,7 +155,7 @@ namespace CXX << "}"; } - if (facets) + if (enum_facets) { typedef Cult::Containers::Set Enums; Enums enums; diff --git a/xsde/cxx/serializer/elements.cxx b/xsde/cxx/serializer/elements.cxx index 5395953..62fd32d 100644 --- a/xsde/cxx/serializer/elements.cxx +++ b/xsde/cxx/serializer/elements.cxx @@ -213,6 +213,42 @@ namespace CXX return t.context ().get ("s:impl"); } + Boolean Context:: + has_facets (SemanticGraph::Complex& c) + { + if (validation && restriction_p (c)) + { + using SemanticGraph::Restricts; + Restricts& r (dynamic_cast (c.inherits ())); + + if (r.facet_empty ()) + return false; + + SemanticGraph::Type& ub (ultimate_base (c)); + + if (ub.is_a () || + ub.is_a () || + ub.is_a () || + ub.is_a () || + ub.is_a () || + ub.is_a ()) + { + Restricts::FacetIterator end (r.facet_end ()); + + if (r.facet_find (L"length") != end || + r.facet_find (L"minLength") != end || + r.facet_find (L"maxLength") != end || + r.facet_find (L"minInclusive") != end || + r.facet_find (L"minExclusive") != end || + r.facet_find (L"maxInclusive") != end || + r.facet_find (L"maxExclusive") != end) + return true; + } + } + + return false; + } + // Includes // Void TypeForward:: diff --git a/xsde/cxx/serializer/elements.hxx b/xsde/cxx/serializer/elements.hxx index df1f486..7d2bf80 100644 --- a/xsde/cxx/serializer/elements.hxx +++ b/xsde/cxx/serializer/elements.hxx @@ -175,6 +175,10 @@ namespace CXX eimpl (SemanticGraph::Type&); public: + Boolean + has_facets (SemanticGraph::Complex& c); + + public: CLI::Options const& options; String& xml_serializer; String& serializer_base; diff --git a/xsde/cxx/serializer/serializer-header.cxx b/xsde/cxx/serializer/serializer-header.cxx index 550d742..9f0c1f3 100644 --- a/xsde/cxx/serializer/serializer-header.cxx +++ b/xsde/cxx/serializer/serializer-header.cxx @@ -275,13 +275,15 @@ namespace CXX SemanticGraph::Type& base (e.inherits ().base ()); String fq_base (fq_name (base)); - Boolean facets (false); // Whether we need to set facets. + Boolean enum_facets (false); // Whether we need to set enum facets. if (validation) { - StringBasedType t (facets); + StringBasedType t (enum_facets); t.dispatch (e); } + Boolean facets (enum_facets || has_facets (e)); + os << "class " << name << ": public " << (mixin ? "virtual " : "") << fq_base << "{" @@ -362,7 +364,7 @@ namespace CXX << name << " (" << name << "*, void*);"; } - if (facets) + if (enum_facets) { UnsignedLong enum_count (0); @@ -1041,6 +1043,7 @@ namespace CXX // as in the base. We only need the serialization/validation code. // Boolean restriction (restriction_p (c)); + Boolean facets (has_facets (c)); Boolean hb (c.inherits_p ()); Boolean he (has (c)); @@ -1057,36 +1060,6 @@ namespace CXX names (c, names_test); } - Boolean facets (false); // Defines facets. - if (validation && restriction) - { - SemanticGraph::Type& ub (ultimate_base (c)); - - if (ub.is_a () || - ub.is_a () || - ub.is_a () || - ub.is_a () || - ub.is_a () || - ub.is_a ()) - { - using SemanticGraph::Restricts; - Restricts& r (dynamic_cast (c.inherits ())); - - if (!r.facet_empty ()) - { - Restricts::FacetIterator end (r.facet_end ()); - facets = - r.facet_find (L"length") != end || - r.facet_find (L"minLength") != end || - r.facet_find (L"maxLength") != end || - r.facet_find (L"minInclusive") != end || - r.facet_find (L"minExclusive") != end || - r.facet_find (L"maxInclusive") != end || - r.facet_find (L"maxExclusive") != end; - } - } - } - // // os << "class " << name << ": public "; diff --git a/xsde/cxx/serializer/serializer-inline.cxx b/xsde/cxx/serializer/serializer-inline.cxx index fe36e4a..f888e4f 100644 --- a/xsde/cxx/serializer/serializer-inline.cxx +++ b/xsde/cxx/serializer/serializer-inline.cxx @@ -14,6 +14,48 @@ namespace CXX { namespace { + Void + facet_calls (SemanticGraph::Complex& c, Context& ctx) + { + std::wostream& os (ctx.os); + + using SemanticGraph::Restricts; + Restricts& r (dynamic_cast (c.inherits ())); + + for (Restricts::FacetIterator i (r.facet_begin ()); + i != r.facet_end (); ++i) + { + if (i->first == L"length") + { + os << "this->_length_facet (" << i->second << "UL);"; + } + else if (i->first == L"minLength") + { + os << "this->_min_length_facet (" << i->second << "UL);"; + } + else if (i->first == L"maxLength") + { + os << "this->_max_length_facet (" << i->second << "UL);"; + } + else if (i->first == L"minInclusive") + { + os << "this->_min_facet (" << i->second << ", true);"; + } + else if (i->first == L"minExclusive") + { + os << "this->_min_facet (" << i->second << ", false);"; + } + else if (i->first == L"maxInclusive") + { + os << "this->_max_facet (" << i->second << ", true);"; + } + else if (i->first == L"maxExclusive") + { + os << "this->_max_facet (" << i->second << ", false);"; + } + } + } + struct Enumeration: Traversal::Enumeration, Context { Enumeration (Context& c) @@ -26,21 +68,23 @@ namespace CXX { String const& name (ename (e)); - Boolean facets (false); // Whether we need to set facets. + Boolean enum_facets (false); // Whether we need to set enum facets. if (validation) { - StringBasedType t (facets); + StringBasedType t (enum_facets); t.dispatch (e); } UnsignedLong enum_count (0); - if (facets) + if (enum_facets) { for (Type::NamesIterator i (e.names_begin ()), end (e.names_end ()); i != end; ++i) ++enum_count; } + Boolean facets (enum_facets || has_facets (e)); + if (facets || tiein) os << "// " << name << endl << "//" << endl @@ -51,10 +95,9 @@ namespace CXX os << inl << name << "::" << endl << name << " ()" << endl - << "{" - << "this->_enumeration_facet (_xsde_" << name << "_enums_, " << - enum_count << "UL);" - << "}"; + << "{"; + facet_calls (e, enum_count); + os << "}"; } if (tiein) @@ -76,8 +119,7 @@ namespace CXX << "{"; if (facets) - os << "this->_enumeration_facet (_xsde_" << name << - "_enums_, " << enum_count << "UL);"; + facet_calls (e, enum_count); os << "}"; @@ -89,12 +131,22 @@ namespace CXX << "{"; if (facets) - os << "this->_enumeration_facet (_xsde_" << name << - "_enums_, " << enum_count << "UL);"; + facet_calls (e, enum_count); os << "}"; } } + + private: + Void + facet_calls (Type& e, UnsignedLong enum_count) + { + Serializer::facet_calls (e, *this); + + if (enum_count != 0) + os << "this->_enumeration_facet (_xsde_" << ename (e) << + "_enums_, " << enum_count << "UL);"; + } }; @@ -431,36 +483,7 @@ namespace CXX Boolean he (has (c)); Boolean ha (has (c)); Boolean restriction (restriction_p (c)); - - Boolean facets (false); // Defines facets. - if (validation && restriction) - { - SemanticGraph::Type& ub (ultimate_base (c)); - - if (ub.is_a () || - ub.is_a () || - ub.is_a () || - ub.is_a () || - ub.is_a () || - ub.is_a ()) - { - using SemanticGraph::Restricts; - Restricts& r (dynamic_cast (c.inherits ())); - - if (!r.facet_empty ()) - { - Restricts::FacetIterator end (r.facet_end ()); - facets = - r.facet_find (L"length") != end || - r.facet_find (L"minLength") != end || - r.facet_find (L"maxLength") != end || - r.facet_find (L"minInclusive") != end || - r.facet_find (L"minExclusive") != end || - r.facet_find (L"maxInclusive") != end || - r.facet_find (L"maxExclusive") != end; - } - } - } + Boolean facets (has_facets (c)); if (!(tiein || facets || (!restriction && (he || ha)))) return; @@ -579,7 +602,7 @@ namespace CXX os << "{"; if (facets) - facet_calls (c); + facet_calls (c, *this); os << "}"; @@ -634,54 +657,13 @@ namespace CXX os << "{"; if (facets) - facet_calls (c); + facet_calls (c, *this); os << "}"; } } private: - Void - facet_calls (Type& c) - { - using SemanticGraph::Restricts; - Restricts& r (dynamic_cast (c.inherits ())); - - for (Restricts::FacetIterator i (r.facet_begin ()); - i != r.facet_end (); ++i) - { - if (i->first == L"length") - { - os << "this->_length_facet (" << i->second << "UL);"; - } - else if (i->first == L"minLength") - { - os << "this->_min_length_facet (" << i->second << "UL);"; - } - else if (i->first == L"maxLength") - { - os << "this->_max_length_facet (" << i->second << "UL);"; - } - else if (i->first == L"minInclusive") - { - os << "this->_min_facet (" << i->second << ", true);"; - } - else if (i->first == L"minExclusive") - { - os << "this->_min_facet (" << i->second << ", false);"; - } - else if (i->first == L"maxInclusive") - { - os << "this->_max_facet (" << i->second << ", true);"; - } - else if (i->first == L"maxExclusive") - { - os << "this->_max_facet (" << i->second << ", false);"; - } - } - } - - private: // // Traversal::Compositor compositor_accessor_; diff --git a/xsde/cxx/serializer/serializer-source.cxx b/xsde/cxx/serializer/serializer-source.cxx index 029fba3..e75a4ed 100644 --- a/xsde/cxx/serializer/serializer-source.cxx +++ b/xsde/cxx/serializer/serializer-source.cxx @@ -384,10 +384,10 @@ namespace CXX String const& arg (arg_type (e)); SemanticGraph::Type& base (e.inherits ().base ()); - Boolean facets (false); // Whether we need to set facets. + Boolean enum_facets (false); // Whether we need to set enum facets. if (validation) { - StringBasedType t (facets); + StringBasedType t (enum_facets); t.dispatch (e); } @@ -480,7 +480,7 @@ namespace CXX t.dispatch (base); } - if (facets) + if (enum_facets) { typedef Cult::Containers::Set Enums; Enums enums; -- cgit v1.1