LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PtrArray.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2008-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef PTRARRAY_H
23 #define PTRARRAY_H
24 
25 #include "Common.h"
26 
27 #include <glib.h>
28 
29 template<class T>
30 class PtrArray
31 {
32 public:
33 
35  m_array = g_ptr_array_new();
36  }
37 
38  PtrArray(int initialSize) {
39  m_array = g_ptr_array_sized_new(initialSize);
40  }
41 
43  g_ptr_array_free(m_array, TRUE);
44  }
45 
46  PtrArray(const PtrArray<T>& other) {
47  m_array = g_ptr_array_sized_new(other.m_array->len);
48  for (unsigned int i = 0; i < other.m_array->len; i++) {
49  g_ptr_array_add(m_array, g_ptr_array_index(other.m_array, i));
50  }
51  }
52 
54  if (this != &other) {
55  if (m_array)
56  g_ptr_array_free(m_array, TRUE);
57  m_array = g_ptr_array_sized_new(other.m_array->len);
58  for (unsigned int i = 0; i < other.m_array->len; i++) {
59  g_ptr_array_add(m_array, g_ptr_array_index(other.m_array, i));
60  }
61  }
62  return *this;
63  }
64 
65  inline void append(T* t) {
66  g_ptr_array_add(m_array, t);
67  }
68 
69  inline bool remove(T* t) {
70  return g_ptr_array_remove(m_array, t);
71  }
72 
73  void addAfter(T* t, T* newT) {
74 
75  if (empty() || !contains(t)) {
76  append(newT);
77  return;
78  }
79 
80  GPtrArray* oldArray = m_array;
81  m_array = g_ptr_array_sized_new(oldArray->len + 1);
82 
83  for (unsigned int i = 0; i < oldArray->len; i++) {
84  g_ptr_array_add(m_array, g_ptr_array_index(oldArray, i));
85  if (g_ptr_array_index(oldArray, i) == t)
86  g_ptr_array_add(m_array, newT);
87  }
88 
89  g_ptr_array_free(oldArray, TRUE);
90 
91  }
92 
93  void addBefore(T* t, T* newT) {
94 
95  if (empty() || !contains(t)) {
96  append(newT);
97  return;
98  }
99 
100  GPtrArray* oldArray = m_array;
101  m_array = g_ptr_array_sized_new(oldArray->len + 1);
102 
103  for (unsigned int i = 0; i < oldArray->len; i++) {
104  if (g_ptr_array_index(oldArray, i) == t)
105  g_ptr_array_add(m_array, newT);
106  g_ptr_array_add(m_array, g_ptr_array_index(oldArray, i));
107  }
108 
109  g_ptr_array_free(oldArray, TRUE);
110  }
111 
112  inline bool empty() const {
113  return m_array->len == 0;
114  }
115 
116  inline int size() const {
117  return m_array->len;
118  }
119 
120  inline void clear() {
121  g_ptr_array_free(m_array, TRUE);
122  m_array = g_ptr_array_new();
123  }
124 
125  inline T* operator[](int i) const {
126 // if (i < 0 || i >= m_array->len)
127 // return 0;
128  return (T*) g_ptr_array_index(m_array, i);
129  }
130 
131  inline T*& operator[](int i) {
132  return (T*&) g_ptr_array_index(m_array, i);
133  }
134 
135  inline int position(T* t) const {
136  for (unsigned int i = 0; i < m_array->len; i++) {
137  if (g_ptr_array_index(m_array, i) == t)
138  return i;
139  }
140 
141  return -1;
142  }
143 
144  inline bool contains(T* t) const {
145  for (unsigned int i = 0; i < m_array->len; i++) {
146  if (g_ptr_array_index(m_array, i) == t)
147  return true;
148  }
149 
150  return false;
151  }
152 
153 
154  inline T* first() const {
155  if (m_array->len)
156  return (T*) g_ptr_array_index(m_array, 0);
157  return 0;
158  }
159 
160  inline T* last() const {
161  if (m_array->len)
162  return (T*) g_ptr_array_index(m_array, m_array->len - 1);
163  return 0;
164  }
165 
166 private:
167 
168  GPtrArray* m_array;
169 };
170 
171 #endif /* PTRARRAY_H */