1
/* Copyright (c) 2005 PrimeBase Technologies GmbH
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* 2006-05-16 Paul McCullagh
26
#ifndef __ccutils_xt_h__
27
#define __ccutils_xt_h__
32
#include "thread_xt.h"
40
inline XTObject() { o_refcnt = 1; }
42
virtual ~XTObject() { }
44
inline void reference() {
48
inline void release(XTThreadPtr self) {
57
virtual XTObject *factory(XTThreadPtr self) {
60
if (!(new_obj = new XTObject()))
61
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
65
virtual XTObject *clone(XTThreadPtr self) {
68
new_obj = factory(self);
69
new_obj->init(self, this);
73
virtual void init(XTThreadPtr self) { (void) self; }
74
virtual void init(XTThreadPtr self, XTObject *obj) { (void) obj; init(self); }
75
virtual void finalize(XTThreadPtr self) { (void) self; }
76
virtual int compare(const void *key) { (void) key; return -1; }
87
inline XTListImp() : li_referenced(true), li_item_count(0), li_items(NULL) { }
89
inline void setNonReferenced() { li_referenced = false; }
91
void append(XTThreadPtr self, XTObject *info) {
92
if (!xt_realloc(NULL, (void **) &li_items, (li_item_count + 1) * sizeof(void *))) {
95
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
98
li_items[li_item_count] = info;
102
void insert(XTThreadPtr self, XTObject *info, u_int i) {
103
if (!xt_realloc(NULL, (void **) &li_items, (li_item_count + 1) * sizeof(void *))) {
106
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
109
memmove(&li_items[i+1], &li_items[i], (li_item_count-i) * sizeof(XTObject *));
114
void addToFront(XTThreadPtr self, XTObject *info) {
115
insert(self, info, 0);
119
void append(XTThreadPtr self, XTObject *info, void *key);
121
inline bool remove(XTObject *info) {
122
for (u_int i=0; i<li_item_count; i++) {
123
if (li_items[i] == info) {
125
memmove(&li_items[i], &li_items[i+1], (li_item_count - i) * sizeof(XTObject *));
132
inline bool remove(XTThreadPtr self, u_int i) {
135
if (i >= li_item_count)
139
memmove(&li_items[i], &li_items[i+1], (li_item_count - i) * sizeof(void *));
145
inline XTObject *take(u_int i) {
148
if (i >= li_item_count)
152
memmove(&li_items[i], &li_items[i+1], (li_item_count - i) * sizeof(void *));
156
inline u_int size() const { return li_item_count; }
158
inline void setEmpty(XTThreadPtr self) {
160
xt_free(self, li_items);
165
inline bool isEmpty() { return li_item_count == 0; }
167
inline XTObject *itemAt(u_int i) const {
168
if (i >= li_item_count)
175
template <class T> class XTList : public XTListImp
178
inline XTList() : XTListImp() { }
180
inline void append(XTThreadPtr self, T *a) { XTListImp::append(self, a); }
181
inline void insert(XTThreadPtr self, T *a, u_int i) { XTListImp::insert(self, a, i); }
182
inline void addToFront(XTThreadPtr self, T *a) { XTListImp::addToFront(self, a); }
184
inline bool remove(T *a) { return XTListImp::remove(a); }
186
inline bool remove(XTThreadPtr self, u_int i) { return XTListImp::remove(self, i); }
188
inline T *take(u_int i) { return (T *) XTListImp::take(i); }
190
inline T *itemAt(u_int i) const { return (T *) XTListImp::itemAt(i); }
192
inline u_int indexOf(T *a) {
195
for (i=0; i<size(); i++) {
202
void deleteAll(XTThreadPtr self)
204
for (u_int i=0; i<size(); i++) {
206
itemAt(i)->release(self);
211
void clone(XTThreadPtr self, XTListImp *list)
214
for (u_int i=0; i<list->size(); i++) {
215
XTListImp::append(self, list->itemAt(i)->clone(self));