ObjectKey_Table.cpp

Go to the documentation of this file.
00001 // $Id: ObjectKey_Table.cpp 76932 2007-02-06 16:34:57Z johnnyw $
00002 
00003 #include "tao/ObjectKey_Table.h"
00004 #include "tao/ORB_Core.h"
00005 #include "tao/Refcounted_ObjectKey.h"
00006 
00007 ACE_RCSID(tao,
00008           ObjectKey_Table,
00009           "$Id: ObjectKey_Table.cpp 76932 2007-02-06 16:34:57Z johnnyw $")
00010 
00011 
00012 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00013 
00014 bool
00015 TAO::Less_Than_ObjectKey::operator () (const TAO::ObjectKey &lhs,
00016                                        const TAO::ObjectKey &rhs) const
00017 {
00018   const CORBA::ULong rlen = rhs.length ();
00019   const CORBA::ULong llen = lhs.length ();
00020   if (llen < rlen)
00021     {
00022       return 1;
00023     }
00024   else if (llen > rlen)
00025     {
00026       return 0;
00027     }
00028 
00029   const CORBA::Octet * rhs_buff = rhs.get_buffer ();
00030   const CORBA::Octet * lhs_buff = lhs.get_buffer ();
00031   const bool result = (ACE_OS::memcmp (lhs_buff, rhs_buff, rlen) < 0);
00032 
00033   return result;
00034 }
00035 
00036 /********************************************************/
00037 TAO::ObjectKey_Table::ObjectKey_Table (void)
00038   : lock_ (0)
00039   , table_ ()
00040 {
00041 
00042 }
00043 
00044 TAO::ObjectKey_Table::~ObjectKey_Table (void)
00045 {
00046   this->table_.close ();
00047   delete this->lock_;
00048 }
00049 
00050 int
00051 TAO::ObjectKey_Table::init (TAO_ORB_Core *oc)
00052 {
00053   /// Create the lock that is needed for internal usage.
00054   this->lock_ =
00055     oc->resource_factory ()->create_object_key_table_lock ();
00056 
00057   return 0;
00058 }
00059 
00060 int
00061 TAO::ObjectKey_Table::bind (const TAO::ObjectKey &key,
00062                             TAO::Refcounted_ObjectKey *&key_new)
00063 
00064 {
00065   key_new = 0;
00066 
00067   int retval = 0;
00068 
00069   {
00070     ACE_GUARD_RETURN (ACE_Lock,
00071                       ace_mon,
00072                       *this->lock_,
00073                       0);
00074 
00075     // This is a tradeoff.. We could avoid this two stage process of
00076     // using a find () and then a bind () , which would make things
00077     // efficient. BUT we may have to do allocation upfront and delete if
00078     // bind () returns with an entry. We take one of the routes that
00079     // avoids allocation.
00080     retval = this->table_.find (key,
00081                                 key_new);
00082 
00083     if (retval == -1)
00084       {
00085         return this->bind_i (key,
00086                              key_new);
00087       }
00088 
00089     (void) key_new->incr_refcount ();
00090   }
00091 
00092   return retval;
00093 }
00094 
00095 int
00096 TAO::ObjectKey_Table::unbind (TAO::Refcounted_ObjectKey *&key_new)
00097 
00098 {
00099   ACE_GUARD_RETURN (ACE_Lock,
00100                     ace_mon,
00101                     *this->lock_,
00102                     0);
00103 
00104   // If the refcount has dropped to 1, just go ahead and unbind it
00105   // from the table.
00106   if (key_new && key_new->decr_refcount () == 1)
00107     {
00108       return this->unbind_i (key_new);
00109     }
00110 
00111   return 0;
00112 }
00113 
00114 int
00115 TAO::ObjectKey_Table::destroy (void)
00116 {
00117   if (this->table_.current_size ())
00118     {
00119       ACE_GUARD_RETURN (ACE_Lock,
00120                         ace_mon,
00121                         *this->lock_,
00122                         0);
00123 
00124       TABLE::ITERATOR end_iter = this->table_.end ();
00125       TABLE::ITERATOR start;
00126 
00127       while ((start = this->table_.begin ()) != end_iter)
00128         {
00129           TABLE::ENTRY &ent = (*start);
00130 
00131           ent.item ()->decr_refcount ();
00132           this->table_.unbind (&ent);
00133         }
00134     }
00135 
00136   return 0;
00137 }
00138 
00139 int
00140 TAO::ObjectKey_Table::bind_i (const TAO::ObjectKey &key,
00141                               TAO::Refcounted_ObjectKey *&key_new)
00142 {
00143   ACE_NEW_RETURN (key_new,
00144                   TAO::Refcounted_ObjectKey (key),
00145                   -1);
00146 
00147   int const retval =  this->table_.bind (key, key_new);
00148 
00149   if (retval != -1)
00150     {
00151       key_new->incr_refcount ();
00152     }
00153   else
00154     {
00155       key_new->decr_refcount ();
00156     }
00157 
00158   return retval;
00159 }
00160 
00161 int
00162 TAO::ObjectKey_Table::unbind_i (TAO::Refcounted_ObjectKey *&key_new)
00163 {
00164   TAO::Refcounted_ObjectKey *tmp = 0;
00165 
00166   if (this->table_.unbind (key_new->object_key (), tmp) != -1)
00167     {
00168       // @@ Cant do much if the unbind fails.
00169       // Remove our refcount on the ObjectKey
00170       (void) tmp->decr_refcount ();
00171     }
00172 
00173   return 0;
00174 }
00175 
00176 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:37:52 2010 for TAO by  doxygen 1.4.7