00001
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef CORBA_Util_h
00040 #define CORBA_Util_h
00041
00042
00043 #include <ace/Guard_T.h>
00044 #include <ace/Thread_Mutex.h>
00045 #include <ace/Recursive_Thread_Mutex.h>
00046 #include <rtm/RTC.h>
00047
00048 typedef ACE_Guard<ACE_Thread_Mutex> Guard;
00049 typedef ACE_Read_Guard<ACE_Thread_Mutex> Read_Guard;
00050 typedef ACE_Write_Guard<ACE_Thread_Mutex> Write_Guard;
00051
00052 typedef ACE_Guard<ACE_Recursive_Thread_Mutex> Guard_r;
00053 typedef ACE_Read_Guard<ACE_Recursive_Thread_Mutex> Read_Guard_r;
00054 typedef ACE_Write_Guard<ACE_Recursive_Thread_Mutex> Write_Guard_r;
00055
00056
00070 namespace CORBA_Sequence_Util
00071 {
00096 template <class CorbaSequence, class Functor>
00097 Functor for_each(const CorbaSequence& seq, Functor f)
00098 {
00099 CORBA::ULong len;
00100 len = seq.length();
00101 for (CORBA::ULong i = 0; i < len; ++i)
00102 {
00103 f(seq[i]);
00104 }
00105 return f;
00106 }
00107
00108
00139 template <class CorbaSequence, class Functor>
00140 CORBA::Long find(const CorbaSequence& seq, Functor f)
00141 {
00142 CORBA::ULong len(seq.length());
00143 for (CORBA::ULong i = 0; i < len; ++i)
00144 {
00145 if (f(seq[i])) return (CORBA::Long)i;
00146 }
00147 return -1;
00148 }
00149
00172 template <class CorbaSequence, class SequenceElement>
00173 void push_back(CorbaSequecne& seq, SequenceElement elem)
00174 {
00175 CORBA::ULong len(seq.length());
00176 seq.length(len + 1);
00177 seq[len] = item;
00178 }
00179
00180
00209 template <class CorbaSequence, class SequenceElement>
00210 void insert(CorbaSequence& seq, SequenceElement& elem, CORBA::ULong index)
00211 {
00212 CORBA::ULong len(seq.length());
00213 if (index > len) push_back(seq, elem);
00214
00215 seq.length(len + 1);
00216 for (CORBA::ULong i = len; i >= index; --i)
00217 {
00218 seq[i + 1] = seq[i];
00219 }
00220 seq[index] = elem;
00221 }
00222
00223
00242 template <class CorbaSequence, class SequenceElement>
00243 SequenceElement& front(CorbaSequence& seq)
00244 {
00245 return seq[0];
00246 }
00247
00248
00267 template <class CorbaSequence, class SequenceElement>
00268 SequenceElement& back(CorbaSequence& seq)
00269 {
00270 return seq[seq.length() - 1];
00271 }
00272
00273
00296 template <class CorbaSequence>
00297 void erase(CorbaSequence& seq, CORBA::ULong index)
00298 {
00299 CORBA::ULong len(seq.length());
00300 if (index > len) return;
00301
00302 for (CORBA::ULong i = index; i < len - 1; ++i)
00303 {
00304 seq[i] = seq[i + 1];
00305 }
00306 seq.length(len - 1);
00307 }
00308
00323 template <class CorbaSequence>
00324 void clear(CorbaSequence& seq)
00325 {
00326 seq.length(0);
00327 }
00328
00329
00330
00331
00332
00333
00334
00335
00336 template <class T>
00337 struct LockedStruct
00338 {
00339 ACE_Thread_Mutex lock;
00340 T data;
00341 };
00342
00367 template <class CorbaSequence, class SequenceItem, class Mutex>
00368 class SequenceEx
00369 : public CorbaSequence
00370 {
00371 public:
00397 SequenceEx(): CorbaSequence(0) {};
00398
00399
00421 SequenceEx(const CorbaSequence& _sq)
00422 : CorbaSequence(0)
00423 {
00424 this->length(_sq.length());
00425 CORBA::ULong len(this->length());
00426 for (CORBA::ULong i = 0; i < len; ++i)
00427 (*this)[i] = _sq[i];
00428 };
00429
00430
00446 SequenceEx(const SequenceEx& _sq)
00447 : CorbaSequence(0)
00448 {
00449 this->length(_sq.length());
00450 CORBA::ULong len(this->length());
00451 for (CORBA::ULong i = 0; i < len; ++i)
00452 (*this)[i] = _sq[i];
00453 };
00454
00455
00471 SequenceEx& operator=(const SequenceEx& _sq)
00472 {
00473 this->length(_sq.length());
00474 CORBA::ULong len(this->length());
00475 for (CORBA::ULong i = 0; i < len; ++i)
00476 (*this)[i] = _sq[i];
00477 return *this;
00478 };
00479
00480
00496 SequenceEx& operator=(const CorbaSequence& _sq)
00497 {
00498 this->length(_sq.length());
00499 CORBA::ULong len(this->length());
00500 for (CORBA::ULong i = 0; i < len; ++i)
00501 (*this)[i] = _sq[i];
00502 return *this;
00503 };
00504
00505
00517 virtual ~SequenceEx()
00518 {
00519 this->length(0);
00520 };
00521
00522
00544 inline CORBA::ULong size() { return this->length(); }
00545
00546
00568 inline CORBA::ULong max_size() { return this->maximum(); }
00569
00570
00593 inline bool empty() { return (this->length() == 0) ? true : false; }
00594
00595
00626 void resize(CORBA::ULong new_size, SequenceItem& item)
00627 {
00628 ACE_Write_Guard<Mutex> gaurd(lock);
00629 CORBA::ULong len(this->length);
00630 if (new_size > len)
00631 {
00632 this->length(new_size);
00633 for (CORBA::ULong i = len; i < new_size; ++i)
00634 (*this)[i] = item;
00635 }
00636 else if (new_size < len)
00637 {
00638 this->length(new_size);
00639 }
00640 }
00641
00642
00664 void insert(CORBA::ULong position, const SequenceItem& item)
00665 {
00666 ACE_Write_Guard<Mutex> gaurd(lock);
00667 CORBA::ULong len(this->length());
00668
00669 if (position > len) throw;
00670
00671
00672 this->length(len + 1);
00673
00674 for (CORBA::ULong i = (len - 1); i > position; --i)
00675 {
00676 (*this)[i] = (*this)[i-1];
00677 }
00678 (*this)[position] = item;
00679 }
00680
00681
00701 SequenceItem erase(CORBA::ULong position)
00702 {
00703 ACE_Write_Guard<Mutex> gaurd(lock);
00704 CORBA::ULong len(this->length());
00705
00706 if (position > (len - 1)) throw;
00707
00708
00709 SequenceItem erased((*this)[position]);
00710
00711 for (CORBA::ULong i = position; i < (len - 1); ++i)
00712 {
00713 (*this)[i] = (*this)[i+1];
00714 }
00715
00716 this->length(len - 1);
00717 return erased;
00718 }
00719
00740 template <class Predicate>
00741 SequenceItem erase_if(Predicate f)
00742 {
00743 ACE_Write_Guard<Mutex> gaurd(lock);
00744 CORBA::ULong len(this->length());
00745 for (CORBA::ULong i = 0; i < len; ++i)
00746 if (f((*this)[i]))
00747 return erase(i);
00748 throw;
00749 }
00750
00751
00771 void push_back(const SequenceItem& item)
00772 {
00773 ACE_Write_Guard<Mutex> gaurd(lock);
00774 CORBA::ULong len(this->length());
00775 this->length(len + 1);
00776 (*this)[len] = item;
00777 }
00778
00779 void pop_back()
00780 {
00781 ACE_Write_Guard<Mutex> gaurd(lock);
00782 CORBA::ULong len(this->length());
00783 this->len(len - 1);
00784 }
00785
00786 template <class F>
00787 SequenceItem find(F f) const
00788 {
00789 ACE_Read_Guard<Mutex> guard(lock);
00790 CORBA::ULong len(this->length());
00791 for (CORBA::ULong i = 0; i < len; ++i)
00792 if (f((*this)[i]))
00793 return (*this)[i];
00794 throw;
00795 }
00796
00797 mutable Mutex lock;
00798 };
00799
00800
00801 };
00802
00803 #endif // CORBA_Util_h