2 //=============================================================================
6 * This example shows how to use reference counted servants to
7 * automatically manage dynamic memory for servants.
8 * Stubs/Skeletons and client code is available in
11 * @author Irfan Pyarali
13 //=============================================================================
16 #include "ace/Get_Opt.h"
18 #include "ace/OS_NS_stdio.h"
19 #include "ace/OS_NS_string.h"
21 // This is to remove "inherits via dominance" warnings from MSVC.
22 // MSVC is being a little too paranoid.
23 #if defined (_MSC_VER)
24 # pragma warning (disable : 4250)
27 class reference_counted_test_i
:
31 /// Constructor - takes a POA and a value parameter
32 reference_counted_test_i (CORBA::ORB_ptr orb
,
33 PortableServer::POA_ptr poa
);
36 reference_counted_test_i::reference_counted_test_i (CORBA::ORB_ptr orb
,
37 PortableServer::POA_ptr poa
)
43 static const ACE_TCHAR
*ior_output_file
= ACE_TEXT ("ior");
46 parse_args (int argc
, ACE_TCHAR
**argv
)
48 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("f:"));
51 while ((c
= get_opts ()) != -1)
55 ior_output_file
= get_opts
.opt_arg ();
60 ACE_ERROR_RETURN ((LM_ERROR
,
62 "[-f ior_output_file]"
68 // Indicates successful parsing of command line.
73 write_iors_to_file (const char *ior
)
75 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
78 ACE_ERROR_RETURN ((LM_ERROR
, "Cannot open output files for writing IORs: %s\n",
84 result
= ACE_OS::fprintf (output_file
,
87 ACE_OS::fclose (output_file
);
89 if (result
!= ACE_OS::strlen (ior
))
90 ACE_ERROR_RETURN ((LM_ERROR
,
91 "ACE_OS::fprintf failed while writing %C to %s\n",
100 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
104 // Initialize the ORB first.
105 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
107 int result
= parse_args (argc
, argv
);
111 // Obtain the RootPOA.
112 CORBA::Object_var obj
=
113 orb
->resolve_initial_references ("RootPOA");
115 // Get the POA_var object from Object_var.
116 PortableServer::POA_var root_poa
=
117 PortableServer::POA::_narrow (obj
.in ());
119 // Get the POAManager of the RootPOA.
120 PortableServer::POAManager_var poa_manager
=
121 root_poa
->the_POAManager ();
124 reference_counted_test_i
*servant
= 0;
125 ACE_NEW_RETURN (servant
,
126 reference_counted_test_i (orb
.in (),
130 // Get Object Reference for the servant object.
131 test_var test
= servant
->_this ();
133 // This means that the ownership of <servant> now belongs to
135 servant
->_remove_ref ();
137 // Stringyfy all the object references and print them out.
138 CORBA::String_var ior
= orb
->object_to_string (test
.in ());
140 ACE_DEBUG ((LM_DEBUG
,
144 int write_result
= write_iors_to_file (ior
.in ());
145 if (write_result
!= 0)
148 poa_manager
->activate ();
154 catch (const CORBA::Exception
& ex
)
156 ex
._tao_print_exception ("Exception caught");