libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
Clause.cc
Go to the documentation of this file.
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2002,2003 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26// (c) COPYRIGHT URI/MIT 1996,1998,1999
27// Please first read the full copyright statement in the file COPYRIGHT_URI.
28//
29// Authors:
30// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31
32// Implementation for the CE Clause class.
33
34#include "config.h"
35
36#include <algorithm>
37#include <cassert>
38
39#include "Byte.h"
40#include "Clause.h"
41#include "DDS.h"
42#include "Int16.h"
43#include "Int32.h"
44#include "UInt16.h"
45#include "UInt32.h"
46#include "expr.h"
47
48using std::cerr;
49using std::endl;
50
51namespace libdap {
52
53Clause::Clause(const int oper, rvalue *a1, rvalue_list *rv)
54 : _op(oper), _b_func(0), _bt_func(0), _argc(0), _arg1(a1), _args(rv) {
55 assert(OK());
56}
57#if 1
58Clause::Clause(bool_func func, rvalue_list *rv) : _op(0), _b_func(func), _bt_func(0), _argc(0), _arg1(0), _args(rv) {
59 assert(OK());
60
61 if (_args) // account for null arg list
62 _argc = _args->size();
63 else
64 _argc = 0;
65}
66#endif
67Clause::Clause(btp_func func, rvalue_list *rv) : _op(0), _b_func(0), _bt_func(func), _argc(0), _arg1(0), _args(rv) {
68 assert(OK());
69
70 if (_args)
71 _argc = _args->size();
72 else
73 _argc = 0;
74}
75
76Clause::Clause() : _op(0), _b_func(0), _bt_func(0), _argc(0), _arg1(0), _args(0) {}
77
78static inline void delete_rvalue(rvalue *rv) {
79 delete rv;
80 rv = 0;
81}
82
84 if (_arg1) {
85 delete _arg1;
86 _arg1 = 0;
87 }
88
89 if (_args) {
90 // _args is a pointer to a vector<rvalue*> and we must must delete
91 // each rvalue pointer here explicitly. 02/03/04 jhrg
92 for_each(_args->begin(), _args->end(), delete_rvalue);
93 delete _args;
94 _args = 0;
95 }
96}
97
99bool Clause::OK() {
100 // Each clause object can contain one of: a relational clause, a boolean
101 // function clause or a BaseType pointer function clause. It must have a
102 // valid argument list.
103 //
104 // But, a valid arg list might contain zero arguments! 10/16/98 jhrg
105 bool relational = (_op && !_b_func && !_bt_func);
106#if 1
107 bool boolean = (!_op && _b_func && !_bt_func);
108#endif
109 bool basetype = (!_op && !_b_func && _bt_func);
110
111 if (relational)
112 return _arg1 && _args;
113 else if (boolean || basetype)
114 return true; // Until we check arguments...10/16/98 jhrg
115 else
116 return false;
117}
118
121 assert(OK());
122
123 return _op || _b_func;
124}
125
128 assert(OK());
129
130 return (_bt_func != 0);
131}
132
143bool Clause::value(DDS &dds) {
144 assert(OK());
145 assert(_op || _b_func);
146
147 if (_op) { // Is it a relational clause?
148 // rvalue::bvalue(...) returns the rvalue encapsulated in a
149 // BaseType *.
150 BaseType *btp = _arg1->bvalue(dds);
151 // The list of rvalues is an implicit logical OR, so assume
152 // FALSE and return TRUE for the first TRUE subclause.
153 bool result = false;
154 for (rvalue_list_iter i = _args->begin(); i != _args->end() && !result; i++) {
155 result = result || btp->ops((*i)->bvalue(dds), _op);
156 }
157
158 return result;
159 } else if (_b_func) { // ...A bool function?
160 BaseType **argv = build_btp_args(_args, dds);
161
162 bool result = false;
163 (*_b_func)(_argc, argv, dds, &result);
164 delete[] argv; // Cache me!
165 argv = 0;
166
167 return result;
168 } else {
169 throw InternalErr(__FILE__, __LINE__, "A selection expression must contain only boolean clauses.");
170 }
171}
172
186 assert(OK());
187 assert(_bt_func);
188
189 if (_bt_func) {
190 // build_btp_args() is a function defined in RValue.cc. It no longer
191 // reads the values as it builds the arguments, that is now left up
192 // to the functions themselves. 9/25/06 jhrg
193 BaseType **argv = build_btp_args(_args, dds);
194
195 (*_bt_func)(_argc, argv, dds, value);
196
197 delete[] argv; // Cache me!
198 argv = 0;
199
200 if (*value) {
201 // FIXME This comment is likely wrong... 10/19/12
202 // This call to set_send_p was removed because new logic used
203 // in ResponseBuilder will handle it. See send_data(), ...
204 // When the second part of the CE is parsed, if it is null,
205 // then all the variables in the DDS that holds the function
206 // result variables will be sent. If there's a projection in
207 // that second CE, it will denote what is to be sent. Setting
208 // set_send_p(true) here had the affect of overriding that
209 // second CE. Note, however, that the code in send_data() clears
210 // all of the send_p properties for variables in the DDS, so
211 // removing the call here is just removing something that will
212 // actually have no affect. jhrg 10/19/12
213 (*value)->set_send_p(true);
214 (*value)->set_read_p(true);
215 return true;
216 } else {
217 return false;
218 }
219 } else {
220 throw InternalErr(__FILE__, __LINE__,
221 "Clause::value() was called in a context expecting a BaseType pointer return, but the Clause "
222 "was boolean-valued instead.");
223 }
224}
225
226} // namespace libdap
The basic data type for the DODS DAP types.
Definition BaseType.h:118
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition BaseType.cc:1101
A class for software fault reporting.
Definition InternalErr.h:61
top level DAP object to house generic methods
Definition AISConnect.cc:30
std::vector< rvalue * > rvalue_list
Definition RValue.h:69
std::vector< rvalue * >::iterator rvalue_list_iter
Definition RValue.h:71
BaseType ** build_btp_args(const rvalue_list *args, DDS &dds)
Definition RValue.cc:82
bool value_clause()
Return true if the clause returns a value in a BaseType pointer.
Definition Clause.cc:127
bool boolean_clause()
Return true if the clause returns a boolean value.
Definition Clause.cc:120
virtual ~Clause()
Definition Clause.cc:83
bool value(DDS &dds)
Evaluate a clause which returns a boolean value This method must only be evaluated for clauses with r...
Definition Clause.cc:143
bool OK()
Checks the "representation invariant" of a clause.
Definition Clause.cc:99