libdap  Updated for version 3.17.0
D4RValue.cc
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) 2014 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 #include "config.h"
00027 
00028 #include <cassert>
00029 #include <iostream>
00030 
00031 #include "BaseType.h"
00032 #include "Array.h"
00033 #include "Byte.h"
00034 #include "Int8.h"
00035 #include "UInt16.h"
00036 #include "Int16.h"
00037 #include "UInt32.h"
00038 #include "Int32.h"
00039 #include "UInt64.h"
00040 #include "Int64.h"
00041 #include "Float32.h"
00042 #include "Float64.h"
00043 #include "Str.h"
00044 
00045 #include "D4RValue.h"
00046 #include "InternalErr.h"
00047 
00048 #include "dods-datatypes.h"
00049 #include "dods-limits.h"
00050 #include "util.h"
00051 
00052 using namespace std;
00053 
00054 namespace libdap {
00055 
00056 D4RValueList::~D4RValueList()
00057 {
00058         for (std::vector<D4RValue *>::iterator i = d_rvalues.begin(), e = d_rvalues.end(); i != e; ++i)
00059                 delete *i;
00060 }
00061 
00062 template<typename T, class DAP_TYPE>
00063 static BaseType *
00064 build_constant_array(vector<T> &values, DAP_TYPE &dt)
00065 {
00066     Array *array = new Array("", &dt);
00067     array->append_dim(values.size());
00068 
00069     // TODO Make set_value_nocopy() methods so that values' pointers can be copied
00070     // instead of allocating memory twice. jhrg 7/5/13
00071 
00072     array->set_value(values, values.size());
00073 
00074     array->set_read_p(true);
00075 
00076     static unsigned long counter = 1;
00077     array->set_name(string("g") + long_to_string(counter++));
00078 
00079     return array;
00080 }
00081 
00082 D4RValue::D4RValue(unsigned long long ull) : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
00083 {
00084         UInt64 *ui = new UInt64("constant");
00085         ui->set_value(ull);
00086         d_constant = ui;
00087 }
00088 
00089 D4RValue::D4RValue(long long ll) : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00090 {
00091         Int64 *i = new Int64("constant");
00092         i->set_value(ll);
00093         d_constant = i;
00094 }
00095 
00096 D4RValue::D4RValue(double r) : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00097 {
00098         Float64 *f = new Float64("constant");
00099         f->set_value(r);
00100         d_constant = f;
00101 }
00102 
00103 D4RValue::D4RValue(std::string cpps) : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00104 {
00105         Str *s = new Str("constant");
00106         s->set_value(remove_quotes(cpps));
00107         d_constant = s;
00108 }
00109 
00110 D4RValue::D4RValue(std::vector<dods_byte> &byte_args)
00111         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00112 {
00113         Byte b("");
00114         d_constant = build_constant_array(byte_args, b);
00115 }
00116 
00117 D4RValue::D4RValue(std::vector<dods_int8> &byte_int8)
00118         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00119 {
00120         Int8 b("");
00121         d_constant = build_constant_array(byte_int8, b);
00122 }
00123 
00124 D4RValue::D4RValue(std::vector<dods_uint16> &byte_uint16)
00125         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00126 {
00127         UInt16 b("");
00128         d_constant = build_constant_array(byte_uint16, b);
00129 }
00130 
00131 D4RValue::D4RValue(std::vector<dods_int16> &byte_int16)
00132         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00133 {
00134         Int16 b("");
00135         d_constant = build_constant_array(byte_int16, b);
00136 }
00137 
00138 D4RValue::D4RValue(std::vector<dods_uint32> &byte_uint32)
00139         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00140 {
00141         UInt32 b("");
00142         d_constant = build_constant_array(byte_uint32, b);
00143 }
00144 
00145 D4RValue::D4RValue(std::vector<dods_int32> &byte_int32)
00146         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00147 {
00148         Int32 b("");
00149         d_constant = build_constant_array(byte_int32, b);
00150 }
00151 
00152 D4RValue::D4RValue(std::vector<dods_uint64> &byte_uint64)
00153         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00154 {
00155         UInt64 b("");
00156         d_constant = build_constant_array(byte_uint64, b);
00157 }
00158 
00159 D4RValue::D4RValue(std::vector<dods_int64> &byte_int64)
00160         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00161 {
00162         Int64 b("");
00163         d_constant = build_constant_array(byte_int64, b);
00164 }
00165 
00166 D4RValue::D4RValue(std::vector<dods_float32> &byte_float32)
00167         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00168 {
00169         Float32 b("");
00170         d_constant = build_constant_array(byte_float32, b);
00171 }
00172 
00173 D4RValue::D4RValue(std::vector<dods_float64> &byte_float64)
00174         : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
00175 {
00176         Float64 b("");
00177         d_constant = build_constant_array(byte_float64, b);
00178 }
00179 
00180 D4RValue::~D4RValue() {
00181         // d_variable and d_func are weak pointers; don't delete.
00182         delete d_args;
00183         delete d_constant;
00184 }
00185 
00197 BaseType *
00198 D4RValue::value(DMR &dmr)
00199 {
00200         switch (d_value_kind) {
00201         case basetype:
00202                 d_variable->read();
00203                 d_variable->set_read_p(true);
00204                 return d_variable;
00205 
00206         case function:
00207                 return (*d_func)(d_args, dmr);
00208 
00209         case constant:
00210                 return d_constant;
00211 
00212         default:
00213                 throw InternalErr(__FILE__, __LINE__, "Unknown rvalue type.");
00214         };
00215 }
00216 
00217 } // namespace libdap
00218