bes  Updated for version 3.17.0
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  {
54  if( pl->_persistence_obj )
55  {
56  delete pl->_persistence_obj ;
57  }
58  BESDefinitionStorageList::persistence_list *next = pl->_next ;
59  delete pl ;
60  pl = next ;
61  }
62 }
63 
76 bool
78 {
79  bool ret = false ;
80  if( !_first )
81  {
82  _first = new BESDefinitionStorageList::persistence_list ;
83  _first->_persistence_obj = cp ;
84  _first->_reference = 1 ;
85  _first->_next = 0 ;
86  ret = true ;
87  }
88  else
89  {
90  BESDefinitionStorageList::persistence_list *pl = _first ;
91  bool done = false ;
92  while( done == false )
93  {
94  if( pl->_persistence_obj->get_name() != cp->get_name() )
95  {
96  if( pl->_next )
97  {
98  pl = pl->_next ;
99  }
100  else
101  {
102  pl->_next = new BESDefinitionStorageList::persistence_list ;
103  pl->_next->_persistence_obj = cp ;
104  pl->_next->_reference = 1 ;
105  pl->_next->_next = 0 ;
106  done = true ;
107  ret = true ;
108  }
109  }
110  else
111  {
112  done = true ;
113  ret = false ;
114  }
115  }
116  }
117  return ret ;
118 }
119 
128 bool
129 BESDefinitionStorageList::ref_persistence( const string &persist_name )
130 {
131  bool ret = false ;
132  BESDefinitionStorageList::persistence_list *pl = _first ;
133 
134  bool done = false ;
135  while( done == false )
136  {
137  if( pl )
138  {
139  if( pl->_persistence_obj &&
140  pl->_persistence_obj->get_name() == persist_name )
141  {
142  ret = true ;
143  done = true ;
144  pl->_reference++ ;
145  }
146  else
147  {
148  pl = pl->_next ;
149  }
150  }
151  else
152  {
153  done = true ;
154  }
155  }
156 
157  return ret ;
158 }
159 
170 bool
171 BESDefinitionStorageList::deref_persistence( const string &persist_name )
172 {
173  bool ret = false ;
174  BESDefinitionStorageList::persistence_list *pl = _first ;
175  BESDefinitionStorageList::persistence_list *last = 0 ;
176 
177  bool done = false ;
178  while( done == false )
179  {
180  if( pl )
181  {
182  if( pl->_persistence_obj &&
183  pl->_persistence_obj->get_name() == persist_name )
184  {
185  ret = true ;
186  done = true ;
187  pl->_reference-- ;
188  if( !pl->_reference )
189  {
190  if( pl == _first )
191  {
192  _first = _first->_next ;
193  }
194  else
195  {
196  if (!last)
197  throw BESInternalError("ContainerStorageList last is null", __FILE__, __LINE__);
198  last->_next = pl->_next ;
199  }
200  delete pl->_persistence_obj ;
201  delete pl ;
202  pl = 0 ;
203  }
204  }
205  else
206  {
207  last = pl ;
208  pl = pl->_next ;
209  }
210  }
211  else
212  {
213  done = true ;
214  }
215  }
216 
217  return ret ;
218 }
219 
229 BESDefinitionStorageList::find_persistence( const string &persist_name )
230 {
231  BESDefinitionStorage *ret = NULL ;
232  BESDefinitionStorageList::persistence_list *pl = _first ;
233  bool done = false ;
234  while( done == false )
235  {
236  if( pl )
237  {
238  if( persist_name == pl->_persistence_obj->get_name() )
239  {
240  ret = pl->_persistence_obj ;
241  done = true ;
242  }
243  else
244  {
245  pl = pl->_next ;
246  }
247  }
248  else
249  {
250  done = true ;
251  }
252  }
253  return ret ;
254 }
255 
266 BESDefine *
267 BESDefinitionStorageList::look_for( const string &def_name )
268 {
269  BESDefine *ret_def = NULL ;
270  BESDefinitionStorageList::persistence_list *pl = _first ;
271  bool done = false ;
272  while( done == false )
273  {
274  if( pl )
275  {
276  ret_def = pl->_persistence_obj->look_for( def_name ) ;
277  if( ret_def )
278  {
279  done = true ;
280  }
281  else
282  {
283  pl = pl->_next ;
284  }
285  }
286  else
287  {
288  done = true ;
289  }
290  }
291  return ret_def ;
292 }
293 
308 void
310 {
311  BESDefinitionStorageList::persistence_list *pl = _first ;
312  bool first = true ;
313  while( pl )
314  {
315  if( !first )
316  {
317  // separate each store with a blank line
318  info.add_break( 1 ) ;
319  }
320  first = false ;
321  map<string,string> props ;
322  props["name"] = pl->_persistence_obj->get_name() ;
323  info.begin_tag( "store", &props ) ;
324  pl->_persistence_obj->show_definitions( info ) ;
325  info.end_tag( "store" ) ;
326  pl = pl->_next ;
327  }
328 }
329 
331 BESDefinitionStorageList::TheList()
332 {
333  if( _instance == 0 )
334  {
335  _instance = new BESDefinitionStorageList ;
336  }
337  return _instance ;
338 }
339 
347 void
348 BESDefinitionStorageList::dump( ostream &strm ) const
349 {
350  strm << BESIndent::LMarg << "BESDefinitionStorageList::dump - ("
351  << (void *)this << ")" << endl;
352  BESIndent::Indent() ;
353  if( _first )
354  {
355  strm << BESIndent::LMarg << "registered definition storage:" << endl ;
356  BESIndent::Indent() ;
357  BESDefinitionStorageList::persistence_list *pl = _first ;
358  while( pl )
359  {
360  pl->_persistence_obj->dump( strm ) ;
361  pl = pl->_next ;
362  }
363  BESIndent::UnIndent() ;
364  }
365  else
366  {
367  strm << BESIndent::LMarg << "registered definition storage: none" << endl ;
368  }
369  BESIndent::UnIndent() ;
370 }
371 
provides persistent storage for a specific view of different containers including contraints and aggr...
exception thrown if inernal error encountered
virtual BESDefine * look_for(const string &def_name)
look for the specified definition in the list of defintion stores.
virtual BESDefinitionStorage * find_persistence(const string &persist_name)
find the persistence store with the given name
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 void dump(ostream &strm) const
dumps information about this object
virtual const string & get_name() const
retrieve the name of this persistent store
virtual bool ref_persistence(const string &persist_name)
reference a persistent store in the list
virtual bool deref_persistence(const string &persist_name)
de-reference a persistent store in the list
Provides a mechanism for accessing definitions from different definition stores registered with this ...