Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Table.h
1 /*
2  * Table and AllocatedTable class interfaces
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2004-08, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_GENSTRUCT_TABLE_H
22 #define ELM_GENSTRUCT_TABLE_H
23 
24 #include <elm/assert.h>
25 #include <elm/deprecated.h>
26 #include <elm/util/IndexedIterator.h>
27 #include <elm/util/array.h>
28 
29 namespace elm { namespace genstruct {
30 
31 // Table class
32 template <class T>
33 class Table {
34 public:
35  static Table<T> EMPTY;
36 
37  inline Table(void): tab(0), cnt(0) { }
38  inline Table(T *table, int count): tab(table), cnt(count)
39  { ASSERTP(count == 0 || (count > 0 && table), "null pointer for table"); }
40  inline Table(const Table<T>& table): tab(table.tab), cnt(table.cnt) { }
41 
42  // Accessors
43  inline int size(void) const { return cnt; }
44  inline const T& get(int index) const
45  { ASSERTP(index >= 0 && index < cnt, "index out of bounds"); return tab[index]; }
46  inline T& get(int index)
47  { ASSERTP(index >= 0 && index < cnt, "index out of bounds"); return tab[index]; }
48  inline void set(int index, const T& value)
49  { ASSERTP(index >= 0 && index < cnt, "index out of bounds"); tab[index] = value; }
50  inline bool isEmpty(void) const { return cnt == 0; }
51 
52  // Mutators
53  inline void copy(const Table<T>& table) { tab = table.tab; cnt = table.cnt; }
54 
55  // Operators
56  inline const T& operator[](int index) const { return get(index); }
57  inline T& operator[](int index) { return get(index); }
58  inline Table<T>& operator=(const Table& table) { copy(table); return *this; }
59  inline operator bool(void) const { return !isEmpty(); }
60  inline const T *operator*(void) const { return tab; }
61  inline T *operator*(void) { return tab; }
62 
63  // Iterator class
64  class Iterator: public IndexedIterator<Iterator, T, Table<T> > {
65  public:
66  inline Iterator(const Table<T>& table): IndexedIterator<Iterator, T, Table<T> >(table) { }
67  inline Iterator(const Iterator &iter): IndexedIterator<Iterator, T, Table<T> >(iter) { }
68  };
69 
70  // Deprecated
71  inline int count(void) const { return cnt; }
72  inline const T *table(void) const { ELM_DEPRECATED; return tab; }
73  inline T *table(void) { ELM_DEPRECATED return tab; }
74 
75 protected:
76  T *tab;
77  int cnt;
78 };
79 
80 template <class T>
81 class DeletableTable: public Table<T> {
82 public:
83  inline DeletableTable(void) { }
84  inline DeletableTable(T *table, int count): Table<T>(table, count) { }
85  inline DeletableTable(const Table<T>& table): Table<T>(table) { }
86  inline ~DeletableTable(void) { if(Table<T>::tab) delete [] Table<T>::tab; }
88  { Table<T>::copy(table); return *this; }
90  { copy(table); return *this; }
91  inline void copy(const Table<T>& t)
92  { clear(); if(t.size()) { Table<T>::copy(Table<T>(new T[t.count()], t.count())); array::copy(**this, *t, t.count()); } }
93  inline void clear(void) { if(Table<T>::tab) { delete [] Table<T>::tab; copy(Table<T>::EMPTY); } }
94 };
95 
96 // AllocatedTable class
97 template <class T>
98 class AllocatedTable: public Table<T> {
99 public:
100  inline AllocatedTable(void) { }
101  inline AllocatedTable(int count)
102  : Table<T>(new T[count], count) { }
103  inline AllocatedTable(const Table<T>& table): Table<T>(table ? new T[table.count()] : 0, table.count())
104  { copyItems(*table); }
105  inline ~AllocatedTable(void) { free(); }
106 
107  inline void allocate(int count)
108  { free(); Table<T>::cnt = count; Table<T>::tab = new T[count]; }
109  inline void free(void)
110  { if(Table<T>::tab) delete [] Table<T>::tab; Table<T>::tab = 0; Table<T>::cnt = 0; }
111 
113  { *this = (const Table<T>&)table; return *this; } //
115  { allocate(table.count()); copyItems(*table); return *this; }
116 
117 private:
118  inline void copyItems(const T *tab)
119  { for(int i = 0; i < Table<T>::cnt; i++) (*this)[i] = tab[i]; }
120 };
121 
122 
123 // statics
124 template <class T>
125 Table<T> Table<T>::EMPTY;
126 
127 } // genstruct
128 
129 // shortcuts
130 template <class T> struct table: public genstruct::Table<T> {
131  inline table(void) { }
132  inline table(T *table, int count): genstruct::Table<T>(table, count) { }
133  inline table(const genstruct::Table<T>& table): genstruct::Table<T>(table) { }
134 };
135 
136 template <class T> struct dtable: public genstruct::DeletableTable<T> {
137  inline dtable(void) { }
138  inline dtable(T *table, int count): genstruct::DeletableTable<T>(table, count) { }
139  inline dtable(const genstruct::Table<T>& table): genstruct::DeletableTable<T>(table) { }
140 };
141 
142 } // elm
143 
144 #endif // ELM_GENSTRUCT_TABLE_H
void copy(T *target, const T *source, int size)
Definition: array.h:59
void clear(void)
Definition: Table.h:93
AllocatedTable(const Table< T > &table)
Definition: Table.h:103
void copy(const Table< T > &t)
Definition: Table.h:91
~DeletableTable(void)
Definition: Table.h:86
T * operator*(void)
Definition: Table.h:61
const T * table(void) const
Definition: Table.h:72
Table(T *table, int count)
Definition: Table.h:38
table(const genstruct::Table< T > &table)
Definition: Table.h:133
Table(void)
Definition: Table.h:37
Definition: Table.h:81
int size(void) const
Definition: Table.h:43
table(T *table, int count)
Definition: Table.h:132
Definition: Table.h:33
AllocatedTable< T > & operator=(const AllocatedTable< T > &table)
Definition: Table.h:112
Definition: Table.h:130
Definition: Table.h:64
const T * operator*(void) const
Definition: Table.h:60
T * tab
Definition: Table.h:76
DeletableTable(const Table< T > &table)
Definition: Table.h:85
value_t value(CString name, int value)
Definition: rtti.h:40
const T & operator[](int index) const
Definition: Table.h:56
void copy(const Table< T > &table)
Definition: Table.h:53
AllocatedTable(void)
Definition: Table.h:100
void allocate(int count)
Definition: Table.h:107
DeletableTable(T *table, int count)
Definition: Table.h:84
DeletableTable< T > & operator=(const Table< T > &table)
Definition: Table.h:87
~AllocatedTable(void)
Definition: Table.h:105
int count(void) const
Definition: Table.h:71
table(void)
Definition: Table.h:131
Table< T > & operator=(const Table &table)
Definition: Table.h:58
dtable(void)
Definition: Table.h:137
int cnt
Definition: Table.h:77
Iterator(const Table< T > &table)
Definition: Table.h:66
dtable(T *table, int count)
Definition: Table.h:138
DeletableTable(void)
Definition: Table.h:83
Definition: Table.h:136
DeletableTable< T > & operator=(const DeletableTable< T > &table)
Definition: Table.h:89
T & operator[](int index)
Definition: Table.h:57
Definition: Table.h:98
void free(void)
Definition: Table.h:109
bool isEmpty(void) const
Definition: Table.h:50
Table(const Table< T > &table)
Definition: Table.h:40
dtable(const genstruct::Table< T > &table)
Definition: Table.h:139
Iterator(const Iterator &iter)
Definition: Table.h:67
AllocatedTable(int count)
Definition: Table.h:101
static Table< T > EMPTY
Definition: Table.h:35
AllocatedTable< T > & operator=(const Table< T > &table)
Definition: Table.h:114
T * table(void)
Definition: Table.h:73
Definition: IndexedIterator.h:31
void set(int index, const T &value)
Definition: Table.h:48