Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / examples / Registry / test_registry_iterator.cpp
blob6ee7023c825379d2b07ef5d46de3b017f95c80d1
1 // This example uses the ACE_Registry class to iterator through the
2 // entries in the predefined registries. The host of iteration can be
3 // varied through argv[1]. If no host is specified the local host is
4 // used. This is very similar to how regedt32 starts up.
5 //
6 // This examples points the cool iterators in ACE_Registry
8 #include "ace/OS_main.h"
10 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
12 #include "ace/Registry.h"
14 // FUZZ: disable check_for_streams_include
15 #include "ace/streams.h"
18 // Indentation while printing names
19 static const u_long INDENTATION_LEVEL = 3;
21 // Prototypes
22 static int print_naming_context (ACE_Registry::Naming_Context &naming_context,
23 u_long indentation);
24 static void print_object (const ACE_TString &name,
25 u_long indentation);
26 static void print_context (ACE_Registry::Naming_Context &parent,
27 const ACE_TString &name,
28 u_long indentation);
29 static void indent (u_long indentation);
31 int
32 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
34 int result;
35 ACE_Registry::Naming_Context naming_context;
37 // Connect to a predefined naming context
38 result = ACE_Predefined_Naming_Contexts::connect (naming_context,
39 HKEY_LOCAL_MACHINE,
40 // HKEY_CLASSES_ROOT,
41 // HKEY_USERS,
42 // HKEY_CURRENT_USER,
43 argc == 2 ? argv[1] : 0);
45 if (result != 0)
46 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Predefined_Naming_Contexts::connect failed"), -1);
48 // Print contents of naming context
49 result = ::print_naming_context (naming_context, 0);
50 if (result != 0)
51 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "print_naming_context failed"), -1);
53 return 0;
57 // Print contents of <naming_context>
58 static int
59 print_naming_context (ACE_Registry::Naming_Context &naming_context,
60 u_long indentation)
62 ACE_Registry::Binding_List list;
64 // Get the list of all entries
65 int result = naming_context.list (list);
66 if (result != 0)
67 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Registry::Naming_Context::list"), -1);
69 // Iterator through all entries
70 for (ACE_Registry::Binding_List::iterator i = list.begin ();
71 i != list.end ();
72 ++i)
74 // Yeeesss! STL rules!
75 ACE_Registry::Binding &binding = *i;
77 if (binding.type () == ACE_Registry::OBJECT)
78 // If object
79 ::print_object (binding.name (),
80 indentation);
81 else
82 // If context
83 ::print_context (naming_context,
84 binding.name (),
85 indentation);
87 return 0;
91 // Print an object with <name>
92 static void
93 print_object (const ACE_TString &name,
94 u_long indentation)
96 // Set indentation
97 ::indent (indentation);
98 cout << name << endl;
102 // Print an context with <name> and <parent>
103 static void
104 print_context (ACE_Registry::Naming_Context &parent,
105 const ACE_TString &name,
106 u_long indentation)
108 // Set indentation
109 indent (indentation);
110 cout << name << endl;
112 ACE_Registry::Naming_Context child_context;
113 // Find child context
114 int result = parent.resolve_context (name,
115 child_context,
116 KEY_READ);
117 if (result != 0)
118 ACE_ERROR ((LM_ERROR, "%s %s\n", "ACE_Registry::Naming_Context::resolve_context failed for:", name.c_str ()));
119 else
121 // Print contents of the child
122 result = ::print_naming_context (child_context,
123 indentation + INDENTATION_LEVEL);
124 if (result != 0)
125 ACE_ERROR ((LM_ERROR, "%p\n", "print_naming_context failed"));
130 // Pretty formating
131 static void
132 indent (u_long indentation)
134 for (; indentation > 0; indentation--)
135 cout << " ";
137 #else /* !ACE_WIN32 || ACE_LACKS_WIN32_REGISTRY */
139 ACE_TMAIN (int , ACE_TCHAR *[])
141 return 0;
143 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_REGISTRY */