Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Registry.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Registry.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $
00003 
00004 #include "ace/Registry.h"
00005 
00006 ACE_RCSID(ace, Registry, "$Id: Registry.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $")
00007 
00008 #if defined (ACE_WIN32)
00009 
00010 // Funky macro to deal with strange error passing semantics
00011 // of Win32 Reg*() functions
00012 #define ACE_REGISTRY_CALL_RETURN(X) \
00013   do { \
00014     if (X != ERROR_SUCCESS) \
00015     { \
00016       errno = X; \
00017       return -1; \
00018     } \
00019     else \
00020       return 0; \
00021     } while (0)
00022 
00023 
00024 // Separator for components in a name
00025 /* static */
00026 const ACE_TCHAR *ACE_Registry::STRING_SEPARATOR = ACE_LIB_TEXT ("\\");
00027 
00028 int
00029 ACE_Registry::Name_Component::operator== (const Name_Component &rhs) const
00030 {
00031   return
00032     rhs.id_ == this->id_ &&
00033     rhs.kind_ == this->kind_;
00034 }
00035 
00036 int
00037 ACE_Registry::Name_Component::operator!= (const Name_Component &rhs) const
00038 {
00039   return !this->operator== (rhs);
00040 }
00041 
00042 // Simple binding constructor
00043 ACE_Registry::Binding::Binding ()
00044   : name_ (),
00045     type_ (INVALID)
00046 {
00047 }
00048 
00049 
00050 // Binding constructor
00051 // (Name version)
00052 ACE_Registry::Binding::Binding (const Name &name,
00053                                 Binding_Type type)
00054   : name_ (ACE_Registry::make_string (name)),
00055     type_ (type)
00056 {
00057 }
00058 
00059 
00060 // Binding constructor
00061 // (String version)
00062 ACE_Registry::Binding::Binding (const ACE_TString &name,
00063                                 Binding_Type type)
00064   : name_ (name),
00065     type_ (type)
00066 {
00067 }
00068 
00069 
00070 int
00071 ACE_Registry::Binding::operator== (const Binding &rhs) const
00072 {
00073   return
00074     rhs.name_ == this->name_ &&
00075     rhs.type_ == this->type_;
00076 }
00077 
00078 int
00079 ACE_Registry::Binding::operator!= (const Binding &rhs) const
00080 {
00081   return !this->operator== (rhs);
00082 }
00083 
00084 // Name accessor
00085 // (Name version)
00086 void
00087 ACE_Registry::Binding::name (Name &name)
00088 {
00089   name = ACE_Registry::make_name (this->name_);
00090 }
00091 
00092 
00093 // Name accessors
00094 // (String version)
00095 void
00096 ACE_Registry::Binding::name (ACE_TString &name)
00097 {
00098   name = this->name_;
00099 }
00100 
00101 
00102 // Name accessors
00103 // (String version)
00104 ACE_TString
00105 ACE_Registry::Binding::name (void)
00106 {
00107   return this->name_;
00108 }
00109 
00110 
00111 // Type accessor
00112 ACE_Registry::Binding_Type
00113 ACE_Registry::Binding::type (void)
00114 {
00115   return this->type_;
00116 }
00117 
00118 
00119 // Simple object constructor
00120 ACE_Registry::Object::Object (void *data,
00121                               u_long size,
00122                               u_long type)
00123   : data_ (data),
00124     size_ (size),
00125     type_ (type)
00126 {
00127 }
00128 
00129 // Object accessors and set methods
00130 void
00131 ACE_Registry::Object::data (void *data)
00132 {
00133   this->data_ = data;
00134 }
00135 
00136 
00137 void *
00138 ACE_Registry::Object::data (void) const
00139 {
00140   return this->data_;
00141 }
00142 
00143 
00144 void
00145 ACE_Registry::Object::size (u_long size)
00146 {
00147   this->size_ = size;
00148 }
00149 
00150 
00151 u_long
00152 ACE_Registry::Object::size (void) const
00153 {
00154   return this->size_;
00155 }
00156 
00157 
00158 void
00159 ACE_Registry::Object::type (u_long type)
00160 {
00161   this->type_ = type;
00162 }
00163 
00164 
00165 u_long
00166 ACE_Registry::Object::type (void) const
00167 {
00168   return this->type_;
00169 }
00170 
00171 
00172 // Simple context constructor
00173 ACE_Registry::Naming_Context::Naming_Context (void)
00174   : key_ ((HKEY) 0),
00175     parent_key_ ((HKEY) 0),
00176     name_ ()
00177 {
00178 }
00179 
00180 
00181 // Context constructor
00182 ACE_Registry::Naming_Context::Naming_Context (const HKEY &key)
00183   : key_ (key),
00184     parent_key_ ((HKEY) 0),
00185     name_ ()
00186 {
00187 }
00188 
00189 
00190 ACE_Registry::Naming_Context::Naming_Context (const Naming_Context &rhs)
00191   : key_ (rhs.key_),
00192     parent_key_ (rhs.parent_key_),
00193     name_ (rhs.name_)
00194 {
00195   // This is incorrect.
00196   // Rather than copying key, we should call ::DuplicateHandle()
00197   // But since this is private (and not used), I don't care much
00198 }
00199 
00200 
00201 const ACE_Registry::Naming_Context &
00202 ACE_Registry::Naming_Context::operator= (const Naming_Context &rhs)
00203 {
00204         ACE_UNUSED_ARG(rhs);
00205 
00206         // Not implemented
00207         return *this;
00208 }
00209 
00210 
00211 // Destructor
00212 ACE_Registry::Naming_Context::~Naming_Context ()
00213 {
00214   this->close ();
00215 }
00216 
00217 
00218 // Insert <object> with <name> into <this> context
00219 // (Name version)
00220 int
00221 ACE_Registry::Naming_Context::bind_new (const Name &name,
00222                                         const Object &object)
00223 {
00224   return this->bind_new (ACE_Registry::make_string (name), object);
00225 }
00226 
00227 
00228 // Insert <object> with <name> into <this> context
00229 // (String version)
00230 int
00231 ACE_Registry::Naming_Context::bind_new (const ACE_TString &name,
00232                                         const Object &object)
00233 {
00234   // temporary object
00235   Object temp;
00236   long result = this->resolve (name, temp);
00237   if (result == 0)
00238     // resolve succeeded
00239     result = -1;
00240   else
00241     // resolve failed
00242     result = this->bind (name, object);
00243   return result;
00244 }
00245 
00246 
00247 // Insert or update <object> with <name> into <this> context
00248 // (Name version)
00249 int
00250 ACE_Registry::Naming_Context::bind (const Name &name,
00251                                     const Object &object)
00252 {
00253   return this->bind (ACE_Registry::make_string (name), object);
00254 }
00255 
00256 
00257 // Insert or update <object> with <name> into <this> context
00258 // (String version)
00259 int
00260 ACE_Registry::Naming_Context::bind (const ACE_TString &name,
00261                                     const Object &object)
00262 {
00263   long result = ACE_TEXT_RegSetValueEx (this->key_,
00264                                         name.c_str (),
00265                                         0,
00266                                         object.type (),
00267                                         (const BYTE *) object.data (),
00268                                         object.size ());
00269   ACE_REGISTRY_CALL_RETURN (result);
00270 }
00271 
00272 
00273 // Update <object> with <name> in <this> context
00274 // (Name version)
00275 int
00276 ACE_Registry::Naming_Context::rebind (const Name &name,
00277                                       const Object &new_object)
00278 {
00279   return this->rebind (ACE_Registry::make_string (name), new_object);
00280 }
00281 
00282 
00283 // Update <object> with <name> in <this> context
00284 // (String version)
00285 int
00286 ACE_Registry::Naming_Context::rebind (const ACE_TString &name,
00287                                       const Object &new_object)
00288 {
00289   Object old_object;
00290   // find the old one first
00291   long result = this->resolve (name, old_object);
00292   if (result == 0)
00293     // no need to delete first
00294     result = this->bind (name, new_object);
00295   return result;
00296 }
00297 
00298 
00299 // Find <object> with <name> in <this> context
00300 // (Name version)
00301 int
00302 ACE_Registry::Naming_Context::resolve (const Name &name,
00303                                        Object &object)
00304 {
00305   return this->resolve (ACE_Registry::make_string (name), object);
00306 }
00307 
00308 
00309 // Find <object> with <name> in <this> context
00310 // (String version)
00311 int
00312 ACE_Registry::Naming_Context::resolve (const ACE_TString &name,
00313                                        Object &object)
00314 {
00315   // Get object state
00316   u_long type;
00317   void *data = object.data ();
00318   u_long size = object.size ();
00319 
00320   long result = ACE_TEXT_RegQueryValueEx (this->key_,
00321                                           name.c_str (),
00322                                           0,
00323                                           &type,
00324                                           (BYTE *)data,
00325                                           &size);
00326   if (result == ERROR_SUCCESS)
00327     {
00328       // Reset object state
00329       // No need to set object.data()
00330       object.type (type);
00331       object.size (size);
00332     }
00333 
00334   ACE_REGISTRY_CALL_RETURN (result);
00335 }
00336 
00337 
00338 // Remove object with <name> in <this> context
00339 // (Name version)
00340 int
00341 ACE_Registry::Naming_Context::unbind (const Name &name)
00342 {
00343   return this->unbind (ACE_Registry::make_string (name));
00344 }
00345 
00346 
00347 // Remove object with <name> in <this> context
00348 // (String version)
00349 int
00350 ACE_Registry::Naming_Context::unbind (const ACE_TString &name)
00351 {
00352   long result = ACE_TEXT_RegDeleteValue (this->key_,
00353                                          name.c_str ());
00354 
00355   ACE_REGISTRY_CALL_RETURN (result);
00356 }
00357 
00358 
00359 // Create new <naming_context> relative to <this> context
00360 // This method may not mean a lot in this implementation
00361 int
00362 ACE_Registry::Naming_Context::new_context (Naming_Context &naming_context)
00363 {
00364   // Make sure that we reset the state and close keys
00365   return naming_context.close ();
00366 }
00367 
00368 
00369 // Insert <naming_context> with <name> relative to <this> context
00370 // (Name version)
00371 int
00372 ACE_Registry::Naming_Context::bind_new_context (const Name &name,
00373                                                 Naming_Context &naming_context,
00374                                                 u_long persistence,
00375                                                 u_long security_access,
00376                                                 LPSECURITY_ATTRIBUTES security_attributes)
00377 {
00378   return this->bind_new_context (ACE_Registry::make_string (name),
00379                                  naming_context,
00380                                  persistence,
00381                                  security_access,
00382                                  security_attributes);
00383 }
00384 
00385 
00386 // Insert <naming_context> with <name> relative to <this> context
00387 // (String version)
00388 int
00389 ACE_Registry::Naming_Context::bind_new_context (const ACE_TString &name,
00390                                                 Naming_Context &naming_context,
00391                                                 u_long persistence,
00392                                                 u_long security_access,
00393                                                 LPSECURITY_ATTRIBUTES security_attributes)
00394 {
00395   u_long reason;
00396 
00397   long result = ACE_TEXT_RegCreateKeyEx (this->key_,
00398                                          name.c_str (),
00399                                          0,
00400                                          0,
00401                                          persistence,
00402                                          security_access,
00403                                          security_attributes,
00404                                          &naming_context.key_,
00405                                          &reason);
00406   if (result == ERROR_SUCCESS)
00407     // If create succeeds
00408     {
00409       if (reason == REG_CREATED_NEW_KEY)
00410         // If new key: success
00411         {
00412           // Set the correct parent
00413           naming_context.parent (this->key_);
00414           // Set the correct name
00415           naming_context.name (name);
00416         }
00417       else
00418         // reason == REG_OPENED_EXISTING_KEY
00419         // Failed to make new key
00420         {
00421           // reset result to failure
00422           result = -1;
00423           // Close the key first
00424           ::RegCloseKey (naming_context.key_);
00425           // Reset key
00426           naming_context.key_ = (HKEY) 0;
00427         }
00428     }
00429 
00430   ACE_REGISTRY_CALL_RETURN (result);
00431 }
00432 
00433 
00434 // Insert or update <naming_context> with <name> relative to <this> context
00435 // (Name version)
00436 int
00437 ACE_Registry::Naming_Context::bind_context (const Name &name,
00438                                             /* const */ Naming_Context &naming_context,
00439                                             u_long persistence,
00440                                             u_long security_access,
00441                                             LPSECURITY_ATTRIBUTES security_attributes)
00442 {
00443   return this->bind_context (ACE_Registry::make_string (name),
00444                              naming_context,
00445                              persistence,
00446                              security_access,
00447                              security_attributes);
00448 }
00449 
00450 
00451 // Insert or update <naming_context> with <name> relative to <this> context
00452 // (String version)
00453 int
00454 ACE_Registry::Naming_Context::bind_context (const ACE_TString &name,
00455                                             /* const */ Naming_Context &naming_context,
00456                                             u_long persistence,
00457                                             u_long security_access,
00458                                             LPSECURITY_ATTRIBUTES security_attributes)
00459 {
00460   u_long reason;
00461 
00462   long result = ACE_TEXT_RegCreateKeyEx (this->key_,
00463                                          name.c_str (),
00464                                          0,
00465                                          0,
00466                                          persistence,
00467                                          security_access,
00468                                          security_attributes,
00469                                          &naming_context.key_,
00470                                          &reason);
00471   if (result == ERROR_SUCCESS)
00472     {
00473       // Set the correct parent
00474       naming_context.parent (this->key_);
00475       // Set the correct name
00476       naming_context.name (name);
00477     }
00478 
00479   ACE_REGISTRY_CALL_RETURN (result);
00480 }
00481 
00482 
00483 // Rename <naming_context> to <name>
00484 // (Name version)
00485 int
00486 ACE_Registry::Naming_Context::rebind_context (const Name &name,
00487                                               /* const */ Naming_Context &new_naming_context)
00488 {
00489   return this->rebind_context (ACE_Registry::make_string (name),
00490                                new_naming_context);
00491 }
00492 
00493 
00494 // Rename <naming_context> to <name>
00495 // (String version)
00496 int
00497 ACE_Registry::Naming_Context::rebind_context (const ACE_TString &name,
00498                                               /* const */ Naming_Context &new_naming_context)
00499 {
00500   Naming_Context old_naming_context;
00501   // find the old one first
00502   long result = this->resolve_context (name,
00503                                        old_naming_context);
00504   if (result == 0)
00505     {
00506       // naming_context is found: delete entry
00507       result = this->unbind_context (name);
00508       if (result == 0)
00509         {
00510           // successful deletion; rebind
00511           // beware of race conditions here
00512           // (lets resolve this later)
00513           result = this->bind_new_context (name, new_naming_context);
00514         }
00515     }
00516   return result;
00517 }
00518 
00519 
00520 // Remove naming_context with <name> from <this> context
00521 // (Name version)
00522 int
00523 ACE_Registry::Naming_Context::unbind_context (const Name &name)
00524 {
00525   return this->unbind_context (ACE_Registry::make_string (name));
00526 }
00527 
00528 
00529 // Remove naming_context with <name> from <this> context
00530 // (String version)
00531 int
00532 ACE_Registry::Naming_Context::unbind_context (const ACE_TString &name)
00533 {
00534   long result = ACE_TEXT_RegDeleteKey (this->key_,
00535                                                        name.c_str ());
00536 
00537   ACE_REGISTRY_CALL_RETURN (result);
00538 }
00539 
00540 
00541 // Find <naming_context> with <name> in <this> context
00542 // (Name version)
00543 int
00544 ACE_Registry::Naming_Context::resolve_context (const Name &name,
00545                                                Naming_Context &naming_context,
00546                                                u_long security_access)
00547 {
00548   return this->resolve_context (ACE_Registry::make_string (name),
00549                                 naming_context,
00550                                 security_access);
00551 }
00552 
00553 
00554 // Find <naming_context> with <name> in <this> context
00555 // (String version)
00556 int
00557 ACE_Registry::Naming_Context::resolve_context (const ACE_TString &name,
00558                                                Naming_Context &naming_context,
00559                                                u_long security_access)
00560 {
00561   long result = ACE_TEXT_RegOpenKeyEx (this->key_,
00562                                        name.c_str (),
00563                                        0,
00564                                        security_access,
00565                                        &naming_context.key_);
00566   if (result == ERROR_SUCCESS)
00567     {
00568       // set the correct parent
00569       naming_context.parent (this->key_);
00570       // set the correct name
00571       naming_context.name (name);
00572     }
00573 
00574   ACE_REGISTRY_CALL_RETURN (result);
00575 }
00576 
00577 
00578 // Same as unbind_context() with <this> as naming_context
00579 int
00580 ACE_Registry::Naming_Context::destroy (void)
00581 {
00582   // hopefully the parent_key_ is still open
00583   long result = ACE_TEXT_RegDeleteKey (this->parent_key_,
00584                                                        this->name_.c_str ());
00585 
00586   ACE_REGISTRY_CALL_RETURN (result);
00587 }
00588 
00589 
00590 // Sync content of context to disk
00591 int
00592 ACE_Registry::Naming_Context::flush (void)
00593 {
00594   long result = ::RegFlushKey (this->key_);
00595   ACE_REGISTRY_CALL_RETURN (result);
00596 }
00597 
00598 
00599 // Close the handle of the context
00600 int
00601 ACE_Registry::Naming_Context::close (void)
00602 {
00603   long result = this->key_ ? ::RegCloseKey (this->key_) : ERROR_SUCCESS;
00604   ACE_REGISTRY_CALL_RETURN (result);
00605 }
00606 
00607 
00608 // Convert a <name> to a <string>
00609 ACE_TString
00610 ACE_Registry::make_string (const Name &const_name)
00611 {
00612   ACE_TString string;
00613   Name &name = ACE_const_cast (Name &, const_name);
00614 
00615   // Iterator through the components of name
00616   for (Name::iterator iterator = name.begin ();
00617        iterator != name.end ();
00618        iterator++)
00619     {
00620       if (iterator != name.begin ())
00621         // If this is not the first component, we will add separators
00622         string += ACE_Registry::STRING_SEPARATOR;
00623       const Name_Component &component = *iterator;
00624       // Add to string
00625       string += component.id_;
00626     }
00627 
00628   return string;
00629 }
00630 
00631 
00632 // Convert a <string> to a <name>
00633 ACE_Registry::Name
00634 ACE_Registry::make_name (const ACE_TString &string)
00635 {
00636   ssize_t new_position = 0;
00637   ssize_t last_position = 0;
00638   Name name;
00639 
00640   // Rememeber: NPOS is -1
00641   while (new_position != ACE_TString::npos)
00642     {
00643       Name_Component component;
00644       // Find the separator
00645       new_position = string.find (ACE_Registry::STRING_SEPARATOR, new_position);
00646       if (new_position != ACE_TString::npos)
00647         // If we have not gone past the end
00648         {
00649           // Get the substring
00650           component.id_ = string.substr (last_position,
00651                                          new_position - last_position);
00652           // Skip past the seperator
00653           new_position +=
00654             ACE_OS_String::strlen (ACE_Registry::STRING_SEPARATOR);
00655         }
00656       else
00657         {
00658           // Get the last substring
00659           component.id_ = string.substr (last_position);
00660         }
00661       // Update positions
00662       last_position = new_position;
00663       // Insert component into name
00664       name.insert (component);
00665     }
00666 
00667   return name;
00668 }
00669 
00670 
00671 // Set key
00672 void
00673 ACE_Registry::Naming_Context::key (HKEY key)
00674 {
00675   this->key_ = key;
00676 }
00677 
00678 
00679 // Get key
00680 HKEY
00681 ACE_Registry::Naming_Context::key (void)
00682 {
00683   return this->key_;
00684 }
00685 
00686 
00687 // Set parent
00688 void
00689 ACE_Registry::Naming_Context::parent (HKEY parent)
00690 {
00691   this->parent_key_ = parent;
00692 }
00693 
00694 
00695 // Get parent
00696 HKEY
00697 ACE_Registry::Naming_Context::parent (void)
00698 {
00699   return this->parent_key_;
00700 }
00701 
00702 
00703 // Set name
00704 // (Name version)
00705 void
00706 ACE_Registry::Naming_Context::name (const Name &name)
00707 {
00708   this->name_ = ACE_Registry::make_string (name);
00709 }
00710 
00711 
00712 // Get name
00713 // (Name version)
00714 void
00715 ACE_Registry::Naming_Context::name (Name &name)
00716 {
00717   name = ACE_Registry::make_name (this->name_);
00718 }
00719 
00720 
00721 // Set name
00722 // (String version)
00723 void
00724 ACE_Registry::Naming_Context::name (const ACE_TString &name)
00725 {
00726   this->name_ = name;
00727 }
00728 
00729 
00730 // Get name
00731 // (String version)
00732 ACE_TString
00733 ACE_Registry::Naming_Context::name (void)
00734 {
00735   return this->name_;
00736 }
00737 
00738 
00739 // Get name
00740 // (String version)
00741 void
00742 ACE_Registry::Naming_Context::name (ACE_TString &name)
00743 {
00744   name = this->name_;
00745 }
00746 
00747 
00748 // listing function: iterator creator
00749 // This is useful when there are many objects and contexts
00750 // in <this> context and you only want to look at a few entries
00751 // at a time
00752 int
00753 ACE_Registry::Naming_Context::list (u_long how_many,
00754                                     Binding_List &list,
00755                                     Binding_Iterator &iter)
00756 {
00757   // Empty list
00758   static const ACE_Registry::Binding_List empty_list;
00759   // Make sure that the list is empty
00760   list = empty_list;
00761 
00762   // Correctly initalize the iterator
00763   iter.reset ();
00764 
00765   // Make sure that the iterator uses <this> naming context
00766   iter.naming_context (*this);
00767 
00768   // Start iterations from the objects
00769   iter.current_enumeration (iter.object_iteration_);
00770 
00771   // Get the next <how_many> values
00772   long result = iter.next_n (how_many,
00773                                  list);
00774   return result;
00775 }
00776 
00777 
00778 // listing function: iterator creator
00779 // This gives back a listing of all entries in <this> context.
00780 int
00781 ACE_Registry::Naming_Context::list (Binding_List &list)
00782 {
00783   // Empty list
00784   static const ACE_Registry::Binding_List empty_list;
00785   // Make sure that the list is empty
00786   list = empty_list;
00787 
00788   // Create an iterator
00789   ACE_Registry::Binding_Iterator iterator;
00790 
00791   // Make sure that the iterator uses <this> naming context
00792   iterator.naming_context (*this);
00793 
00794   // Start iterations from the objects
00795   iterator.current_enumeration (iterator.object_iteration_);
00796 
00797   long result = 0;
00798   while (1)
00799     {
00800       ACE_Registry::Binding binding;
00801       result = iterator.next_one (binding);
00802       if (result == 0)
00803         list.insert (binding);
00804       else
00805         break;
00806     }
00807   return 0;
00808 }
00809 
00810 
00811 // Default constructor
00812 ACE_Registry::Binding_Iterator::Binding_Iterator ()
00813   : object_iteration_ (*this),
00814     context_iteration_ (*this),
00815     iteration_complete_ (*this)
00816 {
00817   this->reset ();
00818 }
00819 
00820 
00821 void
00822 ACE_Registry::Binding_Iterator::reset ()
00823 {
00824   this->current_enumeration_ = &this->iteration_complete_;
00825   this->iteration_complete_.reset ();
00826   this->object_iteration_.reset ();
00827   this->context_iteration_.reset ();
00828 }
00829 
00830 
00831 void
00832 ACE_Registry::Binding_Iterator::Iteration_State::reset ()
00833 {
00834   this->index_ = 0;
00835 }
00836 
00837 
00838 ACE_Registry::Binding_Iterator::Iteration_State::Iteration_State (Binding_Iterator &iter)
00839   : parent_ (&iter),
00840     index_ (0)
00841 {
00842 }
00843 
00844 
00845 ACE_Registry::Binding_Iterator::Object_Iteration::Object_Iteration (Binding_Iterator &iter)
00846   : Iteration_State (iter)
00847 {
00848 }
00849 
00850 
00851 ACE_Registry::Binding_Iterator::Context_Iteration::Context_Iteration (Binding_Iterator &iter)
00852   : Iteration_State (iter)
00853 {
00854 }
00855 
00856 
00857 ACE_Registry::Binding_Iterator::Iteration_Complete::Iteration_Complete (Binding_Iterator &iter)
00858   : Iteration_State (iter)
00859 {
00860 }
00861 
00862 
00863 // Next entry
00864 int
00865 ACE_Registry::Binding_Iterator::next_one (Binding &binding)
00866 {
00867   u_long how_many = 1;
00868   Binding_List list;
00869 
00870   // Get next n (where n is one)
00871   long result = this->next_n (how_many, list);
00872 
00873   if (result == 0)
00874     // Success
00875     binding = (*list.begin ());
00876 
00877   return result;
00878 }
00879 
00880 
00881 // Next <how_many> entries
00882 int
00883 ACE_Registry::Binding_Iterator::next_n (u_long how_many,
00884                                         Binding_List &list)
00885 {
00886   // Empty list
00887   static const ACE_Registry::Binding_List empty_list;
00888   // Make sure that the list is empty
00889   list = empty_list;
00890 
00891   return this->current_enumeration_->next_n (how_many, list);
00892 }
00893 
00894 
00895 // Destroy iterator
00896 int
00897 ACE_Registry::Binding_Iterator::destroy (void)
00898 {
00899   this->reset ();
00900   return 0;
00901 }
00902 
00903 
00904 // Set/Get naming_context
00905 void
00906 ACE_Registry::Binding_Iterator::naming_context (Naming_Context &naming_context)
00907 {
00908   this->naming_context_ = &naming_context;
00909 }
00910 
00911 
00912 ACE_Registry::Naming_Context &
00913 ACE_Registry::Binding_Iterator::naming_context (void)
00914 {
00915   return *this->naming_context_;
00916 }
00917 
00918 
00919 // Set/Get current enumeration
00920 void
00921 ACE_Registry::Binding_Iterator::current_enumeration (Iteration_State &current_enumeration)
00922 {
00923   this->current_enumeration_ = &current_enumeration;
00924 }
00925 
00926 
00927 ACE_Registry::Binding_Iterator::Iteration_State &
00928 ACE_Registry::Binding_Iterator::current_enumeration (void)
00929 {
00930   return *this->current_enumeration_;
00931 }
00932 
00933 
00934 int
00935 ACE_Registry::Binding_Iterator::Object_Iteration::next_n (u_long how_many,
00936                                                           Binding_List &list)
00937 {
00938   // Make a copy
00939   u_long requested = how_many;
00940 
00941   // While there are more entries to be added to the list
00942   while (how_many > 0)
00943     {
00944       ACE_TCHAR string [ACE_Registry::Naming_Context::MAX_OBJECT_NAME_SIZE];
00945       u_long size = sizeof string / sizeof (ACE_TCHAR);
00946       long result = ACE_TEXT_RegEnumValue (this->parent_->naming_context ().key (),
00947                                            this->index_,
00948                                            string,
00949                                            &size,
00950                                            0,
00951                                            0,
00952                                            0,
00953                                            0);
00954       switch (result)
00955         {
00956         case ERROR_SUCCESS:
00957           // Object found
00958           {
00959             // Readjust counters
00960             this->index_++;
00961             how_many--;
00962 
00963             // Add to list
00964             // Create binding
00965             Binding binding (string, OBJECT);
00966             // Add to binding list
00967             list.insert (binding);
00968           }
00969         // Continue to add to list
00970         break;
00971 
00972         case ERROR_NO_MORE_ITEMS:
00973           // Enumeration of objects complete
00974           // Reset index
00975           this->index_ = 0;
00976 
00977           // Current enumeration will become CONTEXTS
00978           this->parent_->current_enumeration (this->parent_->context_iteration_);
00979           result = this->parent_->current_enumeration ().next_n (how_many,
00980                                                                  list);
00981           // If we were able to add objects
00982           if (requested != how_many)
00983             return 0;
00984           else
00985             return result;
00986 
00987         default:
00988           // Strange error
00989           // Reset index
00990           this->index_ = 0;
00991           // Current enumeration will become COMPLETE
00992           this->parent_->current_enumeration (this->parent_->iteration_complete_);
00993           // strange error
00994           return -1;
00995         }
00996     }
00997   // If we reach here, all of <how_many> pairs were added to the list
00998   // Since more entries may be available
00999   // current enumeration will remain OBJECTS
01000   return 0;
01001 }
01002 
01003 
01004 int
01005 ACE_Registry::Binding_Iterator::Context_Iteration::next_n (u_long how_many,
01006                                                            Binding_List &list)
01007 {
01008   // Make a copy
01009   u_long requested = how_many;
01010 
01011   // While there are more entries to be added to the list
01012   while (how_many > 0)
01013     {
01014       ACE_TCHAR string [ACE_Registry::Naming_Context::MAX_CONTEXT_NAME_SIZE];
01015       u_long size = sizeof string / sizeof (ACE_TCHAR);
01016       long result = ACE_TEXT_RegEnumKeyEx (this->parent_->naming_context (). key (),
01017                                            this->index_,
01018                                            string,
01019                                            &size,
01020                                            0,
01021                                            0,
01022                                            0,
01023                                            0);
01024       switch (result)
01025         {
01026         case ERROR_SUCCESS:
01027           // Object found
01028           {
01029             // Readjust counters
01030             this->index_++;
01031             how_many--;
01032 
01033             // Add to list
01034             // Create binding
01035             Binding binding (string, CONTEXT);
01036             // Add to binding list
01037             list.insert (binding);
01038           }
01039         // Continue to add to list
01040         break;
01041 
01042         case ERROR_NO_MORE_ITEMS:
01043           // Enumeration of objects complete
01044 
01045           /* FALL THROUGH */
01046 
01047         default:
01048           // Strange error
01049 
01050           // Reset index
01051           this->index_ = 0;
01052           // Current enumeration will become CONTEXTS
01053           this->parent_->current_enumeration (this->parent_->iteration_complete_);
01054 
01055           // If we were able to add contexts
01056           if (requested != how_many)
01057             return 0;
01058           else
01059             return -1;
01060         }
01061     }
01062   // If we reach here, all of <how_many> pairs were added to the list
01063   // Since more entries may be available
01064   // current enumeration will remain CONTEXTS
01065   return 0;
01066 }
01067 
01068 
01069 int
01070 ACE_Registry::Binding_Iterator::Iteration_Complete::next_n (u_long how_many,
01071                                                             Binding_List &list)
01072 {
01073         ACE_UNUSED_ARG(list);
01074         ACE_UNUSED_ARG(how_many);
01075 
01076         // No more values
01077         return -1;
01078 }
01079 
01080 
01081 // Factory method to connect to predefined registries
01082 // This method works for both remote and local machines
01083 // However, for remote machines CLASSES_ROOT and CURRENT_USER
01084 // types are not allowed
01085 /* static */
01086 int
01087 ACE_Predefined_Naming_Contexts::connect (ACE_Registry::Naming_Context &naming_context,
01088                                          HKEY predefined,
01089                                          const ACE_TCHAR *machine_name)
01090 {
01091 #if defined (ACE_HAS_WINCE)
01092   return -1;
01093 #else
01094   long result = -1;
01095 
01096   if (machine_name != 0 && ACE_OS::strcmp (ACE_LIB_TEXT ("localhost"), machine_name) == 0)
01097     machine_name = 0;
01098 
01099   if (predefined == HKEY_LOCAL_MACHINE || predefined == HKEY_USERS)
01100     result = ACE_TEXT_RegConnectRegistry (ACE_const_cast(ACE_TCHAR *, machine_name),
01101                                                           predefined,
01102                                                           &naming_context.key_);
01103   if (predefined == HKEY_CURRENT_USER || predefined == HKEY_CLASSES_ROOT)
01104     // Make sure that for these types, the machine is local
01105     if (machine_name == 0 ||
01106         ACE_Predefined_Naming_Contexts::is_local_host (machine_name))
01107       {
01108         naming_context.key_ = predefined;
01109         result = 0;
01110       }
01111     else
01112       result = -1;
01113 
01114   ACE_REGISTRY_CALL_RETURN (result);
01115 #endif  // ACE_HAS_WINCE
01116 }
01117 
01118 // Check if <machine_name> is the local host
01119 /* static */
01120 int
01121 ACE_Predefined_Naming_Contexts::is_local_host (const ACE_TCHAR *machine_name)
01122 {
01123   ACE_TCHAR local_host[MAXHOSTNAMELEN];
01124   int result = ACE_OS::hostname (local_host, sizeof local_host / sizeof (ACE_TCHAR));
01125   if (result == 0)
01126     result = !ACE_OS::strcmp (local_host, machine_name);
01127   else
01128     result = 0;
01129   return result;
01130 }
01131 
01132 #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
01133 template class ACE_Node<ACE_Registry::Binding>;
01134 template class ACE_Unbounded_Set<ACE_Registry::Binding>;
01135 template class ACE_Unbounded_Set_Iterator<ACE_Registry::Binding>;
01136 template class ACE_Node<ACE_Registry::Name_Component>;
01137 template class ACE_Unbounded_Set<ACE_Registry::Name_Component>;
01138 template class ACE_Unbounded_Set_Iterator<ACE_Registry::Name_Component>;
01139 #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
01140 #pragma instantiate ACE_Node<ACE_Registry::Binding>
01141 #pragma instantiate ACE_Unbounded_Set<ACE_Registry::Binding>
01142 #pragma instantiate ACE_Unbounded_Set_Iterator<ACE_Registry::Binding>
01143 #pragma instantiate ACE_Node<ACE_Registry::Name_Component>
01144 #pragma instantiate ACE_Unbounded_Set<ACE_Registry::Name_Component>
01145 #pragma instantiate ACE_Unbounded_Set_Iterator<ACE_Registry::Name_Component>
01146 #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
01147 
01148 #endif /* ACE_WIN32 */

Generated on Mon Jun 16 11:21:03 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002