ccp4srs  Version 1.0.0
ccp4srs_container.h
Go to the documentation of this file.
1 // $Id: ccp4srs_container.h $
2 // =================================================================
3 //
4 // CCP4 SRS Library: Storage, Retrieval and Search support for
5 // CCP4 ligand data.
6 //
7 // Copyright (C) Eugene Krissinel 2010-2013.
8 //
9 // This library is free software: you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License version 3, modified in accordance with the provisions
12 // of the license to address the requirements of UK law.
13 //
14 // You should have received a copy of the modified GNU Lesser
15 // General Public License along with this library. If not, copies
16 // may be downloaded from http://www.ccp4.ac.uk/ccp4license.php
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Lesser General Public License for more details.
22 //
23 // =================================================================
24 //
25 // 18.09.13 <-- Date of Last Modification.
26 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 // -----------------------------------------------------------------
28 //
29 // **** Module : ccp4srs_container <interface>
30 // ~~~~~~~~~
31 // **** Classes : ccp4srs::Container - template container class
32 // ~~~~~~~~~
33 //
34 // (C) E. Krissinel 2010-2013
35 //
36 // =================================================================
37 //
38 
39 #ifndef CCP4SRS_CONTAINER_H
40 #define CCP4SRS_CONTAINER_H
41 
42 #include "memio_.h"
43 
44 namespace ccp4srs {
45 
46  template <class T>
47 
48  class Container {
49 
50  protected:
51  int n_objects;
52  T **object;
53 
54  private:
55  int n_alloc;
56 
57  public:
58 
60  object = NULL;
61  n_objects = 0;
62  n_alloc = 0;
63  }
64 
65  virtual ~Container() {
66  empty();
67  }
68 
69  void empty() {
70  int i;
71  if (object) {
72  for (i=0;i<n_alloc;i++)
73  if (object[i]) delete object[i];
74  delete[] object;
75  object = NULL;
76  }
77  n_objects = 0;
78  n_alloc = 0;
79  }
80 
81  inline int numberOf() { return n_objects; }
82  inline T* at ( int pos ) { return object[pos]; }
83 
84  int index ( mmdb::cpstr id ) {
85  int i,k;
86  k = -1;
87  if (id) {
88  for (i=0;(i<n_objects) && (k<0);i++)
89  if (!strcmp(id,object[i]->id())) k = i;
90  }
91  return k;
92  }
93 
94  void add ( T* obj ) {
95  T** tobject;
96  int i;
97  if (n_objects>=n_alloc) {
98  n_alloc = n_objects+10;
99  tobject = new T*[n_alloc];
100  for (i=0;i<n_objects;i++)
101  tobject[i] = object[i];
102  for (i=n_objects;i<n_alloc;i++)
103  tobject[i] = NULL;
104  delete[] object;
105  object = tobject;
106  }
107  object[n_objects++] = obj;
108  }
109 
110  void write_mem ( PMemIO memIO, int version ) {
111  int i;
112  memIO->put_integer ( n_objects );
113  for (i=0;i<n_objects;i++)
114  object[i]->write_mem ( memIO,version );
115  }
116 
117  bool read_mem ( PMemIO memIO, int version, bool * Ok ) {
118  int i;
119  bool success;
120  empty();
121  if (Ok) success = *Ok;
122  else success = true;
123  memIO->get_integer ( n_objects,&success );
124  n_alloc = n_objects;
125  if (n_objects>0) {
126  object = new T*[n_alloc];
127  for (i=0;i<n_objects;i++) {
128  object[i] = new T();
129  object[i]->read_mem ( memIO,version,&success );
130  }
131  }
132  if (Ok) *Ok = success;
133  return success;
134  }
135 
136  };
137 
138 } // namespace ccp4srs
139 
140 #endif // CCP4SRS_CONTAINER_H
void add(T *obj)
Definition: ccp4srs_container.h:94
void write_mem(PMemIO memIO, int version)
Definition: ccp4srs_container.h:110
bool read_mem(PMemIO memIO, int version, bool *Ok)
Definition: ccp4srs_container.h:117
int index(mmdb::cpstr id)
Definition: ccp4srs_container.h:84
T * at(int pos)
Definition: ccp4srs_container.h:82
Definition: ccp4srs_angle.cpp:42
void empty()
Definition: ccp4srs_container.h:69
Container()
Definition: ccp4srs_container.h:59
Definition: ccp4srs_container.h:48
int n_objects
Definition: ccp4srs_container.h:51
int numberOf()
Definition: ccp4srs_container.h:81
T ** object
Definition: ccp4srs_container.h:52
virtual ~Container()
Definition: ccp4srs_container.h:65