CORBA_SeqUtil.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #ifndef CORBA_SEQUTIL_H
00020 #define CORBA_SEQUTIL_H
00021 
00022 #include <rtm/RTC.h>
00023 #include <rtm/Manager.h>
00024 
00066 namespace CORBA_SeqUtil
00067 {
00097   template <class CorbaSequence, class Functor>
00098   Functor for_each(CorbaSequence& seq, Functor f)
00099   {
00100     CORBA::ULong len;
00101     len = seq.length();
00102     for (CORBA::ULong i = 0; i < len; ++i)
00103       {
00104         f(seq[i]);
00105       }
00106     return f;
00107   }
00108   
00141   template <class CorbaSequence, class Functor>
00142   CORBA::Long find(const CorbaSequence& seq, Functor f)
00143   {
00144     CORBA::ULong len(seq.length());
00145     for (CORBA::ULong i = 0; i < len; ++i)
00146       {
00147         if (f(seq[i])) return (CORBA::Long)i;
00148       }
00149     return -1;
00150   }
00151   
00174   template <class CorbaSequence, class SequenceElement>
00175   void push_back(CorbaSequence& seq, SequenceElement elem)
00176   {
00177     CORBA::ULong len(seq.length());
00178     seq.length(len + 1);
00179     seq[len] = elem;
00180   }
00181   
00202   template <class CorbaSequence>
00203   void push_back_list(CorbaSequence& seq1, const CorbaSequence& seq2)
00204   {
00205     CORBA::ULong len1(seq1.length());
00206     CORBA::ULong len2(seq2.length());
00207     CORBA::ULong len(len1 + len2);
00208     seq1.length(len);
00209     
00210     for (CORBA::ULong i = 0; i < len2; ++i)
00211       {
00212         seq1[len1 + i] = seq2[i];
00213       }
00214   }
00215   
00244   template <class CorbaSequence, class SequenceElement>
00245   void insert(CorbaSequence& seq, SequenceElement& elem, CORBA::ULong index)
00246   {
00247     CORBA::ULong len(seq.length());
00248     if (index > len)
00249       {
00250         push_back(seq, elem);
00251         return;
00252       }
00253     
00254     seq.length(len + 1);
00255     for (CORBA::ULong i = len; i > index; --i)
00256       {
00257         seq[i] = seq[i - 1];
00258       }
00259     seq[index] = elem;
00260   }
00261   
00287   template <class CorbaSequence, class SequenceElement>
00288   SequenceElement& front(CorbaSequence& seq)
00289   {
00290     return seq[0];
00291   }
00292   
00316   template <class CorbaSequence, class SequenceElement>
00317   SequenceElement& back(CorbaSequence& seq)
00318   {
00319     return seq[seq.length() - 1];
00320   }
00321   
00344   template <class CorbaSequence>
00345   void erase(CorbaSequence& seq, CORBA::ULong index)
00346   {
00347     CORBA::ULong len(seq.length());
00348     if (index > len) return;
00349     
00350     for (CORBA::ULong i = index; i < len - 1; ++i)
00351       {
00352         seq[i] = seq[i + 1];
00353       }
00354     seq.length(len - 1);
00355   }
00356   
00380   template <class CorbaSequence, class Functor>
00381   void erase_if(CorbaSequence& seq, Functor f)
00382   {
00383     CORBA::Long index;
00384     index = find(seq, f);
00385     if (index < 0) return;
00386     CORBA_SeqUtil::erase(seq, index);
00387   }
00388   
00404   template <class CorbaSequence>
00405   void clear(CorbaSequence& seq)
00406   {
00407     seq.length(0);
00408   }
00409   // End of CORBA sequence helper functions
00410 
00411 
00412   template <class CorbaRefSequence>
00413   coil::vstring refToVstring(const CorbaRefSequence& objlist)
00414   {
00415     coil::vstring iorlist;
00416     CORBA::ORB_ptr orb = ::RTC::Manager::instance().getORB();
00417 
00418     for (CORBA::ULong i(0), len(objlist.length()); i < len; ++i)
00419       {
00420 #ifndef ORB_IS_RTORB
00421         CORBA::String_var str_var = orb->object_to_string(objlist[i].in());
00422 #else
00423         CORBA_Object obj = (objlist.cobj())->_buffer[i];
00424         CORBA::String_var str_var = orb->object_to_string2(obj);
00425 #endif
00426         iorlist.push_back(str_var.in());
00427       }
00428     return iorlist;
00429   }
00430 }; // namespace CORBA_SeqUtil
00431 #endif // CORBA_SEQUTIL_H


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:37