libdap++  Updated for version 3.8.2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 
35 #include "config.h"
36 
37 #include <cassert>
38 #include <algorithm>
39 
40 #include "expr.h"
41 #include "Byte.h"
42 #include "Int16.h"
43 #include "UInt16.h"
44 #include "Int32.h"
45 #include "UInt32.h"
46 #include "DDS.h"
47 #include "Clause.h"
48 
49 using std::cerr;
50 using std::endl;
51 
52 namespace libdap {
53 
54 Clause::Clause(const int oper, rvalue *a1, rvalue_list *rv)
55  : _op(oper), _b_func(0), _bt_func(0), _arg1(a1), _args(rv)
56 {
57  assert(OK());
58 }
59 #if 1
60 Clause::Clause(bool_func func, rvalue_list *rv)
61  : _op(0), _b_func(func), _bt_func(0), _arg1(0), _args(rv)
62 {
63  assert(OK());
64 
65  if (_args) // account for null arg list
66  _argc = _args->size();
67  else
68  _argc = 0;
69 }
70 #endif
71 Clause::Clause(btp_func func, rvalue_list *rv)
72  : _op(0), _b_func(0), _bt_func(func), _arg1(0), _args(rv)
73 {
74  assert(OK());
75 
76  if (_args)
77  _argc = _args->size();
78  else
79  _argc = 0;
80 }
81 
82 Clause::Clause() : _op(0), _b_func(0), _bt_func(0), _arg1(0), _args(0)
83 {}
84 
85 static inline void
86 delete_rvalue(rvalue *rv)
87 {
88  delete rv; rv = 0;
89 }
90 
92 {
93  if (_arg1) {
94  delete _arg1; _arg1 = 0;
95  }
96 
97  if (_args) {
98  // _args is a pointer to a vector<rvalue*> and we must must delete
99  // each rvalue pointer here explicitly. 02/03/04 jhrg
100  for_each(_args->begin(), _args->end(), delete_rvalue);
101  delete _args; _args = 0;
102  }
103 }
104 
106 bool
108 {
109  // Each clause object can contain one of: a relational clause, a boolean
110  // function clause or a BaseType pointer function clause. It must have a
111  // valid argument list.
112  //
113  // But, a valid arg list might contain zero arguments! 10/16/98 jhrg
114  bool relational = (_op && !_b_func && !_bt_func);
115 #if 1
116  bool boolean = (!_op && _b_func && !_bt_func);
117 #endif
118  bool basetype = (!_op && !_b_func && _bt_func);
119 
120  if (relational)
121  return _arg1 && _args;
122  else if (boolean || basetype)
123  return true; // Until we check arguments...10/16/98 jhrg
124  else
125  return false;
126 }
127 
129 bool
131 {
132  assert(OK());
133 
134  return _op || _b_func;
135 }
136 
138 bool
140 {
141  assert(OK());
142 
143  return (_bt_func != 0);
144 }
145 
155 bool
157 {
158  assert(OK());
159  assert(_op || _b_func);
160 
161  if (_op) { // Is it a relational clause?
162  // rvalue::bvalue(...) returns the rvalue encapsulated in a
163  // BaseType *.
164  BaseType *btp = _arg1->bvalue(dds);
165  // The list of rvalues is an implicit logical OR, so assume
166  // FALSE and return TRUE for the first TRUE subclause.
167  bool result = false;
168  for (rvalue_list_iter i = _args->begin();
169  i != _args->end() && !result;
170  i++) {
171  result = result || btp->ops((*i)->bvalue(dds), _op);
172  }
173 
174  return result;
175  }
176  else if (_b_func) { // ...A bool function?
177  BaseType **argv = build_btp_args(_args, dds);
178 
179  bool result = false;
180  (*_b_func)(_argc, argv, dds, &result);
181  delete[] argv; // Cache me!
182  argv = 0;
183 
184  return result;
185  }
186  else {
187  throw InternalErr(__FILE__, __LINE__,
188  "A selection expression must contain only boolean clauses.");
189  }
190 }
191 
203 bool
205 {
206  assert(OK());
207  assert(_bt_func);
208 
209  if (_bt_func) {
210  // build_btp_args() is a function defined in RValue.cc. It no longer
211  // reads the values as it builds the arguments, that is now left up
212  // to the functions themselves. 9/25/06 jhrg
213  BaseType **argv = build_btp_args(_args, dds);
214 
215  (*_bt_func)(_argc, argv, dds, value);
216 
217  delete[] argv; // Cache me!
218  argv = 0;
219 
220  if (*value) {
221  (*value)->set_send_p(true);
222  (*value)->set_read_p(true);
223  return true;
224  }
225  else {
226  return false;
227  }
228  }
229  else {
230  throw InternalErr(__FILE__, __LINE__,
231  "Clause::value() was called in a context expecting a BaseType pointer return, but the Clause was boolean-valued instead.");
232  }
233 }
234 
235 } // namespace libdap
BaseType * bvalue(DDS &dds)
Definition: RValue.cc:160
std::vector< rvalue * > rvalue_list
Definition: RValue.h:67
std::vector< rvalue * >::iterator rvalue_list_iter
Definition: RValue.h:69
A class for software fault reporting.
Definition: InternalErr.h:64
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:156
BaseType ** build_btp_args(rvalue_list *args, DDS &dds)
Definition: RValue.cc:93
The basic data type for the DODS DAP types.
Definition: BaseType.h:190
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: BaseType.cc:1081
bool OK()
Checks the "representation invariant" of a clause.
Definition: Clause.cc:107
bool value_clause()
Return true if the clause returns a value in a BaseType pointer.
Definition: Clause.cc:139
bool boolean_clause()
Return true if the clause returns a boolean value.
Definition: Clause.cc:130
virtual ~Clause()
Definition: Clause.cc:91