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
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 71473 2006-03-10 07:19:20Z jtc $")
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 orbs_ (0),
00028 num_orbs_ (0)
00029 {
00030 }
00031
00032 int
00033 TAO::ORB_Table::bind (char const * orb_id,
00034 TAO_ORB_Core * orb_core)
00035 {
00036
00037
00038 if (orb_id == 0 || orb_core == 0)
00039 {
00040 errno = EINVAL;
00041 return -1;
00042 };
00043
00044 value_type const value =
00045 std::make_pair (key_type (orb_id), data_type (orb_core));
00046
00047 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00048 guard,
00049 this->lock_,
00050 -1);
00051
00052 std::pair<iterator, bool> result = this->table_.insert (value);
00053
00054 if (result.second)
00055 {
00056
00057
00058
00059 if (this->first_orb_ != 0
00060 && this->first_orb_not_default_)
00061 {
00062 this->first_orb_ = orb_core;
00063 this->first_orb_not_default_ = false;
00064 }
00065
00066
00067
00068 if (this->first_orb_ == 0)
00069 {
00070 this->first_orb_ = orb_core;
00071 }
00072 }
00073
00074 return (result.second ? 0 : 1);
00075 }
00076
00077 TAO_ORB_Core *
00078 TAO::ORB_Table::find (char const * orb_id)
00079 {
00080 TAO_ORB_Core * orb_core = 0;
00081
00082 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00083 guard,
00084 this->lock_,
00085 0);
00086
00087 iterator const i = this->table_.find (Table::key_type (orb_id));
00088
00089
00090 if (i != this->end ())
00091 {
00092 orb_core = (*i).second.core ();
00093 (void) orb_core->_incr_refcnt ();
00094 }
00095
00096 return orb_core;
00097 }
00098
00099 int
00100 TAO::ORB_Table::unbind (const char *orb_id)
00101 {
00102 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00103 guard,
00104 this->lock_,
00105 -1);
00106
00107 iterator const result = this->table_.find (key_type (orb_id));
00108
00109 if (result != this->end ())
00110 {
00111 TAO::ORB_Core_Ref_Counter oc ((*result).second);
00112
00113 this->table_.erase (result);
00114
00115 if (oc.core () == this->first_orb_)
00116 {
00117 if (!this->table_.empty ())
00118 {
00119 this->first_orb_ = (*this->begin ()).second.core ();
00120 }
00121 else
00122 {
00123 this->first_orb_ = 0;
00124 }
00125 }
00126 }
00127
00128 return 0;
00129 }
00130
00131 void
00132 TAO::ORB_Table::set_default (char const * orb_id)
00133 {
00134 ACE_GUARD (TAO_SYNCH_MUTEX,
00135 guard,
00136 this->lock_);
00137
00138 iterator const i = this->table_.find (key_type (orb_id));
00139
00140 if (i != this->end ())
00141 this->first_orb_ = (*i).second.core ();
00142 }
00143
00144 void
00145 TAO::ORB_Table::not_default (char const * orb_id)
00146 {
00147
00148
00149
00150
00151
00152
00153 ACE_GUARD (TAO_SYNCH_MUTEX,
00154 guard,
00155 this->lock_);
00156
00157
00158
00159
00160 if (this->first_orb_ != 0)
00161 {
00162 if (ACE_OS::strcmp (this->first_orb_->orbid (), orb_id) != 0)
00163 {
00164
00165 return;
00166 }
00167 else
00168 {
00169
00170
00171 this->first_orb_not_default_ = true;
00172 }
00173 }
00174 }
00175
00176 TAO::ORB_Table *
00177 TAO::ORB_Table::instance (void)
00178 {
00179 return TAO_Singleton<TAO::ORB_Table, TAO_SYNCH_MUTEX>::instance ();
00180 }
00181
00182 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
00183 template TAO_Singleton<TAO::ORB_Table,TAO_SYNCH_MUTEX> * TAO_Singleton<TAO::ORB_Table,TAO_SYNCH_MUTEX>::singleton_;
00184 #endif
00185
00186 TAO_END_VERSIONED_NAMESPACE_DECL