bes  Updated for version 3.20.6
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 #include "config.h"
30 
31 #include "MyBaseTypeFactory.h"
32 
33 #include "BaseType.h"
34 #include "BaseTypeFactory.h"
35 
36 #include "Array.h"
37 #include "Byte.h"
38 #include "Float32.h"
39 #include "Float64.h"
40 #include "Grid.h"
41 #include "Int16.h"
42 #include "Int32.h"
43 #include "NCMLArray.h"
44 #include "Sequence.h"
45 #include "Str.h"
46 #include "Structure.h"
47 #include "UInt16.h"
48 #include "UInt32.h"
49 #include "Url.h"
50 
51 using namespace libdap;
52 using namespace std;
53 
54 namespace ncml_module {
55 
56 /* static */
57 libdap::BaseTypeFactory* MyBaseTypeFactory::_spFactory = new BaseTypeFactory();
58 
59 MyBaseTypeFactory::MyBaseTypeFactory()
60 {
61 }
62 
63 MyBaseTypeFactory::~MyBaseTypeFactory()
64 {
65 }
66 
67 auto_ptr<libdap::BaseType> MyBaseTypeFactory::makeVariable(const libdap::Type& t, const string &name)
68 {
69  switch (t) {
70  case dods_byte_c:
71  return auto_ptr<BaseType>(_spFactory->NewByte(name));
72  break;
73 
74  case dods_int16_c:
75  return auto_ptr<BaseType>(_spFactory->NewInt16(name));
76  break;
77 
78  case dods_uint16_c:
79  return auto_ptr<BaseType>(_spFactory->NewUInt16(name));
80  break;
81 
82  case dods_int32_c:
83  return auto_ptr<BaseType>(_spFactory->NewInt32(name));
84  break;
85 
86  case dods_uint32_c:
87  return auto_ptr<BaseType>(_spFactory->NewUInt32(name));
88  break;
89 
90  case dods_float32_c:
91  return auto_ptr<BaseType>(_spFactory->NewFloat32(name));
92  break;
93 
94  case dods_float64_c:
95  return auto_ptr<BaseType>(_spFactory->NewFloat64(name));
96  break;
97 
98  case dods_str_c:
99  return auto_ptr<BaseType>(_spFactory->NewStr(name));
100  break;
101 
102  case dods_url_c:
103  return auto_ptr<BaseType>(_spFactory->NewUrl(name));
104  break;
105 
106  case dods_array_c:
107  THROW_NCML_INTERNAL_ERROR("MyBaseTypeFactory::makeVariable(): no longer can make Array, instead use Array<T> form!");
108  break;
109 
110  case dods_structure_c:
111  return auto_ptr<BaseType>(_spFactory->NewStructure(name));
112  break;
113 
114  case dods_sequence_c:
115  return auto_ptr<BaseType>(_spFactory->NewSequence(name));
116  break;
117 
118  case dods_grid_c:
119  return auto_ptr<BaseType>(_spFactory->NewGrid(name));
120  break;
121 
122  default:
123  return auto_ptr<BaseType>(0);
124  }
125 }
126 
127 auto_ptr<libdap::BaseType> MyBaseTypeFactory::makeVariable(const string& type, const std::string& name)
128 {
129  if (isArrayTemplate(type)) {
130  // create the template var by default... if the caller readds it, this one will
131  // be deleted. Better safe than bus error.
132  return auto_ptr<BaseType>(makeArrayTemplateVariable(type, name, true).release());
133  }
134  else {
135  return makeVariable(getType(type), name);
136  }
137 }
138 
140 libdap::Type MyBaseTypeFactory::getType(const string& name)
141 {
142  if (name == "Byte") {
143  return dods_byte_c;
144  }
145  else if (name == "Int16") {
146  return dods_int16_c;
147  }
148  else if (name == "UInt16") {
149  return dods_uint16_c;
150  }
151 
152  else if (name == "Int32") {
153  return dods_int32_c;
154  }
155 
156  else if (name == "UInt32") {
157  return dods_uint32_c;
158  }
159 
160  else if (name == "Float32") {
161  return dods_float32_c;
162  }
163 
164  else if (name == "Float64") {
165  return dods_float64_c;
166  }
167 
168  else if (name == "String" || name == "string") {
169  return dods_str_c;
170  }
171 
172  else if (name == "URL") {
173  return dods_url_c;
174  }
175 
176  else if (name == "Array") {
177  return dods_array_c;
178  }
179 
180  else if (name == "Structure") {
181  return dods_structure_c;
182  }
183 
184  else if (name == "Sequence") {
185  return dods_sequence_c;
186  }
187 
188  else if (name == "Grid") {
189  return dods_grid_c;
190  }
191 
192  else {
193  return dods_null_c;
194  }
195 }
196 
197 bool MyBaseTypeFactory::isSimpleType(const string& name)
198 {
199  Type t = getType(name);
200  switch (t) {
201  case dods_byte_c:
202  case dods_int16_c:
203  case dods_uint16_c:
204  case dods_int32_c:
205  case dods_uint32_c:
206  case dods_float32_c:
207  case dods_float64_c:
208  case dods_str_c:
209  case dods_url_c:
210  return true;
211  default:
212  return false;
213  }
214 }
215 
216 bool MyBaseTypeFactory::isArrayTemplate(const string& typeName)
217 {
218  // Just check for the form.We won't typecheck the template arg here sicne we'll just match strings.
219  return (typeName.find("Array<") == 0 && (typeName.at(typeName.size() - 1) == '>'));
220 }
221 
222 std::auto_ptr<libdap::Array> MyBaseTypeFactory::makeArrayTemplateVariable(const string& type, const string& name,
223  bool makeTemplateVar)
224 {
225  // For the add_var's here, we use the auto_ptr get() since it's copied
226  // in add_var and we want the factoried one to destroy right afterwards.
227  // TODO when we have non copy adds in libdap, tighten this up with release().
228  Array* pNew = 0;
229  if (type == "Array<Byte>") {
230  pNew = new NCMLArray<dods_byte>(name);
231  if (makeTemplateVar) {
232  pNew->add_var_nocopy(makeVariable("Byte", name).release());
233  }
234  }
235  else if (type == "Array<Int16>") {
236  pNew = new NCMLArray<dods_int16>(name);
237  if (makeTemplateVar) {
238  pNew->add_var_nocopy(makeVariable("Int16", name).release());
239  }
240  }
241  else if (type == "Array<UInt16>") {
242  pNew = new NCMLArray<dods_uint16>(name);
243  if (makeTemplateVar) {
244  pNew->add_var_nocopy(makeVariable("UInt16", name).release());
245  }
246  }
247  else if (type == "Array<Int32>") {
248  pNew = new NCMLArray<dods_int32>(name);
249  if (makeTemplateVar) {
250  pNew->add_var_nocopy(makeVariable("Int32", name).release());
251  }
252  }
253  else if (type == "Array<UInt32>") {
254  pNew = new NCMLArray<dods_uint32>(name);
255  if (makeTemplateVar) {
256  pNew->add_var_nocopy(makeVariable("UInt32", name).release());
257  }
258  }
259  else if (type == "Array<Float32>") {
260  pNew = new NCMLArray<dods_float32>(name);
261  if (makeTemplateVar) {
262  pNew->add_var_nocopy(makeVariable("Float32", name).release());
263  }
264  }
265  else if (type == "Array<Float64>") {
266  pNew = new NCMLArray<dods_float64>(name);
267  if (makeTemplateVar) {
268  pNew->add_var_nocopy(makeVariable("Float64", name).release());
269  }
270  }
271  else if (type == "Array<String>" || type == "Array<Str>") {
272  pNew = new NCMLArray<std::string>(name);
273  if (makeTemplateVar) {
274  pNew->add_var_nocopy(makeVariable("String", name).release());
275  }
276  }
277  else if (type == "Array<URL>" || type == "Array<Url>") {
278  pNew = new NCMLArray<std::string>(name);
279  if (makeTemplateVar) {
280  pNew->add_var_nocopy(makeVariable("URL", name).release());
281  }
282  }
283  else {
284  THROW_NCML_INTERNAL_ERROR("MyBaseTypeFactory::makeArrayTemplateVariable(): can't create type=" + type);
285  }
286 
287  // OOM condition...
288  if (!pNew) {
289  THROW_NCML_INTERNAL_ERROR(
290  "MyBaseTypeFactory::makeArrayTemplateVariable(): failed to allocate memory for type=" + type);
291  }
292  return auto_ptr<Array>(pNew);
293 }
294 } // namespace ncml_module
Type
Type
Type of JSON value.
Definition: cmr_module/rapidjson/rapidjson.h:603
libdap
Definition: BESDapFunctionResponseCache.h:35
ncml_module
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
Definition: AggregationElement.cc:72
ncml_module::NCMLArray
A parameterized subclass of libdap::Array that allows us to apply constraints on NcML-specified data ...
Definition: NCMLArray.h:86