1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// ServerFunctionsList.cc
// This file is part of bes, A C++ back-end server implementation framework
// for the OPeNDAP Data Access Protocol.
// Copyright (c) 2013 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
#include "config.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <pthread.h>
#include <iostream>
#include <algorithm>
//#define DODS_DEBUG
#include <expr.h>
#include "debug.h"
#include "ServerFunctionsList.h"
using namespace std;
using namespace libdap;
namespace libdap {
static pthread_once_t ServerFunctionsList_instance_control = PTHREAD_ONCE_INIT;
ServerFunctionsList *ServerFunctionsList::d_instance = 0 ;
/**
* private static that only gets called once in the life cycle of the process.
*/
void ServerFunctionsList::initialize_instance() {
if (d_instance == 0) {
DBG(cerr << "ServerFunctionsList::initialize_instance() - Creating singleton ServerFunctionList instance." << endl);
d_instance = new ServerFunctionsList;
#if HAVE_ATEXIT
atexit(delete_instance);
#endif
}
}
/**
* Private static function can only be called by friends andf pThreads code.
*/
void ServerFunctionsList::delete_instance() {
DBG(cerr << "ServerFunctionsList::delete_instance() - Deleting singleton ServerFunctionList instance." << endl);
delete d_instance;
d_instance = 0;
}
/**
* Private method insures that nobody can try to delete the singleton class.
*/
ServerFunctionsList::~ServerFunctionsList() {
SFLIter fit;
for(fit=d_func_list.begin(); fit!=d_func_list.end() ; fit++){
ServerFunction *func = fit->second;
DBG(cerr << "ServerFunctionsList::~ServerFunctionsList() - Deleting ServerFunction " << func->getName() << " from ServerFunctionsList." << endl);
delete func;
}
d_func_list.clear();
}
ServerFunctionsList * ServerFunctionsList::TheList() {
pthread_once(&ServerFunctionsList_instance_control, initialize_instance);
DBG(cerr << "ServerFunctionsList::TheList() - Returning singleton ServerFunctionList instance." << endl);
return d_instance;
}
/**
* Adds the passed ServerFunction pointer to the list of ServerFunctions using
* the value of ServerFunction.getName() as the key in the list.
*
* @brief Adds the passed ServerFunction pointer to the list of ServerFunctions.
* @param *func A pointer to the ServerFunction object to add to the ServerFunctionList.
* The pointer is copied, not the object referenced; this class does not
* delete the pointer.
*/
void ServerFunctionsList::add_function(ServerFunction *func )
{
DBG(cerr << "ServerFunctionsList::add_function() - Adding ServerFunction " << func->getName() << endl);
d_func_list.insert(std::make_pair(func->getName(),func));
}
/**
* Returns the first boolean function in the list whose key value matches the passed string name.
* When a match is found the function returns true and sets returned value parameter *f to
* the boolean function held by the ServerFunction object extracted from the list.
*
* Method:
* Looks through the list of ServerFunctions and compares each function's key value (which
* would be the value of SurverFunction.getName()) with the value of the string parameter
* 'name'. When they match then the returned value parameter is set to the value returned
* by ServerFunction.get_btp_func(). If the ServerFunction _is not_ a instance of a boolean
* function then the return value will be 0 (null) and the search for matching function will continue.
* If the ServerFunction _is_ a boolean function then the returned value will be non-zero and
* the search will return true (it found the thing) and the returned value parameter *f will have
* it's value set to the boolean function.
*
* @brief Find a boolean function with a given name in the function list.
* @param name A string containing the name of the function to find.
* @param *f A returned value parameter through which a point to the desired function is returned.
*
*/
bool ServerFunctionsList::find_function(const std::string &name, bool_func *f) const
{
if (d_func_list.empty())
return false;
std::pair <SFLCIter, SFLCIter> ret;
ret = d_func_list.equal_range(name);
for (SFLCIter it = ret.first; it != ret.second; ++it) {
if (name == it->first && it->second->get_bool_func()) {
*f = it->second->get_bool_func();
return true;
}
}
return false;
}
/**
* Returns the first BaseType function in the list whose key value matches the passed string name.
* When a match is found the function returns true and sets returned value parameter *f to
* the BaseType function held by the ServerFunction object extracted from the list.
*
* Method:
* Looks through the list of ServerFunctions and compares each function's key value (which
* would be the value of SurverFunction.getName()) with the value of the string parameter
* 'name'. When they match then the returned value parameter is set to the value returned
* by ServerFunction.get_btp_func(). If the ServerFunction _is not_ a instance of a BaseType
* function then the return value will be 0 (null) and the search for matching function will continue.
* If the ServerFunction _is_ a BaseType function then the returned value will be non-zero and
* the search will return true (it found the thing) and the returned value parameter *f will have
* it's value set to the BaseType function.
*
* @brief Find a BaseType function with a given name in the function list.
* @param name A string containing the name of the function to find.
* @param *f A returned value parameter through which a point to the desired function is returned.
*
*/
bool ServerFunctionsList::find_function(const string &name, btp_func *f) const
{
if (d_func_list.empty())
return false;
DBG(cerr << "ServerFunctionsList::find_function() - Looking for ServerFunction '" << name << "'" << endl);
std::pair <SFLCIter, SFLCIter> ret;
ret = d_func_list.equal_range(name);
for (SFLCIter it = ret.first; it != ret.second; ++it) {
if (name == it->first && it->second->get_btp_func()) {
*f = it->second->get_btp_func();
return true;
}
}
return false;
}
/**
* Returns the first projection function in the list whose key value matches the passed string name.
* When a match is found the function returns true and sets returned value parameter *f to
* the projection function held by the ServerFunction object extracted from the list.
*
* Method:
* Looks through the list of ServerFunctions and looks at each function's key value (which
* would be the value of SurverFunction.getName() for each function). When a function has the same
* key name as the value of the string parameter 'name', then the returned value parameter is set
* the value returned by ServerFunction.get_proj_func(). If the ServerFunction _is not_ a projection
* function then the return value will be 0 (null) and the search for matching function will continue.
* If the ServerFunction _is_ a projection then the returned value will be non-zero and the search will
* return true (it found the thing) and the returned value parameter *f will have it's value set
* to the projection function.
*
* @brief Find a projection function with a given name in the function list.
* @param name A string containing the name of the function to find.
* @param *f A returned value parameter through which a point to the desired function is returned.
*
*/
bool ServerFunctionsList::find_function(const string &name, proj_func *f) const
{
if (d_func_list.empty())
return false;
std::pair <SFLCIter, SFLCIter> ret;
ret = d_func_list.equal_range(name);
for (SFLCIter it = ret.first; it != ret.second; ++it) {
if (name == it->first && it->second->get_proj_func()) {
*f = it->second->get_proj_func();
return true;
}
}
return false;
}
/**
* Find a DAP4 function in the Server Functions List.
*
* @param name Look for this function name
* @param f Value-result parameter. NULL if the function is not found
* @return True if the function was found, otherwise false.
*/
bool ServerFunctionsList::find_function(const string &name, D4Function *f) const
{
if (d_func_list.empty())
return false;
std::pair <SFLCIter, SFLCIter> ret;
ret = d_func_list.equal_range(name);
for (SFLCIter it = ret.first; it != ret.second; ++it) {
if (name == it->first && it->second->get_d4_function()) {
*f = it->second->get_d4_function();
return true;
}
}
return false;
}
/** @brief Returns an iterator pointing to the first key pair in the ServerFunctionList. */
ServerFunctionsList::SFLIter ServerFunctionsList::begin()
{
return d_func_list.begin();
}
/** @brief Returns an iterator pointing to the last key pair in the ServerFunctionList. */
ServerFunctionsList::SFLIter ServerFunctionsList::end()
{
return d_func_list.end();
}
/**
*
*
* @brief Returns the ServerFunction pointed to by the passed iterator.
*
*/
ServerFunction *ServerFunctionsList::getFunction(SFLIter it)
{
return (*it).second;
}
void ServerFunctionsList::getFunctionNames(vector<string> *names){
SFLIter fit;
for(fit = d_func_list.begin(); fit != d_func_list.end(); fit++) {
ServerFunction *func = fit->second;
names->push_back(func->getName());
}
}
} // namespace libdap
|