libdap++  Updated for version 3.13.3
ServerFunctionsList.cc
Go to the documentation of this file.
1 // ServerFunctionsList.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) 2013 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
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 OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #include "config.h"
26 
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30 
31 #include <pthread.h>
32 
33 #include <iostream>
34 #include <algorithm>
35 
36 //#define DODS_DEBUG
37 
38 #include <expr.h>
39 #include "debug.h"
40 
41 #include "ServerFunctionsList.h"
42 
43 using std::cerr;
44 using std::string;
45 using std::endl;
46 using namespace std;
47 using namespace libdap;
48 
49 namespace libdap {
50 
51 static pthread_once_t ServerFunctionsList_instance_control = PTHREAD_ONCE_INIT;
52 
53 ServerFunctionsList *ServerFunctionsList::d_instance = 0 ;
54 
58 void ServerFunctionsList::initialize_instance() {
59  if (d_instance == 0) {
60  DBG(cerr << "ServerFunctionsList::initialize_instance() - Creating singleton ServerFunctionList instance." << endl);
61  d_instance = new ServerFunctionsList;
62  #if HAVE_ATEXIT
63  atexit(delete_instance);
64  #endif
65  }
66 }
67 
71 void ServerFunctionsList::delete_instance() {
72  DBG(cerr << "ServerFunctionsList::delete_instance() - Deleting singleton ServerFunctionList instance." << endl);
73  delete d_instance;
74  d_instance = 0;
75 }
76 
81 ServerFunctionsList::~ServerFunctionsList() {
82  std::multimap<string,libdap::ServerFunction *>::iterator fit;
83  for(fit=d_func_list.begin(); fit!=d_func_list.end() ; fit++){
84  libdap::ServerFunction *func = fit->second;
85  DBG(cerr << "ServerFunctionsList::~ServerFunctionsList() - Deleting ServerFunction " << func->getName() << " from ServerFunctionsList." << endl);
86  delete func;
87  }
88  d_func_list.clear();
89 }
90 
91 ServerFunctionsList * ServerFunctionsList::TheList() {
92  pthread_once(&ServerFunctionsList_instance_control, initialize_instance);
93  DBG(cerr << "ServerFunctionsList::TheList() - Returning singleton ServerFunctionList instance." << endl);
94  return d_instance;
95 }
96 
106 void ServerFunctionsList::add_function(ServerFunction *func )
107 {
108  DBG(cerr << "ServerFunctionsList::add_function() - Adding ServerFunction " << func->getName() << endl);
109  d_func_list.insert(std::make_pair(func->getName(),func));
110 }
111 
112 #if 0
113 
114 bool
115 ServerFunctionsList::add_function( string name, btp_func func )
116 {
117  if (d_btp_func_list[name] == 0) {
118  d_btp_func_list[name] = func;
119  return true;
120  }
121 
122  return false;
123 }
124 
125 
126 bool
127 ServerFunctionsList::add_function( string name, bool_func func )
128 {
129  if (d_bool_func_list[name] == 0) {
130  d_bool_func_list[name] = func;
131  return true;
132  }
133 
134  return false;
135 }
136 
137 bool
138 ServerFunctionsList::add_function( string name, proj_func func )
139 {
140  if (d_proj_func_list[name] == 0) {
141  d_proj_func_list[name] = func;
142  return true;
143  }
144 
145  return false;
146 }
147 #endif
148 
149 #if 0
150 void ServerFunctionsList::store_functions(ConstraintEvaluator &ce)
151 {
152  if (d_btp_func_list.size() > 0) {
153  map<string, btp_func>::iterator i = d_btp_func_list.begin();
154  map<string, btp_func>::iterator e = d_btp_func_list.end();
155  while (i != e) {
156  ce.add_function((*i).first, (*i).second);
157  ++i;
158  }
159  }
160 
161  if (d_bool_func_list.size() > 0) {
162  map<string, bool_func>::iterator i = d_bool_func_list.begin();
163  map<string, bool_func>::iterator e = d_bool_func_list.end();
164  while (i != e) {
165  ce.add_function((*i).first, (*i).second);
166  ++i;
167  }
168  }
169 
170  if (d_proj_func_list.size() > 0) {
171  map<string, proj_func>::iterator i = d_proj_func_list.begin();
172  map<string, proj_func>::iterator e = d_proj_func_list.end();
173  while (i != e) {
174  ce.add_function((*i).first, (*i).second);
175  ++i;
176  }
177  }
178 }
179 #endif
180 
201 bool ServerFunctionsList::find_function(const std::string &name, bool_func *f) const
202 {
203 #if 0
204  if (d_bool_func_list.empty())
205  return false;
206 
207  map<string, bool_func>::const_iterator i = d_bool_func_list.begin();
208  while(i != d_bool_func_list.end()) {
209  if (name == (*i).first && (*f = (*i).second)) {
210  return true;
211  }
212  ++i;
213  }
214 
215  return false;
216 #endif
217 
218  if (d_func_list.empty())
219  return false;
220 
221  std::pair <std::multimap<std::string,libdap::ServerFunction *>::const_iterator, std::multimap<std::string,libdap::ServerFunction *>::const_iterator> ret;
222  ret = d_func_list.equal_range(name);
223  for (std::multimap<std::string,libdap::ServerFunction *>::const_iterator it=ret.first; it!=ret.second; ++it) {
224  if (name == it->first && (*f = it->second->get_bool_func())){
225  DBG(cerr << "ServerFunctionsList::find_function() - Found boolean function " << it->second->getName() << endl);
226  return true;
227  }
228  }
229  return false;
230 
231 }
232 
233 
234 
255 bool ServerFunctionsList::find_function(const string &name, btp_func *f) const
256 {
257 
258 #if 0
259  if (d_btp_func_list.empty())
260  return false;
261 
262  map<string, btp_func>::const_iterator i = d_btp_func_list.begin();
263  while(i != d_btp_func_list.end()) {
264  if (name == (*i).first && (*f = (*i).second)) {
265  return true;
266  }
267  ++i;
268  }
269 
270  return false;
271 #endif
272 
273  if (d_func_list.empty())
274  return false;
275  DBG(cerr << "ServerFunctionsList::find_function() - Looking for ServerFunction '" << name << "'" << endl);
276 
277  std::pair <std::multimap<string,libdap::ServerFunction *>::const_iterator, std::multimap<string,libdap::ServerFunction *>::const_iterator> ret;
278  ret = d_func_list.equal_range(name);
279  for (std::multimap<string,libdap::ServerFunction *>::const_iterator it=ret.first; it!=ret.second; ++it) {
280  if (name == it->first && (*f = it->second->get_btp_func())){
281  DBG(cerr << "ServerFunctionsList::find_function() - Found basetype function " << it->second->getName() << endl);
282  return true;
283  }
284  }
285 
286  return false;
287 
288 
289 
290 }
291 
312 bool ServerFunctionsList::find_function(const string &name, proj_func *f) const
313 {
314 
315 #if 0
316  if (d_proj_func_list.empty())
317  return false;
318 
319  map<string, proj_func>::const_iterator i = d_proj_func_list.begin();
320  while(i != d_proj_func_list.end()) {
321  if (name == (*i).first && (*f = (*i).second)) {
322  return true;
323  }
324  ++i;
325  }
326 
327  return false;
328 #endif
329 
330  if (d_func_list.empty())
331  return false;
332 
333  std::pair <std::multimap<string,libdap::ServerFunction *>::const_iterator, std::multimap<string,libdap::ServerFunction *>::const_iterator> ret;
334  ret = d_func_list.equal_range(name);
335  for (std::multimap<string,libdap::ServerFunction *>::const_iterator it=ret.first; it!=ret.second; ++it) {
336  if (name == it->first && (*f = it->second->get_proj_func())){
337  DBG(cerr << "ServerFunctionsList::find_function() - Found projection function " << it->second->getName() << endl);
338  return true;
339  }
340  }
341  return false;
342 
343 }
344 
345 
346 
348 std::multimap<string,libdap::ServerFunction *>::iterator ServerFunctionsList::begin()
349 {
350  return d_func_list.begin();
351 }
352 
354 std::multimap<string,libdap::ServerFunction *>::iterator ServerFunctionsList::end()
355 {
356  return d_func_list.end();
357 }
358 
359 
366 libdap::ServerFunction *ServerFunctionsList::getFunction(std::multimap<string,libdap::ServerFunction *>::iterator it)
367 {
368  return (*it).second;
369 }
370 
371 
372 
373 
374 
375 
376 #if 0
377 
384 void ServerFunctionsList::dump(ostream &strm) const
385 {
386  strm << BESIndent::LMarg << "ServerFunctionsList::dump - (" << (void *) this << ")" << endl;
387  BESIndent::Indent();
388 
389  if (d_btp_func_list.size() > 0) {
390  strm << BESIndent::LMarg << "registered btp functions:" << endl;
391  BESIndent::Indent();
392  map<string, btp_func>::const_iterator i = d_btp_func_list.begin();
393  map<string, btp_func>::const_iterator e = d_btp_func_list.end();
394  while (i != e) {
395  strm << (*i).first << endl;
396  ++i;
397  }
398  BESIndent::UnIndent();
399  }
400  else {
401  strm << BESIndent::LMarg << "registered btp functions: none" << endl;
402  }
403 
404  if (d_bool_func_list.size() > 0) {
405  strm << BESIndent::LMarg << "registered bool functions:" << endl;
406  BESIndent::Indent();
407  map<string, bool_func>::const_iterator i = d_bool_func_list.begin();
408  map<string, bool_func>::const_iterator e = d_bool_func_list.end();
409  while (i != e) {
410  strm << (*i).first << endl;
411  ++i;
412  }
413  BESIndent::UnIndent();
414  }
415  else {
416  strm << BESIndent::LMarg << "registered bool functions: none" << endl;
417  }
418 
419  if (d_proj_func_list.size() > 0) {
420  strm << BESIndent::LMarg << "registered projection functions:" << endl;
421  BESIndent::Indent();
422  map<string, proj_func>::const_iterator i = d_proj_func_list.begin();
423  map<string, proj_func>::const_iterator e = d_proj_func_list.end();
424  while (i != e) {
425  strm << (*i).first << endl;
426  ++i;
427  }
428  BESIndent::UnIndent();
429  }
430  else {
431  strm << BESIndent::LMarg << "registered projection functions: none" << endl;
432  }
433 
434  BESIndent::UnIndent();
435 }
436 
438 ServerFunctionsList::TheList()
439 {
440  if (d_instance == 0) {
441  d_instance = new ServerFunctionsList;
442  }
443  return d_instance;
444 }
445 
446 #endif
447 
448 void ServerFunctionsList::getFunctionNames(vector<string> *names){
449  std::multimap<string,libdap::ServerFunction *>::iterator fit;
450  for(fit=d_func_list.begin(); fit!=d_func_list.end() ; fit++){
451  ServerFunction *func = fit->second;
452  names->push_back(func->getName());
453  }
454 }
455 
456 
457 }
#define DBG(x)
Definition: debug.h:58
Evaluate a constraint expression.