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 namespace libdap
00054 {
00055 
00077 enum AttrType {
00078     Attr_unknown,
00079     Attr_container,
00080     Attr_byte,
00081     Attr_int16,
00082     Attr_uint16,
00083     Attr_int32,
00084     Attr_uint32,
00085     Attr_float32,
00086     Attr_float64,
00087     Attr_string,
00088     Attr_url,
00089     Attr_other_xml
00090 };
00091 
00092 string AttrType_to_String(const AttrType at);
00093 AttrType String_to_AttrType(const string &s);
00094 
00146 class AttrTable : public DapObj
00147 {
00148     // entry needs to be made public to make up for issues with this class'
00149     // design. It should probably be moved to it's own class. 05/22/03 jhrg
00150 public:
00155     struct entry
00156     {
00157         string name;
00158         AttrType type;
00159 
00160         bool is_alias;
00161         string aliased_to;
00162 
00163         bool is_global; // use this to mark non-container attributes. see below.
00164 
00165         // If type == Attr_container, use attributes to read the contained
00166         // table, otherwise use attr to read the vector of values.
00167         AttrTable *attributes;
00168         std::vector<string> *attr; // a vector of values. jhrg 12/5/94
00169 
00170         entry(): name(""), type(Attr_unknown), is_alias(false),
00171                 aliased_to(""), is_global(true), attributes(0), attr(0) {}
00172 
00173         entry(const entry &rhs)
00174         {
00175             clone(rhs);
00176         }
00177 
00178         void delete_entry()
00179         {
00180             if (is_alias) // alias copies the pointers.
00181                 return;
00182             if (type == Attr_container) {
00183                 delete attributes; attributes = 0;
00184             }
00185             else {
00186                 delete attr; attr = 0;
00187             }
00188         }
00189 
00190         virtual ~entry()
00191         {
00192             delete_entry();
00193         }
00194 
00195         void clone(const entry &rhs)
00196         {
00197             name = rhs.name;
00198             type = rhs.type;
00199             is_alias = rhs.is_alias;
00200             aliased_to = rhs.aliased_to;
00201             is_global = rhs.is_global;
00202             switch (rhs.type) {
00203             case Attr_unknown:
00204                 break;
00205             case Attr_container: {
00206                 if (rhs.is_alias)
00207                     attributes = rhs.attributes;
00208                 else
00209                     attributes = new AttrTable(*rhs.attributes);
00210                 break;
00211             }
00212             default: {
00213                 if (rhs.is_alias)
00214                     attr = rhs.attr;
00215                 else
00216                     attr = new std::vector<string>(*rhs.attr);
00217                 break;
00218             }
00219             }
00220         }
00221 
00222         entry &operator=(const entry &rhs)
00223         {
00224             if (this != &rhs) {
00225                 delete_entry();
00226                 clone(rhs);
00227             }
00228             return *this;
00229         }
00230     };
00231 
00232     typedef std::vector<entry *>::const_iterator Attr_citer ;
00233     typedef std::vector<entry *>::iterator Attr_iter ;
00234 
00235 private:
00236     string d_name;
00237     AttrTable *d_parent;
00238     std::vector<entry *> attr_map;
00239 
00240     // Use this to mark container attributes. Look at the methods
00241     // is_global_attribute() and set_is_...., esp. at the versions that take
00242     // an iterator. This code is tricky because it has to track both whole
00243     // containers that are global and individual attributes that are 'global'
00244     // relative to a constructor. That is, there are some attributes that are
00245     // bound to a container and not any of the container's children.
00246     bool d_is_global_attribute;
00247 
00248     void delete_attr_table();
00249 
00250     friend class AttrTableTest;
00251 
00252 protected:
00253     void clone(const AttrTable &at);
00254 
00255     void simple_print(FILE *out, string pad, Attr_iter i,
00256                       bool dereference);
00257     void simple_print(ostream &out, string pad, Attr_iter i,
00258                       bool dereference);
00259 
00260 public:
00261     AttrTable();
00262     AttrTable(const AttrTable &rhs);
00263     virtual ~AttrTable();
00264     AttrTable & operator=(const AttrTable &rhs);
00265 
00266     virtual void erase();
00267 
00268     virtual unsigned int get_size() const;
00269     virtual string get_name() const;
00270     virtual void set_name(const string &n);
00271 
00275     virtual AttrTable *get_parent() const
00276     {
00277         return d_parent;
00278     }
00279 
00280     virtual bool is_global_attribute() const { return d_is_global_attribute; }
00281     virtual void set_is_global_attribute(bool ga) { d_is_global_attribute = ga; }
00282 
00283     virtual unsigned int append_attr(const string &name, const string &type,
00284                                      const string &value);
00285     virtual unsigned int append_attr(const string &name, const string &type,
00286                                      vector<string> *values);
00287 
00288     virtual AttrTable *append_container(const string &name);
00289     virtual AttrTable *append_container(AttrTable *at, const string &name);
00290 
00291     virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
00292     virtual AttrTable *find_container(const string &target);
00293     virtual AttrTable *recurrsive_find(const string &target,
00294                                        Attr_iter *location);
00295 
00296     Attr_iter simple_find(const string &target);
00297     AttrTable *simple_find_container(const string &target);
00298 
00299 
00300     virtual AttrTable *get_attr_table(const string &name);
00301     virtual string get_type(const string &name);
00302     virtual AttrType get_attr_type(const string &name);
00303     virtual unsigned int get_attr_num(const string &name);
00304     virtual string get_attr(const string &name, unsigned int i = 0);
00305     virtual vector<string> *get_attr_vector(const string &name);
00306     virtual void del_attr(const string &name, int i = -1);
00307 
00308     virtual Attr_iter attr_begin();
00309     virtual Attr_iter attr_end();
00310     virtual Attr_iter get_attr_iter(int i);
00311     virtual string get_name(Attr_iter iter);
00312     virtual bool is_container(Attr_iter iter);
00313     virtual AttrTable *get_attr_table(Attr_iter iter);
00314     virtual Attr_iter del_attr_table(Attr_iter iter);
00315     virtual string get_type(Attr_iter iter);
00316     virtual AttrType get_attr_type(Attr_iter iter);
00317     virtual unsigned int get_attr_num(Attr_iter iter);
00318     virtual string get_attr(Attr_iter iter, unsigned int i = 0);
00319     virtual std::vector<string> *get_attr_vector(Attr_iter iter);
00320     virtual bool is_global_attribute(Attr_iter iter);
00321     virtual void set_is_global_attribute(Attr_iter iter, bool ga);
00322 
00323     virtual void add_container_alias(const string &name, AttrTable *src);
00324     virtual void add_value_alias(AttrTable *at, const string &name,
00325                          const string &source);
00326     virtual bool attr_alias(const string &alias,
00327                             AttrTable *at,
00328                             const string &name);
00329     virtual bool attr_alias(const string &alias, const string &name);
00330 
00331     virtual void print(FILE *out, string pad = "    ",
00332                        bool dereference = false);
00333     virtual void print(ostream &out, string pad = "    ",
00334                        bool dereference = false);
00335 
00336     virtual void print_xml(FILE *out, string pad = "    ",
00337                            bool constrained = false);
00338     virtual void print_xml(ostream &out, string pad = "    ",
00339                            bool constrained = false);
00340 
00341     virtual void dump(ostream &strm) const ;
00342 };
00343 
00344 } // namespace libdap
00345 
00346 #endif // _attrtable_h