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