bes  Updated for version 3.20.5
BESDefinitionStorageList.cc
1 // BESDefinitionStorageList.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
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 as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <iostream>
34 
35 using std::endl;
36 
37 #include "BESDefinitionStorageList.h"
38 #include "BESDefinitionStorage.h"
39 #include "BESDefine.h"
40 #include "BESInfo.h"
41 
42 BESDefinitionStorageList *BESDefinitionStorageList::_instance = 0;
43 
44 BESDefinitionStorageList::BESDefinitionStorageList() :
45  _first(0)
46 {
47 }
48 
49 BESDefinitionStorageList::~BESDefinitionStorageList()
50 {
51  BESDefinitionStorageList::persistence_list *pl = _first;
52  while (pl) {
53  if (pl->_persistence_obj) {
54  delete pl->_persistence_obj;
55  }
56  BESDefinitionStorageList::persistence_list *next = pl->_next;
57  delete pl;
58  pl = next;
59  }
60 }
61 
75 {
76  bool ret = false;
77  if (!_first) {
78  _first = new BESDefinitionStorageList::persistence_list;
79  _first->_persistence_obj = cp;
80  _first->_reference = 1;
81  _first->_next = 0;
82  ret = true;
83  }
84  else {
85  BESDefinitionStorageList::persistence_list *pl = _first;
86  bool done = false;
87  while (done == false) {
88  if (pl->_persistence_obj->get_name() != cp->get_name()) {
89  if (pl->_next) {
90  pl = pl->_next;
91  }
92  else {
93  pl->_next = new BESDefinitionStorageList::persistence_list;
94  pl->_next->_persistence_obj = cp;
95  pl->_next->_reference = 1;
96  pl->_next->_next = 0;
97  done = true;
98  ret = true;
99  }
100  }
101  else {
102  done = true;
103  ret = false;
104  }
105  }
106  }
107  return ret;
108 }
109 
118 bool BESDefinitionStorageList::ref_persistence(const string &persist_name)
119 {
120  bool ret = false;
121  BESDefinitionStorageList::persistence_list *pl = _first;
122 
123  bool done = false;
124  while (done == false) {
125  if (pl) {
126  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
127  ret = true;
128  done = true;
129  pl->_reference++;
130  }
131  else {
132  pl = pl->_next;
133  }
134  }
135  else {
136  done = true;
137  }
138  }
139 
140  return ret;
141 }
142 
153 bool BESDefinitionStorageList::deref_persistence(const string &persist_name)
154 {
155  bool ret = false;
156  BESDefinitionStorageList::persistence_list *pl = _first;
157  BESDefinitionStorageList::persistence_list *last = 0;
158 
159  bool done = false;
160  while (done == false) {
161  if (pl) {
162  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
163  ret = true;
164  done = true;
165  pl->_reference--;
166  if (!pl->_reference) {
167  if (pl == _first) {
168  _first = _first->_next;
169  }
170  else {
171  if (!last) throw BESInternalError("ContainerStorageList last is null", __FILE__, __LINE__);
172  last->_next = pl->_next;
173  }
174  delete pl->_persistence_obj;
175  delete pl;
176  pl = 0;
177  }
178  }
179  else {
180  last = pl;
181  pl = pl->_next;
182  }
183  }
184  else {
185  done = true;
186  }
187  }
188 
189  return ret;
190 }
191 
202 {
203  BESDefinitionStorage *ret = NULL;
204  BESDefinitionStorageList::persistence_list *pl = _first;
205  bool done = false;
206  while (done == false) {
207  if (pl) {
208  if (persist_name == pl->_persistence_obj->get_name()) {
209  ret = pl->_persistence_obj;
210  done = true;
211  }
212  else {
213  pl = pl->_next;
214  }
215  }
216  else {
217  done = true;
218  }
219  }
220  return ret;
221 }
222 
233 BESDefine *
234 BESDefinitionStorageList::look_for(const string &def_name)
235 {
236  BESDefine *ret_def = NULL;
237  BESDefinitionStorageList::persistence_list *pl = _first;
238  bool done = false;
239  while (done == false) {
240  if (pl) {
241  ret_def = pl->_persistence_obj->look_for(def_name);
242  if (ret_def) {
243  done = true;
244  }
245  else {
246  pl = pl->_next;
247  }
248  }
249  else {
250  done = true;
251  }
252  }
253  return ret_def;
254 }
255 
271 {
272  BESDefinitionStorageList::persistence_list *pl = _first;
273  bool first = true;
274  while (pl) {
275  if (!first) {
276  // separate each store with a blank line
277  info.add_break(1);
278  }
279  first = false;
280  map<string, string> props;
281  props["name"] = pl->_persistence_obj->get_name();
282  info.begin_tag("store", &props);
283  pl->_persistence_obj->show_definitions(info);
284  info.end_tag("store");
285  pl = pl->_next;
286  }
287 }
288 
290 BESDefinitionStorageList::TheList()
291 {
292  if (_instance == 0) {
293  _instance = new BESDefinitionStorageList;
294  }
295  return _instance;
296 }
297 
305 void BESDefinitionStorageList::dump(ostream &strm) const
306 {
307  strm << BESIndent::LMarg << "BESDefinitionStorageList::dump - (" << (void *) this << ")" << endl;
308  BESIndent::Indent();
309  if (_first) {
310  strm << BESIndent::LMarg << "registered definition storage:" << endl;
311  BESIndent::Indent();
312  BESDefinitionStorageList::persistence_list *pl = _first;
313  while (pl) {
314  pl->_persistence_obj->dump(strm);
315  pl = pl->_next;
316  }
317  BESIndent::UnIndent();
318  }
319  else {
320  strm << BESIndent::LMarg << "registered definition storage: none" << endl;
321  }
322  BESIndent::UnIndent();
323 }
324 
provides persistent storage for a specific view of different containers including contraints and aggr...
exception thrown if inernal error encountered
virtual void dump(std::ostream &strm) const
dumps information about this object
informational response object
Definition: BESInfo.h:68
virtual bool add_persistence(BESDefinitionStorage *p)
Add a persistent store to the list.
virtual void show_definitions(BESInfo &info)
show information for each definition in each persistence store
virtual bool ref_persistence(const std::string &persist_name)
reference a persistent store in the list
virtual BESDefinitionStorage * find_persistence(const std::string &persist_name)
find the persistence store with the given name
virtual BESDefine * look_for(const std::string &def_name)
look for the specified definition in the list of defintion stores.
virtual bool deref_persistence(const std::string &persist_name)
de-reference a persistent store in the list
virtual const string & get_name() const
retrieve the name of this persistent store
Provides a mechanism for accessing definitions from different definition stores registered with this ...