ORB_Table.cpp

Go to the documentation of this file.
00001 #include "tao/ORB_Table.h"
00002 #include "tao/ORB_Core.h"
00003 #include "tao/TAO_Singleton.h"
00004 
00005 #if !defined (__ACE_INLINE__)
00006 # include "tao/ORB_Table.inl"
00007 #endif /* ! __ACE_INLINE__ */
00008 
00009 #include "ace/SString.h"
00010 #include "ace/OS_NS_string.h"
00011 
00012 
00013 ACE_RCSID (tao,
00014            ORB_Table,
00015            "$Id: ORB_Table.cpp 80823 2008-03-04 10:11:35Z johnnyw $")
00016 
00017 
00018 // ****************************************************************
00019 
00020 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00021 
00022 TAO::ORB_Table::ORB_Table (void)
00023   : lock_ (),
00024     first_orb_not_default_ (false),
00025     table_ (TAO_DEFAULT_ORB_TABLE_SIZE),
00026     first_orb_ (0)
00027 {
00028 }
00029 
00030 int
00031 TAO::ORB_Table::bind (char const * orb_id,
00032                       TAO_ORB_Core * orb_core)
00033 {
00034   // Make sure that the supplied ORB core pointer is valid,
00035   // i.e. non-zero.
00036   if (orb_id == 0 || orb_core == 0)
00037     {
00038       errno = EINVAL;
00039       return -1;
00040     };
00041 
00042   value_type const value =
00043     std::make_pair (key_type (orb_id), data_type (orb_core));
00044 
00045   ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00046                     guard,
00047                     this->lock_,
00048                     -1);
00049 
00050   std::pair<iterator, bool> result = this->table_.insert (value);
00051 
00052   if (result.second)
00053     {
00054       // This is not the first ORB, but if the current default ORB
00055       // decided not to be the default and there is more than one ORB
00056       // then set this ORB to be the default.
00057       if (this->first_orb_ != 0
00058           && this->first_orb_not_default_)
00059         {
00060           this->first_orb_ = orb_core;
00061           this->first_orb_not_default_ = false;
00062         }
00063 
00064       // Set the "first_orb_" member for the first given ORB Core
00065       // that was successfully added to the ORB table.
00066       if (this->first_orb_ == 0)
00067         {
00068           this->first_orb_ = orb_core;
00069         }
00070     }
00071 
00072   return (result.second ? 0 : 1);
00073 }
00074 
00075 TAO_ORB_Core *
00076 TAO::ORB_Table::find (char const * orb_id)
00077 {
00078   TAO_ORB_Core * orb_core = 0;
00079 
00080   ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00081                     guard,
00082                     this->lock_,
00083                     0);
00084 
00085   iterator const i = this->table_.find (Table::key_type (orb_id));
00086 
00087   // Maintain ownership of the ORB_Core.
00088   if (i != this->end ())
00089     {
00090       orb_core = (*i).second.core ();
00091       (void) orb_core->_incr_refcnt ();
00092     }
00093 
00094   return orb_core;
00095 }
00096 
00097 int
00098 TAO::ORB_Table::unbind (const char *orb_id)
00099 {
00100   ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00101                     guard,
00102                     this->lock_,
00103                     -1);
00104 
00105   iterator const result = this->table_.find (key_type (orb_id));
00106 
00107   if (result != this->end ())
00108     {
00109       TAO::ORB_Core_Ref_Counter oc ((*result).second);
00110 
00111       this->table_.erase (result);
00112 
00113       if (oc.core () == this->first_orb_)
00114         {
00115           if (!this->table_.empty ())
00116             {
00117               this->first_orb_ = (*this->begin ()).second.core ();
00118             }
00119           else
00120             {
00121               this->first_orb_ = 0;
00122             }
00123         }
00124     }
00125 
00126   return 0;
00127 }
00128 
00129 void
00130 TAO::ORB_Table::set_default (char const * orb_id)
00131 {
00132   ACE_GUARD (TAO_SYNCH_MUTEX,
00133              guard,
00134              this->lock_);
00135 
00136   iterator const i = this->table_.find (key_type (orb_id));
00137 
00138   if (i != this->end ())
00139     this->first_orb_ = (*i).second.core ();
00140 }
00141 
00142 void
00143 TAO::ORB_Table::not_default (char const * orb_id)
00144 {
00145   // @@  This method now works for restricted cases. Should work on
00146   //     generalizing it. It works if the first ORB that is registered
00147   //     decides to not want be the default ORB. Should generalize it
00148   //     to handle all cases.
00149 
00150 
00151   ACE_GUARD (TAO_SYNCH_MUTEX,
00152              guard,
00153              this->lock_);
00154 
00155   // Check if there is a default ORB already and if it is *not* the
00156   // same as the orb_id thats passed in.  We don't have to do
00157   // anything.
00158   if (this->first_orb_ != 0)
00159     {
00160       if (ACE_OS::strcmp (this->first_orb_->orbid (), orb_id) != 0)
00161         {
00162           // There is another default ORB. No need to change anything
00163           return;
00164         }
00165       else
00166         {
00167           // The ORB with orbid 'orb_id' is the default now. We need
00168           // to change it.
00169           this->first_orb_not_default_ = true;
00170         }
00171     }
00172 }
00173 
00174 TAO::ORB_Table *
00175 TAO::ORB_Table::instance (void)
00176 {
00177   return TAO_Singleton<TAO::ORB_Table, TAO_SYNCH_MUTEX>::instance ();
00178 }
00179 
00180 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
00181 template TAO_Singleton<TAO::ORB_Table,TAO_SYNCH_MUTEX> * TAO_Singleton<TAO::ORB_Table,TAO_SYNCH_MUTEX>::singleton_;
00182 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
00183 
00184 TAO_END_VERSIONED_NAMESPACE_DECL

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