From: Christian Bauer Date: Tue, 26 Aug 2003 20:57:44 +0000 (+0000) Subject: generous use of auto_ptr to provide better exception safety and make the code X-Git-Tag: release_1-2-0~119 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=6d7bf9ee5a7ce05cb3a23dae664e781d7325d7b8 generous use of auto_ptr to provide better exception safety and make the code more expressive (doesn't that sound great?) --- diff --git a/ginac/add.cpp b/ginac/add.cpp index 3dcdf2cc..cb6c8031 100644 --- a/ginac/add.cpp +++ b/ginac/add.cpp @@ -86,13 +86,12 @@ add::add(const epvector & v, const ex & oc) GINAC_ASSERT(is_canonical()); } -add::add(epvector * vp, const ex & oc) +add::add(std::auto_ptr vp, const ex & oc) { tinfo_key = TINFO_add; GINAC_ASSERT(vp!=0); overall_coeff = oc; construct_from_epvector(*vp); - delete vp; GINAC_ASSERT(is_canonical()); } @@ -291,7 +290,7 @@ int add::ldegree(const ex & s) const ex add::coeff(const ex & s, int n) const { - epvector *coeffseq = new epvector(); + std::auto_ptr coeffseq(new epvector); // Calculate sum of coefficients in each term epvector::const_iterator i = seq.begin(), end = seq.end(); @@ -314,8 +313,8 @@ ex add::coeff(const ex & s, int n) const * @param level cut-off in recursive evaluation */ ex add::eval(int level) const { - epvector *evaled_seqp = evalchildren(level); - if (evaled_seqp) { + std::auto_ptr evaled_seqp = evalchildren(level); + if (evaled_seqp.get()) { // do more evaluation later return (new add(evaled_seqp, overall_coeff))-> setflag(status_flags::dynallocated); @@ -355,7 +354,7 @@ ex add::evalm() const { // Evaluate children first and add up all matrices. Stop if there's one // term that is not a matrix. - epvector *s = new epvector; + std::auto_ptr s(new epvector); s->reserve(seq.size()); bool all_matrices = true; @@ -377,10 +376,9 @@ ex add::evalm() const ++it; } - if (all_matrices) { - delete s; + if (all_matrices) return sum + overall_coeff; - } else + else return (new add(s, overall_coeff))->setflag(status_flags::dynallocated); } @@ -398,7 +396,7 @@ ex add::eval_ncmul(const exvector & v) const * @see ex::diff */ ex add::derivative(const symbol & y) const { - epvector *s = new epvector(); + std::auto_ptr s(new epvector); s->reserve(seq.size()); // Only differentiate the "rest" parts of the expairs. This is faster @@ -438,7 +436,7 @@ ex add::thisexpairseq(const epvector & v, const ex & oc) const return (new add(v,oc))->setflag(status_flags::dynallocated); } -ex add::thisexpairseq(epvector * vp, const ex & oc) const +ex add::thisexpairseq(std::auto_ptr vp, const ex & oc) const { return (new add(vp,oc))->setflag(status_flags::dynallocated); } @@ -508,8 +506,8 @@ ex add::recombine_pair_to_ex(const expair & p) const ex add::expand(unsigned options) const { - epvector *vp = expandchildren(options); - if (vp == NULL) { + std::auto_ptr vp = expandchildren(options); + if (vp.get() == 0) { // the terms have not changed, so it is safe to declare this expanded return (options == 0) ? setflag(status_flags::expanded) : *this; } diff --git a/ginac/add.h b/ginac/add.h index d636d0ad..f4862c58 100644 --- a/ginac/add.h +++ b/ginac/add.h @@ -41,7 +41,7 @@ public: add(const exvector & v); add(const epvector & v); add(const epvector & v, const ex & oc); - add(epvector * vp, const ex & oc); + add(std::auto_ptr vp, const ex & oc); // functions overriding virtual functions from base classes public: @@ -64,7 +64,7 @@ protected: unsigned return_type() const; unsigned return_type_tinfo() const; ex thisexpairseq(const epvector & v, const ex & oc) const; - ex thisexpairseq(epvector * vp, const ex & oc) const; + ex thisexpairseq(std::auto_ptr vp, const ex & oc) const; expair split_ex_to_pair(const ex & e) const; expair combine_ex_with_coeff_to_pair(const ex & e, const ex & c) const; diff --git a/ginac/clifford.cpp b/ginac/clifford.cpp index 3dbad50f..717e444c 100644 --- a/ginac/clifford.cpp +++ b/ginac/clifford.cpp @@ -104,7 +104,7 @@ clifford::clifford(unsigned char rl, const exvector & v, bool discardable) : inh tinfo_key = TINFO_clifford; } -clifford::clifford(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl) +clifford::clifford(unsigned char rl, std::auto_ptr vp) : inherited(sy_none(), vp), representation_label(rl) { tinfo_key = TINFO_clifford; } @@ -479,7 +479,7 @@ ex clifford::thiscontainer(const exvector & v) const return clifford(representation_label, v); } -ex clifford::thiscontainer(exvector * vp) const +ex clifford::thiscontainer(std::auto_ptr vp) const { return clifford(representation_label, vp); } diff --git a/ginac/clifford.h b/ginac/clifford.h index eebe5e1f..f551abb7 100644 --- a/ginac/clifford.h +++ b/ginac/clifford.h @@ -45,14 +45,14 @@ public: // internal constructors clifford(unsigned char rl, const exvector & v, bool discardable = false); - clifford(unsigned char rl, exvector * vp); // vp will be deleted + clifford(unsigned char rl, std::auto_ptr vp); // functions overriding virtual functions from base classes protected: ex eval_ncmul(const exvector & v) const; bool match_same_type(const basic & other) const; ex thiscontainer(const exvector & v) const; - ex thiscontainer(exvector * vp) const; + ex thiscontainer(std::auto_ptr vp) const; unsigned return_type() const { return return_types::noncommutative; } unsigned return_type_tinfo() const { return TINFO_clifford + representation_label; } diff --git a/ginac/color.cpp b/ginac/color.cpp index e84dcdba..9a364c43 100644 --- a/ginac/color.cpp +++ b/ginac/color.cpp @@ -94,7 +94,7 @@ color::color(unsigned char rl, const exvector & v, bool discardable) : inherited tinfo_key = TINFO_color; } -color::color(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl) +color::color(unsigned char rl, std::auto_ptr vp) : inherited(sy_none(), vp), representation_label(rl) { tinfo_key = TINFO_color; } @@ -183,7 +183,7 @@ ex color::thiscontainer(const exvector & v) const return color(representation_label, v); } -ex color::thiscontainer(exvector * vp) const +ex color::thiscontainer(std::auto_ptr vp) const { return color(representation_label, vp); } diff --git a/ginac/color.h b/ginac/color.h index 3af0c5c2..8ff75442 100644 --- a/ginac/color.h +++ b/ginac/color.h @@ -47,14 +47,14 @@ public: // internal constructors color(unsigned char rl, const exvector & v, bool discardable = false); - color(unsigned char rl, exvector * vp); // vp will be deleted + color(unsigned char rl, std::auto_ptr vp); // functions overriding virtual functions from base classes protected: ex eval_ncmul(const exvector & v) const; bool match_same_type(const basic & other) const; ex thiscontainer(const exvector & v) const; - ex thiscontainer(exvector * vp) const; + ex thiscontainer(std::auto_ptr vp) const; unsigned return_type() const { return return_types::noncommutative; } unsigned return_type_tinfo() const { return TINFO_color + representation_label; } diff --git a/ginac/container.h b/ginac/container.h index c6a72e27..e3465c46 100644 --- a/ginac/container.h +++ b/ginac/container.h @@ -28,6 +28,7 @@ #include #include #include +#include #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 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) @@ -291,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 vp) const { return container(vp); } virtual void printseq(const print_context & c, char openbracket, char delim, char closebracket, unsigned this_precedence, @@ -335,7 +330,7 @@ protected: 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 subschildren(const exmap & m, unsigned options = 0) const; }; /** Default constructor */ @@ -442,8 +437,8 @@ ex container::eval(int level) const template