libdap  Updated for version 3.17.0
ServerFunctionsList.cc
00001 // ServerFunctionsList.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2013 OPeNDAP, Inc.
00007 // Author: James Gallagher <jgallagher@opendap.org>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 //
00023 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00024 
00025 #include "config.h"
00026 
00027 #ifdef HAVE_STDLIB_H
00028 #include <stdlib.h>
00029 #endif
00030 
00031 #include <pthread.h>
00032 
00033 #include <iostream>
00034 #include <algorithm>
00035 
00036 //#define DODS_DEBUG
00037 
00038 #include <expr.h>
00039 #include "debug.h"
00040 
00041 #include "ServerFunctionsList.h"
00042 
00043 using std::cerr;
00044 using std::string;
00045 using std::endl;
00046 using namespace std;
00047 using namespace libdap;
00048 
00049 namespace libdap {
00050 
00051 static pthread_once_t ServerFunctionsList_instance_control = PTHREAD_ONCE_INIT;
00052 
00053 ServerFunctionsList *ServerFunctionsList::d_instance = 0 ;
00054 
00058 void ServerFunctionsList::initialize_instance() {
00059     if (d_instance == 0) {
00060         DBG(cerr << "ServerFunctionsList::initialize_instance() - Creating singleton ServerFunctionList instance." << endl);
00061         d_instance = new ServerFunctionsList;
00062         #if HAVE_ATEXIT
00063             atexit(delete_instance);
00064         #endif
00065     }
00066 }
00067 
00071 void ServerFunctionsList::delete_instance() {
00072     DBG(cerr << "ServerFunctionsList::delete_instance() - Deleting singleton ServerFunctionList instance." << endl);
00073     delete d_instance;
00074     d_instance = 0;
00075 }
00076 
00081 ServerFunctionsList::~ServerFunctionsList() {
00082     SFLIter fit;
00083     for(fit=d_func_list.begin(); fit!=d_func_list.end() ; fit++){
00084         ServerFunction *func = fit->second;
00085         DBG(cerr << "ServerFunctionsList::~ServerFunctionsList() - Deleting ServerFunction " << func->getName() << " from ServerFunctionsList." << endl);
00086         delete func;
00087     }
00088     d_func_list.clear();
00089 }
00090 
00091 ServerFunctionsList * ServerFunctionsList::TheList() {
00092     pthread_once(&ServerFunctionsList_instance_control, initialize_instance);
00093     DBG(cerr << "ServerFunctionsList::TheList() - Returning singleton ServerFunctionList instance." << endl);
00094     return d_instance;
00095 }
00096 
00106 void ServerFunctionsList::add_function(ServerFunction *func )
00107 {
00108     DBG(cerr << "ServerFunctionsList::add_function() - Adding ServerFunction " << func->getName() << endl);
00109     d_func_list.insert(std::make_pair(func->getName(),func));
00110 }
00111 
00132 bool ServerFunctionsList::find_function(const std::string &name, bool_func *f) const
00133 {
00134     if (d_func_list.empty())
00135         return false;
00136 
00137     std::pair <SFLCIter, SFLCIter> ret;
00138     ret = d_func_list.equal_range(name);
00139     for (SFLCIter it = ret.first; it != ret.second; ++it) {
00140         if (name == it->first && (*f = it->second->get_bool_func())){
00141             DBG(cerr << "ServerFunctionsList::find_function() - Found boolean function " << it->second->getName() << endl);
00142             return true;
00143         }
00144     }
00145 
00146     return false;
00147 }
00148 
00169 bool ServerFunctionsList::find_function(const string &name, btp_func *f) const
00170 {
00171     if (d_func_list.empty())
00172         return false;
00173     DBG(cerr << "ServerFunctionsList::find_function() - Looking for ServerFunction '" << name << "'" << endl);
00174 
00175     std::pair <SFLCIter, SFLCIter> ret;
00176     ret = d_func_list.equal_range(name);
00177     for (SFLCIter it = ret.first; it != ret.second; ++it) {
00178         if (name == it->first && (*f = it->second->get_btp_func())){
00179             DBG(cerr << "ServerFunctionsList::find_function() - Found basetype function " << it->second->getName() << endl);
00180             return true;
00181         }
00182     }
00183 
00184     return false;
00185 }
00186 
00207 bool ServerFunctionsList::find_function(const string &name, proj_func *f) const
00208 {
00209     if (d_func_list.empty())
00210         return false;
00211 
00212     std::pair <SFLCIter, SFLCIter> ret;
00213     ret = d_func_list.equal_range(name);
00214     for (SFLCIter it = ret.first; it != ret.second; ++it) {
00215         if (name == it->first && (*f = it->second->get_proj_func())){
00216             DBG(cerr << "ServerFunctionsList::find_function() - Found projection function " << it->second->getName() << endl);
00217            return true;
00218         }
00219     }
00220 
00221     return false;
00222 }
00223 
00231 bool ServerFunctionsList::find_function(const string &name, D4Function *f) const
00232 {
00233     if (d_func_list.empty())
00234         return false;
00235 
00236     std::pair <SFLCIter, SFLCIter> ret;
00237     ret = d_func_list.equal_range(name);
00238     for (SFLCIter it = ret.first; it != ret.second; ++it) {
00239         if (name == it->first && (*f = it->second->get_d4_function())) {
00240             return true;
00241         }
00242     }
00243 
00244     return false;
00245 }
00246 
00248 ServerFunctionsList::SFLIter ServerFunctionsList::begin()
00249 {
00250     return d_func_list.begin();
00251 }
00252 
00254 ServerFunctionsList::SFLIter ServerFunctionsList::end()
00255 {
00256     return d_func_list.end();
00257 }
00258 
00265 ServerFunction *ServerFunctionsList::getFunction(SFLIter it)
00266 {
00267     return (*it).second;
00268 }
00269 
00270 void ServerFunctionsList::getFunctionNames(vector<string> *names){
00271         SFLIter fit;
00272     for(fit = d_func_list.begin(); fit != d_func_list.end(); fit++) {
00273         ServerFunction *func = fit->second;
00274         names->push_back(func->getName());
00275     }
00276 }
00277 
00278 } // namespace libdap