OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
MyBaseTypeFactory.cc
Go to the documentation of this file.
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 
57  /* static */
58  libdap::BaseTypeFactory* MyBaseTypeFactory::_spFactory = new BaseTypeFactory();
59 
60  MyBaseTypeFactory::MyBaseTypeFactory()
61  {
62  }
63 
64  MyBaseTypeFactory::~MyBaseTypeFactory()
65  {
66  }
67 
68  auto_ptr<libdap::BaseType>
69  MyBaseTypeFactory::makeVariable(const libdap::Type& t, const string &name)
70  {
71  switch (t)
72  {
73  case dods_byte_c:
74  return auto_ptr<BaseType>(_spFactory->NewByte(name));
75  break;
76 
77  case dods_int16_c:
78  return auto_ptr<BaseType>(_spFactory->NewInt16(name));
79  break;
80 
81  case dods_uint16_c:
82  return auto_ptr<BaseType>(_spFactory->NewUInt16(name));
83  break;
84 
85  case dods_int32_c:
86  return auto_ptr<BaseType>(_spFactory->NewInt32(name));
87  break;
88 
89  case dods_uint32_c:
90  return auto_ptr<BaseType>(_spFactory->NewUInt32(name));
91  break;
92 
93  case dods_float32_c:
94  return auto_ptr<BaseType>(_spFactory->NewFloat32(name));
95  break;
96 
97  case dods_float64_c:
98  return auto_ptr<BaseType>(_spFactory->NewFloat64(name));
99  break;
100 
101  case dods_str_c:
102  return auto_ptr<BaseType>(_spFactory->NewStr(name));
103  break;
104 
105  case dods_url_c:
106  return auto_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  return auto_ptr<BaseType>(_spFactory->NewArray(name));
112  break;
113 
114  case dods_structure_c:
115  return auto_ptr<BaseType>(_spFactory->NewStructure(name));
116  break;
117 
118  case dods_sequence_c:
119  return auto_ptr<BaseType>(_spFactory->NewSequence(name));
120  break;
121 
122  case dods_grid_c:
123  return auto_ptr<BaseType>(_spFactory->NewGrid(name));
124  break;
125 
126  default:
127  return auto_ptr<BaseType>(0);
128  }
129  }
130 
131  auto_ptr<libdap::BaseType>
132  MyBaseTypeFactory::makeVariable(const string& type, const std::string& name)
133  {
134  if (isArrayTemplate(type))
135  {
136  // create the template var by default... if the caller readds it, this one will
137  // be deleted. Better safe than bus error.
138  return auto_ptr<BaseType>(makeArrayTemplateVariable(type, name, true).release());
139  }
140  else
141  {
142  return makeVariable(getType(type), name);
143  }
144  }
145 
147  libdap::Type
148  MyBaseTypeFactory::getType(const string& name)
149  {
150  if (name == "Byte")
151  {
152  return dods_byte_c;
153  }
154  else if (name == "Int16")
155  {
156  return dods_int16_c;
157  }
158  else if (name == "UInt16")
159  {
160  return dods_uint16_c;
161  }
162 
163  else if (name == "Int32")
164  {
165  return dods_int32_c;
166  }
167 
168  else if (name == "UInt32")
169  {
170  return dods_uint32_c;
171  }
172 
173  else if (name == "Float32")
174  {
175  return dods_float32_c;
176  }
177 
178  else if (name == "Float64")
179  {
180  return dods_float64_c;
181  }
182 
183  else if (name == "String" || name == "string")
184  {
185  return dods_str_c;
186  }
187 
188  else if (name == "URL")
189  {
190  return dods_url_c;
191  }
192 
193  else if (name == "Array")
194  {
195  return dods_array_c;
196  }
197 
198  else if (name == "Structure")
199  {
200  return dods_structure_c;
201  }
202 
203  else if (name == "Sequence")
204  {
205  return dods_sequence_c;
206  }
207 
208  else if (name == "Grid")
209  {
210  return dods_grid_c;
211  }
212 
213  else
214  {
215  return dods_null_c;
216  }
217  }
218 
219  bool
220  MyBaseTypeFactory::isSimpleType(const string& name)
221  {
222  Type t = getType(name);
223  switch (t) {
224  case dods_byte_c:
225  case dods_int16_c:
226  case dods_uint16_c:
227  case dods_int32_c:
228  case dods_uint32_c:
229  case dods_float32_c:
230  case dods_float64_c:
231  case dods_str_c:
232  case dods_url_c:
233  return true;
234  default:
235  return false;
236  }
237  }
238 
239  bool
240  MyBaseTypeFactory::isArrayTemplate(const string& typeName)
241  {
242  // Just check for the form.We won't typecheck the template arg here sicne we'll just match strings.
243  return (typeName.find("Array<") == 0 && (typeName.at(typeName.size()-1) == '>'));
244  }
245 
246  std::auto_ptr<libdap::Array>
247  MyBaseTypeFactory::makeArrayTemplateVariable(const string& type, const string& name, bool makeTemplateVar)
248  {
249  // For the add_var's here, we use the auto_ptr get() since it's copied
250  // in add_var and we want the factoried one to destroy right afterwards.
251  // TODO when we have non copy adds in libdap, tighten this up with release().
252  Array* pNew = 0;
253  if (type == "Array<Byte>")
254  {
255  pNew = new NCMLArray<dods_byte>(name);
256  if (makeTemplateVar)
257  {
258  pNew->add_var(makeVariable("Byte", name).get());
259  }
260  }
261  else if (type == "Array<Int16>")
262  {
263  pNew = new NCMLArray<dods_int16>(name);
264  if (makeTemplateVar)
265  {
266  pNew->add_var(makeVariable("Int16", name).get());
267  }
268  }
269  else if (type == "Array<UInt16>")
270  {
271  pNew = new NCMLArray<dods_uint16>(name);
272  if (makeTemplateVar)
273  {
274  pNew->add_var(makeVariable("UInt16", name).get());
275  }
276  }
277  else if (type == "Array<Int32>")
278  {
279  pNew = new NCMLArray<dods_int32>(name);
280  if (makeTemplateVar)
281  {
282  pNew->add_var(makeVariable("Int32", name).get());
283  }
284  }
285  else if (type == "Array<UInt32>")
286  {
287  pNew = new NCMLArray<dods_uint32>(name);
288  if (makeTemplateVar)
289  {
290  pNew->add_var(makeVariable("UInt32", name).get());
291  }
292  }
293  else if (type == "Array<Float32>")
294  {
295  pNew = new NCMLArray<dods_float32>(name);
296  if (makeTemplateVar)
297  {
298  pNew->add_var(makeVariable("Float32", name).get());
299  }
300  }
301  else if (type == "Array<Float64>")
302  {
303  pNew = new NCMLArray<dods_float64>(name);
304  if (makeTemplateVar)
305  {
306  pNew->add_var(makeVariable("Float64", name).get());
307  }
308  }
309  else if (
310  type == "Array<String>" ||
311  type == "Array<Str>"
312  )
313  {
314  pNew = new NCMLArray<std::string>(name);
315  if (makeTemplateVar)
316  {
317  pNew->add_var(makeVariable("String", name).get());
318  }
319  }
320  else if ( type == "Array<URL>" ||
321  type == "Array<Url>")
322  {
323  pNew = new NCMLArray<std::string>(name);
324  if (makeTemplateVar)
325  {
326  pNew->add_var(makeVariable("URL", name).get());
327  }
328  }
329  else
330  {
331  THROW_NCML_INTERNAL_ERROR("MyBaseTypeFactory::makeArrayTemplateVariable(): can't create type=" + type);
332  }
333 
334  // OOM condition...
335  if (!pNew)
336  {
337  THROW_NCML_INTERNAL_ERROR("MyBaseTypeFactory::makeArrayTemplateVariable(): failed to allocate memory for type=" + type);
338  }
339  return auto_ptr<Array>(pNew);
340  }
341 } // namespace ncml_module
A parameterized subclass of libdap::Array that allows us to apply constraints on NcML-specified data ...
Definition: NCMLArray.h:83
An abstract superclass for NCMLArray that handles the non-parameterized functionality and allows u...
STL namespace.
static class NCMLUtil overview
#define THROW_NCML_INTERNAL_ERROR(msg)
Definition: NCMLDebug.h:61