Easy way to add CORBA sequences using C ++

I am using omniORB and C ++.

In my application, I get several small CORBA sequences from different modules, and then I need to combine them into one large sequence for further processing. Is there an easy way to do this? Something like seq2.append(seq1) or seq2.push_back(seq1) . Or some operators? (I'm really new to STL stuff).

The only way I found is to manually go through each element of small sequences and add it to a larger sequence.

 //idl struct Device; typedef sequence<Device> DevicesList; //c++ icore::DevicesList full_list; foreach (const DStatusList &stlist, states_) { icore::DevicesList list = convertList(stlist); int newlength = full_list.length() + list.length(); int last_index = full_list.length(); full_list.length(newlength); int j=0; for(int i=last_index; i< last_index+list.length(); i++,j++) { full_list[i] = list[j]; } } 

Thanks.

+4
source share
1 answer

It is not too difficult to make small utility functions for such things. For example, you can make your own push_back for CORBA sequences:

 template<class Seq, class T> void push_back(Seq& seq, const T& item) { const CORBA::ULong len = seq.length(); seq.length(len + 1); seq[len] = item; } MySequence s; push_back(s, 42); 

Just remember that some types of the ORB sequence do not reallocate memory to push_back (for example, std::vector ), so this can cause reallocation and copying on each call to push_back() . This can be a performance issue. You will need to check if your ORB is working or not, perhaps looking at the source to see if this is a problem. Unfortunately, I think omniORB has this problem; at least that happened a few years ago.

One way to mitigate this is to create your sequences with maximum values ​​(if you know it beforehand). This will result in one large distribution ahead, and you can then call push_back() to that maximum without causing a redistribution:

 MySequence s(100); // construct with maximum; allocation happens here push_back(s, 42); // no reallocation push_back(s, 0); // no reallocation 

I also agree with stefaanv's advice in the comments. Use CORBA sequences as little as possible. Mostly use it only "around the edges" of your application, where you need to make / receive CORBA calls. Retrieve data in standard containers where it is easier to manipulate as soon as possible. Do not let CORBA seep into your application. It also helps make your application more portable if you ever decide to connect to a non-CORBA environment.

I also heard about the new IDL planning for C ++ 11 that is being proposed. This maps the IDL sequences directly to std::vector , which will greatly simplify the process.

+5
source

Source: https://habr.com/ru/post/1484370/


All Articles