Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Param_Test / recursive_struct.cpp
blobc05eb8f3c90f748f9c03a7bc23ce3e9ffaa1f579
2 //=============================================================================
3 /**
4 * @file recursive_struct.cpp
6 * test structure that contains a sequence of itself
8 * @author Aniruddha Gokhale
9 * @author Jeff Parsons
11 //=============================================================================
14 #include "recursive_struct.h"
16 const CORBA::ULong MAX_DEPTH = 5;
17 const CORBA::ULong MAX_SEQ_LENGTH = 3;
19 // ************************************************************************
20 // Test_Recursive_Struct
21 // ************************************************************************
23 Test_Recursive_Struct::Test_Recursive_Struct ()
24 : opname_ (CORBA::string_dup ("test_recursive_struct"))
28 Test_Recursive_Struct::~Test_Recursive_Struct ()
30 CORBA::string_free (this->opname_);
31 this->opname_ = 0;
32 // the other data members will be freed as they are "_var"s and objects
33 // (rather than pointers to objects)
36 const char *
37 Test_Recursive_Struct::opname () const
39 return this->opname_;
42 void
43 Test_Recursive_Struct::dii_req_invoke (CORBA::Request *req)
45 req->add_in_arg ("s1") <<= this->in_;
46 req->add_inout_arg ("s2") <<= this->inout_.in ();
47 req->add_out_arg ("s3") <<= this->out_.in ();
49 req->set_return_type (Param_Test::_tc_Recursive_Struct);
51 req->invoke ();
53 const Param_Test::Recursive_Struct *tmp = 0;
54 req->return_value () >>= tmp;
55 this->ret_ = new Param_Test::Recursive_Struct (*tmp);
57 CORBA::NamedValue_ptr o2 =
58 req->arguments ()->item (1);
59 *o2->value () >>= tmp;
60 this->inout_ = new Param_Test::Recursive_Struct (*tmp);
62 CORBA::NamedValue_ptr o3 =
63 req->arguments ()->item (2);
64 *o3->value () >>= tmp;
65 this->out_ = new Param_Test::Recursive_Struct (*tmp);
68 int
69 Test_Recursive_Struct::init_parameters (Param_Test_ptr)
71 // The client calls init_parameters() before the first
72 // call and reset_parameters() after each call. For this
73 // test, we want the same thing to happen each time.
74 return this->reset_parameters ();
77 int
78 Test_Recursive_Struct::reset_parameters ()
80 // Since these are _vars, we do this the first call and
81 // every call thereafter (if any).
82 this->inout_ = new Param_Test::Recursive_Struct;
83 this->out_ = new Param_Test::Recursive_Struct;
84 this->ret_ = new Param_Test::Recursive_Struct;
86 // value generator
87 Generator *gen = GENERATOR::instance ();
89 // Set the depth of recursion.
90 CORBA::ULong depth = (CORBA::ULong) (gen->gen_long () % MAX_DEPTH) + 1;
92 // No recursion for inout_ until after the call.
93 this->inout_->children.length (0);
95 // Keeps Purify happy.
96 this->inout_->x = 0;
98 // Call the recursive helper function.
99 this->deep_init (this->in_,
100 gen,
101 depth);
103 return 0;
107 Test_Recursive_Struct::run_sii_test (Param_Test_ptr objref)
111 Param_Test::Recursive_Struct_out out (this->out_.out ());
113 this->ret_ = objref->test_recursive_struct (this->in_,
114 this->inout_.inout (),
115 out);
117 return 0;
119 catch (const CORBA::Exception& ex)
121 ex._tao_print_exception ("Test_Recursive_Struct::run_sii_test\n");
123 return -1;
126 CORBA::Boolean
127 Test_Recursive_Struct::check_validity ()
129 // Pair in_ with each of the returned values and call the
130 // helper function with that pair.
132 if (this->deep_check (this->in_, this->inout_.in ()) == 0)
134 ACE_DEBUG ((LM_DEBUG,
135 "mismatch of inout arg\n"));
137 return 0;
140 if (this->deep_check (this->in_, this->out_.in ()) == 0)
142 ACE_DEBUG ((LM_DEBUG,
143 "mismatch of out arg\n"));
145 return 0;
148 if (this->deep_check (this->in_, this->ret_.in ()) == 0)
150 ACE_DEBUG ((LM_DEBUG,
151 "mismatch of ret value\n"));
153 return 0;
156 // If we get this far, all is correct.
157 return 1;
160 CORBA::Boolean
161 Test_Recursive_Struct::check_validity (CORBA::Request_ptr)
163 return this->check_validity ();
166 void
167 Test_Recursive_Struct::print_values ()
171 // Private helper function to recursively initialize the struct.
172 void
173 Test_Recursive_Struct::deep_init (Param_Test::Recursive_Struct &rs,
174 Generator *gen,
175 CORBA::ULong level)
177 rs.x = gen->gen_long ();
179 if (level == 1)
180 // No more recursion.
182 rs.children.length (0);
184 return;
186 else
188 // Generate a sequence length.
189 CORBA::ULong len = (CORBA::ULong) (gen->gen_long () % MAX_SEQ_LENGTH) + 1;
191 rs.children.length (len);
193 // We recurse for each element of the member sequence.
194 for (CORBA::ULong i = 0; i < len; i++)
196 this->deep_init (rs.children[i],
197 gen,
198 level - 1);
203 // Private helper function for check_validity (so we can recurse).
204 CORBA::Boolean
205 Test_Recursive_Struct::deep_check (const Param_Test::Recursive_Struct &in_struct,
206 const Param_Test::Recursive_Struct &test_struct)
208 // Do the CORBA::Long members match?
209 if (in_struct.x != test_struct.x)
211 ACE_DEBUG ((LM_DEBUG,
212 "mismatch of CORBA::Long struct members\n"));
214 return 0;
217 // Do the sequence lengths match?
218 if (in_struct.children.length () != test_struct.children.length ())
220 ACE_DEBUG ((LM_DEBUG,
221 "mismatch of member sequence lengths\n"));
223 return 0;
226 // At the bottom level, the length is 0 and we skip this part.
227 // Otherwise recurse.
228 for (CORBA::ULong i = 0; i < in_struct.children.length (); i++)
230 if (!this->deep_check (in_struct.children[i],
231 test_struct.children[i]))
233 ACE_DEBUG ((LM_DEBUG,
234 "mismatch of contained structs\n"));
236 return 0;
240 return 1;