libdap++  Updated for version 3.14.0
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., 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 // (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 
142 class AttrTable : public DapObj
143 {
144  // entry needs to be made public to make up for issues with this class'
145  // design. It should probably be moved to it's own class. 05/22/03 jhrg
146 public:
151  struct entry
152  {
153  string name;
155 
156  bool is_alias;
157  string aliased_to;
158 
159  bool is_global; // use this to mark non-container attributes. see below.
160 
161  // If type == Attr_container, use attributes to read the contained
162  // table, otherwise use attr to read the vector of values.
164  std::vector<string> *attr; // a vector of values. jhrg 12/5/94
165 
166  entry(): name(""), type(Attr_unknown), is_alias(false),
167  aliased_to(""), is_global(true), attributes(0), attr(0) {}
168 
169  entry(const entry &rhs)
170  {
171  clone(rhs);
172  }
173 
175  {
176  if (is_alias) // alias copies the pointers.
177  return;
178  if (type == Attr_container) {
179  delete attributes; attributes = 0;
180  }
181  else {
182  delete attr; attr = 0;
183  }
184  }
185 
186  virtual ~entry()
187  {
188  delete_entry();
189  }
190 
191  void clone(const entry &rhs)
192  {
193  name = rhs.name;
194  type = rhs.type;
195  is_alias = rhs.is_alias;
196  aliased_to = rhs.aliased_to;
197  is_global = rhs.is_global;
198  switch (rhs.type) {
199  case Attr_unknown:
200  break;
201  case Attr_container: {
202  if (rhs.is_alias)
203  attributes = rhs.attributes;
204  else
205  attributes = new AttrTable(*rhs.attributes);
206  break;
207  }
208  default: {
209  if (rhs.is_alias)
210  attr = rhs.attr;
211  else
212  attr = new std::vector<string>(*rhs.attr);
213  break;
214  }
215  }
216  }
217 
218  entry &operator=(const entry &rhs)
219  {
220  if (this != &rhs) {
221  delete_entry();
222  clone(rhs);
223  }
224  return *this;
225  }
226  };
227 
228  typedef std::vector<entry *>::const_iterator Attr_citer ;
229  typedef std::vector<entry *>::iterator Attr_iter ;
230 
231 private:
232  string d_name;
233  AttrTable *d_parent;
234  std::vector<entry *> attr_map;
235 
236  // Use this to mark container attributes. Look at the methods
237  // is_global_attribute() and set_is_...., esp. at the versions that take
238  // an iterator. This code is tricky because it has to track both whole
239  // containers that are global and individual attributes that are 'global'
240  // relative to a constructor. That is, there are some attributes that are
241  // bound to a container and not any of the container's children.
242  bool d_is_global_attribute;
243 
244  void delete_attr_table();
245 
246  friend class AttrTableTest;
247 
248 protected:
249  void clone(const AttrTable &at);
250 
251  void simple_print(FILE *out, string pad, Attr_iter i,
252  bool dereference);
253  void simple_print(ostream &out, string pad, Attr_iter i,
254  bool dereference);
255 
256 public:
257  AttrTable();
258  AttrTable(const AttrTable &rhs);
259  virtual ~AttrTable();
260  AttrTable & operator=(const AttrTable &rhs);
261 
262  virtual void erase();
263 
264  virtual unsigned int get_size() const;
265  virtual string get_name() const;
266  virtual void set_name(const string &n);
267 
271  virtual AttrTable *get_parent() const
272  {
273  return d_parent;
274  }
275 
276  virtual bool is_global_attribute() const { return d_is_global_attribute; }
277  virtual void set_is_global_attribute(bool ga) { d_is_global_attribute = ga; }
278 
279  virtual unsigned int append_attr(const string &name, const string &type,
280  const string &value);
281  virtual unsigned int append_attr(const string &name, const string &type,
282  vector<string> *values);
283 
284  virtual AttrTable *append_container(const string &name);
285  virtual AttrTable *append_container(AttrTable *at, const string &name);
286 
287  virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
288  virtual AttrTable *find_container(const string &target);
289  virtual AttrTable *recurrsive_find(const string &target,
290  Attr_iter *location);
291 
292  Attr_iter simple_find(const string &target);
293  AttrTable *simple_find_container(const string &target);
294 
295 
296  virtual AttrTable *get_attr_table(const string &name);
297  virtual string get_type(const string &name);
298  virtual AttrType get_attr_type(const string &name);
299  virtual unsigned int get_attr_num(const string &name);
300  virtual string get_attr(const string &name, unsigned int i = 0);
301  virtual vector<string> *get_attr_vector(const string &name);
302  virtual void del_attr(const string &name, int i = -1);
303 
304  virtual Attr_iter attr_begin();
305  virtual Attr_iter attr_end();
306  virtual Attr_iter get_attr_iter(int i);
307  virtual string get_name(Attr_iter iter);
308  virtual bool is_container(Attr_iter iter);
309  virtual AttrTable *get_attr_table(Attr_iter iter);
310  virtual Attr_iter del_attr_table(Attr_iter iter);
311  virtual string get_type(Attr_iter iter);
312  virtual AttrType get_attr_type(Attr_iter iter);
313  virtual unsigned int get_attr_num(Attr_iter iter);
314  virtual string get_attr(Attr_iter iter, unsigned int i = 0);
315  virtual std::vector<string> *get_attr_vector(Attr_iter iter);
316  virtual bool is_global_attribute(Attr_iter iter);
317  virtual void set_is_global_attribute(Attr_iter iter, bool ga);
318 
319  virtual void add_container_alias(const string &name, AttrTable *src);
320  virtual void add_value_alias(AttrTable *at, const string &name,
321  const string &source);
322  virtual bool attr_alias(const string &alias,
323  AttrTable *at,
324  const string &name);
325  virtual bool attr_alias(const string &alias, const string &name);
326 
327  virtual void print(FILE *out, string pad = " ",
328  bool dereference = false);
329  virtual void print(ostream &out, string pad = " ",
330  bool dereference = false);
331 
332  virtual void print_xml(FILE *out, string pad = " ",
333  bool constrained = false);
334  virtual void print_xml(ostream &out, string pad = " ",
335  bool constrained = false);
336 
337  void print_xml_writer(XMLWriter &xml);
338 
339  void print_dap4(XMLWriter &xml);
340 
341  virtual void dump(ostream &strm) const ;
342 };
343 
344 } // namespace libdap
345 
346 #endif // _attrtable_h
std::vector< entry * >::iterator Attr_iter
Definition: AttrTable.h:229
virtual Attr_iter attr_end()
Definition: AttrTable.cc:718
Contains the attributes for a dataset.
Definition: AttrTable.h:142
string AttrType_to_String(const AttrType at)
Definition: AttrTable.cc:96
virtual string get_type(const string &name)
Get the type name of an attribute within this attribute table.
Definition: AttrTable.cc:612
void clone(const AttrTable &at)
Definition: AttrTable.cc:159
virtual void add_container_alias(const string &name, AttrTable *src)
Add an alias to a container held by this attribute table.
Definition: AttrTable.cc:907
virtual void print_xml(FILE *out, string pad=" ", bool constrained=false)
Definition: AttrTable.cc:1312
virtual ~AttrTable()
Definition: AttrTable.cc:208
virtual bool is_global_attribute() const
Definition: AttrTable.h:276
virtual string get_attr(const string &name, unsigned int i=0)
Definition: AttrTable.cc:857
virtual void del_attr(const string &name, int i=-1)
Deletes an attribute.
Definition: AttrTable.cc:674
virtual void set_is_global_attribute(bool ga)
Definition: AttrTable.h:277
virtual AttrTable * recurrsive_find(const string &target, Attr_iter *location)
Definition: AttrTable.cc:512
void print_xml_writer(XMLWriter &xml)
Definition: AttrTable.cc:1424
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
Definition: AttrTable.cc:1242
virtual AttrTable * find_container(const string &target)
Find an attribute with a given name.
Definition: AttrTable.cc:565
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:237
AttrTable & operator=(const AttrTable &rhs)
Definition: AttrTable.cc:214
void print_dap4(XMLWriter &xml)
Definition: AttrTable.cc:1497
AttrTable * attributes
Definition: AttrTable.h:163
virtual Attr_iter get_attr_iter(int i)
Definition: AttrTable.cc:731
virtual void add_value_alias(AttrTable *at, const string &name, const string &source)
Add an alias for an attribute.
Definition: AttrTable.cc:941
virtual bool is_container(Attr_iter iter)
Definition: AttrTable.cc:745
AttrType String_to_AttrType(const string &s)
Definition: AttrTable.cc:126
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition: AttrTable.cc:409
AttrTable * simple_find_container(const string &target)
Definition: AttrTable.cc:582
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition: AttrTable.cc:606
void clone(const entry &rhs)
Definition: AttrTable.h:191
friend class AttrTableTest
Definition: AttrTable.h:246
virtual void erase()
Erase the attribute table.
Definition: AttrTable.cc:1035
entry & operator=(const entry &rhs)
Definition: AttrTable.h:218
entry(const entry &rhs)
Definition: AttrTable.h:169
virtual bool attr_alias(const string &alias, AttrTable *at, const string &name)
Adds an alias to the set of attributes.
Definition: AttrTable.cc:1014
virtual Attr_iter attr_begin()
Definition: AttrTable.cc:710
std::vector< entry * >::const_iterator Attr_citer
Definition: AttrTable.h:228
virtual unsigned int get_attr_num(const string &name)
Get the number of attributes in this container.
Definition: AttrTable.cc:633
virtual AttrTable * get_parent() const
Definition: AttrTable.h:271
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
Definition: AttrTable.cc:306
libdap base object for common functionality of libdap objects
Definition: DapObj.h:55
void simple_print(FILE *out, string pad, Attr_iter i, bool dereference)
Definition: AttrTable.cc:1102
virtual AttrType get_attr_type(const string &name)
Get the type of an attribute.
Definition: AttrTable.cc:620
virtual void dump(ostream &strm) const
dumps information about this object
Definition: AttrTable.cc:1509
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
Definition: AttrTable.cc:652
virtual void set_name(const string &n)
Set the name of this attribute table.
Definition: AttrTable.cc:244
std::vector< string > * attr
Definition: AttrTable.h:164
virtual Attr_iter del_attr_table(Attr_iter iter)
Definition: AttrTable.cc:780
virtual void find(const string &target, AttrTable **at, Attr_iter *iter)
Definition: AttrTable.cc:480
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:230
AttrType
Definition: AttrTable.h:81
Attr_iter simple_find(const string &target)
Definition: AttrTable.cc:540