libdap  Updated for version 3.17.2
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 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  SFLIter fit;
83  for(fit=d_func_list.begin(); fit!=d_func_list.end() ; fit++){
84  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 
132 bool ServerFunctionsList::find_function(const std::string &name, bool_func *f) const
133 {
134  if (d_func_list.empty())
135  return false;
136 
137  std::pair <SFLCIter, SFLCIter> ret;
138  ret = d_func_list.equal_range(name);
139  for (SFLCIter it = ret.first; it != ret.second; ++it) {
140  if (name == it->first && (*f = it->second->get_bool_func())){
141  DBG(cerr << "ServerFunctionsList::find_function() - Found boolean function " << it->second->getName() << endl);
142  return true;
143  }
144  }
145 
146  return false;
147 }
148 
169 bool ServerFunctionsList::find_function(const string &name, btp_func *f) const
170 {
171  if (d_func_list.empty())
172  return false;
173  DBG(cerr << "ServerFunctionsList::find_function() - Looking for ServerFunction '" << name << "'" << endl);
174 
175  std::pair <SFLCIter, SFLCIter> ret;
176  ret = d_func_list.equal_range(name);
177  for (SFLCIter it = ret.first; it != ret.second; ++it) {
178  if (name == it->first && (*f = it->second->get_btp_func())){
179  DBG(cerr << "ServerFunctionsList::find_function() - Found basetype function " << it->second->getName() << endl);
180  return true;
181  }
182  }
183 
184  return false;
185 }
186 
207 bool ServerFunctionsList::find_function(const string &name, proj_func *f) const
208 {
209  if (d_func_list.empty())
210  return false;
211 
212  std::pair <SFLCIter, SFLCIter> ret;
213  ret = d_func_list.equal_range(name);
214  for (SFLCIter it = ret.first; it != ret.second; ++it) {
215  if (name == it->first && (*f = it->second->get_proj_func())){
216  DBG(cerr << "ServerFunctionsList::find_function() - Found projection function " << it->second->getName() << endl);
217  return true;
218  }
219  }
220 
221  return false;
222 }
223 
231 bool ServerFunctionsList::find_function(const string &name, D4Function *f) const
232 {
233  if (d_func_list.empty())
234  return false;
235 
236  std::pair <SFLCIter, SFLCIter> ret;
237  ret = d_func_list.equal_range(name);
238  for (SFLCIter it = ret.first; it != ret.second; ++it) {
239  if (name == it->first && (*f = it->second->get_d4_function())) {
240  return true;
241  }
242  }
243 
244  return false;
245 }
246 
248 ServerFunctionsList::SFLIter ServerFunctionsList::begin()
249 {
250  return d_func_list.begin();
251 }
252 
254 ServerFunctionsList::SFLIter ServerFunctionsList::end()
255 {
256  return d_func_list.end();
257 }
258 
265 ServerFunction *ServerFunctionsList::getFunction(SFLIter it)
266 {
267  return (*it).second;
268 }
269 
270 void ServerFunctionsList::getFunctionNames(vector<string> *names){
271  SFLIter fit;
272  for(fit = d_func_list.begin(); fit != d_func_list.end(); fit++) {
273  ServerFunction *func = fit->second;
274  names->push_back(func->getName());
275  }
276 }
277 
278 } // namespace libdap
STL namespace.