bes  Updated for version 3.20.6
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 using std::string;
37 using std::ostream;
38 
39 #include "BESDefinitionStorageList.h"
40 #include "BESDefinitionStorage.h"
41 #include "BESDefine.h"
42 #include "BESInfo.h"
43 
44 BESDefinitionStorageList *BESDefinitionStorageList::_instance = 0;
45 
46 BESDefinitionStorageList::BESDefinitionStorageList() :
47  _first(0)
48 {
49 }
50 
51 BESDefinitionStorageList::~BESDefinitionStorageList()
52 {
53  BESDefinitionStorageList::persistence_list *pl = _first;
54  while (pl) {
55  if (pl->_persistence_obj) {
56  delete pl->_persistence_obj;
57  }
58  BESDefinitionStorageList::persistence_list *next = pl->_next;
59  delete pl;
60  pl = next;
61  }
62 }
63 
77 {
78  bool ret = false;
79  if (!_first) {
80  _first = new BESDefinitionStorageList::persistence_list;
81  _first->_persistence_obj = cp;
82  _first->_reference = 1;
83  _first->_next = 0;
84  ret = true;
85  }
86  else {
87  BESDefinitionStorageList::persistence_list *pl = _first;
88  bool done = false;
89  while (done == false) {
90  if (pl->_persistence_obj->get_name() != cp->get_name()) {
91  if (pl->_next) {
92  pl = pl->_next;
93  }
94  else {
95  pl->_next = new BESDefinitionStorageList::persistence_list;
96  pl->_next->_persistence_obj = cp;
97  pl->_next->_reference = 1;
98  pl->_next->_next = 0;
99  done = true;
100  ret = true;
101  }
102  }
103  else {
104  done = true;
105  ret = false;
106  }
107  }
108  }
109  return ret;
110 }
111 
120 bool BESDefinitionStorageList::ref_persistence(const string &persist_name)
121 {
122  bool ret = false;
123  BESDefinitionStorageList::persistence_list *pl = _first;
124 
125  bool done = false;
126  while (done == false) {
127  if (pl) {
128  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
129  ret = true;
130  done = true;
131  pl->_reference++;
132  }
133  else {
134  pl = pl->_next;
135  }
136  }
137  else {
138  done = true;
139  }
140  }
141 
142  return ret;
143 }
144 
155 bool BESDefinitionStorageList::deref_persistence(const string &persist_name)
156 {
157  bool ret = false;
158  BESDefinitionStorageList::persistence_list *pl = _first;
159  BESDefinitionStorageList::persistence_list *last = 0;
160 
161  bool done = false;
162  while (done == false) {
163  if (pl) {
164  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
165  ret = true;
166  done = true;
167  pl->_reference--;
168  if (!pl->_reference) {
169  if (pl == _first) {
170  _first = _first->_next;
171  }
172  else {
173  if (!last) throw BESInternalError("ContainerStorageList last is null", __FILE__, __LINE__);
174  last->_next = pl->_next;
175  }
176  delete pl->_persistence_obj;
177  delete pl;
178  pl = 0;
179  }
180  }
181  else {
182  last = pl;
183  pl = pl->_next;
184  }
185  }
186  else {
187  done = true;
188  }
189  }
190 
191  return ret;
192 }
193 
204 {
205  BESDefinitionStorage *ret = NULL;
206  BESDefinitionStorageList::persistence_list *pl = _first;
207  bool done = false;
208  while (done == false) {
209  if (pl) {
210  if (persist_name == pl->_persistence_obj->get_name()) {
211  ret = pl->_persistence_obj;
212  done = true;
213  }
214  else {
215  pl = pl->_next;
216  }
217  }
218  else {
219  done = true;
220  }
221  }
222  return ret;
223 }
224 
235 BESDefine *
236 BESDefinitionStorageList::look_for(const string &def_name)
237 {
238  BESDefine *ret_def = NULL;
239  BESDefinitionStorageList::persistence_list *pl = _first;
240  bool done = false;
241  while (done == false) {
242  if (pl) {
243  ret_def = pl->_persistence_obj->look_for(def_name);
244  if (ret_def) {
245  done = true;
246  }
247  else {
248  pl = pl->_next;
249  }
250  }
251  else {
252  done = true;
253  }
254  }
255  return ret_def;
256 }
257 
273 {
274  BESDefinitionStorageList::persistence_list *pl = _first;
275  bool first = true;
276  while (pl) {
277  if (!first) {
278  // separate each store with a blank line
279  info.add_break(1);
280  }
281  first = false;
282  std::map<string, string> props;
283  props["name"] = pl->_persistence_obj->get_name();
284  info.begin_tag("store", &props);
285  pl->_persistence_obj->show_definitions(info);
286  info.end_tag("store");
287  pl = pl->_next;
288  }
289 }
290 
292 BESDefinitionStorageList::TheList()
293 {
294  if (_instance == 0) {
295  _instance = new BESDefinitionStorageList;
296  }
297  return _instance;
298 }
299 
307 void BESDefinitionStorageList::dump(ostream &strm) const
308 {
309  strm << BESIndent::LMarg << "BESDefinitionStorageList::dump - (" << (void *) this << ")" << endl;
310  BESIndent::Indent();
311  if (_first) {
312  strm << BESIndent::LMarg << "registered definition storage:" << endl;
313  BESIndent::Indent();
314  BESDefinitionStorageList::persistence_list *pl = _first;
315  while (pl) {
316  pl->_persistence_obj->dump(strm);
317  pl = pl->_next;
318  }
319  BESIndent::UnIndent();
320  }
321  else {
322  strm << BESIndent::LMarg << "registered definition storage: none" << endl;
323  }
324  BESIndent::UnIndent();
325 }
326 
BESDefinitionStorageList::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESDefinitionStorageList.cc:307
BESDefinitionStorageList::ref_persistence
virtual bool ref_persistence(const std::string &persist_name)
reference a persistent store in the list
Definition: BESDefinitionStorageList.cc:120
BESDefinitionStorage
provides persistent storage for a specific view of different containers including contraints and aggr...
Definition: BESDefinitionStorage.h:62
BESDefinitionStorage::get_name
virtual const std::string & get_name() const
retrieve the name of this persistent store
Definition: BESDefinitionStorage.h:82
BESInfo
informational response object
Definition: BESInfo.h:63
BESDefinitionStorageList
Provides a mechanism for accessing definitions from different definition stores registered with this ...
Definition: BESDefinitionStorageList.h:69
BESDefine
Definition: BESDefine.h:42
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
BESDefinitionStorageList::deref_persistence
virtual bool deref_persistence(const std::string &persist_name)
de-reference a persistent store in the list
Definition: BESDefinitionStorageList.cc:155
BESDefinitionStorageList::show_definitions
virtual void show_definitions(BESInfo &info)
show information for each definition in each persistence store
Definition: BESDefinitionStorageList.cc:272
BESDefinitionStorageList::find_persistence
virtual BESDefinitionStorage * find_persistence(const std::string &persist_name)
find the persistence store with the given name
Definition: BESDefinitionStorageList.cc:203
BESDefinitionStorageList::add_persistence
virtual bool add_persistence(BESDefinitionStorage *p)
Add a persistent store to the list.
Definition: BESDefinitionStorageList.cc:76
BESDefinitionStorageList::look_for
virtual BESDefine * look_for(const std::string &def_name)
look for the specified definition in the list of defintion stores.
Definition: BESDefinitionStorageList.cc:236