summaryrefslogtreecommitdiff
path: root/ACE/examples/Registry
diff options
context:
space:
mode:
Diffstat (limited to 'ACE/examples/Registry')
-rw-r--r--ACE/examples/Registry/Makefile.am62
-rw-r--r--ACE/examples/Registry/Registry.mpc17
-rw-r--r--ACE/examples/Registry/test_registry_iterator.cpp146
-rw-r--r--ACE/examples/Registry/test_registry_update.cpp161
4 files changed, 386 insertions, 0 deletions
diff --git a/ACE/examples/Registry/Makefile.am b/ACE/examples/Registry/Makefile.am
new file mode 100644
index 00000000000..5118f95341c
--- /dev/null
+++ b/ACE/examples/Registry/Makefile.am
@@ -0,0 +1,62 @@
+## Process this file with automake to create Makefile.in
+##
+## $Id$
+##
+## This file was generated by MPC. Any changes made directly to
+## this file will be lost the next time it is generated.
+##
+## MPC Command:
+## ./bin/mwc.pl -type automake -noreldefs ACE.mwc
+
+ACE_BUILDDIR = $(top_builddir)
+ACE_ROOT = $(top_srcdir)
+
+noinst_PROGRAMS =
+
+## Makefile.Registry_Iterator.am
+
+if BUILD_WINREGISTRY
+if !BUILD_ACE_FOR_TAO
+
+noinst_PROGRAMS += iterator
+
+iterator_CPPFLAGS = \
+ -I$(ACE_ROOT) \
+ -I$(ACE_BUILDDIR)
+
+iterator_SOURCES = \
+ test_registry_iterator.cpp
+
+iterator_LDADD = \
+ $(ACE_BUILDDIR)/ace/libACE.la
+
+endif !BUILD_ACE_FOR_TAO
+endif BUILD_WINREGISTRY
+
+## Makefile.Registry_Update.am
+
+if BUILD_WINREGISTRY
+if !BUILD_ACE_FOR_TAO
+
+noinst_PROGRAMS += update
+
+update_CPPFLAGS = \
+ -I$(ACE_ROOT) \
+ -I$(ACE_BUILDDIR)
+
+update_SOURCES = \
+ test_registry_update.cpp
+
+update_LDADD = \
+ $(ACE_BUILDDIR)/ace/libACE.la
+
+endif !BUILD_ACE_FOR_TAO
+endif BUILD_WINREGISTRY
+
+## Clean up template repositories, etc.
+clean-local:
+ -rm -f *~ *.bak *.rpo *.sym lib*.*_pure_* core core.*
+ -rm -f gcctemp.c gcctemp so_locations *.ics
+ -rm -rf cxx_repository ptrepository ti_files
+ -rm -rf templateregistry ir.out
+ -rm -rf ptrepository SunWS_cache Templates.DB
diff --git a/ACE/examples/Registry/Registry.mpc b/ACE/examples/Registry/Registry.mpc
new file mode 100644
index 00000000000..e2e048f02c0
--- /dev/null
+++ b/ACE/examples/Registry/Registry.mpc
@@ -0,0 +1,17 @@
+// -*- MPC -*-
+// $Id$
+
+project(*iterator) : aceexe, winregistry {
+ exename = iterator
+
+ Source_Files {
+ test_registry_iterator.cpp
+ }
+}
+project(*update) : aceexe, winregistry {
+ exename = update
+
+ Source_Files {
+ test_registry_update.cpp
+ }
+}
diff --git a/ACE/examples/Registry/test_registry_iterator.cpp b/ACE/examples/Registry/test_registry_iterator.cpp
new file mode 100644
index 00000000000..df776ca2fad
--- /dev/null
+++ b/ACE/examples/Registry/test_registry_iterator.cpp
@@ -0,0 +1,146 @@
+// $Id$
+
+// This example uses the ACE_Registry class to iterator through the
+// entries in the predefined registries. The host of iteration can be
+// varied through argv[1]. If no host is specified the local host is
+// used. This is very similar to how regedt32 starts up.
+//
+// This examples points the cool iterators in ACE_Registry
+
+#include "ace/OS_main.h"
+
+#if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
+
+#include "ace/Registry.h"
+
+// FUZZ: disable check_for_streams_include
+#include "ace/streams.h"
+
+ACE_RCSID(Registry, test_registry_iterator, "$Id$")
+
+// Indentation while printing names
+static const u_long INDENTATION_LEVEL = 3;
+
+// Prototypes
+static int print_naming_context (ACE_Registry::Naming_Context &naming_context,
+ u_long indentation);
+static void print_object (const ACE_TString &name,
+ u_long indentation);
+static void print_context (ACE_Registry::Naming_Context &parent,
+ const ACE_TString &name,
+ u_long indentation);
+static void indent (u_long indentation);
+
+int
+ACE_TMAIN (int argc, ACE_TCHAR *argv[])
+{
+ int result;
+ ACE_Registry::Naming_Context naming_context;
+
+ // Connect to a predefined naming context
+ result = ACE_Predefined_Naming_Contexts::connect (naming_context,
+ HKEY_LOCAL_MACHINE,
+ // HKEY_CLASSES_ROOT,
+ // HKEY_USERS,
+ // HKEY_CURRENT_USER,
+ argc == 2 ? argv[1] : 0);
+
+ if (result != 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Predefined_Naming_Contexts::connect failed"), -1);
+
+ // Print contents of naming context
+ result = ::print_naming_context (naming_context, 0);
+ if (result != 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "print_naming_context failed"), -1);
+
+ return 0;
+}
+
+
+// Print contents of <naming_context>
+static int
+print_naming_context (ACE_Registry::Naming_Context &naming_context,
+ u_long indentation)
+{
+ ACE_Registry::Binding_List list;
+
+ // Get the list of all entries
+ int result = naming_context.list (list);
+ if (result != 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Registry::Naming_Context::list"), -1);
+
+ // Iterator through all entries
+ for (ACE_Registry::Binding_List::iterator i = list.begin ();
+ i != list.end ();
+ ++i)
+ {
+ // Yeeesss! STL rules!
+ ACE_Registry::Binding &binding = *i;
+
+ if (binding.type () == ACE_Registry::OBJECT)
+ // If object
+ ::print_object (binding.name (),
+ indentation);
+ else
+ // If context
+ ::print_context (naming_context,
+ binding.name (),
+ indentation);
+ }
+ return 0;
+}
+
+
+// Print an object with <name>
+static void
+print_object (const ACE_TString &name,
+ u_long indentation)
+{
+ // Set indentation
+ ::indent (indentation);
+ cout << name << endl;
+}
+
+
+// Print an context with <name> and <parent>
+static void
+print_context (ACE_Registry::Naming_Context &parent,
+ const ACE_TString &name,
+ u_long indentation)
+{
+ // Set indentation
+ indent (indentation);
+ cout << name << endl;
+
+ ACE_Registry::Naming_Context child_context;
+ // Find child context
+ int result = parent.resolve_context (name,
+ child_context,
+ KEY_READ);
+ if (result != 0)
+ ACE_ERROR ((LM_ERROR, "%s %s\n", "ACE_Registry::Naming_Context::resolve_context failed for:", name.c_str ()));
+ else
+ {
+ // Print contents of the child
+ result = ::print_naming_context (child_context,
+ indentation + INDENTATION_LEVEL);
+ if (result != 0)
+ ACE_ERROR ((LM_ERROR, "%p\n", "print_naming_context failed"));
+ }
+}
+
+
+// Pretty formating
+static void
+indent (u_long indentation)
+{
+ for (; indentation > 0; indentation--)
+ cout << " ";
+}
+#else /* !ACE_WIN32 || ACE_LACKS_WIN32_REGISTRY */
+int
+ACE_TMAIN (int , ACE_TCHAR *[])
+{
+ return 0;
+}
+#endif /* ACE_WIN32 && !ACE_LACKS_WIN32_REGISTRY */
diff --git a/ACE/examples/Registry/test_registry_update.cpp b/ACE/examples/Registry/test_registry_update.cpp
new file mode 100644
index 00000000000..78cd0aabd8c
--- /dev/null
+++ b/ACE/examples/Registry/test_registry_update.cpp
@@ -0,0 +1,161 @@
+// $Id$
+
+// Suppose this application belonged to AcmeSoft. AcmeSoft wants to
+// keep track of the number of times this application is
+// executed. They want two counters: one counts the number of
+// executions per machine, the other keeps track of the number of
+// executions per user.
+//
+// This application uses the ACE_Registry class to create and update
+// entries in the LOCAL_MACHINE and CURRENT_USER predefined registries
+// to store the counters.
+//
+// Note that this application will not work with remote registries
+// if used with the CURRENT_USER predefined registry.
+
+#include "ace/OS_main.h"
+
+#if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
+
+#include "ace/Registry.h"
+
+// FUZZ: disable check_for_streams_include
+#include "ace/streams.h"
+
+ACE_RCSID(Registry, test_registry_update, "$Id$")
+
+// Name for application's naming context
+static ACE_Registry::Name application_context_name;
+
+// Name for instance counter.
+static ACE_Registry::Name counter_name;
+
+// Protypes
+static int update_counter (HKEY predefined,
+ u_long &current_counter);
+static void setup_names ();
+
+int
+ACE_TMAIN (int, ACE_TCHAR *[])
+{
+ int result;
+ u_long current_counter = 0;
+
+ // initialize name
+ setup_names ();
+
+ // Update counter per user
+ result = ::update_counter (HKEY_CURRENT_USER,
+ current_counter);
+ if (result == 0)
+ {
+ cout << "User counter: " << current_counter << endl;
+
+ // Update counter per machine
+ result = ::update_counter (HKEY_LOCAL_MACHINE,
+ current_counter);
+ if (result == 0)
+ cout << "Machine counter: " << current_counter << endl;
+ }
+
+ if (result != 0)
+ ACE_DEBUG ((LM_DEBUG, "test failed\n"));
+ else
+ ACE_DEBUG ((LM_DEBUG, "test succeeded\n"));
+
+ return 0;
+}
+
+static int
+update_counter (HKEY predefined,
+ u_long &current_counter)
+{
+ int result;
+ ACE_Registry::Naming_Context parent_context;
+ ACE_Registry::Naming_Context application_context;
+
+ // Connect to predefined entry
+ result = ACE_Predefined_Naming_Contexts::connect (parent_context,
+ predefined);
+ if (result != 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Predefined_Naming_Contexts::connect failed"), -1);
+
+ // Find application context name
+ result = parent_context.resolve_context (application_context_name,
+ application_context);
+
+ if (result != 0)
+ // Failed to find: create a new context
+ result = parent_context.bind_new_context (application_context_name,
+ application_context);
+
+ if (result != 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Registry::Naming_Contexts::bind/resolve_context failed"), -1);
+
+ // Counter
+ u_long counter = 0;
+ // Represent counter as an ACE_Registry::Object
+ ACE_Registry::Object object ((void *) &counter,
+ sizeof counter,
+ REG_DWORD);
+ // Find counter
+ result = application_context.resolve (counter_name,
+ object);
+
+ if (result != 0)
+ // Failed to find: create new binding for object
+ {
+ counter = 1;
+ result = application_context.bind (counter_name,
+ object);
+ }
+ else
+ // Counter was found
+ {
+ // increment counter
+ counter++;
+ // Update
+ result = application_context.rebind (counter_name,
+ object);
+ }
+
+ if (result != 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Registry::Naming_Contexts::bind/resolve failed"), -1);
+ else
+ current_counter = counter;
+
+ return 0;
+}
+
+
+static void
+setup_names ()
+{
+ // Stupid implementation of STL is broken
+ /*
+ ::application_context_name.reserve (4);
+ ::application_context_name [0].id_ = ACE_TEXT ("Software");
+ ::application_context_name [1].id_ = ACE_TEXT ("AcmeSoft");
+ ::application_context_name [2].id_ = ACE_TEXT ("AcmeApplication");
+ ::application_context_name [3].id_ = ACE_TEXT ("1.0");
+
+ ::counter_name.reserve (1);
+ ::counter_name [0].id_ = ACE_TEXT ("Instance Counter");
+ */
+
+ ACE_Registry::Name_Component component;
+
+ component.id_ = ACE_TEXT ("Software"), ::application_context_name.insert (component);
+ component.id_ = ACE_TEXT ("AcmeSoft"), ::application_context_name.insert (component);
+ component.id_ = ACE_TEXT ("AcmeApplication"), ::application_context_name.insert (component);
+ component.id_ = ACE_TEXT ("1.0"), ::application_context_name.insert (component);
+
+ component.id_ = ACE_TEXT ("Instance Counter"), ::counter_name.insert (component);
+}
+#else /* !ACE_WIN32 || ACE_LACKS_WIN32_REGISTRY */
+int
+ACE_TMAIN (int , ACE_TCHAR *[])
+{
+ return 0;
+}
+#endif /* ACE_WIN32 && !ACE_LACKS_WIN32_REGISTRY */