Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
170 andreas 1
/*
2
 * Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#ifndef __TVECTOR_H__
20
#define __TVECTOR_H__
21
 
22
#include <vector>
23
#include <mutex>
24
 
25
 
26
template <class T, class Alloc=std::allocator<T>>
27
class TVector
28
{
29
    public:
30
        TVector() {}
31
        TVector(const TVector& orig) : vec{orig.vec} {}
32
        TVector(std::vector<T>& v) : vec{v} {}
33
        TVector(TVector&& orig) : vec{std::move(orig.vec)} {}
34
 
35
        typedef typename std::vector<T>::size_type size_type;
36
        typedef typename std::vector<T>::value_type value_type;
37
        typedef typename std::vector<T>::iterator iterator;
38
        typedef typename std::vector<T>::const_iterator const_iterator;
39
        typedef typename std::vector<T>::reverse_iterator reverse_iterator;
40
        typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
41
        typedef typename std::vector<T>::reference reference;
42
        typedef typename std::vector<T>::const_reference const_reference;
43
 
44
        /*wrappers for three different at() functions*/
45
        template <class InputIterator>
46
        void assign(InputIterator first, InputIterator last)
47
        {
48
            // using a local lock_guard to lock mutex guarantees that the
49
            // mutex will be unlocked on destruction and in the case of an
50
            // exception being thrown.
51
            std::lock_guard<std::mutex> vectorLockGuard(mut);
52
 
53
            vec.assign(first, last);
54
        }
55
 
56
        void assign(size_type n, const value_type& val)
57
        {
58
            std::lock_guard<std::mutex> vectorLockGuard(mut);
59
 
60
            vec.assign(n, val);
61
        }
62
 
63
        void assign(std::initializer_list<value_type> il)
64
        {
65
            std::lock_guard<std::mutex> vectorLockGuard(mut);
66
 
67
            vec.assign(il.begin(), il.end());
68
        }
69
 
70
        /*wrappers for at() functions*/
71
        reference at(size_type n)
72
        {
73
            return vec.at(n);
74
        }
75
 
76
        const_reference at(size_type n) const
77
        {
78
            return vec.at(n);
79
        }
80
 
81
        /*wrappers for back() functions*/
82
        reference back()
83
        {
84
            std::lock_guard<std::mutex> vectorLockGuard(mut);
85
 
86
            return vec.back();
87
        }
88
 
89
        const reference back() const
90
        {
91
            return vec.back();
92
        }
93
 
94
        /*wrappers for begin() functions*/
95
        iterator begin()
96
        {
97
            std::lock_guard<std::mutex> vectorLockGuard(mut);
98
 
99
            return vec.begin();
100
        }
101
 
102
        const iterator begin() const noexcept
103
        {
104
            return vec.begin();
105
        }
106
 
107
        /*wrapper for capacity() fucntion*/
108
        size_type capacity() const noexcept
109
        {
110
            return vec.capacity();
111
        }
112
 
113
        /*wrapper for cbegin() function*/
114
        const iterator cbegin()
115
        {
116
            return vec.cbegin();
117
        }
118
 
119
        /*wrapper for cend() function*/
120
        const iterator cend()
121
        {
122
            return vec.cend();
123
        }
124
 
125
        /*wrapper for clear() function*/
126
        void clear()
127
        {
128
            std::lock_guard<std::mutex> vectorLockGuard(mut);
129
 
130
            vec.clear();
131
        }
132
 
133
        /*wrapper for crbegin() function*/
134
        const_reverse_iterator crbegin() const noexcept
135
        {
136
            return vec.crbegin();
137
        }
138
 
139
        /*wrapper for crend() function*/
140
        const_reverse_iterator crend() const noexcept
141
        {
142
            return vec.crend();
143
        }
144
 
145
        /*wrappers for data() functions*/
146
        value_type* data()
147
        {
148
            std::lock_guard<std::mutex> vectorLockGuard(mut);
149
 
150
            return vec.data();
151
        }
152
 
153
        const value_type* data() const noexcept
154
        {
172 andreas 155
            std::lock_guard<const std::mutex> vectorLockGuard(mut);
170 andreas 156
 
157
            return vec.data();
158
        }
159
 
160
        /*wrapper for emplace() function*/
161
        template <class... Args>
162
        void emplace(const iterator position, Args&&... args)
163
        {
164
            std::lock_guard<std::mutex> vectorLockGuard(mut);
165
 
166
            vec.emplace(position, args...);
167
        }
168
 
169
        /*wrapper for emplace_back() function*/
170
        template <class... Args>
171
        void emplace_back(Args&&... args)
172
        {
173
            std::lock_guard<std::mutex> vectorLockGuard(mut);
174
 
175
            vec.emplace_back(args...);
176
        }
177
 
178
        /*wrapper for empty() function*/
179
        bool empty() const noexcept
180
        {
181
            return vec.empty();
182
        }
183
 
184
        /*wrappers for end() functions*/
185
        iterator end()
186
        {
187
            std::lock_guard<std::mutex> vectorLockGuard(mut);
188
 
189
            return vec.end();
190
        }
191
 
192
        const iterator end() const noexcept
193
        {
194
            return vec.end();
195
        }
196
 
197
        /*wrapper functions for erase()*/
198
        iterator erase(const_iterator position)
199
        {
200
            std::lock_guard<std::mutex> vectorLockGuard(mut);
201
 
202
            return vec.erase(position);
203
        }
204
 
205
        iterator erase(const_iterator first, const_iterator last)
206
        {
207
            std::lock_guard<std::mutex> vectorLockGuard(mut);
208
 
209
            return vec.erase(first, last);
210
        }
