ObjectKey_Table.cpp

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

Generated on Thu Nov 9 11:54:17 2006 for TAO by doxygen 1.3.6