libdap++ Updated for version 3.8.2

RValue.cc

Go to the documentation of this file.
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 // (c) COPYRIGHT URI/MIT 1996-1999
00027 // Please read the full copyright statement in the file COPYRIGHT_URI.
00028 //
00029 // Authors:
00030 //      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>
00031 
00032 // This file contains mfuncs defined for struct rvalue (see expr.h) that
00033 // *cannot* be included in that struct's declaration because their
00034 // definitions must follow *both* rvalue's and func_rvalue's declarations.
00035 // jhrg 3/4/96
00036 
00037 #include "config.h"
00038 
00039 static char rcsid[] not_used =
00040     {"$Id: RValue.cc 21699 2009-11-05 00:06:01Z jimg $"
00041     };
00042 
00043 #include <cassert>
00044 #include <iostream>
00045 
00046 #include "BaseType.h"
00047 #include "expr.h"
00048 #include "RValue.h"
00049 #include "DDS.h"
00050 #include "dods-limits.h"
00051 #include "util.h"
00052 
00053 using namespace std;
00054 
00055 namespace libdap {
00056 
00057 rvalue_list *
00058 make_rvalue_list(rvalue *rv)
00059 {
00060     assert(rv);
00061 
00062     rvalue_list *rvals = new rvalue_list;
00063 
00064     return append_rvalue_list(rvals, rv);
00065 }
00066 
00067 // Given a rvalue_list pointer RVALS and a value pointer VAL, make a variable
00068 // to hold VAL and append that variable to the list RVALS.
00069 //
00070 // Returns: A pointer to the updated rvalue_list.
00071 
00072 rvalue_list *
00073 append_rvalue_list(rvalue_list *rvals, rvalue *rv)
00074 {
00075     rvals->push_back(rv);
00076 
00077     return rvals;
00078 }
00079 
00080 
00092 BaseType **
00093 build_btp_args(rvalue_list *args, DDS &dds)
00094 {
00095     int argc = 0;
00096 
00097     if (args)
00098         argc = args->size();
00099 
00100     // Sanitize allocation size
00101     if (!size_ok(sizeof(BaseType*), argc + 1))
00102         throw Error(malformed_expr, string("Malformed argument list (")
00103                 + long_to_string(argc) + string(")."));
00104 
00105     // Add space for a null terminator
00106     BaseType **argv = new BaseType*[argc + 1];
00107 
00108     int index = 0;
00109     if (argv && argc) {
00110         for (rvalue::Args_iter i = args->begin(); i != args->end() && index
00111                 < argc + 1; ++i)
00112             argv[index++] = (*i)->bvalue(dds);
00113     }
00114 
00115     if (index != argc) {
00116         delete[] argv;
00117         throw InternalErr(__FILE__, __LINE__, "index out of range.");
00118     }
00119 
00120     argv[index] = 0; // Add the null terminator.
00121 
00122     return argv;
00123 }
00124 
00125 rvalue::rvalue(BaseType *bt): d_value(bt), d_func(0), d_args(0)
00126 {}
00127 
00128 rvalue::rvalue(btp_func f, vector<rvalue *> *a) : d_value(0), d_func(f), d_args(a)
00129 {}
00130 
00131 rvalue::rvalue(): d_value(0), d_func(0), d_args(0)
00132 {}
00133 
00134 rvalue::~rvalue()
00135 {
00136     // Deleting the BaseType pointers in value and args is a bad idea since
00137     // those might be variables in the dataset. The DDS dtor will take care
00138     // of deleting them. The constants wrapped in BaseType objects should be
00139     // pushed on the list of CE-allocated temp objects which the CE frees.
00140 }
00141 
00142 string
00143 rvalue::value_name()
00144 {
00145     assert(d_value);
00146 
00147     return d_value->name();
00148 }
00149 
00159 BaseType *
00160 rvalue::bvalue(DDS &dds)
00161 {
00162     if (d_value) {        // i.e., if this RValue is a BaseType
00163         return d_value;
00164     }
00165     else if (d_func) {
00166         // If func is true, then args must be set. See the constructor.
00167         // 12/23/04 jhrg
00168         BaseType **argv = build_btp_args(d_args, dds);
00169         BaseType *ret_val;
00170         (*d_func)(d_args->size(), argv, dds, &ret_val);
00171         delete[] argv;
00172         return ret_val;
00173     }
00174     else {
00175         return 0;
00176     }
00177 }
00178 
00179 } // namespace libdap
00180