1 // Copyright (C) 2006-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
35 #include "YacsTrace.hxx"
37 int main(int argc, char** argv)
41 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
42 CORBA::Object_var obj = orb->string_to_object("corbaname:rir:#test.my_context/Echo.Object");
43 eo::Echo_var echoref = eo::Echo::_narrow(obj);
44 if( CORBA::is_nil(echoref) )
46 std::cerr << "Can't narrow reference to type Echo (or it was nil)." << std::endl;
50 CORBA::String_var src = (const char*) "Hello!";
51 CORBA::String_var dest = echoref->echoString(src);
52 std::cerr << "I said, \"" << (char*)src << "\"." << std::endl
53 << "The Echo object replied, \"" << (char*)dest <<"\"." << std::endl;
55 CORBA::Object_var ob = orb->string_to_object("corbaname:rir:#test.my_context/D.Object");
56 eo::D_var Dref = eo::D::_narrow(ob);
57 if( CORBA::is_nil(Dref) ) {
58 std::cerr << "Can't narrow reference to type D (or it was nil)." << std::endl;
64 echoref->echoD(Dref,dout);
65 std::cerr << dout->echoLong2(10) << std::endl;
68 echoref->echoD(dout,ddout);
69 std::cerr << dout->echoLong2(10) << std::endl;
71 CORBA::Object_var oob = orb->string_to_object("corbaname:rir:#test.my_context/Obj.Object");
72 eo::Obj_var Objref = eo::Obj::_narrow(oob);
75 echoref->echoObj2(Objref,Objout);
76 std::cerr << Objout->echoLong(10) << std::endl;
78 CORBA::Object_var cob = orb->string_to_object("corbaname:rir:#test.my_context/C.Object");
79 eo::C_var Cref = eo::C::_narrow(cob);
81 echoref->echoC(Cref,Cout);
83 echoref->echoObj2(Cref,Objout);
84 //echoref->echoC(Cref,Objout); compilation impossible
85 //echoref->echoObj2(Cref,Cout); compilation impossible
86 //echoref->echoC(Objref,Cout); compilation impossible
88 CORBA::Object_var dobj = orb->resolve_initial_references("DynAnyFactory");
89 DynamicAny::DynAnyFactory_var dynFactory = DynamicAny::DynAnyFactory::_narrow(dobj);
91 CORBA::TypeCode_var content_type=orb->create_interface_tc("IDL:eo/Obj:1.0","Obj");
93 std::vector<CORBA::Any*> v;
94 std::vector<CORBA::Any*>::const_iterator iter;
96 CORBA::Any *any = new CORBA::Any();
101 CORBA::Any *any2 = new CORBA::Any();
105 std::cerr << "refcount: " << oob->_PR_getobj()->pd_refCount << std::endl;
108 CORBA::Any *any3 = new CORBA::Any();
112 std::cerr << "refcount: " << oob->_PR_getobj()->pd_refCount << std::endl;
118 std::cerr << "refcount: " << xobj->_PR_getobj()->pd_refCount << std::endl;
123 std::cerr << "refcount: " << yobj->_PR_getobj()->pd_refCount << std::endl;
126 CORBA::Object_var zobj ;
127 *any2 >>= CORBA::Any::to_object(zobj) ;
129 std::cerr << "refcount: " << zobj->_PR_getobj()->pd_refCount << std::endl;
132 // Build an Any from vector v
134 CORBA::TypeCode_var tc;
138 // Check if it's an homogeneous vector : all TypeCodes must be equivalent
139 CORBA::TypeCode_var any_tc=v[0]->type();
140 tc=orb->create_sequence_tc(0,any_tc);
142 for(iter=v.begin();iter!=v.end();iter++)
145 CORBA::TypeCode_var tc2=a->type();
146 if(!tc2->equivalent(any_tc))
148 // It's a non homogeneous vector : create a sequence of any
149 tc=orb->create_sequence_tc(0,CORBA::_tc_any);
154 // It's a homogeneous vector : create a sequence of the homogeneous type
158 // It's an empty vector : create a sequence of any
159 tc=orb->create_sequence_tc(0,CORBA::_tc_any);
163 DynamicAny::DynAny_var dynany = dynFactory->create_dyn_any_from_type_code(tc);
164 DynamicAny::DynSequence_var ds = DynamicAny::DynSequence::_narrow(dynany);
166 ds->set_length(v.size());
167 for(iter=v.begin();iter!=v.end();iter++)
169 DynamicAny::DynAny_var temp=ds->current_component();
175 //delete intermediate any
179 CORBA::Any_var seqany=ds->to_any();
183 std::map<std::string,CORBA::Any*> m;
184 CORBA::Any *any4 = new CORBA::Any();
188 CORBA::Any *any5 = new CORBA::Any();
191 CORBA::Any *any6 = new CORBA::Any();
194 CORBA::Any *any7 = new CORBA::Any();
195 *any7 <<= (const char*)"coucou";
197 CORBA::Any *any8 = new CORBA::Any();
198 *any8 <<= CORBA::Any::from_boolean(1);
213 CORBA::StructMemberSeq mseq;
214 mseq.length(nMember);
215 mseq[0].name=CORBA::string_dup("x");
216 mseq[0].type=CORBA::TypeCode::_duplicate(CORBA::_tc_double);
217 mseq[1].name=CORBA::string_dup("y");
218 mseq[1].type=CORBA::TypeCode::_duplicate(CORBA::_tc_long);
219 mseq[2].name=CORBA::string_dup("s");
220 mseq[2].type=CORBA::TypeCode::_duplicate(CORBA::_tc_string);
221 mseq[3].name=CORBA::string_dup("b");
222 mseq[3].type=CORBA::TypeCode::_duplicate(CORBA::_tc_boolean);
223 mseq[4].name=CORBA::string_dup("ob");
224 mseq[4].type=content_type;
225 for(int i=0;i<nMember;i++)
227 const char* name=mseq[i].name;
228 std::cerr << mseq[i].name << std::endl;
229 if(m.count(name) !=0)
231 std::cerr << "member: " << mseq[i].name << std::endl;
232 mseq[i].type=m[name]->type();
235 CORBA::TypeCode_var ts= orb->create_struct_tc("","",mseq);
236 //CORBA::TypeCode_var ts= orb->create_struct_tc("IDL:eo/S3:1.0","S3",mseq);
237 dynany = dynFactory->create_dyn_any_from_type_code(ts);
238 DynamicAny::DynStruct_var dst = DynamicAny::DynStruct::_narrow(dynany);
240 for(int i=0;i<nMember;i++)
242 DynamicAny::DynAny_var temp=dst->current_component();
243 const char* name=mseq[i].name;
244 std::cerr << name << std::endl;
245 CORBA::Any* a=m[name];
247 //delete intermediate any
251 CORBA::Any_var structany=dst->to_any();
255 for(int i=0;i<nMember;i++)
257 const char * name=tst->memberName(i);
258 //TypeCode* tm=tst->memberType(i);
259 //do not test member presence : test has been done in ToYacs convertor
260 CORBA::Any* a=m[name];
261 members[i].id=CORBA::string_dup(name);
263 //delete intermediate any
270 CORBA::TypeCode_var tcout=orb->create_sequence_tc(0,content_type);
272 CORBA::Request_var req = echoref->_request("echoStruct2");
273 //CORBA::Request_var req = echoref->_request("echoObjectVec");
274 CORBA::NVList_ptr arguments = req->arguments() ;
276 arguments->add_value( "s1" , structany , CORBA::ARG_IN ) ;
277 //arguments->add_value( "s1" , seqany , CORBA::ARG_IN ) ;
280 out.replace(ts, (void*) 0);
281 //out.replace(tcout, (void*) 0);
282 arguments->add_value( "s2" , out , CORBA::ARG_OUT );
284 req->set_return_type(CORBA::_tc_void);
286 //req->exceptions()->add(eo::_tc_SALOME_Exception);
289 CORBA::Exception *exc =req->env()->exception();
292 std::cerr << "The raised exception is of Type:" << exc->_name() << std::endl;
293 if(strcmp(exc->_name(),"MARSHAL") == 0)
295 const char* ms = ((CORBA::MARSHAL*)exc)->NP_minorString();
297 std::cerr << "(CORBA::MARSHAL: minor = " << ms << ")" << std::endl;
302 std::cerr << "Before request" << std::endl;
303 req = echoref->_request("echoObj2");
304 std::cerr << "After request" << std::endl;
305 req->add_in_arg() <<= Objref;
306 std::cerr << "After add_in_arg" << std::endl;
307 req->add_out_arg() <<= Objout;
308 std::cerr << "After add_out_arg" << std::endl;
309 req->set_return_type(CORBA::_tc_void);
310 std::cerr << "After set_return_type" << std::endl;
312 std::cerr << "After invoke" << std::endl;
313 exc =req->env()->exception();
316 std::cerr << "The raised exception is of Type:" << exc->_name() << std::endl;
318 //end of DII on echoObj2
321 req = echoref->_request("echoObjVec");
322 CORBA::Any *aAny = new CORBA::Any();
325 CORBA::TypeCode_var any_tc2=aAny->type();
326 CORBA::TypeCode_var tc2=orb->create_sequence_tc(0,any_tc2);
327 DynamicAny::DynAny_var dynany2 = dynFactory->create_dyn_any_from_type_code(tc2);
328 DynamicAny::DynSequence_var ds2 = DynamicAny::DynSequence::_narrow(dynany2);
330 DynamicAny::DynAny_var temp=ds2->current_component();
331 temp->from_any(*aAny);
333 temp=ds2->current_component();
334 CORBA::Any *aAny2 = new CORBA::Any();
336 CORBA::Object_var zzobj ;
337 *aAny2 >>= CORBA::Any::to_object(zzobj) ;
338 temp->insert_reference(zzobj);
341 CORBA::Any_var seqany2=ds2->to_any();
344 arguments = req->arguments() ;
345 arguments->add_value( "s1" , seqany2 , CORBA::ARG_IN ) ;
348 out2.replace(tc2, (void*) 0);
349 arguments->add_value( "s2" , out2 , CORBA::ARG_OUT );
350 req->set_return_type(CORBA::_tc_void);
353 CORBA::Exception *exc2 =req->env()->exception();
356 std::cerr << "The raised exception is of Type:" << exc2->_name() << std::endl;
357 if(strcmp(exc2->_name(),"MARSHAL") == 0)
359 const char* ms = ((CORBA::MARSHAL*)exc2)->NP_minorString();
361 std::cerr << "(CORBA::MARSHAL: minor = " << ms << ")" << std::endl;
366 CORBA::Any *ob=arguments->item(1)->value();
367 DynamicAny::DynAny_var dynany= dynFactory->create_dyn_any(*ob);
368 DynamicAny::DynSequence_ptr ds=DynamicAny::DynSequence::_narrow(dynany);
369 DynamicAny::AnySeq_var as=ds->get_elements();
370 int length=as->length();
372 for(int i=0;i<length; i++)
374 CORBA::TypeCode_var t=as[i].type();
378 DEBTRACE(xobj->_PD_repoId);
383 //end of DII on echoObjVec
385 // Delete allocated objects to remove memory leaks (valgrind tests)
389 catch(DynamicAny::DynAny::TypeMismatch& ex)
391 std::cerr << "Caught a DynamicAny::DynAny::TypeMismatch exception" << std::endl;
393 catch(DynamicAny::DynAny::InvalidValue& ex)
395 std::cerr << "Caught a DynamicAny::DynAny::InvalidValue exception" << std::endl;
397 catch(CORBA::COMM_FAILURE& ex)
399 std::cerr << "Caught system exception COMM_FAILURE -- unable to contact the object." << std::endl;
401 catch(CORBA::SystemException&)
403 std::cerr << "Caught a CORBA::SystemException." << std::endl;
405 catch(CORBA::Exception&)
407 std::cerr << "Caught CORBA::Exception." << std::endl;
409 catch(omniORB::fatalException& fe)
411 std::cerr << "Caught omniORB::fatalException:" << std::endl;
412 std::cerr << " file: " << fe.file() << std::endl;
413 std::cerr << " line: " << fe.line() << std::endl;
414 std::cerr << " mesg: " << fe.errmsg() << std::endl;
418 std::cerr << "Caught unknown exception." << std::endl;