0

The IDL file defines a sequence, whose element are IPCParams type.

module project{
  struct IPCParams{
     string ipcSetId;
     any params;  
  };

 typedef sequence<IPCParams> Params;
};

My sample code(It is developed in VS08 with ACE_TAO ) is below:

    project::Params  params(4);
    params.length(4);
    for(int i=0; i<4;i++){
        project::IPCParams ipcP;

        std::stringstream sstm("");
        sstm <<"ipcid-" << " "<<i;
        std::string val = sstm.str();
        ipcP.ipcSetId = val.c_str();

        std::stringstream xxxx("");
        xxxx <<" paramters " << i ;
        val = xxxx.str();
        std::cout<<" str: "<<val.c_str()<<"  || "<<val <<"||" <<std::endl;
        ipcP.params <<= CORBA::Any::from_string(val.c_str(),100);
        params[i] = ipcP;
    }

    CORBA::Any any;
    any<<=params;

    project::Params* params_out;
    any >>= params_out;
    //CORBA::Boolean flag = ttt->release();
    project::IPCParams* ipcPtr = params_out->get_buffer();
    for(int i=0; i< params_out->length(); i++){
             // change ipcPtr[i] = > params_out[i] would cause   error C2440: 'initializing' : cannot convert from 'project::Params' to 'project::IPCParams' 
        project::IPCParams ipcp = ipcPtr[i];
        const char*  realVal;
        ipcp.params >>= CORBA::Any::to_string(realVal,100);

        std::cout<<" id: "<<ipcp.ipcSetId <<"  anyValue: "<< realVal <<std::endl;
    }

}

The code above tries to demo insert project::params, a sequence of IPCParams, into any object and then extract all values out from this any object. One member of IPCParams is also any object and i just insert a string into it.

The two problems here are:

  • Is there any other ways to extract sequences info except using dynamicAny or get_buffer.

  • Wrong execution result for IPCParams::params value which is any type. Here is output:

Calling test Params...
 str:  paramters 0  || ||004CF928
 str:  paramters 1  || ||004CF928
 str:  paramters 2  || ||004CF928
 str:  paramters 3  || ||004CF928
  Arg name: Params
 id: ipc id 0  anyValue:   -004CF7AC
 id: ipc id 1  anyValue:   -004CF7AC
 id: ipc id 2  anyValue:   -004CF7AC
 id: ipc id 3  anyValue:   -004CF7AC

Clearly, the str in line

        const char* str = xxxx.str().c_str();      // Q2: str is blank??

in the loop of insertion sections points to an empty string(it should non-blank). I can not explain why

xxxx.str().c_str() => Correct string

str => blank..

Could anyone help? Thanks in advance.


Two of * rules for Using Sequences* in Advanced CORBA Programming with C++:

1- Avoid using the data constructor for elements of complex type. 2- Do not use the data constructor or the buffer manipulation functions unless you really need to.

After extract value from Any, i tried get the i-th element using:

            project::IPCParams ipcp = params_out[i];

instead of

            project::IPCParams* ipcPtr = params_out->get_buffer()
            project::IPCParams ipcp = ipcPtr[i];

But the compilation fails, as you can see error message in the comment.

shijie xu
  • 1,975
  • 21
  • 52
  • 1
    Your code is full of errors, please take a step back and first learn the IDL to C++ language mapping. For insertion/extraction of a string into an Any use CORBA::Any::to_string and CORBA::Any::from_string. – Johnny Willemsen Feb 27 '14 at 12:08
  • Thanks @JohnnyWillemsen. 1) I have updated the code above. I have difficulty in figure out the wrong place. The only place i am not sure is about params_out->get_buffer which should involve check release(false as default) flags in sequence. 2) CORBA::Any::from(to)_string is used for bounded string while i use it as unbouned string. Is there any disadvantage to use bounded string. – shijie xu Feb 27 '14 at 21:08
  • The from/to string are also for regular sstrings. Make use of the operator[] of the sequence and not get its buffer. Also check the usage of strings, probably some CORBA::string_dup calls are missing – Johnny Willemsen Feb 28 '14 at 07:45

0 Answers0