libdap  Updated for version 3.20.6
libdap4 is an implementation of OPeNDAP's DAP protocol.
ServerFunctionsList.cc
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 namespace std;
44 using namespace libdap;
45 
46 namespace libdap {
47 
48 static pthread_once_t ServerFunctionsList_instance_control = PTHREAD_ONCE_INIT;
49 
50 ServerFunctionsList *ServerFunctionsList::d_instance = 0 ;
51 
55 void ServerFunctionsList::initialize_instance() {
56  if (d_instance == 0) {
57  DBG(cerr << "ServerFunctionsList::initialize_instance() - Creating singleton ServerFunctionList instance." << endl);
58  d_instance = new ServerFunctionsList;
59  #if HAVE_ATEXIT
60  atexit(delete_instance);
61  #endif
62  }
63 }
64 
68 void ServerFunctionsList::delete_instance() {
69  DBG(cerr << "ServerFunctionsList::delete_instance() - Deleting singleton ServerFunctionList instance." << endl);
70  delete d_instance;
71  d_instance = 0;
72 }
73 
78 ServerFunctionsList::~ServerFunctionsList() {
79  SFLIter fit;
80  for(fit=d_func_list.begin(); fit!=d_func_list.end() ; fit++){
81  ServerFunction *func = fit->second;
82  DBG(cerr << "ServerFunctionsList::~ServerFunctionsList() - Deleting ServerFunction " << func->getName() << " from ServerFunctionsList." << endl);
83  delete func;
84  }
85  d_func_list.clear();
86 }
87 
88 ServerFunctionsList * ServerFunctionsList::TheList() {
89  pthread_once(&ServerFunctionsList_instance_control, initialize_instance);
90  DBG(cerr << "ServerFunctionsList::TheList() - Returning singleton ServerFunctionList instance." << endl);
91  return d_instance;
92 }
93 
103 void ServerFunctionsList::add_function(ServerFunction *func )
104 {
105  DBG(cerr << "ServerFunctionsList::add_function() - Adding ServerFunction " << func->getName() << endl);
106  d_func_list.insert(std::make_pair(func->getName(),func));
107 }
108 
129 bool ServerFunctionsList::find_function(const std::string &name, bool_func *f) const
130 {
131  if (d_func_list.empty())
132  return false;
133 
134  std::pair <SFLCIter, SFLCIter> ret;
135  ret = d_func_list.equal_range(name);
136  for (SFLCIter it = ret.first; it != ret.second; ++it) {
137  if (name == it->first && (*f = it->second->get_bool_func())){
138  DBG(cerr << "ServerFunctionsList::find_function() - Found boolean function " << it->second->getName() << endl);
139  return true;
140  }
141  }
142 
143  return false;
144 }
145 
166 bool ServerFunctionsList::find_function(const string &name, btp_func *f) const
167 {
168  if (d_func_list.empty())
169  return false;
170  DBG(cerr << "ServerFunctionsList::find_function() - Looking for ServerFunction '" << name << "'" << endl);
171 
172  std::pair <SFLCIter, SFLCIter> ret;
173  ret = d_func_list.equal_range(name);
174  for (SFLCIter it = ret.first; it != ret.second; ++it) {
175  if (name == it->first && (*f = it->second->get_btp_func())){
176  DBG(cerr << "ServerFunctionsList::find_function() - Found basetype function " << it->second->getName() << endl);
177  return true;
178  }
179  }
180 
181  return false;
182 }
183 
204 bool ServerFunctionsList::find_function(const string &name, proj_func *f) const
205 {
206  if (d_func_list.empty())
207  return false;
208 
209  std::pair <SFLCIter, SFLCIter> ret;
210  ret = d_func_list.equal_range(name);
211  for (SFLCIter it = ret.first; it != ret.second; ++it) {
212  if (name == it->first && (*f = it->second->get_proj_func())){
213  DBG(cerr << "ServerFunctionsList::find_function() - Found projection function " << it->second->getName() << endl);
214  return true;
215  }
216  }
217 
218  return false;
219 }
220 
228 bool ServerFunctionsList::find_function(const string &name, D4Function *f) const
229 {
230  if (d_func_list.empty())
231  return false;
232 
233  std::pair <SFLCIter, SFLCIter> ret;
234  ret = d_func_list.equal_range(name);
235  for (SFLCIter it = ret.first; it != ret.second; ++it) {
236  if (name == it->first && (*f = it->second->get_d4_function())) {
237  return true;
238  }
239  }
240 
241  return false;
242 }
243 
245 ServerFunctionsList::SFLIter ServerFunctionsList::begin()
246 {
247  return d_func_list.begin();
248 }
249 
251 ServerFunctionsList::SFLIter ServerFunctionsList::end()
252 {
253  return d_func_list.end();
254 }
255 
262 ServerFunction *ServerFunctionsList::getFunction(SFLIter it)
263 {
264  return (*it).second;
265 }
266 
267 void ServerFunctionsList::getFunctionNames(vector<string> *names){
268  SFLIter fit;
269  for(fit = d_func_list.begin(); fit != d_func_list.end(); fit++) {
270  ServerFunction *func = fit->second;
271  names->push_back(func->getName());
272  }
273 }
274 
275 } // namespace libdap
STL namespace.
top level DAP object to house generic methods
Definition: AISConnect.cc:30