Link with C++ linker
[oweals/cde.git] / cde / programs / nsgmls / NCVector.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: NCVector.C /main/1 1996/07/29 16:58:08 cde-hp $ */
24 // Copyright (c) 1994, 1996 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef NCVector_DEF_INCLUDED
28 #define NCVector_DEF_INCLUDED 1
29
30 #include <stddef.h>
31 #include <string.h>
32
33 #ifdef SP_QUAL_TEMPLATE_DTOR_BROKEN
34 #define DTOR(T) ~T
35 #else
36 #define DTOR(T) T::~T
37 #endif
38
39 #ifdef SP_NAMESPACE
40 namespace SP_NAMESPACE {
41 #endif
42
43 template<class T>
44 NCVector<T>::~NCVector()
45 {
46   if (ptr_) {
47     erase(ptr_, ptr_ + size_);
48     ::operator delete((void *)ptr_);
49   }
50 }
51
52 #if 0
53
54 template<class T>
55 NCVector<T>::NCVector(const NCVector<T> &v)
56 : ptr_(0), size_(0), alloc_(0)
57 {
58   insert(ptr_ + size_, v.ptr_, v.ptr_ + v.size_);
59 }
60
61 template<class T>
62 NCVector<T>::NCVector(size_t n, const T &t)
63 : ptr_(0), size_(0), alloc_(0)
64 {
65   insert(ptr_ + size_, n, t);
66 }
67
68 template<class T>
69 NCVector<T> &NCVector<T>::operator=(const NCVector<T> &v)
70 {
71   if (&v != this) {
72     size_t n = v.size_;
73     if (n > size_) {
74       n = size_;
75       insert(ptr_ + size_, v.ptr_ + size_, v.ptr_ + v.size_);
76     }
77     else if (n < size_)
78       erase(ptr_ + n, ptr_ + size_);
79     while (n-- > 0)
80       ptr_[n] = v.ptr_[n];
81   }
82   return *this;
83 }
84
85 template<class T>
86 void NCVector<T>::assign(size_t n, const T &t)
87 {
88   size_t sz = n;
89   if (n > size_) {
90     sz = size_;
91     insert(ptr_ + size_, n - size_, t);
92   }
93   else if (n < size_)
94     erase(ptr_ + n, ptr_ + size_);
95   while (sz-- > 0)
96     ptr_[sz] = t;
97 }
98
99 template<class T>
100 void NCVector<T>::insert(const T *p, size_t n, const T &t)
101 {
102   size_t i = p - ptr_;
103   reserve(size_ + n);
104   if (i != size_)
105     memmove(ptr_ + i + n, ptr_ + i, (size_ - i)*sizeof(T));
106   size_ += n;
107   for (T *pp = ptr_ + i; n-- > 0; pp++)
108     (void)new (pp) T(t);
109 }
110
111 template<class T>
112 void NCVector<T>::insert(const T *p, const T *q1, const T *q2)
113 {
114   size_t i = p - ptr_;
115   size_t n = q2 - q1;
116   reserve(size_ + n);
117   if (i != size_)
118     memmove(ptr_ + i + n, ptr_ + i, (size_ - i)*sizeof(T));
119   size_ += n;
120   for (T *pp = ptr_ + i; q1 != q2; q1++, pp++)
121     (void)new (pp) T(*q1);
122 }
123
124 #endif
125
126 template<class T>
127 void NCVector<T>::swap(NCVector<T> &v)
128 {
129   {
130     T *tem = ptr_;
131     ptr_ = v.ptr_;
132     v.ptr_ = tem;
133   }
134   {
135     size_t tem = size_;
136     size_ = v.size_;
137     v.size_ = tem;
138   }
139   {
140     size_t tem = alloc_;
141     alloc_ = v.alloc_;
142     v.alloc_ = tem;
143   }
144 }
145
146 template<class T>
147 void NCVector<T>::append(size_t n)
148 {
149   reserve(size_ + n);
150   while (n-- > 0)
151     (void)new (ptr_ + size_++) T;
152 }
153
154 template<class T>
155 T *NCVector<T>::erase(const T *p1, const T *p2)
156 {
157   // typedef T X;
158   for (const T *p = p1; p != p2; p++)
159     /* ((X *)p)->~X(); */
160     p->~T();
161   if (p2 != ptr_ + size_)
162     memmove((T *)p1, p2, ((const T *)(ptr_ + size_) - p2)*sizeof(T));
163   size_ -= p2 - p1;
164   return (T *)p1;
165 }
166
167 template<class T>
168 void NCVector<T>::reserve1(size_t size)
169 {
170   alloc_ *= 2;
171   if (size > alloc_)
172     alloc_ += size;
173   void *p = ::operator new(alloc_*sizeof(T));
174   if (ptr_) {
175     memcpy(p, ptr_, size_*sizeof(T));
176     ::operator delete((void *)ptr_);
177   }
178   ptr_ = (T *)p;
179 }
180
181 #ifdef SP_NAMESPACE
182 }
183 #endif
184
185 #endif /* not NCVector_DEF_INCLUDED */