3 * MICO --- an Open Source CORBA implementation
4 * Copyright (c) 1997-2001 by The Mico Team
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.
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.
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.
20 * For more information, visit the MICO Home Page at
21 * http://www.mico.org/
24 #ifndef __mico_sequence_h__
25 #define __mico_sequence_h__
27 template<class T> class TSeqVar;
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
35 template<class T, int TID>
38 typedef T &ElementType; // Needed in TSeqVar (see var.h)
39 typedef TSeqVar<SequenceTmpl<T,TID> > _var_type;
44 SequenceTmpl (MICO_ULong maxval)
48 SequenceTmpl (MICO_ULong max, MICO_ULong length, T *value,
49 MICO_Boolean rel = FALSE);
51 SequenceTmpl (const SequenceTmpl<T,TID> &s)
60 void replace (MICO_ULong max, MICO_ULong length, T *value,
61 MICO_Boolean rel = FALSE);
63 SequenceTmpl<T,TID> &operator= (const SequenceTmpl<T,TID> &s)
69 MICO_ULong maximum () const
71 return vec.capacity ();
74 MICO_Boolean release () const
76 // we always own the buffer
80 T* get_buffer (MICO_Boolean orphan = FALSE);
82 const T* get_buffer () const
84 assert (vec.size() > 0);
88 void length (MICO_ULong l);
90 MICO_ULong length () const;
91 T &operator[] (MICO_ULong idx);
92 const T &operator[] (MICO_ULong idx) const;
94 static T *allocbuf (MICO_ULong len)
99 static void freebuf (T *b)
104 #if defined( __SUNPRO_CC )
106 operator== (const SequenceTmpl<T,TID> &v1, const SequenceTmpl<T,TID> &v2)
108 if (v1.length() != v2.length())
110 for (MICO_ULong _i = 0; _i < v1.length(); ++_i) {
111 if (!(v1[_i] == v2[_i]))
119 template<class T, int TID>
120 SequenceTmpl<T,TID>::SequenceTmpl (MICO_ULong maxval, MICO_ULong lengthval, T *value,
123 assert (lengthval <= maxval);
124 vec.reserve (maxval);
125 vec.insert (vec.begin(), value, value+lengthval);
130 template<class T, int TID>
132 SequenceTmpl<T,TID>::replace (MICO_ULong maxval, MICO_ULong lengthval, T *value,
135 assert (lengthval <= maxval);
136 vec.erase (vec.begin(), vec.end());
137 vec.reserve (maxval);
138 vec.insert (vec.begin(), value, value+lengthval);
143 template<class T, int TID>
145 SequenceTmpl<T,TID>::length (MICO_ULong l)
147 if (l < vec.size ()) {
148 vec.erase (vec.begin() + l, vec.end());
149 } else if (l > vec.size()) {
151 // the (long) cast is needed for SGI STL
152 vec.insert (vec.end(), long(l - vec.size()), *t);
157 template<class T, int TID>
159 SequenceTmpl<T,TID>::length () const
164 template<class T, int TID>
166 SequenceTmpl<T,TID>::operator[] (MICO_ULong idx)
172 template<class T, int TID>
174 SequenceTmpl<T,TID>::operator[] (MICO_ULong idx) const
179 template<class T, int TID>
181 SequenceTmpl<T,TID>::get_buffer (MICO_Boolean orphan)
184 T *b = allocbuf (vec.capacity());
185 for (mico_vec_size_type i = 0; i < vec.size(); ++i)
187 vec.erase (vec.begin(), vec.end());
190 assert (vec.size() > 0);
195 #if !defined( __SUNPRO_CC )
196 template<class T, int TID>
198 operator== (const SequenceTmpl<T,TID> &v1, const SequenceTmpl<T,TID> &v2)
200 if (v1.length() != v2.length())
202 for (MICO_ULong _i = 0; _i < v1.length(); ++_i) {
203 if (!(v1[_i] == v2[_i]))
211 * Same as SequenceTmpl except that this template is for bounded sequences.
213 template<class T, int TID, int max>
214 class BoundedSequenceTmpl {
216 typedef T &ElementType; // Needed in TSeqVar (see var.h)
220 BoundedSequenceTmpl ()
224 BoundedSequenceTmpl (MICO_ULong length, T *value,
225 MICO_Boolean rel = TRUE);
227 BoundedSequenceTmpl (const BoundedSequenceTmpl<T, TID, max> &s)
232 ~BoundedSequenceTmpl ()
236 void replace (MICO_ULong length, T *value,
237 MICO_Boolean rel = TRUE);
239 BoundedSequenceTmpl<T, TID, max> &operator=
240 (const BoundedSequenceTmpl<T, TID, max> &s)
246 MICO_ULong maximum () const
251 MICO_Boolean release () const
253 // we always own the buffer
257 T* get_buffer (MICO_Boolean orphan = FALSE);
259 const T* get_buffer () const
261 assert (vec.size() > 0);
265 void length (MICO_ULong l);
267 MICO_ULong length () const
272 T &operator[] (MICO_ULong idx)
277 const T &operator[] (MICO_ULong idx) const
282 static T *allocbuf (MICO_ULong len)
287 static void freebuf (T *b)
292 #if defined( __SUNPRO_CC )
294 operator== (const BoundedSequenceTmpl<T,TID,max> &v1,
295 const BoundedSequenceTmpl<T,TID,max> &v2)
297 if (v1.length() != v2.length())
299 for (MICO_ULong _i = 0; _i < v1.length(); ++_i) {
300 if (!(v1[_i] == v2[_i]))
308 template<class T, int TID, int max>
309 BoundedSequenceTmpl<T,TID,max>::BoundedSequenceTmpl (MICO_ULong lengthval,
313 assert (lengthval <= max);
315 vec.insert (vec.begin(), value, value+lengthval);
320 template<class T, int TID, int max>
322 BoundedSequenceTmpl<T,TID,max>::replace (MICO_ULong lengthval,
323 T *value, MICO_Boolean rel)
325 assert (lengthval <= max);
326 vec.erase (vec.begin(), vec.end());
328 vec.insert (vec.begin(), value, value+lengthval);
333 template<class T, int TID, int max>
335 BoundedSequenceTmpl<T,TID,max>::length (MICO_ULong l)
338 if (l < vec.size ()) {
339 vec.erase (vec.begin() + l, vec.end());
340 } else if (l > vec.size()) {
342 vec.insert (vec.end(), long(l - vec.size()), t);
347 template<class T, int TID, int max>
349 BoundedSequenceTmpl<T,TID,max>::get_buffer (MICO_Boolean orphan)
352 T *b = allocbuf (vec.capacity());
353 for (mico_vec_size_type i = 0; i < vec.size(); ++i)
355 vec.erase (vec.begin(), vec.end());
358 assert (vec.size() > 0);
363 #if !defined( __SUNPRO_CC )
364 template<class T, int TID, int max>
366 operator== (const BoundedSequenceTmpl<T,TID,max> &v1,
367 const BoundedSequenceTmpl<T,TID,max> &v2)
369 if (v1.length() != v2.length())
371 for (MICO_ULong _i = 0; _i < v1.length(); ++_i) {
372 if (!(v1[_i] == v2[_i]))
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.
388 template<class T_elem, class T, MICO_ULong n>
389 class SequenceIndTmpl {
391 typedef T &ElementType; // Needed in TSeqVar (see var.h)
393 std::vector<T_elem*> vec;
395 SequenceIndTmpl () {}
396 SequenceIndTmpl (MICO_ULong maxval)
398 vec.reserve (maxval);
400 SequenceIndTmpl (MICO_ULong max, MICO_ULong length, T* value,
401 MICO_Boolean rel = TRUE);
403 void replace (MICO_ULong max, MICO_ULong length, T *value,
404 MICO_Boolean rel = TRUE);
406 SequenceIndTmpl (const SequenceIndTmpl<T_elem,T,n> &s);
410 SequenceIndTmpl<T_elem,T,n>&
411 operator= (const SequenceIndTmpl<T_elem,T,n> &s);
413 MICO_ULong maximum () const
415 return vec.capacity ();
418 void length (MICO_ULong l);
420 MICO_ULong length () const
425 MICO_Boolean release () const
427 // we always own the buffer
431 // get_buffer() not supported ...
433 T& operator[] (MICO_ULong idx)
435 return (T&) *vec[idx];
438 const T& operator[] (MICO_ULong idx) const
440 return (T&) *vec[idx];
443 static T *allocbuf (MICO_ULong len);
445 static void freebuf( T* b );
447 #if defined( __SUNPRO_CC )
449 operator== (const SequenceIndTmpl<T_elem,T,n> &v1,
450 const SequenceIndTmpl<T_elem,T,n> &v2)
452 if( v1.length() != v2.length() )
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 ];
467 #if !defined( __SUNPRO_CC )
468 template<class T_elem, class T, MICO_ULong n>
470 operator== (const SequenceIndTmpl<T_elem,T,n> &v1,
471 const SequenceIndTmpl<T_elem,T,n> &v2)
473 if( v1.length() != v2.length() )
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 ];
488 * Same as SequenceIndTmpl except that this one is for bounded sequences.
490 template<class T_elem, class T, MICO_ULong n, int max>
491 class BoundedSequenceIndTmpl {
493 typedef T &ElementType; // Needed in TSeqVar (see var.h)
495 std::vector<T_elem*> vec;
497 BoundedSequenceIndTmpl ()
501 BoundedSequenceIndTmpl (MICO_ULong length, T *value,
502 MICO_Boolean rel = TRUE);
504 BoundedSequenceIndTmpl
505 (const BoundedSequenceIndTmpl<T_elem,T,n,max> &s);
507 ~BoundedSequenceIndTmpl ();
509 BoundedSequenceIndTmpl<T_elem,T,n, max> &operator=
510 (const BoundedSequenceIndTmpl<T_elem,T,n, max> &s);
512 void replace (MICO_ULong length, T *value,
513 MICO_Boolean rel = TRUE);
515 MICO_ULong maximum () const
520 void length (MICO_ULong l);
522 MICO_ULong length () const
527 MICO_Boolean release () const
529 // we always own the buffer
533 // get_buffer() not supported ...
535 T &operator[] (MICO_ULong idx)
537 return (T&) *vec[idx];
540 const T &operator[] (MICO_ULong idx) const
542 return (T&) *vec[idx];
545 static T *allocbuf (MICO_ULong len);
547 static void freebuf (T *b);
549 #if defined( __SUNPRO_CC )
551 operator== (const BoundedSequenceIndTmpl<T_elem,T,n,max> &v1,
552 const BoundedSequenceIndTmpl<T_elem,T,n,max> &v2)
554 if( v1.length() != v2.length() )
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 ];
569 #if !defined( __SUNPRO_CC )
570 template<class T_elem, class T, MICO_ULong n, int max>
572 operator== (const BoundedSequenceIndTmpl<T_elem,T,n,max> &v1,
573 const BoundedSequenceIndTmpl<T_elem,T,n,max> &v2)
575 if( v1.length() != v2.length() )
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 ];
589 #endif // __mico_sequence_h__