GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Scruff the Cat       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


ptr.hh

    
#ifndef ALREADY_INCLUDED_2006_LIBD_PTR_HH
#define ALREADY_INCLUDED_2006_LIBD_PTR_HH

namespace dmp
{
   ///
   /// X has to subclass Single so that it has a property called
   /// _ref_count.
   ///
   /// Generally ptr appears without trailing * so that ptr's are deleted
   /// with the enclosing object...  with the exception for Arrays,
   /// but even they are deleted with the ~Array
   ///
   /// sizeof(ptr) == 4 bytes (therefore it fits into a register)
   ///
   /// assumes X class has
   ///
   /// (1) ~X()
   /// (2) _ref_count property
   ///
   /// uses X of ptr template should privately inherit from Single,
   /// declare ptr<X> as a friend of the class and declare ~X() to be
   /// private.
   ///

   template<class X>
   final class ptr
   {
   private:
      X* inner_ptr;

public:
      ptr()
      {
         inner_ptr = null;
      }

ptr(const ptr& p)
      {
         ASSERT(&p != null);
         inner_ptr = p.inner_ptr;
         if (inner_ptr != null)
         {
            inner_ptr->_ref_count++;
         }
      }

ptr& operator = (const ptr& p)
      {
         ASSERT(this != null);
         ASSERT(&p != null);
         //if (p.inner_ptr != null) // protect against s = s
         if (p != null) // protect against s = s
         {
            p.inner_ptr->_ref_count++;
         }
         if (inner_ptr != null)
         {
            assert(inner_ptr->_ref_count > 0); // protect against double deletions
            inner_ptr->_ref_count--;
            if (inner_ptr->_ref_count == 0)
            {
               inner_ptr->_ref_count = 1; // protect against internal deletions
               delete inner_ptr;
            }
         }
         if (p == null)
         {
            inner_ptr = null;
         }
         else
         {
            inner_ptr = p.inner_ptr;
         }
         return *this;
      }

~ptr()
      {
         ASSERT(this != null);
         ///
         /// NOTE: bugfix for davins-allegro-random-midi-player.ch global delete of ptr<List<String>>
         ///
#ifdef ALLEGRO_ONLINE
         if (!allegro_on)
         {
            return;
         }
#endif /* ALLEGRO_ONLINE */
         ASSERT(this != null);
         if (inner_ptr != null)
         {
            assert(inner_ptr->_ref_count > 0); // protect against double deletions
            inner_ptr->_ref_count--;
            if (inner_ptr->_ref_count == 0)
            {
               inner_ptr->_ref_count = 1; // protect against internal deletions
               delete inner_ptr;
            }
#ifndef NDEBUG
            inner_ptr = null;
#endif /* NDEBUG */
         }
      }

ptr(X* x)
      {
         inner_ptr = x;
         if (inner_ptr != null)
         {
            inner_ptr->_ref_count++;
         }
      }

      ///
      /// NOTE: not needed as operator = (const ptr&) serves this purpose
      ///
      /// COOL: cool how methods within a class can appear in any order
      ///
      // ptr& operator = (X* x)
      // {
      //    ASSERT(this != null);
      //    if (inner_ptr != null)
      //    {
      //       assert(inner_ptr->_ref_count > 0); // protect against double deletions
      //       inner_ptr->_ref_count--;
      //       if (inner_ptr->_ref_count == 0)
      //       {
      //          inner_ptr->_ref_count = 1; // protect against internal deletions
      //          delete inner_ptr;
      //       }
      //    }
      //    inner_ptr = x;
      //    if (inner_ptr != null)
      //    {
      //       inner_ptr->_ref_count++;
      //    }
      //    return *this;
      // }

      ///
      /// NOTE: Be careful not to store this pointer in a long-term
      /// variable or else it will wreck the auto-GC system...
      /// (Therefore the Lisp AutoGC system should search for such pointers)
      ///
      /// NOTE: fails if pointer is null, if null is OK, use method get_ptr()
      ///
      X* operator -> () const
      {
         ASSERT(this      != null);
         ASSERT(inner_ptr != null);
         return inner_ptr;
      }

X& operator * () const
      {
         ASSERT(this      != null);
         ASSERT(inner_ptr != null);
         return *inner_ptr;
      }

X* get_ptr() const
      {
         ASSERT(this != null);
         return inner_ptr;
      }

      ///
      /// NOTE: these methods are like Lisp's eq function (not Lisp's equal function)
      ///
      /// NOTE: g++ does not optimise these functions very well...
      ///
      friend bool operator == (const ptr& p1, const ptr& p2)
      {
         if ((&p1 == null) && (&p2 == null))
         {
            return true;
         }
         else if ((&p1 == null) || (&p2 == null))
         {
            return false;
         }
         else
         {
            return (p1.inner_ptr == p2.inner_ptr);
         }
      }
      friend bool operator != (const ptr& p1, const ptr& p2)
      {
         return !(p1 == p2);
      }

#ifdef DAVINS_IO_ONLINE

friend dmp::string_buffer operator + (const dmp::string& str, const ptr& p)
      {
         dmp::string_buffer sb;
         sb << str;
         sb << p.inner_ptr;
         return sb;
      }
      friend dmp::string_buffer operator + (const dmp::string_buffer& csb, const ptr& p)
      {
         dmp::string_buffer sb = const_cast<dmp::string_buffer&>(csb);
         sb << p.inner_ptr;
         return sb;
      }

      ///
      /// NOTE: operator << (Writer&, const ptr&) calls operator << (Writer&,const X&)
      ///
      friend dmp::Writer& operator << (dmp::Writer& w, const ptr<X>& p)
      {
         //CHECKPOINT;
         ASSERT(&w != null);
         ASSERT(&p != null);
         if (p.inner_ptr == null)
         {
            w << "null";
         }
         else
         {
            //CHECKPOINT;
            w << *(p.inner_ptr);
            //CHECKPOINT;
         }
         return w;
      }
      friend dmp::Reader& operator >> (dmp::Reader& r, ptr<X>& p)
      {
         //CHECKPOINT;
         ASSERT(&r != null);
         ASSERT(&p != null);
         //ASSERT(p.inner_ptr == null);
         if (r.looking_at("null"))
         {
            p = null;
            r.next_token(); // more efficient than r.skip("null");
         }
         else
         {
            //CHECKPOINT;
            r >> *(p.inner_ptr);
            //CHECKPOINT;
         }
         //CHECKPOINT;
         return r;
      }
#endif /* DAVINS_IO_ONLINE */
   };
}

#endif /* ALREADY_INCLUDED_2006_LIBD_PTR_HH */
Back
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sat Oct 29 20:43:12 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to