libdap  Updated for version 3.17.2
D4RValue.cc
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) 2014 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 #include "config.h"
27 
28 #include <cassert>
29 #include <iostream>
30 
31 #include "BaseType.h"
32 #include "Array.h"
33 #include "Byte.h"
34 #include "Int8.h"
35 #include "UInt16.h"
36 #include "Int16.h"
37 #include "UInt32.h"
38 #include "Int32.h"
39 #include "UInt64.h"
40 #include "Int64.h"
41 #include "Float32.h"
42 #include "Float64.h"
43 #include "Str.h"
44 
45 #include "D4RValue.h"
46 #include "InternalErr.h"
47 
48 #include "dods-datatypes.h"
49 #include "dods-limits.h"
50 #include "util.h"
51 
52 using namespace std;
53 
54 namespace libdap {
55 
56 D4RValueList::~D4RValueList()
57 {
58  for (std::vector<D4RValue *>::iterator i = d_rvalues.begin(), e = d_rvalues.end(); i != e; ++i)
59  delete *i;
60 }
61 
62 template<typename T, class DAP_TYPE>
63 static BaseType *
64 build_constant_array(vector<T> &values, DAP_TYPE &dt)
65 {
66  Array *array = new Array("", &dt);
67  array->append_dim(values.size());
68 
69  // TODO Make set_value_nocopy() methods so that values' pointers can be copied
70  // instead of allocating memory twice. jhrg 7/5/13
71 
72  array->set_value(values, values.size());
73 
74  array->set_read_p(true);
75 
76  static unsigned long counter = 1;
77  array->set_name(string("g") + long_to_string(counter++));
78 
79  return array;
80 }
81 
82 D4RValue::D4RValue(unsigned long long ull) : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
83 {
84  UInt64 *ui = new UInt64("constant");
85  ui->set_value(ull);
86  d_constant = ui;
87 }
88 
89 D4RValue::D4RValue(long long ll) : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
90 {
91  Int64 *i = new Int64("constant");
92  i->set_value(ll);
93  d_constant = i;
94 }
95 
96 D4RValue::D4RValue(double r) : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
97 {
98  Float64 *f = new Float64("constant");
99  f->set_value(r);
100  d_constant = f;
101 }
102 
103 D4RValue::D4RValue(std::string cpps) : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
104 {
105  Str *s = new Str("constant");
106  s->set_value(remove_quotes(cpps));
107  d_constant = s;
108 }
109 
110 D4RValue::D4RValue(std::vector<dods_byte> &byte_args)
111  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
112 {
113  Byte b("");
114  d_constant = build_constant_array(byte_args, b);
115 }
116 
117 D4RValue::D4RValue(std::vector<dods_int8> &byte_int8)
118  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
119 {
120  Int8 b("");
121  d_constant = build_constant_array(byte_int8, b);
122 }
123 
124 D4RValue::D4RValue(std::vector<dods_uint16> &byte_uint16)
125  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
126 {
127  UInt16 b("");
128  d_constant = build_constant_array(byte_uint16, b);
129 }
130 
131 D4RValue::D4RValue(std::vector<dods_int16> &byte_int16)
132  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
133 {
134  Int16 b("");
135  d_constant = build_constant_array(byte_int16, b);
136 }
137 
138 D4RValue::D4RValue(std::vector<dods_uint32> &byte_uint32)
139  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
140 {
141  UInt32 b("");
142  d_constant = build_constant_array(byte_uint32, b);
143 }
144 
145 D4RValue::D4RValue(std::vector<dods_int32> &byte_int32)
146  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
147 {
148  Int32 b("");
149  d_constant = build_constant_array(byte_int32, b);
150 }
151 
152 D4RValue::D4RValue(std::vector<dods_uint64> &byte_uint64)
153  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
154 {
155  UInt64 b("");
156  d_constant = build_constant_array(byte_uint64, b);
157 }
158 
159 D4RValue::D4RValue(std::vector<dods_int64> &byte_int64)
160  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
161 {
162  Int64 b("");
163  d_constant = build_constant_array(byte_int64, b);
164 }
165 
166 D4RValue::D4RValue(std::vector<dods_float32> &byte_float32)
167  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
168 {
169  Float32 b("");
170  d_constant = build_constant_array(byte_float32, b);
171 }
172 
173 D4RValue::D4RValue(std::vector<dods_float64> &byte_float64)
174  : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
175 {
176  Float64 b("");
177  d_constant = build_constant_array(byte_float64, b);
178 }
179 
180 D4RValue::~D4RValue() {
181  // d_variable and d_func are weak pointers; don't delete.
182  delete d_args;
183  delete d_constant;
184 }
185 
197 BaseType *
199 {
200  switch (d_value_kind) {
201  case basetype:
202  d_variable->read();
203  d_variable->set_read_p(true);
204  return d_variable;
205 
206  case function:
207  return (*d_func)(d_args, dmr);
208 
209  case constant:
210  return d_constant;
211 
212  default:
213  throw InternalErr(__FILE__, __LINE__, "Unknown rvalue type.");
214  };
215 }
216 
217 } // namespace libdap
218 
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:820
STL namespace.
A class for software fault reporting.
Definition: InternalErr.h:64
BaseType * value(DMR &dmr)
Definition: D4RValue.cc:198
string remove_quotes(const string &s)
Definition: util.cc:581