]> www.ginac.de Git - ginac.git/blobdiff - ginac/container.h
generous use of auto_ptr to provide better exception safety and make the code
[ginac.git] / ginac / container.h
index a6dea4a6db38cf5957a2b0000dfbcd8848d90e72..e3465c46decb803392b255cc1c364c5df9b51d08 100644 (file)
@@ -28,6 +28,7 @@
 #include <algorithm>
 #include <vector>
 #include <list>
+#include <memory>
 
 #include "ex.h"
 #include "print.h"
@@ -93,15 +94,9 @@ public:
                        this->seq = s;
        }
 
-       explicit container(STLT * vp) : inherited(get_tinfo())
+       explicit container(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
        {
-               if (vp == 0) {
-                       // lst(0) ends up here
-                       this->seq.push_back(0);
-               } else {
-                       this->seq.swap(*vp);
-                       delete vp;
-               }
+               this->seq.swap(*vp);
        }
 
        container(exvector::const_iterator b, exvector::const_iterator e)
@@ -272,7 +267,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 { return inherited::info(inf); }
        unsigned precedence() const { return 10; }
        size_t nops() const { return this->seq.size(); }
@@ -292,7 +286,7 @@ protected:
 
        /** Similar to duplicate(), but with a preset sequence (which gets
         *  deleted). Must be overridden by derived classes. */
-       virtual ex thiscontainer(STLT * vp) const { return container(vp); }
+       virtual ex thiscontainer(std::auto_ptr<STLT> vp) const { return container(vp); }
 
        virtual void printseq(const print_context & c, char openbracket, char delim,
                              char closebracket, unsigned this_precedence,
@@ -331,8 +325,12 @@ public:
        const_reverse_iterator rend() const {return this->seq.rend();}
 
 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_python(const print_python & c, unsigned level) const;
+       void do_print_python_repr(const print_python_repr & c, unsigned level) const;
        STLT evalchildren(int level) const;
-       STLT *subschildren(const exmap & m, unsigned options = 0) const;
+       std::auto_ptr<STLT> subschildren(const exmap & m, unsigned options = 0) const;
 };
 
 /** Default constructor */
@@ -372,29 +370,38 @@ void container<C>::archive(archive_node &n) const
 }
 
 template <template <class> class C>
-void container<C>::print(const print_context & c, unsigned level) const
+void container<C>::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
-                   << ", nops=" << nops()
-                   << std::endl;
-               unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
-               const_iterator i = this->seq.begin(), end = this->seq.end();
-               while (i != end) {
-                       i->print(c, level + delta_indent);
-                       ++i;
-               }
-               c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
-       } else if (is_a<print_python>(c)) {
-               printseq(c, '[', ',', ']', precedence(), precedence()+1);
-       } else if (is_a<print_python_repr>(c)) {
-               c.s << class_name ();
-               printseq(c, '(', ',', ')', precedence(), precedence()+1);
-       } else {
-               // always print brackets around seq, ignore upper_precedence
-               printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
+       // always print brackets around seq, ignore upper_precedence
+       printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
+}
+
+template <template <class> class C>
+void container<C>::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
+           << ", nops=" << nops()
+           << std::endl;
+       const_iterator i = this->seq.begin(), end = this->seq.end();
+       while (i != end) {
+               i->print(c, level + c.delta_indent);
+               ++i;
        }
+       c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
+}
+
+template <template <class> class C>
+void container<C>::do_print_python(const print_python & c, unsigned level) const
+{
+       printseq(c, '[', ',', ']', precedence(), precedence()+1);
+}
+
+template <template <class> class C>
+void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
+{
+       c.s << class_name();
+       printseq(c, '(', ',', ')', precedence(), precedence()+1);
 }
 
 template <template <class> class C>
@@ -430,8 +437,8 @@ ex container<C>::eval(int level) const
 template <template <class> class C>
 ex container<C>::subs(const exmap & m, unsigned options) const
 {
-       STLT *vp = subschildren(m, options);
-       if (vp)
+       std::auto_ptr<STLT> vp = subschildren(m, options);
+       if (vp.get())
                return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
        else
                return subs_one_level(m, options);
@@ -591,11 +598,11 @@ typename container<C>::STLT container<C>::evalchildren(int level) const
 }
 
 template <template <class> class C>
-typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigned options) const
+std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exmap & m, unsigned options) const
 {
        // returns a NULL pointer if nothing had to be substituted
        // returns a pointer to a newly created epvector otherwise
-       // (which has to be deleted somewhere else)
+       // (and relinquishes responsibility for the epvector)
 
        const_iterator cit = this->seq.begin(), end = this->seq.end();
        while (cit != end) {
@@ -603,7 +610,7 @@ typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigne
                if (!are_ex_trivially_equal(*cit, subsed_ex)) {
 
                        // copy first part of seq which hasn't changed
-                       STLT *s = new STLT(this->seq.begin(), cit);
+                       std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
                        reserve(*s, this->seq.size());
 
                        // insert changed element
@@ -622,7 +629,7 @@ typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigne
                ++cit;
        }
        
-       return 0; // nothing has changed
+       return std::auto_ptr<STLT>(0); // nothing has changed
 }
 
 } // namespace GiNaC