~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to plugin/pbxt/src/ccutils_xt.h

  • Committer: Padraig O'Sullivan
  • Date: 2010-04-17 01:38:47 UTC
  • mfrom: (1237.9.238 bad-staging)
  • Revision ID: osullivan.padraig@gmail.com-20100417013847-ibjioqsfbmf5yg4g
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2005 PrimeBase Technologies GmbH
 
2
 *
 
3
 * PrimeBase XT
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 * 2006-05-16   Paul McCullagh
 
20
 *
 
21
 * H&G2JCtL
 
22
 *
 
23
 * C++ Utilities
 
24
 */
 
25
 
 
26
#ifndef __ccutils_xt_h__
 
27
#define __ccutils_xt_h__
 
28
 
 
29
#include <errno.h>
 
30
 
 
31
#include "xt_defs.h"
 
32
#include "thread_xt.h"
 
33
 
 
34
class XTObject
 
35
{
 
36
        private:
 
37
        u_int                   o_refcnt;
 
38
 
 
39
        public:
 
40
        inline XTObject() { o_refcnt = 1; }
 
41
        
 
42
        virtual ~XTObject() { }
 
43
 
 
44
        inline void reference() {
 
45
                o_refcnt++;
 
46
        }
 
47
 
 
48
        inline void release(XTThreadPtr self) {
 
49
                ASSERT(o_refcnt > 0);
 
50
                o_refcnt--;
 
51
                if (o_refcnt == 0) {
 
52
                        finalize(self);
 
53
                        delete this;
 
54
                }
 
55
        }
 
56
 
 
57
        virtual XTObject *factory(XTThreadPtr self) {
 
58
                XTObject *new_obj;
 
59
                
 
60
                if (!(new_obj = new XTObject()))
 
61
                        xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
62
                return new_obj;
 
63
        }
 
64
 
 
65
        virtual XTObject *clone(XTThreadPtr self) {
 
66
                XTObject *new_obj;
 
67
                
 
68
                new_obj = factory(self);
 
69
                new_obj->init(self, this);
 
70
                return new_obj;
 
71
        }
 
72
 
 
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; }
 
77
};
 
78
 
 
79
class XTListImp
 
80
{
 
81
        protected:
 
82
        bool            li_referenced;
 
83
        u_int           li_item_count;
 
84
        XTObject        **li_items;
 
85
 
 
86
        public:
 
87
        inline XTListImp() : li_referenced(true), li_item_count(0), li_items(NULL) { }
 
88
 
 
89
        inline void setNonReferenced() { li_referenced = false; }
 
90
 
 
91
        void append(XTThreadPtr self, XTObject *info) {
 
92
                if (!xt_realloc(NULL, (void **) &li_items, (li_item_count + 1) * sizeof(void *))) {
 
93
                        if (li_referenced)
 
94
                                info->release(self);
 
95
                        xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
96
                        return;
 
97
                }
 
98
                li_items[li_item_count] = info;
 
99
                li_item_count++;
 
100
        }
 
101
 
 
102
        void insert(XTThreadPtr self, XTObject *info, u_int i) {
 
103
                if (!xt_realloc(NULL, (void **) &li_items, (li_item_count + 1) * sizeof(void *))) {
 
104
                        if (li_referenced)
 
105
                                info->release(self);
 
106
                        xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
107
                        return;
 
108
                }
 
109
                memmove(&li_items[i+1], &li_items[i], (li_item_count-i) * sizeof(XTObject *));
 
110
                li_items[i] = info;
 
111
                li_item_count++;
 
112
        }
 
113
 
 
114
        void addToFront(XTThreadPtr self, XTObject *info) {
 
115
                insert(self, info, 0);
 
116
        }
 
117
 
 
118
        /* Will sort! */
 
119
        void append(XTThreadPtr self, XTObject *info, void *key);
 
120
 
 
121
        inline bool remove(XTObject *info) {
 
122
                for (u_int i=0; i<li_item_count; i++) {
 
123
                        if (li_items[i] == info) {
 
124
                                li_item_count--;
 
125
                                memmove(&li_items[i], &li_items[i+1], (li_item_count - i) * sizeof(XTObject *));
 
126
                                return true;
 
127
                        }
 
128
                }
 
129
                return false;
 
130
        }
 
131
 
 
132
        inline bool remove(XTThreadPtr self, u_int i) {
 
133
                XTObject *item;
 
134
 
 
135
                if (i >= li_item_count)
 
136
                        return false;
 
137
                item = li_items[i];
 
138
                li_item_count--;
 
139
                memmove(&li_items[i], &li_items[i+1], (li_item_count - i) * sizeof(void *));
 
140
                if (li_referenced)
 
141
                        item->release(self);
 
142
                return true;
 
143
        }
 
144
 
 
145
        inline XTObject *take(u_int i) {
 
146
                XTObject *item;
 
147
 
 
148
                if (i >= li_item_count)
 
149
                        return NULL;
 
150
                item = li_items[i];
 
151
                li_item_count--;
 
152
                memmove(&li_items[i], &li_items[i+1], (li_item_count - i) * sizeof(void *));
 
153
                return item;
 
154
        }
 
155
 
 
156
        inline u_int size() const { return li_item_count; }
 
157
 
 
158
        inline void setEmpty(XTThreadPtr self) {
 
159
                if (li_items)
 
160
                        xt_free(self, li_items);
 
161
                li_item_count = 0;
 
162
                li_items = NULL;
 
163
        }
 
164
 
 
165
        inline bool isEmpty() { return li_item_count == 0; }
 
166
 
 
167
        inline XTObject *itemAt(u_int i) const {
 
168
                if (i >= li_item_count)
 
169
                        return NULL;
 
170
                return li_items[i];
 
171
        }
 
172
};
 
173
 
 
174
 
 
175
template <class T> class XTList : public XTListImp
 
176
{
 
177
        public:
 
178
        inline XTList() : XTListImp() { }
 
179
 
 
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); }
 
183
 
 
184
        inline bool remove(T *a) { return XTListImp::remove(a); }
 
185
 
 
186
        inline bool remove(XTThreadPtr self, u_int i) { return XTListImp::remove(self, i); }
 
187
 
 
188
        inline T *take(u_int i) { return (T *) XTListImp::take(i); }
 
189
 
 
190
        inline T *itemAt(u_int i) const { return (T *) XTListImp::itemAt(i); }
 
191
 
 
192
        inline u_int indexOf(T *a) {
 
193
                u_int i;
 
194
 
 
195
                for (i=0; i<size(); i++) {
 
196
                        if (itemAt(i) == a)
 
197
                                break;
 
198
                }
 
199
                return i;
 
200
        }
 
201
 
 
202
        void deleteAll(XTThreadPtr self)
 
203
        {
 
204
                for (u_int i=0; i<size(); i++) {
 
205
                        if (li_referenced)
 
206
                                itemAt(i)->release(self);
 
207
                }
 
208
                setEmpty(self);
 
209
        }
 
210
 
 
211
        void clone(XTThreadPtr self, XTListImp *list)
 
212
        {
 
213
                deleteAll(self);
 
214
                for (u_int i=0; i<list->size(); i++) {
 
215
                        XTListImp::append(self, list->itemAt(i)->clone(self));
 
216
                }
 
217
        }
 
218
};
 
219
 
 
220
#endif