|
1 /* |
|
2 * Copyright (c) 2005-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Class template for constant size arrays. |
|
15 * |
|
16 */ |
|
17 |
|
18 /* |
|
19 * %version: 8 % |
|
20 */ |
|
21 |
|
22 #ifndef _CARRAY_H |
|
23 #define _CARRAY_H |
|
24 |
|
25 /* |
|
26 * |
|
27 * The following code declares class array, |
|
28 * an STL container (as wrapper) for arrays of constant size. |
|
29 * |
|
30 * compile time modifiable polies: |
|
31 * - to call delete or not to 4 members of array |
|
32 * - copy ctor and assignment operator policies; see umaccarraypolicy.h |
|
33 */ |
|
34 |
|
35 #include "algorithm.h" |
|
36 |
|
37 typedef TUint size_t; |
|
38 |
|
39 #include "umactypemanip.h" |
|
40 #include "umaccarraypolicy.h" |
|
41 |
|
42 template |
|
43 < |
|
44 class T, |
|
45 size_t the_size, |
|
46 bool deletepointees = EFalse, // call delete or not 4 members of array |
|
47 class CopyingPolicy = ShallowCopy<T>, // check out policyCarray.h 4 these |
|
48 class HeapAllocationPolicy = OpNewAlloc // our allocation policy 4 a heap allocated Carray |
|
49 > |
|
50 class Carray : protected CopyingPolicy, |
|
51 public HeapAllocationPolicy |
|
52 { |
|
53 typedef Carray<T, the_size, deletepointees, CopyingPolicy, HeapAllocationPolicy> Self; |
|
54 |
|
55 private: |
|
56 T v[the_size]; |
|
57 |
|
58 // compile time function selectives |
|
59 void delete_pointees( Int2Type<ETrue> ); |
|
60 void delete_pointees( Int2Type<EFalse> ) {}; |
|
61 |
|
62 public: |
|
63 |
|
64 // type definitions |
|
65 typedef T value_type; |
|
66 typedef T* iterator; |
|
67 typedef const T* const_iterator; |
|
68 typedef T& reference; |
|
69 typedef const T& const_reference; |
|
70 typedef size_t size_type; |
|
71 typedef size_t TUint; |
|
72 |
|
73 // default constructor |
|
74 Carray() {}; |
|
75 // construct with inital data |
|
76 // do not allow to be used in explicit conversations |
|
77 explicit |
|
78 Carray(T* p) { copy(p, p + the_size, begin()); } |
|
79 |
|
80 ~Carray() { delete_pointees( Int2Type<deletepointees>() ); } |
|
81 |
|
82 // iterator support |
|
83 iterator begin() {return v;} |
|
84 const_iterator begin() const {return v;} |
|
85 iterator end() {return v + the_size;} |
|
86 const_iterator end() const {return v + the_size;} |
|
87 |
|
88 // size is constant |
|
89 size_type size() const {return the_size;} |
|
90 size_type max_size() const {return the_size;} |
|
91 |
|
92 // direct element access |
|
93 reference operator[](size_t i) {return v[i];} |
|
94 const_reference operator[](size_t i) const {return v[i];} |
|
95 |
|
96 // front() and back() |
|
97 reference front() {return v[0]; } |
|
98 const_reference front() const {return v[0]; } |
|
99 reference back() {return v[the_size-1]; } |
|
100 const_reference back() const {return v[the_size-1];} |
|
101 |
|
102 //swap |
|
103 void swap(Self& r) { swap_ranges(begin(), end(), r.begin()); } |
|
104 |
|
105 // conversion to ordinary array |
|
106 const T* data() const {return v;} |
|
107 T* data() {return v;} |
|
108 |
|
109 // assignment |
|
110 Self& operator=(const Self&); |
|
111 |
|
112 // copy ctor |
|
113 Carray(const Self& o) |
|
114 { Copy(const_cast<iterator>(o.begin()), const_cast<iterator>(o.end()), begin()); } |
|
115 }; |
|
116 |
|
117 template<class T, size_t the_size, bool deletepointees, |
|
118 class CopyingPolicy, class HeapAllocationPolicy> |
|
119 inline |
|
120 void Carray<T, the_size, deletepointees, CopyingPolicy, |
|
121 HeapAllocationPolicy>::delete_pointees(Int2Type<ETrue>) |
|
122 { |
|
123 for( size_t i = 0 ; i < size() ; ++i ) |
|
124 { |
|
125 delete v[i]; |
|
126 } |
|
127 } |
|
128 |
|
129 // asignment operator |
|
130 template<class T, size_t the_size, bool deletepointees, class CopyingPolicy, class HeapAllocationPolicy> |
|
131 inline |
|
132 Carray<T, the_size, deletepointees, CopyingPolicy, HeapAllocationPolicy>& |
|
133 Carray<T, the_size, deletepointees, CopyingPolicy, HeapAllocationPolicy>::operator=(const Self& o) |
|
134 { |
|
135 // use policy chosen at compile time |
|
136 Copy(const_cast<iterator>(o.begin()), const_cast<iterator>(o.end()), begin()); |
|
137 return (*this); |
|
138 } |
|
139 |
|
140 // comparisons |
|
141 template<class T, size_t the_size, bool deletepointees, class CopyingPolicy, class HeapAllocationPolicy> |
|
142 inline |
|
143 bool operator == (const Carray<T, the_size, deletepointees, CopyingPolicy, HeapAllocationPolicy>& x, |
|
144 const Carray<T, the_size, deletepointees, CopyingPolicy, HeapAllocationPolicy>& y) |
|
145 { |
|
146 return equal(x.begin(), x.end(), y.begin()); |
|
147 } |
|
148 |
|
149 // global swap() |
|
150 template<class T, size_t the_size, bool deletepointees, class CopyingPolicy, class HeapAllocationPolicy> |
|
151 inline void swap (Carray<T, the_size, deletepointees, CopyingPolicy, HeapAllocationPolicy>& x, |
|
152 Carray<T, the_size, deletepointees, CopyingPolicy, HeapAllocationPolicy>& y) |
|
153 { |
|
154 x.swap(y); |
|
155 } |
|
156 |
|
157 #endif //_CARRAY_H |