libdap++  Updated for version 3.8.2
AttrTable.h
Go to the documentation of this file.
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 // (c) COPYRIGHT URI/MIT 1994-1999
00027 // Please read the full copyright statement in the file COPYRIGHT_URI.
00028 //
00029 // Authors:
00030 //      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>
00031 
00032 // An AttrTable is a table of attributes (type-name-value tuples).
00033 
00034 #ifndef _attrtable_h
00035 #define _attrtable_h 1
00036 
00037 
00038 #include <string>
00039 #include <vector>
00040 
00041 #ifndef _error_h
00042 #include "Error.h"
00043 #endif
00044 
00045 using std::vector;
00046 using std::string;
00047 using std::vector;
00048 
00049 #ifndef A_DapObj_h
00050 #include "DapObj.h"
00051 #endif
00052 
00053 #ifndef XMLWRITER_H_
00054 #include "XMLWriter.h"
00055 #endif
00056 
00057 namespace libdap
00058 {
00059 
00081 enum AttrType {
00082     Attr_unknown,
00083     Attr_container,
00084     Attr_byte,
00085     Attr_int16,
00086     Attr_uint16,
00087     Attr_int32,
00088     Attr_uint32,
00089     Attr_float32,
00090     Attr_float64,
00091     Attr_string,
00092     Attr_url,
00093     Attr_other_xml
00094 };
00095 
00096 string AttrType_to_String(const AttrType at);
00097 AttrType String_to_AttrType(const string &s);
00098 
00150 class AttrTable : public DapObj
00151 {
00152     // entry needs to be made public to make up for issues with this class'
00153     // design. It should probably be moved to it's own class. 05/22/03 jhrg
00154 public:
00159     struct entry
00160     {
00161         string name;
00162         AttrType type;
00163 
00164         bool is_alias;
00165         string aliased_to;
00166 
00167         bool is_global; // use this to mark non-container attributes. see below.
00168 
00169         // If type == Attr_container, use attributes to read the contained
00170         // table, otherwise use attr to read the vector of values.
00171         AttrTable *attributes;
00172         std::vector<string> *attr; // a vector of values. jhrg 12/5/94
00173 
00174         entry(): name(""), type(Attr_unknown), is_alias(false),
00175                 aliased_to(""), is_global(true), attributes(0), attr(0) {}
00176 
00177         entry(const entry &rhs)
00178         {
00179             clone(rhs);
00180         }
00181 
00182         void delete_entry()
00183         {
00184             if (is_alias) // alias copies the pointers.
00185                 return;
00186             if (type == Attr_container) {
00187                 delete attributes; attributes = 0;
00188             }
00189             else {
00190                 delete attr; attr = 0;
00191             }
00192         }
00193 
00194         virtual ~entry()
00195         {
00196             delete_entry();
00197         }
00198 
00199         void clone(const entry &rhs)
00200         {
00201             name = rhs.name;
00202             type = rhs.type;
00203             is_alias = rhs.is_alias;
00204             aliased_to = rhs.aliased_to;
00205             is_global = rhs.is_global;
00206             switch (rhs.type) {
00207             case Attr_unknown:
00208                 break;
00209             case Attr_container: {
00210                 if (rhs.is_alias)
00211                     attributes = rhs.attributes;
00212                 else
00213                     attributes = new AttrTable(*rhs.attributes);
00214                 break;
00215             }
00216             default: {
00217                 if (rhs.is_alias)
00218                     attr = rhs.attr;
00219                 else
00220                     attr = new std::vector<string>(*rhs.attr);
00221                 break;
00222             }
00223             }
00224         }
00225 
00226         entry &operator=(const entry &rhs)
00227         {
00228             if (this != &rhs) {
00229                 delete_entry();
00230                 clone(rhs);
00231             }
00232             return *this;
00233         }
00234     };
00235 
00236     typedef std::vector<entry *>::const_iterator Attr_citer ;
00237     typedef std::vector<entry *>::iterator Attr_iter ;
00238 
00239 private:
00240     string d_name;
00241     AttrTable *d_parent;
00242     std::vector<entry *> attr_map;
00243 
00244     // Use this to mark container attributes. Look at the methods
00245     // is_global_attribute() and set_is_...., esp. at the versions that take
00246     // an iterator. This code is tricky because it has to track both whole
00247     // containers that are global and individual attributes that are 'global'
00248     // relative to a constructor. That is, there are some attributes that are
00249     // bound to a container and not any of the container's children.
00250     bool d_is_global_attribute;
00251 
00252     void delete_attr_table();
00253 
00254     friend class AttrTableTest;
00255 
00256 protected:
00257     void clone(const AttrTable &at);
00258 
00259     void simple_print(FILE *out, string pad, Attr_iter i,
00260                       bool dereference);
00261     void simple_print(ostream &out, string pad, Attr_iter i,
00262                       bool dereference);
00263 
00264 public:
00265     AttrTable();
00266     AttrTable(const AttrTable &rhs);
00267     virtual ~AttrTable();
00268     AttrTable & operator=(const AttrTable &rhs);
00269 
00270     virtual void erase();
00271 
00272     virtual unsigned int get_size() const;
00273     virtual string get_name() const;
00274     virtual void set_name(const string &n);
00275 
00279     virtual AttrTable *get_parent() const
00280     {
00281         return d_parent;
00282     }
00283 
00284     virtual bool is_global_attribute() const { return d_is_global_attribute; }
00285     virtual void set_is_global_attribute(bool ga) { d_is_global_attribute = ga; }
00286 
00287     virtual unsigned int append_attr(const string &name, const string &type,
00288                                      const string &value);
00289     virtual unsigned int append_attr(const string &name, const string &type,
00290                                      vector<string> *values);
00291 
00292     virtual AttrTable *append_container(const string &name);
00293     virtual AttrTable *append_container(AttrTable *at, const string &name);
00294 
00295     virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
00296     virtual AttrTable *find_container(const string &target);
00297     virtual AttrTable *recurrsive_find(const string &target,
00298                                        Attr_iter *location);
00299 
00300     Attr_iter simple_find(const string &target);
00301     AttrTable *simple_find_container(const string &target);
00302 
00303 
00304     virtual AttrTable *get_attr_table(const string &name);
00305     virtual string get_type(const string &name);
00306     virtual AttrType get_attr_type(const string &name);
00307     virtual unsigned int get_attr_num(const string &name);
00308     virtual string get_attr(const string &name, unsigned int i = 0);
00309     virtual vector<string> *get_attr_vector(const string &name);
00310     virtual void del_attr(const string &name, int i = -1);
00311 
00312     virtual Attr_iter attr_begin();
00313     virtual Attr_iter attr_end();
00314     virtual Attr_iter get_attr_iter(int i);
00315     virtual string get_name(Attr_iter iter);
00316     virtual bool is_container(Attr_iter iter);
00317     virtual AttrTable *get_attr_table(Attr_iter iter);
00318     virtual Attr_iter del_attr_table(Attr_iter iter);
00319     virtual string get_type(Attr_iter iter);
00320     virtual AttrType get_attr_type(Attr_iter iter);
00321     virtual unsigned int get_attr_num(Attr_iter iter);
00322     virtual string get_attr(Attr_iter iter, unsigned int i = 0);
00323     virtual std::vector<string> *get_attr_vector(Attr_iter iter);
00324     virtual bool is_global_attribute(Attr_iter iter);
00325     virtual void set_is_global_attribute(Attr_iter iter, bool ga);
00326 
00327     virtual void add_container_alias(const string &name, AttrTable *src);
00328     virtual void add_value_alias(AttrTable *at, const string &name,
00329                          const string &source);
00330     virtual bool attr_alias(const string &alias,
00331                             AttrTable *at,
00332                             const string &name);
00333     virtual bool attr_alias(const string &alias, const string &name);
00334 
00335     virtual void print(FILE *out, string pad = "    ",
00336                        bool dereference = false);
00337     virtual void print(ostream &out, string pad = "    ",
00338                        bool dereference = false);
00339 
00340     virtual void print_xml(FILE *out, string pad = "    ",
00341                            bool constrained = false);
00342     virtual void print_xml(ostream &out, string pad = "    ",
00343                            bool constrained = false);
00344 
00345     void print_xml_writer(XMLWriter &xml);
00346 
00347     virtual void dump(ostream &strm) const ;
00348 };
00349 
00350 } // namespace libdap
00351 
00352 #endif // _attrtable_h