211
 
212
        /*wrapper functions for front()*/
213
        reference front()
214
        {
215
            std::lock_guard<std::mutex> vectorLockGuard(mut);
216
 
217
            return vec.front();
218
        }
219
 
220
        const reference front() const
221
        {
222
            return vec.front();
223
        }
224
 
225
        /*wrapper function for get_allocator()*/
226
        value_type get_allocator() const noexcept
227
        {
228
            return vec.get_allocator();
229
        }
230
 
231
        /*wrapper functions for insert*/
232
        iterator insert(const_iterator position, const value_type& val)
233
        {
234
            std::lock_guard<std::mutex> vectorLockGuard(mut);
235
 
236
            vec.insert(position, val);
237
        }
238
 
239
        iterator insert(const_iterator position, size_type n, const value_type& val)
240
        {
241
            std::lock_guard<std::mutex> vectorLockGuard(mut);
242
 
243
            vec.insert(position, n, val);
244
        }
245
 
246
        template <class InputIterator>
247
        iterator insert(const_iterator position, InputIterator first, InputIterator last)
248
        {
249
            std::lock_guard<std::mutex> vectorLockGuard(mut);
250
 
251
            vec.insert(position, first, last);
252
        }
253
 
254
        iterator insert(const_iterator position, value_type&& val)
255
        {
256
            std::lock_guard<std::mutex> vectorLockGuard(mut);
257
 
258
            vec.insert(position, val);
259
        }
260
 
261
        iterator insert(const_iterator position, std::initializer_list<value_type> il)
262
        {
263
            std::lock_guard<std::mutex> vectorLockGuard(mut);
264
 
265
            vec.insert(position, il.begin(), il.end());
266
        }
267
 
268
        /*wrapper function for max_size*/
269
        size_type max_size() const noexcept
270
        {
271
            return vec.max_size();
272
        }
273
 
274
        /*wrapper functions for operator =*/
275
        std::vector<T>& operator= (const std::vector<T>& x)
276
        {
277
            std::lock_guard<std::mutex> vectorLockGuard(mut);
278
 
279
            vec.swap(x);
280
        }
281
 
282
        std::vector<T>& operator= (std::vector<T>&& x)
283
        {
284
            std::lock_guard<std::mutex> vectorLockGuard(mut);
285
 
286
            vec = std::move(x);
287
            return vec;
288
        }
289
 
290
        std::vector<T>& operator= (std::initializer_list<value_type> il)
291
        {
292
            std::lock_guard<std::mutex> vectorLockGuard(mut);
293
 
294
            vec.assign(il.begin(), il.end());
295
            return vec;
296
        }
297
 
298
        /*wrapper functions for operator []*/
299
        reference operator[] (size_type n)
300
        {
301
            return std::ref(n);
302
        }
303
 
304
        const_reference operator[] (size_type n) const
305
        {
306
            return std::cref(n);
307
        }
308
 
309
        /*wrapper function for pop_back()*/
310
        void pop_back()
311
        {
312
            std::lock_guard<std::mutex> vectorLockGuard(mut);
313
 
314
            vec.pop_back();
315
        }
316
 
317
        /*wrapper functions for push_back*/
318
        void push_back(const value_type& val)
319
        {
320
            std::lock_guard<std::mutex> vectorLockGuard(mut);
321
 
322
            vec.push_back(val);
323
        }
324
 
325
        void push_back(value_type&& val)
326
        {
327
            std::lock_guard<std::mutex> vectorLockGuard(mut);
328
 
329
            vec.push_back(val);
330
        }
331
 
332
        /*wrapper functions for rbegin()*/
333
        reverse_iterator rbegin() noexcept
334
        {
335
            std::lock_guard<std::mutex> vectorLockGuard(mut);
336
 
337
            return vec.rbegin();
338
        }
339
 
340
        const_reverse_iterator rbegin() const noexcept
341
        {
342
            return vec.rbegin();
343
        }
344
 
345
        /*wrapper functions for rend()*/
346
        reverse_iterator rend() noexcept
347
        {
348
            std::lock_guard<std::mutex> vectorLockGuard(mut);
349
 
350
            return vec.rend();
351
        }
352
 
353
        const_reverse_iterator rend() const noexcept
354
        {
355
            return vec.rend();
356
        }
357
 
358
        /*wrapper function for reserve()*/
359
        void reserve(size_type n)
360
        {
361
            std::lock_guard<std::mutex> vectorLockGuard(mut);
362
 
363
            vec.reserve(n);
364
        }
365
 
366
        /*wrapper functions for resize()*/
367
        void resize(size_type n)
368
        {
369
            std::lock_guard<std::mutex> vectorLockGuard(mut);
370
 
371
            vec.resize(n);
372
        }
373
 
374
        void resize(size_type n, const value_type& val)
375
        {
376
            std::lock_guard<std::mutex> vectorLockGuard(mut);
377
 
378
            vec.resize(n, val);
379
        }
380
 
381
        void shrink_to_fit()
382
        {
383
            std::lock_guard<std::mutex> vectorLockGuard(mut);
384
 
385
            vec.shrink_to_fit();
386
        }
387
 
388
        //add function for size
389
        size_type size() const noexcept
390
        {
391
            return vec.size();
392
        }
393
 
394
        /*wrapper function for swap()*/
395
        void swap(std::vector<T>& x)
396
        {
397
            std::lock_guard<std::mutex> vectorLockGuard(mut);
398
 
399
            vec.swap(x);
400
        }
401
 
402
    private:
403
        std::vector<T> vec;
404
        std::mutex mut;
405
};
406
 
407
#endif