Ipopt  trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
IpReferenced.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2004, 2006 International Business Machines and others.
00002 // All Rights Reserved.
00003 // This code is published under the Eclipse Public License.
00004 //
00005 // $Id$
00006 //
00007 // Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13
00008 
00009 #ifndef __IPREFERENCED_HPP__
00010 #define __IPREFERENCED_HPP__
00011 
00012 #include "IpTypes.hpp"
00013 #include "IpDebug.hpp"
00014 
00015 #include <list>
00016 
00017 #if COIN_IPOPT_CHECKLEVEL > 3
00018   #define IP_DEBUG_REFERENCED
00019 #endif
00020 
00021 namespace Ipopt
00022 {
00023 
00027   class Referencer
00028     {}
00029   ;
00030 
00174   class ReferencedObject
00175   {
00176   public:
00177     ReferencedObject()
00178         :
00179         reference_count_(0)
00180     {}
00181 
00182     virtual ~ReferencedObject()
00183     {
00184       DBG_ASSERT(reference_count_ == 0);
00185     }
00186 
00187     inline
00188     Index ReferenceCount() const;
00189 
00190     inline
00191     void AddRef(const Referencer* referencer) const;
00192 
00193     inline
00194     void ReleaseRef(const Referencer* referencer) const;
00195 
00196   private:
00197     mutable Index reference_count_;
00198 
00199 #   ifdef IP_DEBUG_REFERENCED
00200     mutable std::list<const Referencer*> referencers_;
00201 #   endif
00202 
00203   };
00204 
00205   /* inline methods */
00206   inline
00207   Index ReferencedObject::ReferenceCount() const
00208   {
00209     //    DBG_START_METH("ReferencedObject::ReferenceCount()", 0);
00210     //    DBG_PRINT((1,"Returning reference_count_ = %d\n", reference_count_));
00211     return reference_count_;
00212   }
00213 
00214   inline
00215   void ReferencedObject::AddRef(const Referencer* referencer) const
00216   {
00217     //    DBG_START_METH("ReferencedObject::AddRef(const Referencer* referencer)", 0);
00218     reference_count_++;
00219     //    DBG_PRINT((1, "New reference_count_ = %d\n", reference_count_));
00220 #   ifdef IP_DEBUG_REFERENCED
00221     referencers_.push_back(referencer);
00222 #   endif
00223 
00224   }
00225 
00226   inline
00227   void ReferencedObject::ReleaseRef(const Referencer* referencer) const
00228   {
00229     //    DBG_START_METH("ReferencedObject::ReleaseRef(const Referencer* referencer)",
00230     //                   0);
00231     reference_count_--;
00232     //    DBG_PRINT((1, "New reference_count_ = %d\n", reference_count_));
00233 
00234 #   ifdef IP_DEBUG_REFERENCED
00235 
00236     bool found = false;
00237     std::list<const Referencer*>::iterator iter;
00238     for (iter = referencers_.begin(); iter != referencers_.end(); iter++) {
00239       if ((*iter) == referencer) {
00240         found = true;
00241         break;
00242       }
00243     }
00244 
00245     // cannot call release on a reference that was never added...
00246     DBG_ASSERT(found);
00247 
00248     if (found) {
00249       referencers_.erase(iter);
00250     }
00251 #   endif
00252 
00253   }
00254 
00255 
00256 } // namespace Ipopt
00257 
00258 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines