use new-style print methods
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Wed, 6 Aug 2003 18:43:52 +0000 (18:43 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Wed, 6 Aug 2003 18:43:52 +0000 (18:43 +0000)
ginac/constant.cpp
ginac/constant.h
ginac/idx.cpp
ginac/idx.h
ginac/symmetry.cpp
ginac/symmetry.h

index d3c9158..baf1080 100644 (file)
 
 namespace GiNaC {
 
-GINAC_IMPLEMENT_REGISTERED_CLASS(constant, basic)
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(constant, basic,
+  print_func<print_context>(&constant::do_print).
+  print_func<print_latex>(&constant::do_print_latex).
+  print_func<print_tree>(&constant::do_print_tree).
+  print_func<print_python_repr>(&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<print_tree>(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<print_latex>(c)) {
-               c.s << TeX_name;
-       } else if (is_a<print_python_repr>(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
index 0092ac3..f44ec9d 100644 (file)
@@ -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
index 0169229..1b4bf6f 100644 (file)
 
 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<print_context>(&idx::do_print).
+  print_func<print_latex>(&idx::do_print_latex).
+  print_func<print_tree>(&idx::do_print_tree))
+
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(varidx, idx,
+  print_func<print_context>(&varidx::do_print).
+  // print_latex inherited from idx
+  print_func<print_tree>(&varidx::do_print_tree))
+
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(spinidx, varidx,
+  print_func<print_context>(&spinidx::do_print).
+  print_func<print_latex>(&spinidx::do_print_latex).
+  print_func<print_tree>(&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<print_tree>(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<const print_tree &>(c).delta_indent;
-               value.print(c, level + delta_indent);
-               dim.print(c, level + delta_indent);
-
-       } else {
-
-               if (is_a<print_latex>(c))
-                       c.s << "{";
-               else
-                       c.s << ".";
-               bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(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<print_latex>(c))
-                       c.s << "}";
+void idx::do_print_idx(const print_context & c, unsigned level) const
+{
+       bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(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<print_tree>(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<const print_tree &>(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<print_latex>(c))
-                       c.s << "{";
-               else {
-                       if (covariant)
-                               c.s << ".";
-                       else
-                               c.s << "~";
-               }
-               bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(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<print_latex>(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<print_tree>(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<const print_tree &>(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<print_latex>(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<numeric>(value) || is_a<symbol>(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
index 985226d..16c515f 100644 (file)
@@ -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;
index 504a9fc..6a50fe1 100644 (file)
@@ -34,7 +34,9 @@
 
 namespace GiNaC {
 
-GINAC_IMPLEMENT_REGISTERED_CLASS(symmetry, basic)
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symmetry, basic,
+  print_func<print_context>(&symmetry::do_print).
+  print_func<print_tree>(&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<print_tree>(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 << "<unknown>"; 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<unsigned>::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<num; i++) {
+                       children[i].print(c);
+                       if (i != num - 1)
+                               c.s << ",";
                }
-               c.s << ")\n";
+               c.s << ')';
+       }
+}
 
-               unsigned delta_indent = static_cast<const print_tree &>(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 << "<unknown>"; break;
+       }
 
-       } else {
+       c.s << ", indices=(";
+       if (!indices.empty()) {
+               std::set<unsigned>::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; i<num; i++) {
-                               children[i].print(c);
-                               if (i != num - 1)
-                                       c.s << ",";
-                       }
-                       c.s << ')';
-               }
+       exvector::const_iterator i = children.begin(), end = children.end();
+       while (i != end) {
+               i->print(c, level + c.delta_indent);
+               ++i;
        }
 }
 
index 0b805ed..d5a3ca8 100644 (file)
@@ -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. */