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
[])
105 // Initialize the ORB first.
106 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
108 int result
= parse_args (argc
, argv
);
112 // Obtain the RootPOA.
113 CORBA::Object_var obj
=
114 orb
->resolve_initial_references ("RootPOA");
116 // Get the POA_var object from Object_var.
117 PortableServer::POA_var root_poa
=
118 PortableServer::POA::_narrow (obj
.in ());
120 // Get the POAManager of the RootPOA.
121 PortableServer::POAManager_var poa_manager
=
122 root_poa
->the_POAManager ();
125 reference_counted_test_i
*servant
= 0;
126 ACE_NEW_RETURN (servant
,
127 reference_counted_test_i (orb
.in (),
131 // Get Object Reference for the servant object.
132 test_var test
= servant
->_this ();
134 // This means that the ownership of <servant> now belongs to
136 servant
->_remove_ref ();
138 // Stringyfy all the object references and print them out.
139 CORBA::String_var ior
= orb
->object_to_string (test
.in ());
141 ACE_DEBUG ((LM_DEBUG
,
145 int write_result
= write_iors_to_file (ior
.in ());
146 if (write_result
!= 0)
149 poa_manager
->activate ();
155 catch (const CORBA::Exception
& ex
)
157 ex
._tao_print_exception ("Exception caught");