OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / include / mico / sequence.h
1 // -*- c++ -*-
2 /*
3  *  MICO --- an Open Source CORBA implementation
4  *  Copyright (c) 1997-2001 by The Mico Team
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  *  For more information, visit the MICO Home Page at
21  *  http://www.mico.org/
22  */
23
24 #ifndef __mico_sequence_h__
25 #define __mico_sequence_h__
26
27 template<class T> class TSeqVar;
28
29
30 /*
31  * C++ template for unbounded sequences. The element type of the sequence
32  * can be of any type except for arrays and recursive types (see
33  * SequenceIndTmpl)
34  */
35 template<class T, int TID>
36 class SequenceTmpl {
37 public:
38     typedef T &ElementType; // Needed in TSeqVar (see var.h)
39     typedef TSeqVar<SequenceTmpl<T,TID> > _var_type;
40 private:
41     std::vector<T> vec;
42 public:
43     SequenceTmpl () {}
44     SequenceTmpl (MICO_ULong maxval)
45     {
46         vec.reserve (maxval);
47     }
48     SequenceTmpl (MICO_ULong max, MICO_ULong length, T *value,
49                   MICO_Boolean rel = FALSE);
50
51     SequenceTmpl (const SequenceTmpl<T,TID> &s)
52     {
53         vec = s.vec;
54     }
55     
56     ~SequenceTmpl ()
57     {
58     }
59     
60     void replace (MICO_ULong max, MICO_ULong length, T *value,
61                   MICO_Boolean rel = FALSE);
62
63     SequenceTmpl<T,TID> &operator= (const SequenceTmpl<T,TID> &s)
64     {
65         vec = s.vec;
66         return *this;
67     }
68
69     MICO_ULong maximum () const
70     {
71         return vec.capacity ();
72     }
73
74     MICO_Boolean release () const
75     {
76         // we always own the buffer
77         return TRUE;
78     }
79
80     T* get_buffer (MICO_Boolean orphan = FALSE);
81
82     const T* get_buffer () const
83     {
84         assert (vec.size() > 0);
85         return &vec[0];
86     }
87
88     void length (MICO_ULong l);
89
90     MICO_ULong length () const;
91     T &operator[] (MICO_ULong idx);
92     const T &operator[] (MICO_ULong idx) const;
93
94     static T *allocbuf (MICO_ULong len)
95     {
96         return new T[len];
97     }
98
99     static void freebuf (T *b)
100     {
101         delete[] b;
102     }
103
104 #if defined( __SUNPRO_CC )
105     friend MICO_Boolean
106     operator== (const SequenceTmpl<T,TID> &v1, const SequenceTmpl<T,TID> &v2)
107     {
108         if (v1.length() != v2.length())
109             return FALSE;
110         for (MICO_ULong _i = 0; _i < v1.length(); ++_i) {
111             if (!(v1[_i] == v2[_i]))
112                 return FALSE;
113         }
114         return TRUE;
115     }
116 #endif
117 };
118
119 template<class T, int TID>
120 SequenceTmpl<T,TID>::SequenceTmpl (MICO_ULong maxval, MICO_ULong lengthval, T *value,
121                                    MICO_Boolean rel)
122 {
123     assert (lengthval <= maxval);
124     vec.reserve (maxval);
125     vec.insert (vec.begin(), value, value+lengthval);
126     if (rel)
127         freebuf (value);
128 }
129
130 template<class T, int TID>
131 void
132 SequenceTmpl<T,TID>::replace (MICO_ULong maxval, MICO_ULong lengthval, T *value,
133                               MICO_Boolean rel)
134 {
135     assert (lengthval <= maxval);
136     vec.erase (vec.begin(), vec.end());
137     vec.reserve (maxval);
138     vec.insert (vec.begin(), value, value+lengthval);
139     if (rel)
140         freebuf (value);
141 }
142
143 template<class T, int TID>
144 void
145 SequenceTmpl<T,TID>::length (MICO_ULong l)
146 {
147     if (l < vec.size ()) {
148         vec.erase (vec.begin() + l, vec.end());
149     } else if (l > vec.size()) {
150         T* t = new T;
151         // the (long) cast is needed for SGI STL
152         vec.insert (vec.end(), long(l - vec.size()), *t);
153         delete t;
154     }
155 }
156
157 template<class T, int TID>
158 inline MICO_ULong
159 SequenceTmpl<T,TID>::length () const
160 {
161     return vec.size ();
162 }
163
164 template<class T, int TID>
165 inline T &
166 SequenceTmpl<T,TID>::operator[] (MICO_ULong idx)
167 {
168     return vec[idx];
169 }
170     
171
172 template<class T, int TID>
173 inline const T &
174 SequenceTmpl<T,TID>::operator[] (MICO_ULong idx) const
175 {
176     return vec[idx];
177 }
178
179 template<class T, int TID>
180 T *
181 SequenceTmpl<T,TID>::get_buffer (MICO_Boolean orphan)
182 {
183     if (orphan) {
184         T *b = allocbuf (vec.capacity());
185         for (mico_vec_size_type i = 0; i < vec.size(); ++i)
186             b[i] = vec[i];
187         vec.erase (vec.begin(), vec.end());
188         return b;
189     } else {
190         assert (vec.size() > 0);
191         return &vec[0];
192     }
193 }
194
195 #if !defined( __SUNPRO_CC )
196 template<class T, int TID>
197 MICO_Boolean
198 operator== (const SequenceTmpl<T,TID> &v1, const SequenceTmpl<T,TID> &v2)
199 {
200     if (v1.length() != v2.length())
201         return FALSE;
202     for (MICO_ULong _i = 0; _i < v1.length(); ++_i) {
203         if (!(v1[_i] == v2[_i]))
204             return FALSE;
205     }
206     return TRUE;
207 }
208 #endif
209
210 /*
211  * Same as SequenceTmpl except that this template is for bounded sequences.
212  */
213 template<class T, int TID, int max>
214 class BoundedSequenceTmpl {
215 public:
216     typedef T &ElementType; // Needed in TSeqVar (see var.h)
217 private:
218     std::vector<T> vec;
219 public:
220     BoundedSequenceTmpl ()
221     {
222         vec.reserve (max);
223     }
224     BoundedSequenceTmpl (MICO_ULong length, T *value,
225                          MICO_Boolean rel = TRUE);
226     
227     BoundedSequenceTmpl (const BoundedSequenceTmpl<T, TID, max> &s)
228     {
229         vec = s.vec;
230     }
231
232     ~BoundedSequenceTmpl ()
233     {
234     }
235
236     void replace (MICO_ULong length, T *value,
237                   MICO_Boolean rel = TRUE);
238
239     BoundedSequenceTmpl<T, TID, max> &operator=
240         (const BoundedSequenceTmpl<T, TID, max> &s)
241     {
242         vec = s.vec;
243         return *this;
244     }
245
246     MICO_ULong maximum () const
247     {
248         return max;
249     }
250
251     MICO_Boolean release () const
252     {
253         // we always own the buffer
254         return TRUE;
255     }
256
257     T* get_buffer (MICO_Boolean orphan = FALSE);
258
259     const T* get_buffer () const
260     {
261         assert (vec.size() > 0);
262         return &vec[0];
263     }
264
265     void length (MICO_ULong l);
266
267     MICO_ULong length () const
268     {
269         return vec.size ();
270     }
271
272     T &operator[] (MICO_ULong idx)
273     {
274         return vec[idx];
275     }
276
277     const T &operator[] (MICO_ULong idx) const
278     {
279         return vec[idx];
280     }
281
282     static T *allocbuf (MICO_ULong len)
283     {
284         return new T[len];
285     }
286
287     static void freebuf (T *b)
288     {
289         delete[] b;
290     }
291
292 #if defined( __SUNPRO_CC )
293     friend MICO_Boolean
294     operator== (const BoundedSequenceTmpl<T,TID,max> &v1,
295                 const BoundedSequenceTmpl<T,TID,max> &v2)
296     {
297         if (v1.length() != v2.length())
298             return FALSE;
299         for (MICO_ULong _i = 0; _i < v1.length(); ++_i) {
300             if (!(v1[_i] == v2[_i]))
301                 return FALSE;
302         }
303         return TRUE;
304     }
305 #endif
306 };
307
308 template<class T, int TID, int max>
309 BoundedSequenceTmpl<T,TID,max>::BoundedSequenceTmpl (MICO_ULong lengthval,
310                                                      T *value,
311                                                      MICO_Boolean rel)
312 {
313     assert (lengthval <= max);
314     vec.reserve (max);
315     vec.insert (vec.begin(), value, value+lengthval);
316     if (rel)
317         freebuf (value);
318 }
319
320 template<class T, int TID, int max>
321 void
322 BoundedSequenceTmpl<T,TID,max>::replace (MICO_ULong lengthval,
323                                          T *value, MICO_Boolean rel)
324 {
325     assert (lengthval <= max);
326     vec.erase (vec.begin(), vec.end());
327     vec.reserve (max);
328     vec.insert (vec.begin(), value, value+lengthval);
329     if (rel)
330         freebuf (value);
331 }
332
333 template<class T, int TID, int max>
334 void
335 BoundedSequenceTmpl<T,TID,max>::length (MICO_ULong l)
336 {
337     assert (l <= max);
338     if (l < vec.size ()) {
339         vec.erase (vec.begin() + l, vec.end());
340     } else if (l > vec.size()) {
341         T t;
342         vec.insert (vec.end(), long(l - vec.size()), t);
343     }
344 }
345
346
347 template<class T, int TID, int max>
348 T *
349 BoundedSequenceTmpl<T,TID,max>::get_buffer (MICO_Boolean orphan)
350 {
351     if (orphan) {
352         T *b = allocbuf (vec.capacity());
353         for (mico_vec_size_type i = 0; i < vec.size(); ++i)
354             b[i] = vec[i];
355         vec.erase (vec.begin(), vec.end());
356         return b;
357     } else {
358         assert (vec.size() > 0);
359         return &vec[0];
360     }
361 }
362
363 #if !defined( __SUNPRO_CC )
364 template<class T, int TID, int max>
365 MICO_Boolean
366 operator== (const BoundedSequenceTmpl<T,TID,max> &v1,
367             const BoundedSequenceTmpl<T,TID,max> &v2)
368 {
369     if (v1.length() != v2.length())
370         return FALSE;
371     for (MICO_ULong _i = 0; _i < v1.length(); ++_i) {
372         if (!(v1[_i] == v2[_i]))
373             return FALSE;
374     }
375     return TRUE;
376 }
377 #endif
378
379 /*
380  * C++ template for sequences of arrays and recursive types. The difference
381  * is, that this template maintains it members only indirectly through
382  * pointers. This is especially necessary to break infinite recursion
383  * for recursive types. If this template is used for arrays, then T_elem
384  * denotes the element type of the array, T the array type itself and
385  * n the total number of array elements. If the template is used for
386  * recursive types then T_elem and T are the same and n should be set to 1.
387  */
388 template<class T_elem, class T, MICO_ULong n>
389 class SequenceIndTmpl {
390 public:
391   typedef T &ElementType; // Needed in TSeqVar (see var.h)
392 private:
393   std::vector<T_elem*> vec;
394 public:
395   SequenceIndTmpl () {}
396   SequenceIndTmpl (MICO_ULong maxval)
397   {
398     vec.reserve (maxval);
399   }
400   SequenceIndTmpl (MICO_ULong max, MICO_ULong length, T* value,
401                    MICO_Boolean rel = TRUE);
402
403   void replace (MICO_ULong max, MICO_ULong length, T *value,
404                 MICO_Boolean rel = TRUE);
405
406   SequenceIndTmpl (const SequenceIndTmpl<T_elem,T,n> &s);
407     
408   ~SequenceIndTmpl ();
409   
410   SequenceIndTmpl<T_elem,T,n>& 
411   operator= (const SequenceIndTmpl<T_elem,T,n> &s);
412   
413   MICO_ULong maximum () const
414   {
415     return vec.capacity ();
416   }
417   
418   void length (MICO_ULong l);
419   
420   MICO_ULong length () const
421   {
422     return vec.size ();
423   }
424
425   MICO_Boolean release () const
426   {
427     // we always own the buffer
428     return TRUE;
429   }
430
431   // get_buffer() not supported ...
432   
433   T& operator[] (MICO_ULong idx)
434   {
435     return (T&) *vec[idx];
436   }
437   
438   const T& operator[] (MICO_ULong idx) const
439   {
440     return (T&) *vec[idx];
441   }
442   
443   static T *allocbuf (MICO_ULong len);
444   
445   static void freebuf( T* b );
446   
447 #if defined( __SUNPRO_CC )
448   friend MICO_Boolean
449   operator== (const SequenceIndTmpl<T_elem,T,n> &v1,
450               const SequenceIndTmpl<T_elem,T,n> &v2)
451   {
452     if( v1.length() != v2.length() )
453       return FALSE;
454     for( MICO_ULong i = 0; i < v1.length(); i++ ) {
455       for( MICO_ULong j = 0; j < n; j++ ) {
456         T_elem e1 = ((T_elem*) v1[ i ])[ j ];
457         T_elem e2 = ((T_elem*) v2[ i ])[ j ];
458         if( !(e1 == e2) )
459           return FALSE;
460       }
461     }
462     return TRUE;
463   }
464 #endif
465 };
466
467 #if !defined( __SUNPRO_CC )
468 template<class T_elem, class T, MICO_ULong n>
469 MICO_Boolean
470 operator== (const SequenceIndTmpl<T_elem,T,n> &v1,
471             const SequenceIndTmpl<T_elem,T,n> &v2)
472 {
473   if( v1.length() != v2.length() )
474     return FALSE;
475   for( MICO_ULong i = 0; i < v1.length(); i++ ) {
476     for( MICO_ULong j = 0; j < n; j++ ) {
477       T_elem e1 = ((T_elem*) v1[ i ])[ j ];
478       T_elem e2 = ((T_elem*) v2[ i ])[ j ];
479       if( !(e1 == e2) )
480         return FALSE;
481     }
482   }
483   return TRUE;
484 }
485 #endif
486
487 /*
488  * Same as SequenceIndTmpl except that this one is for bounded sequences.
489  */
490 template<class T_elem, class T, MICO_ULong n, int max>
491 class BoundedSequenceIndTmpl {
492 public:
493   typedef T &ElementType; // Needed in TSeqVar (see var.h)
494 private:
495   std::vector<T_elem*> vec;
496 public:
497   BoundedSequenceIndTmpl ()
498   {
499     vec.reserve (max);
500   }
501   BoundedSequenceIndTmpl (MICO_ULong length, T *value,
502                           MICO_Boolean rel = TRUE);
503   
504   BoundedSequenceIndTmpl
505   (const BoundedSequenceIndTmpl<T_elem,T,n,max> &s);
506   
507   ~BoundedSequenceIndTmpl ();
508   
509   BoundedSequenceIndTmpl<T_elem,T,n, max> &operator=
510   (const BoundedSequenceIndTmpl<T_elem,T,n, max> &s);
511   
512   void replace (MICO_ULong length, T *value,
513                 MICO_Boolean rel = TRUE);
514
515   MICO_ULong maximum () const
516   {
517     return max;
518   }
519   
520   void length (MICO_ULong l);
521   
522   MICO_ULong length () const
523   {
524     return vec.size ();
525   }
526   
527   MICO_Boolean release () const
528   {
529     // we always own the buffer
530     return TRUE;
531   }
532
533   // get_buffer() not supported ...
534
535   T &operator[] (MICO_ULong idx)
536   {
537     return (T&) *vec[idx];
538   }
539   
540   const T &operator[] (MICO_ULong idx) const
541   {
542     return (T&) *vec[idx];
543   }
544   
545   static T *allocbuf (MICO_ULong len);
546   
547   static void freebuf (T *b);
548
549 #if defined( __SUNPRO_CC )
550   friend MICO_Boolean
551   operator== (const BoundedSequenceIndTmpl<T_elem,T,n,max> &v1,
552               const BoundedSequenceIndTmpl<T_elem,T,n,max> &v2)
553   {
554     if( v1.length() != v2.length() )
555       return FALSE;
556     for( MICO_ULong i = 0; i < v1.length(); i++ ) {
557       for( MICO_ULong j = 0; j < n; j++ ) {
558         T_elem e1 = ((T_elem*) v1[ i ])[ j ];
559         T_elem e2 = ((T_elem*) v2[ i ])[ j ];
560         if( !(e1 == e2) )
561           return FALSE;
562       }
563     }
564     return TRUE;
565   }
566 #endif
567 };
568
569 #if !defined( __SUNPRO_CC )
570 template<class T_elem, class T, MICO_ULong n, int max>
571 MICO_Boolean
572 operator== (const BoundedSequenceIndTmpl<T_elem,T,n,max> &v1,
573             const BoundedSequenceIndTmpl<T_elem,T,n,max> &v2)
574 {
575   if( v1.length() != v2.length() )
576     return FALSE;
577   for( MICO_ULong i = 0; i < v1.length(); i++ ) {
578     for( MICO_ULong j = 0; j < n; j++ ) {
579       T_elem e1 = ((T_elem*) v1[ i ])[ j ];
580       T_elem e2 = ((T_elem*) v2[ i ])[ j ];
581       if( !(e1 == e2) )
582         return FALSE;
583     }
584   }
585   return TRUE;
586 }
587 #endif
588
589 #endif // __mico_sequence_h__