GTK+ IOStream  Beta
<< GTK+ >> add C++ IOStream operators to GTK+. Now with extra abilities ... like network serialisation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Array.H
Go to the documentation of this file.
1 /* Copyright 2000-2012 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 /* Copyright 2000-2012 Matt Flax <flatmax@flatmax.org>
18  This file is part of GTK+ IOStream class set
19 
20  GTK+ IOStream is free software; you can redistribute it and/or modify
21  it under the terms of the GNU General Public License as published by
22  the Free Software Foundation; either version 2 of the License, or
23  (at your option) any later version.
24 
25  GTK+ IOStream is distributed in the hope that it will be useful,
26  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  GNU General Public License for more details.
29 
30  You have received a copy of the GNU General Public License
31  along with GTK+ IOStream
32 */
33 #ifndef ARRAY_H
34 #define ARRAY_H
35 #include <iostream>
36 using namespace std;
37 #include <stdlib.h>
38 
39 template<class ATYPE>
40 class Array {
41  ATYPE *array;
42  unsigned int size;
43 
44  void allocMem(int sizeIn){
45  if (array)
46  deleteMem();
47  //cout<<"allocMem("<<sizeIn<<")"<<endl;
48  array= new ATYPE[sizeIn];
49  if (!array){
50  cerr<<"Couldn't allocate array memory of size "<<size<<endl;
51  exit(-1);
52  }
53  size=sizeIn;
54  }
55 
56  void deleteMem(ATYPE* ptr=NULL){
57  if (ptr=NULL) ptr=array;
58  //cout<<"deleteMem"<<endl;
59  if (ptr){
60  delete [] ptr;
61  ptr=NULL;
62  size=0;
63  }
64  }
65 public:
66  Array(void){
67  size=0;
68  array=NULL;
69  }
70  Array(unsigned int sizeIn){
71  size=0;
72  array=NULL;
73  allocMem(sizeIn);
74  }
75 
76  ~Array(void){
77  deleteMem();
78  }
79 
80  int len(void){return size;}
81 
82  ATYPE& operator[](unsigned int index){
83  if (array==NULL) // handle the case when there are no initialised arrays
84  allocMem(index+1);
85  else if ((index+1)>size){// create a larger array and copy data across
86  ATYPE* oldArray=array;
87  int oldSize=size;
88  allocMem(index+1);
89  for (int i=0;i<oldSize;i++)
90  array[i]=oldArray[i];
91  deleteMem(oldArray);
92  }
93  return array[index];
94  }
95 
96 // friend ostream& operator<<(ostream& c, const Array<ATYPE>& array);
97 
98  void dump(void){
99  for (int i=0;i<len();i++)
100  cout<<array[i]<<'\t';
101  }
102 
103 };
104 
105 //ostream& operator<<(ostream& c, const Array<ATYPE>& array){
106 // for (int i=0;i<array.len();i++)
107  // c<<array[i]<<'\t';
108 // return c;
109 //}
110 #endif //ARRAY_H