Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / tao / ORB_Table.cpp
blob174c9272ecf64cea35a42327c795bae33292b484
1 #include "tao/ORB_Table.h"
2 #include "tao/ORB_Core.h"
3 #include "tao/TAO_Singleton.h"
5 #if !defined (__ACE_INLINE__)
6 # include "tao/ORB_Table.inl"
7 #endif /* ! __ACE_INLINE__ */
9 #include "ace/SString.h"
10 #include "ace/OS_NS_string.h"
12 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
14 TAO::ORB_Table::ORB_Table ()
15 : lock_ (),
16 first_orb_not_default_ (false),
17 table_ (TAO_DEFAULT_ORB_TABLE_SIZE),
18 first_orb_ (nullptr)
22 int
23 TAO::ORB_Table::bind (char const * orb_id,
24 TAO_ORB_Core * orb_core)
26 // Make sure that the supplied ORB core pointer is valid,
27 // i.e. non-zero.
28 if (orb_id == nullptr || orb_core == nullptr)
30 errno = EINVAL;
31 return -1;
34 value_type const value =
35 std::make_pair (key_type (orb_id), data_type (orb_core));
37 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
38 guard,
39 this->lock_,
40 -1);
42 std::pair<iterator, bool> result = this->table_.insert (value);
44 if (result.second)
46 // This is not the first ORB, but if the current default ORB
47 // decided not to be the default and there is more than one ORB
48 // then set this ORB to be the default.
49 if (this->first_orb_ != nullptr
50 && this->first_orb_not_default_)
52 this->first_orb_ = orb_core;
53 this->first_orb_not_default_ = false;
56 // Set the "first_orb_" member for the first given ORB Core
57 // that was successfully added to the ORB table.
58 if (this->first_orb_ == nullptr)
60 this->first_orb_ = orb_core;
64 return (result.second ? 0 : 1);
67 TAO_ORB_Core *
68 TAO::ORB_Table::find (char const * orb_id)
70 TAO_ORB_Core * orb_core = nullptr;
72 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
73 guard,
74 this->lock_,
75 nullptr);
77 iterator const i = this->table_.find (Table::key_type (orb_id));
79 // Maintain ownership of the ORB_Core.
80 if (i != this->end ())
82 orb_core = (*i).second.core ();
83 (void) orb_core->_incr_refcnt ();
86 return orb_core;
89 int
90 TAO::ORB_Table::unbind (const char *orb_id)
92 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
93 guard,
94 this->lock_,
95 -1);
97 iterator const result = this->table_.find (key_type (orb_id));
99 if (result != this->end ())
101 TAO::ORB_Core_Ref_Counter oc ((*result).second);
103 this->table_.erase (result);
105 if (oc.core () == this->first_orb_)
107 if (!this->table_.empty ())
109 this->first_orb_ = (*this->begin ()).second.core ();
111 else
113 this->first_orb_ = nullptr;
118 return 0;
121 void
122 TAO::ORB_Table::set_default (char const * orb_id)
124 ACE_GUARD (TAO_SYNCH_MUTEX,
125 guard,
126 this->lock_);
128 iterator const i = this->table_.find (key_type (orb_id));
130 if (i != this->end ())
131 this->first_orb_ = (*i).second.core ();
134 void
135 TAO::ORB_Table::not_default (char const * orb_id)
137 // @@ This method now works for restricted cases. Should work on
138 // generalizing it. It works if the first ORB that is registered
139 // decides to not want be the default ORB. Should generalize it
140 // to handle all cases.
141 ACE_GUARD (TAO_SYNCH_MUTEX,
142 guard,
143 this->lock_);
145 // Check if there is a default ORB already and if it is *not* the
146 // same as the orb_id thats passed in. We don't have to do
147 // anything.
148 if (this->first_orb_ != nullptr)
150 if (ACE_OS::strcmp (this->first_orb_->orbid (), orb_id) != 0)
152 // There is another default ORB. No need to change anything
153 return;
155 else
157 // The ORB with orbid 'orb_id' is the default now. We need
158 // to change it.
159 this->first_orb_not_default_ = true;
164 TAO::ORB_Table *
165 TAO::ORB_Table::instance ()
167 return TAO_Singleton<TAO::ORB_Table, TAO_SYNCH_MUTEX>::instance ();
170 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
171 template TAO_Singleton<TAO::ORB_Table,TAO_SYNCH_MUTEX> * TAO_Singleton<TAO::ORB_Table,TAO_SYNCH_MUTEX>::singleton_;
172 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
174 TAO_END_VERSIONED_NAMESPACE_DECL