bes  Updated for version 3.20.13
MyBaseTypeFactory.cc
1 // This file is part of the "NcML Module" project, a BES module designed
3 // to allow NcML files to be used to be used as a wrapper to add
4 // AIS to existing datasets of any format.
5 //
6 // Copyright (c) 2009 OPeNDAP, Inc.
7 // Author: Michael Johnson <m.johnson@opendap.org>
8 //
9 // For more information, please also see the main website: http://opendap.org/
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26 //
27 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29 
30 #include "config.h"
31 
32 #include "MyBaseTypeFactory.h"
33 
34 #include <libdap/BaseType.h>
35 #include <libdap/BaseTypeFactory.h>
36 
37 #include <libdap/Array.h>
38 #include <libdap/Byte.h>
39 #include <libdap/Float32.h>
40 #include <libdap/Float64.h>
41 #include <libdap/Grid.h>
42 #include <libdap/Int16.h>
43 #include <libdap/Int32.h>
44 #include "NCMLArray.h"
45 #include <libdap/Sequence.h>
46 #include <libdap/Str.h>
47 #include <libdap/Structure.h>
48 #include <libdap/UInt16.h>
49 #include <libdap/UInt32.h>
50 #include <libdap/Url.h>
51 
52 using namespace libdap;
53 using namespace std;
54 
55 namespace ncml_module {
56 
57 /* static */
58 libdap::BaseTypeFactory* MyBaseTypeFactory::_spFactory = new BaseTypeFactory();
59 
60 #if 0
61 MyBaseTypeFactory::MyBaseTypeFactory()
62 {
63 }
64 
65 MyBaseTypeFactory::~MyBaseTypeFactory()
66 {
67 }
68 #endif
69 
70 unique_ptr<libdap::BaseType> MyBaseTypeFactory::makeVariable(const libdap::Type& t, const string &name)
71 {
72  switch (t) {
73  case dods_byte_c:
74  return unique_ptr<BaseType>(_spFactory->NewByte(name));
75  break;
76 
77  case dods_int16_c:
78  return unique_ptr<BaseType>(_spFactory->NewInt16(name));
79  break;
80 
81  case dods_uint16_c:
82  return unique_ptr<BaseType>(_spFactory->NewUInt16(name));
83  break;
84 
85  case dods_int32_c:
86  return unique_ptr<BaseType>(_spFactory->NewInt32(name));
87  break;
88 
89  case dods_uint32_c:
90  return unique_ptr<BaseType>(_spFactory->NewUInt32(name));
91  break;
92 
93  case dods_float32_c:
94  return unique_ptr<BaseType>(_spFactory->NewFloat32(name));
95  break;
96 
97  case dods_float64_c:
98  return unique_ptr<BaseType>(_spFactory->NewFloat64(name));
99  break;
100 
101  case dods_str_c:
102  return unique_ptr<BaseType>(_spFactory->NewStr(name));
103  break;
104 
105  case dods_url_c:
106  return unique_ptr<BaseType>(_spFactory->NewUrl(name));
107  break;
108 
109  case dods_array_c:
110  THROW_NCML_INTERNAL_ERROR("MyBaseTypeFactory::makeVariable(): no longer can make Array, instead use Array<T> form!");
111  break;
112 
113  case dods_structure_c:
114  return unique_ptr<BaseType>(_spFactory->NewStructure(name));
115  break;
116 
117  case dods_sequence_c:
118  return unique_ptr<BaseType>(_spFactory->NewSequence(name));
119  break;
120 
121  case dods_grid_c:
122  return unique_ptr<BaseType>(_spFactory->NewGrid(name));
123  break;
124 
125  default:
126  THROW_NCML_INTERNAL_ERROR("MyBaseTypeFactory::makeVariable(): request to make an unknown variable type.");
127 #if 0
128  return unique_ptr<BaseType>(0);
129 #endif
130  }
131 }
132 
133 unique_ptr<libdap::BaseType> MyBaseTypeFactory::makeVariable(const string& type, const std::string& name)
134 {
135  if (isArrayTemplate(type)) {
136  // create the template var by default... if the caller reads it, this one will
137  // be deleted. Better safe than bus error.
138  return unique_ptr<BaseType>(makeArrayTemplateVariable(type, name, true).release());
139  }
140  else {
141  return makeVariable(getType(type), name);
142  }
143 }
144 
146 libdap::Type MyBaseTypeFactory::getType(const string& name)
147 {
148  if (name == "Byte") {
149  return dods_byte_c;
150  }
151  else if (name == "Int16") {
152  return dods_int16_c;
153  }
154  else if (name == "UInt16") {
155  return dods_uint16_c;
156  }
157 
158  else if (name == "Int32") {
159  return dods_int32_c;
160  }
161 
162  else if (name == "UInt32") {
163  return dods_uint32_c;
164  }
165 
166  else if (name == "Float32") {
167  return dods_float32_c;
168  }
169 
170  else if (name == "Float64") {
171  return dods_float64_c;
172  }
173 
174  else if (name == "String" || name == "string") {
175  return dods_str_c;
176  }
177 
178  else if (name == "URL") {
179  return dods_url_c;
180  }
181 
182  else if (name == "Array") {
183  return dods_array_c;
184  }
185 
186  else if (name == "Structure") {
187  return dods_structure_c;
188  }
189 
190  else if (name == "Sequence") {
191  return dods_sequence_c;
192  }
193 
194  else if (name == "Grid") {
195  return dods_grid_c;
196  }
197 
198  else {
199  return dods_null_c;
200  }
201 }
202 
203 bool MyBaseTypeFactory::isSimpleType(const string& name)
204 {
205  Type t = getType(name);
206  switch (t) {
207  case dods_byte_c:
208  case dods_int16_c:
209  case dods_uint16_c:
210  case dods_int32_c:
211  case dods_uint32_c:
212  case dods_float32_c:
213  case dods_float64_c:
214  case dods_str_c:
215  case dods_url_c:
216  return true;
217  default:
218  return false;
219  }
220 }
221 
222 bool MyBaseTypeFactory::isArrayTemplate(const string& typeName)
223 {
224  // Just check for the form. We won't typecheck the template arg here since we'll just match strings.
225  return (typeName.find("Array<") == 0 && (typeName.at(typeName.size() - 1) == '>'));
226 }
227 
228 std::unique_ptr<libdap::Array>
229 MyBaseTypeFactory::makeArrayTemplateVariable(const string& type, const string& name, bool makeTemplateVar)
230 {
231  Array* pNew = nullptr;
232  if (type == "Array<Byte>") {
233  pNew = new NCMLArray<dods_byte>(name);
234  if (makeTemplateVar) {
235  pNew->add_var_nocopy(makeVariable("Byte", name).release());
236  }
237  }
238  else if (type == "Array<Int16>") {
239  pNew = new NCMLArray<dods_int16>(name);
240  if (makeTemplateVar) {
241  pNew->add_var_nocopy(makeVariable("Int16", name).release());
242  }
243  }
244  else if (type == "Array<UInt16>") {
245  pNew = new NCMLArray<dods_uint16>(name);
246  if (makeTemplateVar) {
247  pNew->add_var_nocopy(makeVariable("UInt16", name).release());
248  }
249  }
250  else if (type == "Array<Int32>") {
251  pNew = new NCMLArray<dods_int32>(name);
252  if (makeTemplateVar) {
253  pNew->add_var_nocopy(makeVariable("Int32", name).release());
254  }
255  }
256  else if (type == "Array<UInt32>") {
257  pNew = new NCMLArray<dods_uint32>(name);
258  if (makeTemplateVar) {
259  pNew->add_var_nocopy(makeVariable("UInt32", name).release());
260  }
261  }
262  else if (type == "Array<Float32>") {
263  pNew = new NCMLArray<dods_float32>(name);
264  if (makeTemplateVar) {
265  pNew->add_var_nocopy(makeVariable("Float32", name).release());
266  }
267  }
268  else if (type == "Array<Float64>") {
269  pNew = new NCMLArray<dods_float64>(name);
270  if (makeTemplateVar) {
271  pNew->add_var_nocopy(makeVariable("Float64", name).release());
272  }
273  }
274  else if (type == "Array<String>" || type == "Array<Str>") {
275  pNew = new NCMLArray<std::string>(name);
276  if (makeTemplateVar) {
277  pNew->add_var_nocopy(makeVariable("String", name).release());
278  }
279  }
280  else if (type == "Array<URL>" || type == "Array<Url>") {
281  pNew = new NCMLArray<std::string>(name);
282  if (makeTemplateVar) {
283  pNew->add_var_nocopy(makeVariable("URL", name).release());
284  }
285  }
286  else {
287  THROW_NCML_INTERNAL_ERROR("MyBaseTypeFactory::makeArrayTemplateVariable(): can't create type=" + type);
288  }
289 
290  // OOM condition...
291  if (!pNew) {
292  THROW_NCML_INTERNAL_ERROR(
293  "MyBaseTypeFactory::makeArrayTemplateVariable(): failed to allocate memory for type=" + type);
294  }
295 
296  return unique_ptr<Array>(pNew);
297 }
298 } // namespace ncml_module
A parameterized subclass of libdap::Array that allows us to apply constraints on NcML-specified data ...
Definition: NCMLArray.h:86
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
Type
Type of JSON value.
Definition: rapidjson.h:664