From 86ae5e2dfe39fb17f3d6c52a497a8309c9814111 Mon Sep 17 00:00:00 2001 From: Christian Bauer Date: Wed, 6 Aug 2003 18:43:52 +0000 Subject: [PATCH] use new-style print methods --- ginac/constant.cpp | 42 ++++++---- ginac/constant.h | 13 ++- ginac/idx.cpp | 193 +++++++++++++++++++++------------------------ ginac/idx.h | 18 ++++- ginac/symmetry.cpp | 101 ++++++++++++------------ ginac/symmetry.h | 8 +- 6 files changed, 190 insertions(+), 185 deletions(-) diff --git a/ginac/constant.cpp b/ginac/constant.cpp index d3c91588..baf10805 100644 --- a/ginac/constant.cpp +++ b/ginac/constant.cpp @@ -33,7 +33,11 @@ namespace GiNaC { -GINAC_IMPLEMENT_REGISTERED_CLASS(constant, basic) +GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(constant, basic, + print_func(&constant::do_print). + print_func(&constant::do_print_latex). + print_func(&constant::do_print_tree). + print_func(&constant::do_print_python_repr)) ////////// // default constructor @@ -108,21 +112,29 @@ void constant::archive(archive_node &n) const // public -void constant::print(const print_context & c, unsigned level) const +void constant::do_print(const print_context & c, unsigned level) const { - if (is_a(c)) { - c.s << std::string(level, ' ') << name << " (" << class_name() << ")" - << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec - << std::endl; - } else if (is_a(c)) { - c.s << TeX_name; - } else if (is_a(c)) { - c.s << class_name() << "('" << name << "'"; - if (TeX_name != "\\mbox{" + name + "}") - c.s << ",TeX_name='" << TeX_name << "'"; - c.s << ')'; - } else - c.s << name; + c.s << name; +} + +void constant::do_print_tree(const print_tree & c, unsigned level) const +{ + c.s << std::string(level, ' ') << name << " (" << class_name() << ")" + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << std::endl; +} + +void constant::do_print_latex(const print_latex & c, unsigned level) const +{ + c.s << TeX_name; +} + +void constant::do_print_python_repr(const print_python_repr & c, unsigned level) const +{ + c.s << class_name() << "('" << name << "'"; + if (TeX_name != "\\mbox{" + name + "}") + c.s << ",TeX_name='" << TeX_name << "'"; + c.s << ')'; } ex constant::evalf(int level) const diff --git a/ginac/constant.h b/ginac/constant.h index 0092ac3b..f44ec9dc 100644 --- a/ginac/constant.h +++ b/ginac/constant.h @@ -48,21 +48,20 @@ public: // functions overriding virtual functions from base classes public: - void print(const print_context & c, unsigned level = 0) const; ex evalf(int level = 0) const; protected: ex derivative(const symbol & s) const; bool is_equal_same_type(const basic & other) const; unsigned calchash() const; - // new virtual functions which can be overridden by derived classes - // none - // non-virtual functions in this class - // none - +protected: + void do_print(const print_context & c, unsigned level) const; + void do_print_tree(const print_tree & c, unsigned level) const; + void do_print_latex(const print_latex & c, unsigned level) const; + void do_print_python_repr(const print_python_repr & c, unsigned level) const; + // member variables - private: std::string name; ///< printname of this constant std::string TeX_name; ///< LaTeX name diff --git a/ginac/idx.cpp b/ginac/idx.cpp index 01692294..1b4bf6f5 100644 --- a/ginac/idx.cpp +++ b/ginac/idx.cpp @@ -35,9 +35,20 @@ namespace GiNaC { -GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic) -GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx) -GINAC_IMPLEMENT_REGISTERED_CLASS(spinidx, varidx) +GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(idx, basic, + print_func(&idx::do_print). + print_func(&idx::do_print_latex). + print_func(&idx::do_print_tree)) + +GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(varidx, idx, + print_func(&varidx::do_print). + // print_latex inherited from idx + print_func(&varidx::do_print_tree)) + +GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(spinidx, varidx, + print_func(&spinidx::do_print). + print_func(&spinidx::do_print_latex). + print_func(&spinidx::do_print_tree)) ////////// // default constructor @@ -123,120 +134,92 @@ DEFAULT_UNARCHIVE(spinidx) // functions overriding virtual functions from base classes ////////// -void idx::print(const print_context & c, unsigned level) const -{ - if (is_a(c)) { - - c.s << std::string(level, ' ') << class_name() - << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec - << std::endl; - unsigned delta_indent = static_cast(c).delta_indent; - value.print(c, level + delta_indent); - dim.print(c, level + delta_indent); - - } else { - - if (is_a(c)) - c.s << "{"; - else - c.s << "."; - bool need_parens = !(is_exactly_a(value) || is_a(value)); - if (need_parens) - c.s << "("; - value.print(c); - if (need_parens) - c.s << ")"; - if (c.options & print_options::print_index_dimensions) { - c.s << "["; - dim.print(c); - c.s << "]"; - } - if (is_a(c)) - c.s << "}"; +void idx::do_print_idx(const print_context & c, unsigned level) const +{ + bool need_parens = !(is_exactly_a(value) || is_a(value)); + if (need_parens) + c.s << "("; + value.print(c); + if (need_parens) + c.s << ")"; + if (c.options & print_options::print_index_dimensions) { + c.s << "["; + dim.print(c); + c.s << "]"; } } -void varidx::print(const print_context & c, unsigned level) const +void idx::do_print(const print_context & c, unsigned level) const { - if (is_a(c)) { + c.s << "."; + do_print_idx(c, level); +} - c.s << std::string(level, ' ') << class_name() - << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec - << (covariant ? ", covariant" : ", contravariant") - << std::endl; - unsigned delta_indent = static_cast(c).delta_indent; - value.print(c, level + delta_indent); - dim.print(c, level + delta_indent); +void idx::do_print_latex(const print_latex & c, unsigned level) const +{ + c.s << "{"; + do_print_idx(c, level); + c.s << "}"; +} - } else { - if (is_a(c)) - c.s << "{"; - else { - if (covariant) - c.s << "."; - else - c.s << "~"; - } - bool need_parens = !(is_exactly_a(value) || is_a(value)); - if (need_parens) - c.s << "("; - value.print(c); - if (need_parens) - c.s << ")"; - if (c.options & print_options::print_index_dimensions) { - c.s << "["; - dim.print(c); - c.s << "]"; - } - if (is_a(c)) - c.s << "}"; - } +void idx::do_print_tree(const print_tree & c, unsigned level) const +{ + c.s << std::string(level, ' ') << class_name() + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << std::endl; + value.print(c, level + c.delta_indent); + dim.print(c, level + c.delta_indent); } -void spinidx::print(const print_context & c, unsigned level) const +void varidx::do_print(const print_context & c, unsigned level) const { - if (is_a(c)) { + if (covariant) + c.s << "."; + else + c.s << "~"; + do_print_idx(c, level); +} - c.s << std::string(level, ' ') << class_name() - << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec - << (covariant ? ", covariant" : ", contravariant") - << (dotted ? ", dotted" : ", undotted") - << std::endl; - unsigned delta_indent = static_cast(c).delta_indent; - value.print(c, level + delta_indent); - dim.print(c, level + delta_indent); +void varidx::do_print_tree(const print_tree & c, unsigned level) const +{ + c.s << std::string(level, ' ') << class_name() + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << (covariant ? ", covariant" : ", contravariant") + << std::endl; + value.print(c, level + c.delta_indent); + dim.print(c, level + c.delta_indent); +} - } else { +void spinidx::do_print(const print_context & c, unsigned level) const +{ + if (covariant) + c.s << "."; + else + c.s << "~"; + if (dotted) + c.s << "*"; + do_print_idx(c, level); +} - bool is_tex = is_a(c); - if (is_tex) { - if (covariant) - c.s << "_{"; - else - c.s << "^{"; - } else { - if (covariant) - c.s << "."; - else - c.s << "~"; - } - if (dotted) { - if (is_tex) - c.s << "\\dot{"; - else - c.s << "*"; - } - bool need_parens = !(is_exactly_a(value) || is_a(value)); - if (need_parens) - c.s << "("; - value.print(c); - if (need_parens) - c.s << ")"; - if (is_tex && dotted) - c.s << "}"; - if (is_tex) - c.s << "}"; - } +void spinidx::do_print_latex(const print_latex & c, unsigned level) const +{ + if (dotted) + c.s << "\\dot{"; + else + c.s << "{"; + do_print_idx(c, level); + c.s << "}"; +} + +void spinidx::do_print_tree(const print_tree & c, unsigned level) const +{ + c.s << std::string(level, ' ') << class_name() + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << (covariant ? ", covariant" : ", contravariant") + << (dotted ? ", dotted" : ", undotted") + << std::endl; + value.print(c, level + c.delta_indent); + dim.print(c, level + c.delta_indent); } bool idx::info(unsigned inf) const diff --git a/ginac/idx.h b/ginac/idx.h index 985226d7..16c515f2 100644 --- a/ginac/idx.h +++ b/ginac/idx.h @@ -48,7 +48,6 @@ public: // functions overriding virtual functions from base classes public: - void print(const print_context & c, unsigned level = 0) const; bool info(unsigned inf) const; size_t nops() const; ex op(size_t i) const; @@ -94,6 +93,12 @@ public: * If this is undecidable, throw an exception. */ ex minimal_dim(const idx & other) const; +protected: + void do_print_idx(const print_context & c, unsigned level) const; + void do_print(const print_context & c, unsigned level) const; + void do_print_latex(const print_latex & c, unsigned level) const; + void do_print_tree(const print_tree & c, unsigned level) const; + protected: ex value; /**< Expression that constitutes the index (numeric or symbolic name) */ ex dim; /**< Dimension of space (can be symbolic or numeric) */ @@ -118,7 +123,6 @@ public: // functions overriding virtual functions from base classes public: - void print(const print_context & c, unsigned level = 0) const; bool is_dummy_pair_same_type(const basic & other) const; protected: @@ -135,6 +139,10 @@ public: /** Make a new index with the same value but the opposite variance. */ ex toggle_variance() const; +protected: + void do_print(const print_context & c, unsigned level) const; + void do_print_tree(const print_tree & c, unsigned level) const; + // member variables protected: bool covariant; /**< x.mu, default is contravariant: x~mu */ @@ -163,7 +171,6 @@ public: // functions overriding virtual functions from base classes public: - void print(const print_context & c, unsigned level = 0) const; bool is_dummy_pair_same_type(const basic & other) const; protected: @@ -185,6 +192,11 @@ public: * dottedness. */ ex toggle_variance_dot() const; +protected: + void do_print(const print_context & c, unsigned level) const; + void do_print_latex(const print_latex & c, unsigned level) const; + void do_print_tree(const print_tree & c, unsigned level) const; + // member variables protected: bool dotted; diff --git a/ginac/symmetry.cpp b/ginac/symmetry.cpp index 504a9fc4..6a50fe1d 100644 --- a/ginac/symmetry.cpp +++ b/ginac/symmetry.cpp @@ -34,7 +34,9 @@ namespace GiNaC { -GINAC_IMPLEMENT_REGISTERED_CLASS(symmetry, basic) +GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symmetry, basic, + print_func(&symmetry::do_print). + print_func(&symmetry::do_print_tree)) /* Some notes about the structure of a symmetry tree: @@ -146,63 +148,60 @@ int symmetry::compare_same_type(const basic & other) const return 0; } -void symmetry::print(const print_context & c, unsigned level) const +void symmetry::do_print(const print_context & c, unsigned level) const { - if (is_a(c)) { - - c.s << std::string(level, ' ') << class_name() - << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec - << ", type="; - + if (children.empty()) { + if (indices.size() > 0) + c.s << *(indices.begin()); + else + c.s << "none"; + } else { switch (type) { - case none: c.s << "none"; break; - case symmetric: c.s << "symm"; break; - case antisymmetric: c.s << "anti"; break; - case cyclic: c.s << "cycl"; break; - default: c.s << ""; break; + case none: c.s << '!'; break; + case symmetric: c.s << '+'; break; + case antisymmetric: c.s << '-'; break; + case cyclic: c.s << '@'; break; + default: c.s << '?'; break; } - - c.s << ", indices=("; - if (!indices.empty()) { - std::set::const_iterator i = indices.begin(), end = indices.end(); - --end; - while (i != end) - c.s << *i++ << ","; - c.s << *i; + c.s << '('; + size_t num = children.size(); + for (size_t i=0; i(c).delta_indent; - exvector::const_iterator i = children.begin(), end = children.end(); - while (i != end) { - i->print(c, level + delta_indent); - ++i; - } +void symmetry::do_print_tree(const print_tree & c, unsigned level) const +{ + c.s << std::string(level, ' ') << class_name() + << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << ", type="; + + switch (type) { + case none: c.s << "none"; break; + case symmetric: c.s << "symm"; break; + case antisymmetric: c.s << "anti"; break; + case cyclic: c.s << "cycl"; break; + default: c.s << ""; break; + } - } else { + c.s << ", indices=("; + if (!indices.empty()) { + std::set::const_iterator i = indices.begin(), end = indices.end(); + --end; + while (i != end) + c.s << *i++ << ","; + c.s << *i; + } + c.s << ")\n"; - if (children.empty()) { - if (indices.size() > 0) - c.s << *(indices.begin()); - else - c.s << "none"; - } else { - switch (type) { - case none: c.s << '!'; break; - case symmetric: c.s << '+'; break; - case antisymmetric: c.s << '-'; break; - case cyclic: c.s << '@'; break; - default: c.s << '?'; break; - } - c.s << '('; - size_t num = children.size(); - for (size_t i=0; iprint(c, level + c.delta_indent); + ++i; } } diff --git a/ginac/symmetry.h b/ginac/symmetry.h index 0b805edc..d5a3ca8d 100644 --- a/ginac/symmetry.h +++ b/ginac/symmetry.h @@ -61,10 +61,6 @@ public: /** Create node with two children. */ symmetry(symmetry_type t, const symmetry &c1, const symmetry &c2); - // functions overriding virtual functions from base classes -public: - void print(const print_context & c, unsigned level = 0) const; - // non-virtual functions in this class public: /** Get symmetry type. */ @@ -85,6 +81,10 @@ public: /** Check whether this node actually represents any kind of symmetry. */ bool has_symmetry() const {return type != none || !children.empty(); } +protected: + void do_print(const print_context & c, unsigned level) const; + void do_print_tree(const print_tree & c, unsigned level) const; + // member variables private: /** Type of symmetry described by this node. */ -- 2.44.0