libdap++  Updated for version 3.8.2
RValue.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-1999
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31 
32 // This file contains mfuncs defined for struct rvalue (see expr.h) that
33 // *cannot* be included in that struct's declaration because their
34 // definitions must follow *both* rvalue's and func_rvalue's declarations.
35 // jhrg 3/4/96
36 
37 #include "config.h"
38 
39 static char rcsid[] not_used =
40  {"$Id: RValue.cc 21699 2009-11-05 00:06:01Z jimg $"
41  };
42 
43 #include <cassert>
44 #include <iostream>
45 
46 #include "BaseType.h"
47 #include "expr.h"
48 #include "RValue.h"
49 #include "DDS.h"
50 #include "dods-limits.h"
51 #include "util.h"
52 
53 using namespace std;
54 
55 namespace libdap {
56 
59 {
60  assert(rv);
61 
62  rvalue_list *rvals = new rvalue_list;
63 
64  return append_rvalue_list(rvals, rv);
65 }
66 
67 // Given a rvalue_list pointer RVALS and a value pointer VAL, make a variable
68 // to hold VAL and append that variable to the list RVALS.
69 //
70 // Returns: A pointer to the updated rvalue_list.
71 
74 {
75  rvals->push_back(rv);
76 
77  return rvals;
78 }
79 
80 
92 BaseType **
94 {
95  int argc = 0;
96 
97  if (args)
98  argc = args->size();
99 
100  // Sanitize allocation size
101  if (!size_ok(sizeof(BaseType*), argc + 1))
102  throw Error(malformed_expr, string("Malformed argument list (")
103  + long_to_string(argc) + string(")."));
104 
105  // Add space for a null terminator
106  BaseType **argv = new BaseType*[argc + 1];
107 
108  int index = 0;
109  if (argv && argc) {
110  for (rvalue::Args_iter i = args->begin(); i != args->end() && index
111  < argc + 1; ++i)
112  argv[index++] = (*i)->bvalue(dds);
113  }
114 
115  if (index != argc) {
116  delete[] argv;
117  throw InternalErr(__FILE__, __LINE__, "index out of range.");
118  }
119 
120  argv[index] = 0; // Add the null terminator.
121 
122  return argv;
123 }
124 
125 rvalue::rvalue(BaseType *bt): d_value(bt), d_func(0), d_args(0)
126 {}
127 
128 rvalue::rvalue(btp_func f, vector<rvalue *> *a) : d_value(0), d_func(f), d_args(a)
129 {}
130 
131 rvalue::rvalue(): d_value(0), d_func(0), d_args(0)
132 {}
133 
135 {
136  // Deleting the BaseType pointers in value and args is a bad idea since
137  // those might be variables in the dataset. The DDS dtor will take care
138  // of deleting them. The constants wrapped in BaseType objects should be
139  // pushed on the list of CE-allocated temp objects which the CE frees.
140 }
141 
142 string
144 {
145  assert(d_value);
146 
147  return d_value->name();
148 }
149 
159 BaseType *
161 {
162  if (d_value) { // i.e., if this RValue is a BaseType
163  return d_value;
164  }
165  else if (d_func) {
166  // If func is true, then args must be set. See the constructor.
167  // 12/23/04 jhrg
168  BaseType **argv = build_btp_args(d_args, dds);
169  BaseType *ret_val;
170  (*d_func)(d_args->size(), argv, dds, &ret_val);
171  delete[] argv;
172  return ret_val;
173  }
174  else {
175  return 0;
176  }
177 }
178 
179 } // namespace libdap
180 
BaseType * bvalue(DDS &dds)
Definition: RValue.cc:160
rvalue_list * append_rvalue_list(rvalue_list *rvals, rvalue *rv)
Definition: RValue.cc:73
string name() const
Returns the name of the class instance.
Definition: BaseType.cc:210
std::vector< rvalue * > rvalue_list
Definition: RValue.h:67
#define not_used
Definition: config.h:521
#define malformed_expr
Definition: Error.h:64
virtual ~rvalue()
Definition: RValue.cc:134
bool size_ok(unsigned int sz, unsigned int nelem)
sanitize the size of an array. Test for integer overflow when dynamically allocating an array...
Definition: util.cc:523
A class for software fault reporting.
Definition: InternalErr.h:64
string value_name()
Definition: RValue.cc:143
BaseType ** build_btp_args(rvalue_list *args, DDS &dds)
Definition: RValue.cc:93
string long_to_string(long val, int base)
Definition: util.cc:440
rvalue_list * make_rvalue_list(rvalue *rv)
Definition: RValue.cc:58
The basic data type for the DODS DAP types.
Definition: BaseType.h:190
A class for error processing.
Definition: Error.h:90
std::vector< rvalue * >::iterator Args_iter
Definition: RValue.h:52