Token_Collection.cpp

Go to the documentation of this file.
00001 #include "ace/Token_Collection.h"
00002 
00003 #if defined (ACE_HAS_TOKENS_LIBRARY)
00004 
00005 #if !defined (__ACE_INLINE__)
00006 #include "ace/Token_Collection.inl"
00007 #endif /* __ACE_INLINE__ */
00008 
00009 
00010 ACE_RCSID (ace,
00011            Token_Collection,
00012            "Token_Collection.cpp,v 4.21 2006/04/19 19:13:09 jwillemsen Exp")
00013 
00014 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 ACE_Token_Collection::ACE_Token_Collection (int debug,
00017                                             const ACE_TCHAR *name)
00018 : debug_ (debug)
00019 {
00020   ACE_TRACE ("ACE_Token_Collection::ACE_Token_Collection");
00021 
00022   if (name == 0)
00023     name = ACE_LIB_TEXT ("no name");
00024 
00025   ACE_OS::strsncpy (this->name_,
00026                     const_cast<ACE_TCHAR *> (name),
00027                     ACE_MAXTOKENNAMELEN);
00028 }
00029 
00030 int
00031 ACE_Token_Collection::insert (ACE_Token_Proxy &new_token)
00032 {
00033   ACE_TRACE ("ACE_Token_Collection::insert");
00034 
00035   TOKEN_NAME name (new_token.name ());
00036 
00037   // Check if the new_proxy is already in the list.
00038   if (collection_.find (name) == 1)
00039     // One already exists, so fail.
00040     return -1;
00041 
00042   // Clone the new token.
00043   ACE_Token_Proxy *temp = new_token.clone ();
00044 
00045   if (collection_.bind (name, temp) == -1)
00046     ACE_ERROR_RETURN ((LM_ERROR, ACE_LIB_TEXT ("bind failed\n")), -1);
00047   return 0;
00048 }
00049 
00050 int
00051 ACE_Token_Collection::extract (const ACE_TCHAR *token_name, ACE_Token_Proxy *&proxy)
00052 {
00053   ACE_TRACE ("ACE_Token_Collection::extract");
00054   TOKEN_NAME name (token_name);
00055   return collection_.unbind (token_name, proxy);
00056 }
00057 
00058 ACE_Token_Proxy *
00059 ACE_Token_Collection::is_member (const ACE_TCHAR *token_name)
00060 {
00061   ACE_TRACE ("ACE_Token_Collection::is_member");
00062   TOKEN_NAME name (token_name);
00063   ACE_Token_Proxy *temp;
00064   // Get the token from the collection.
00065   return collection_.find (name, temp) == -1 ? 0 : temp;
00066 }
00067 
00068 int
00069 ACE_Token_Collection::is_member (const ACE_Token_Proxy &token)
00070 {
00071   ACE_TRACE ("ACE_Token_Collection::is_member");
00072   TOKEN_NAME token_name (token.name ());
00073   return collection_.find (token_name) == 0;
00074 }
00075 
00076 int
00077 ACE_Token_Collection::acquire (int notify,
00078                                void (*sleep_hook)(void *),
00079                                ACE_Synch_Options &options)
00080 {
00081   ACE_TRACE ("ACE_Token_Collection::acquire");
00082 
00083   COLLECTION::ITERATOR iterator (collection_);
00084 
00085   for (COLLECTION::ENTRY *temp = 0;
00086        iterator.next (temp) != 0;
00087        iterator.advance ())
00088     {
00089       if (debug_)
00090         ACE_DEBUG ((LM_DEBUG,
00091                     ACE_LIB_TEXT ("collection acquiring %s\n"),
00092                     temp->int_id_->name ()));
00093       if (temp->int_id_->acquire (notify,
00094                                   sleep_hook,
00095                                   options) == -1)
00096         {
00097           // Save/restore errno.
00098           ACE_Errno_Guard error (errno);
00099           this->release ();
00100           ACE_RETURN (-1);
00101         }
00102     }
00103 
00104   return 0;
00105 }
00106 
00107 int
00108 ACE_Token_Collection::acquire (const ACE_TCHAR *token_name,
00109                                int notify,
00110                                void (*sleep_hook)(void *),
00111                                ACE_Synch_Options &options)
00112 {
00113   ACE_TRACE ("ACE_Token_Collection::acquire");
00114   TOKEN_NAME name (token_name);
00115   ACE_Token_Proxy *temp;
00116   // Get the token from the collection.
00117   int result = collection_.find (name, temp);
00118   // did we find it?
00119   if (result == -1)
00120     return result;
00121   // perform the operation
00122   return temp->acquire (notify, sleep_hook, options);
00123 }
00124 
00125 
00126 int
00127 ACE_Token_Collection::tryacquire (const ACE_TCHAR *token_name,
00128                                   void (*sleep_hook)(void *))
00129 {
00130   ACE_TRACE ("ACE_Token_Collection::tryacquire");
00131   TOKEN_NAME name (token_name);
00132   ACE_Token_Proxy *temp;
00133   // Get the token from the collection.
00134   int result = collection_.find (name, temp);
00135   // did we find it?
00136   if (result == -1)
00137     return result;
00138 
00139   // perform the operation
00140   return temp->tryacquire (sleep_hook);
00141 }
00142 
00143 int
00144 ACE_Token_Collection::tryacquire (void (*sleep_hook)(void *))
00145 {
00146   ACE_TRACE ("ACE_Token_Collection::tryacquire");
00147 
00148   COLLECTION::ITERATOR iterator (collection_);
00149 
00150   for (COLLECTION::ENTRY *temp = 0;
00151        iterator.next (temp) != 0;
00152        iterator.advance ())
00153     {
00154       if (debug_)
00155         ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("collection acquiring %s\n"),
00156                     temp->int_id_->name ()));
00157       // We will fail if _any_ token is not free.
00158       if (temp->int_id_->tryacquire (sleep_hook) == -1)
00159         return -1;
00160     }
00161 
00162   return 0;
00163 }
00164 
00165 int
00166 ACE_Token_Collection::renew (int requeue_position,
00167                              ACE_Synch_Options &options)
00168 {
00169   ACE_TRACE ("ACE_Token_Collection::renew");
00170 
00171   COLLECTION::ITERATOR iterator (collection_);
00172 
00173   for (COLLECTION::ENTRY *temp = 0;
00174        iterator.next (temp) != 0;
00175        iterator.advance ())
00176     {
00177       if (debug_)
00178         ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("collection renewing %s\n"),
00179                     temp->int_id_->name ()));
00180       if (temp->int_id_->renew (requeue_position, options) == -1)
00181         return -1;
00182     }
00183 
00184   return 0;
00185 }
00186 
00187 int
00188 ACE_Token_Collection::renew (const ACE_TCHAR *token_name,
00189                              int requeue_position,
00190                              ACE_Synch_Options &options)
00191 {
00192   ACE_TRACE ("ACE_Token_Collection::renew");
00193   TOKEN_NAME name (token_name);
00194   ACE_Token_Proxy *temp;
00195 
00196   // Get the token from the collection.
00197   int result = collection_.find (name, temp);
00198 
00199   // Did we find it?
00200   if (result == -1)
00201     ACE_ERROR_RETURN ((LM_DEBUG, ACE_LIB_TEXT ("%p %s\n"),
00202                        ACE_LIB_TEXT ("not in collection "),
00203                        token_name), -1);
00204   // perform the operation
00205   return temp->renew (requeue_position, options);
00206 }
00207 
00208 int
00209 ACE_Token_Collection::release (ACE_Synch_Options &)
00210 
00211 {
00212   ACE_TRACE ("ACE_Token_Collection::release");
00213   COLLECTION::ITERATOR iterator (collection_);
00214 
00215   for (COLLECTION::ENTRY *temp = 0;
00216        iterator.next (temp) != 0;
00217        iterator.advance ())
00218     {
00219       if (debug_)
00220         ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("collection releasing %s\n"),
00221                     temp->int_id_->name ()));
00222       temp->int_id_->release ();
00223     }
00224 
00225   return 0;
00226 }
00227 
00228 int
00229 ACE_Token_Collection::release (const ACE_TCHAR *token_name,
00230                                ACE_Synch_Options &options)
00231 {
00232   ACE_TRACE ("ACE_Token_Collection::release");
00233   TOKEN_NAME name (token_name);
00234   ACE_Token_Proxy *temp;
00235   // get the token from the collection
00236   int result = collection_.find (name, temp);
00237   // did we find it?
00238   if (result != 0)
00239     return result;
00240   // perform the operation
00241   return temp->release (options);
00242 }
00243 
00244 ACE_Token_Collection::~ACE_Token_Collection (void)
00245 {
00246   ACE_TRACE ("ACE_Token_Collection::~ACE_Token_Collection");
00247   COLLECTION::ITERATOR iterator (collection_);
00248 
00249   for (COLLECTION::ENTRY *temp = 0;
00250        iterator.next (temp) != 0;
00251        iterator.advance ())
00252     {
00253       delete temp->int_id_;
00254       // The ext_id_'s delete themselves when the array of
00255       // COLLECTION::ENTRYs goes away.
00256     }
00257 }
00258 
00259 
00260 // This method doesn't mean anything for a collection.
00261 ACE_Token_Proxy *
00262 ACE_Token_Collection::clone (void) const
00263 {
00264   ACE_TRACE ("ACE_Token_Collection::clone");
00265   return (ACE_Token_Proxy *) 0;
00266 }
00267 
00268 // This method doesn't mean anything for a collection.
00269 ACE_Tokens *
00270 ACE_Token_Collection::create_token (const ACE_TCHAR *)
00271 {
00272   ACE_TRACE ("ACE_Token_Collection::create_token");
00273   return (ACE_Tokens *) 0;
00274 }
00275 
00276 void
00277 ACE_Token_Collection::dump (void) const
00278 {
00279 #if defined (ACE_HAS_DUMP)
00280   ACE_TRACE ("ACE_Token_Collection::dump");
00281   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00282   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("ACE_Token_Collection::dump:\n")
00283               ACE_LIB_TEXT (" debug_ = %d\n"), debug_));
00284   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("collection_\n")));
00285   collection_.dump ();
00286   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("base:\n")));
00287   ACE_Token_Proxy::dump ();
00288   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00289 #endif /* ACE_HAS_DUMP */
00290 }
00291 
00292 ACE_END_VERSIONED_NAMESPACE_DECL
00293 
00294 #endif /* ACE_HAS_TOKENS_LIBRARY */

Generated on Thu Nov 9 09:42:08 2006 for ACE by doxygen 1.3.6