]> www.ginac.de Git - ginac.git/blobdiff - ginac/ex.h
Modification in output of last returned expression (for ginaccint)
[ginac.git] / ginac / ex.h
index c07c3edb79d9ce62059b3e1f341f3868f6e00d9a..313bd6c76ea5f2edadbec496322a2f8090c6e503 100644 (file)
@@ -1,8 +1,9 @@
 /** @file ex.h
  *
- *  Interface to GiNaC's light-weight expression handles.
- *
- *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ *  Interface to GiNaC's light-weight expression handles. */
+
+/*
+ *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include <ginac/basic.h>
 #include <ginac/operators.h>
 
+#ifndef NO_GINAC_NAMESPACE
+namespace GiNaC {
+#endif // ndef NO_GINAC_NAMESPACE
+
 class ex;
 class expand_options;
 class status_flags;
@@ -33,21 +38,13 @@ class status_flags;
 class symbol;
 class lst;
 
-typedef vector<ex> exvector;
+extern ex const & _ex0(void);  /* FIXME: should this pollute headers? */
 
-// enum definitions
-
-ex const & exZERO(void);
-ex const & exONE(void);
-ex const & exTWO(void);
-ex const & exTHREE(void);
-ex const & exMINUSONE(void);
-ex const & exHALF(void);
-ex const & exMINUSHALF(void);
+// typedef vector<ex> exvector;
 
 #define INLINE_EX_CONSTRUCTORS
 
-/** Lightweight interface to GiNaC's symbolic objects. Basically all it does is
+/** Lightweight wrapper for GiNaC's symbolic objects.  Basically all it does is
  *  to hold a pointer to the other objects, manage the reference counting and
  *  provide methods for manipulation of these objects. */
 class ex
@@ -60,13 +57,15 @@ class ex
 public:
     ex()
 #ifdef INLINE_EX_CONSTRUCTORS
-    : bp(exZERO().bp)
+    : bp(_ex0().bp)
         {
-            debugmsg("ex default constructor",LOGLEVEL_CONSTRUCT);
-            ASSERT(exZERO().bp!=0);
-            ASSERT(exZERO().bp->flags & status_flags::dynallocated);
-            ASSERT(bp!=0);
+            GINAC_ASSERT(_ex0().bp!=0);
+            GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
+            GINAC_ASSERT(bp!=0);
             ++bp->refcount;
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
         }
 #else
 ;
@@ -75,9 +74,8 @@ public:
     ~ex()
 #ifdef INLINE_EX_CONSTRUCTORS
         {
-            debugmsg("ex destructor",LOGLEVEL_DESTRUCT);
-            ASSERT(bp!=0);
-            ASSERT(bp->flags & status_flags::dynallocated);
+            GINAC_ASSERT(bp!=0);
+            GINAC_ASSERT(bp->flags & status_flags::dynallocated);
             if (--bp->refcount == 0) {
                 delete bp;
             }
@@ -90,10 +88,12 @@ public:
 #ifdef INLINE_EX_CONSTRUCTORS
     : bp(other.bp)
         {
-            debugmsg("ex copy constructor",LOGLEVEL_CONSTRUCT);
-            ASSERT(bp!=0);
-            ASSERT((bp->flags) & status_flags::dynallocated);
+            GINAC_ASSERT(bp!=0);
+            GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
             ++bp->refcount;
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
         }
 #else
 ;
@@ -102,17 +102,19 @@ public:
     ex const & operator=(ex const & other)
 #ifdef INLINE_EX_CONSTRUCTORS
         {
-            debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);
-            ASSERT(bp!=0);
-            ASSERT(bp->flags & status_flags::dynallocated);
-            ASSERT(other.bp!=0);
-            ASSERT(other.bp->flags & status_flags::dynallocated);
+            GINAC_ASSERT(bp!=0);
+            GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+            GINAC_ASSERT(other.bp!=0);
+            GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
             ++other.bp->refcount;
             basic * tmpbp=other.bp;
             if (--bp->refcount==0) {
                 delete bp;
             }
             bp=tmpbp;
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
             return *this;
         }
 #else
