libdap++  Updated for version 3.8.2
AttrTable.h
Go to the documentation of this file.
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) 2002,2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // (c) COPYRIGHT URI/MIT 1994-1999
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31 
32 // An AttrTable is a table of attributes (type-name-value tuples).
33 
34 #ifndef _attrtable_h
35 #define _attrtable_h 1
36 
37 
38 #include <string>
39 #include <vector>
40 
41 #ifndef _error_h
42 #include "Error.h"
43 #endif
44 
45 using std::vector;
46 using std::string;
47 using std::vector;
48 
49 #ifndef A_DapObj_h
50 #include "DapObj.h"
51 #endif
52 
53 #ifndef XMLWRITER_H_
54 #include "XMLWriter.h"
55 #endif
56 
57 namespace libdap
58 {
59 
81 enum AttrType {
94 };
95 
96 string AttrType_to_String(const AttrType at);
97 AttrType String_to_AttrType(const string &s);
98 
150 class AttrTable : public DapObj
151 {
152  // entry needs to be made public to make up for issues with this class'
153  // design. It should probably be moved to it's own class. 05/22/03 jhrg
154 public:
159  struct entry
160  {
161  string name;
163 
164  bool is_alias;
165  string aliased_to;
166 
167  bool is_global; // use this to mark non-container attributes. see below.
168 
169  // If type == Attr_container, use attributes to read the contained
170  // table, otherwise use attr to read the vector of values.
172  std::vector<string> *attr; // a vector of values. jhrg 12/5/94
173 
174  entry(): name(""), type(Attr_unknown), is_alias(false),
175  aliased_to(""), is_global(true), attributes(0), attr(0) {}
176 
177  entry(const entry &rhs)
178  {
179  clone(rhs);
180  }
181 
183  {
184  if (is_alias) // alias copies the pointers.
185  return;
186  if (type == Attr_container) {
187  delete attributes; attributes = 0;
188  }
189  else {
190  delete attr; attr = 0;
191  }
192  }
193 
194  virtual ~entry()
195  {
196  delete_entry();
197  }
198 
199  void clone(const entry &rhs)
200  {
201  name = rhs.name;
202  type = rhs.type;
203  is_alias = rhs.is_alias;
204  aliased_to = rhs.aliased_to;
205  is_global = rhs.is_global;
206  switch (rhs.type) {
207  case Attr_unknown:
208  break;
209  case Attr_container: {
210  if (rhs.is_alias)
211  attributes = rhs.attributes;
212  else
213  attributes = new AttrTable(*rhs.attributes);
214  break;
215  }
216  default: {
217  if (rhs.is_alias)
218  attr = rhs.attr;
219  else
220  attr = new std::vector<string>(*rhs.attr);
221  break;
222  }
223  }
224  }
225 
226  entry &operator=(const entry &rhs)
227  {
228  if (this != &rhs) {
229  delete_entry();
230  clone(rhs);
231  }
232  return *this;
233  }
234  };
235 
236  typedef std::vector<entry *>::const_iterator Attr_citer ;
237  typedef std::vector<entry *>::iterator Attr_iter ;
238 
239 private:
240  string d_name;
241  AttrTable *d_parent;
242  std::vector<entry *> attr_map;
243 
244  // Use this to mark container attributes. Look at the methods
245  // is_global_attribute() and set_is_...., esp. at the versions that take
246  // an iterator. This code is tricky because it has to track both whole
247  // containers that are global and individual attributes that are 'global'
248  // relative to a constructor. That is, there are some attributes that are
249  // bound to a container and not any of the container's children.
250  bool d_is_global_attribute;
251 
252  void delete_attr_table();
253 
254  friend class AttrTableTest;
255 
256 protected:
257  void clone(const AttrTable &at);
258 
259  void simple_print(FILE *out, string pad, Attr_iter i,
260  bool dereference);
261  void simple_print(ostream &out, string pad, Attr_iter i,
262  bool dereference);
263 
264 public:
265  AttrTable();
266  AttrTable(const AttrTable &rhs);
267  virtual ~AttrTable();
268  AttrTable & operator=(const AttrTable &rhs);
269 
270  virtual void erase();
271 
272  virtual unsigned int get_size() const;
273  virtual string get_name() const;
274  virtual void set_name(const string &n);
275 
279  virtual AttrTable *get_parent() const
280  {
281  return d_parent;
282  }
283 
284  virtual bool is_global_attribute() const { return d_is_global_attribute; }
285  virtual void set_is_global_attribute(bool ga) { d_is_global_attribute = ga; }
286 
287  virtual unsigned int append_attr(const string &name, const string &type,
288  const string &value);
289  virtual unsigned int append_attr(const string &name, const string &type,
290  vector<string> *values);
291 
292  virtual AttrTable *append_container(const string &name);
293  virtual AttrTable *append_container(AttrTable *at, const string &name);
294 
295  virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
296  virtual AttrTable *find_container(const string &target);
297  virtual AttrTable *recurrsive_find(const string &target,
298  Attr_iter *location);
299 
300  Attr_iter simple_find(const string &target);
301  AttrTable *simple_find_container(const string &target);
302 
303 
304  virtual AttrTable *get_attr_table(const string &name);
305  virtual string get_type(const string &name);
306  virtual AttrType get_attr_type(const string &name);
307  virtual unsigned int get_attr_num(const string &name);
308  virtual string get_attr(const string &name, unsigned int i = 0);
309  virtual vector<string> *get_attr_vector(const string &name);
310  virtual void del_attr(const string &name, int i = -1);
311 
312  virtual Attr_iter attr_begin();
313  virtual Attr_iter attr_end();
314  virtual Attr_iter get_attr_iter(int i);
315  virtual string get_name(Attr_iter iter);
316  virtual bool is_container(Attr_iter iter);
317  virtual AttrTable *get_attr_table(Attr_iter iter);
318  virtual Attr_iter del_attr_table(Attr_iter iter);
319  virtual string get_type(Attr_iter iter);
320  virtual AttrType get_attr_type(Attr_iter iter);
321  virtual unsigned int get_attr_num(Attr_iter iter);
322  virtual string get_attr(Attr_iter iter, unsigned int i = 0);
323  virtual std::vector<string> *get_attr_vector(Attr_iter iter);
324  virtual bool is_global_attribute(Attr_iter iter);
325  virtual void set_is_global_attribute(Attr_iter iter, bool ga);
326 
327  virtual void add_container_alias(const string &name, AttrTable *src);
328  virtual void add_value_alias(AttrTable *at, const string &name,
329  const string &source);
330  virtual bool attr_alias(const string &alias,
331  AttrTable *at,
332  const string &name);
333  virtual bool attr_alias(const string &alias, const string &name);
334 
335  virtual void print(FILE *out, string pad = " ",
336  bool dereference = false);
337  virtual void print(ostream &out, string pad = " ",
338  bool dereference = false);
339 
340  virtual void print_xml(FILE *out, string pad = " ",
341  bool constrained = false);
342  virtual void print_xml(ostream &out, string pad = " ",
343  bool constrained = false);
344 
345  void print_xml_writer(XMLWriter &xml);
346 
347  virtual void dump(ostream &strm) const ;
348 };
349 
350 } // namespace libdap
351 
352 #endif // _attrtable_h