@@ -123,10 +125,12 @@ public:
 public:
     ex(basic const & other)
 #ifdef INLINE_EX_CONSTRUCTORS
-    {
-        debugmsg("ex constructor from basic",LOGLEVEL_CONSTRUCT);
-        construct_from_basic(other);
-    }
+        {
+            construct_from_basic(other);
+#ifdef OBSCURE_CINT_HACK
+            update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+        }
 #else
 ;
 #endif // def INLINE_EX_CONSTRUCTORS
@@ -153,7 +157,7 @@ public:
     void dbgprint(void) const;
     void dbgprinttree(void) const;
     bool info(unsigned inf) const;
-    int nops() const;
+    unsigned nops() const;
     ex expand(unsigned options=0) const;
     bool has(ex const & other) const;
     int degree(symbol const & s) const;
@@ -187,8 +191,8 @@ public:
     int compare(ex const & other) const
 #ifdef INLINE_EX_CONSTRUCTORS
         {
-            ASSERT(bp!=0);
-            ASSERT(other.bp!=0);
+            GINAC_ASSERT(bp!=0);
+            GINAC_ASSERT(other.bp!=0);
             if (bp==other.bp) {
                 // special case: both expression point to same basic, trivially equal
                 return 0; 
@@ -201,8 +205,8 @@ public:
     bool is_equal(ex const & other) const
 #ifdef INLINE_EX_CONSTRUCTORS
         {
-            ASSERT(bp!=0);
-            ASSERT(other.bp!=0);
+            GINAC_ASSERT(bp!=0);
+            GINAC_ASSERT(other.bp!=0);
             if (bp==other.bp) {
                 // special case: both expression point to same basic, trivially equal
                 return true; 
@@ -212,7 +216,7 @@ public:
 #else
 ;
 #endif // def INLINE_EX_CONSTRUCTORS
-    bool is_zero(void) const {return compare(exZERO()) == 0;};
+    bool is_zero(void) const {return compare(_ex0())==0;};
         
     unsigned return_type(void) const;
     unsigned return_type_tinfo(void) const;
@@ -225,15 +229,47 @@ private:
     void construct_from_basic(basic const & other);
     void makewriteable();
 
+#ifdef OBSCURE_CINT_HACK
+public:
+    static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
+        {
+            if (last_created_or_assigned_bp==0) return false;
+            if ((last_created_or_assigned_bp->flags &
+                 status_flags::dynallocated)==0) return false;
+            if ((last_created_or_assigned_bp->flags &
+                 status_flags::evaluated)==0) return false;
+            return true;
+        }
+protected:
+    void update_last_created_or_assigned_bp(void)
+        {
+            if (last_created_or_assigned_bp!=0) {
+                if (--last_created_or_assigned_bp->refcount == 0) {
+                    delete last_created_or_assigned_bp;
+                }
+            }
+            last_created_or_assigned_bp=bp;
+            ++last_created_or_assigned_bp->refcount;
+        }
+#endif // def OBSCURE_CINT_HACK
+
 // member variables
 
 public:
     basic *bp;
-
+#ifdef OBSCURE_CINT_HACK
+    static basic *last_created_or_assigned_bp;
+#endif // def OBSCURE_CINT_HACK
 };
 
+// utility functions
+inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
+{
+       return e1.bp == e2.bp;
+}
+
 // wrapper functions around member functions
-inline int nops(ex const & thisex)
+inline unsigned nops(ex const & thisex)
 { return thisex.nops(); }
 
 inline ex expand(ex const & thisex, unsigned options = 0)
@@ -281,4 +317,9 @@ inline ex subs(ex const & thisex, lst const & ls, lst const & lr)
 inline void swap(ex & e1, ex & e2)
 { e1.swap(e2); }
 
+#ifndef NO_GINAC_NAMESPACE
+} // namespace GiNaC
+#endif // ndef NO_GINAC_NAMESPACE
+
 #endif // ndef __GINAC_EX_H__
+