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         // If type == Attr_container, use attributes to read the contained
00164         // table, otherwise use attr to read the vector of values.
00165         AttrTable *attributes;
00166         std::vector<string> *attr; // a vector of values. jhrg 12/5/94
00167 
00168         entry(): name(""), type(Attr_unknown), is_alias(false),
00169                 aliased_to(""), attributes(0), attr(0) {}
00170 
00171         entry(const entry &rhs)
00172         {
00173             clone(rhs);
00174         }
00175 
00176         void delete_entry()
00177         {
00178             if (is_alias) // alias copies the pointers.
00179                 return;
00180             if (type == Attr_container) {
00181                 delete attributes; attributes = 0;
00182             }
00183             else {
00184                 delete attr; attr = 0;
00185             }
00186         }
00187 
00188         virtual ~entry()
00189         {
00190             delete_entry();
00191         }
00192 
00193         void clone(const entry &rhs)
00194         {
00195             name = rhs.name;
00196             type = rhs.type;
00197             is_alias = rhs.is_alias;
00198             aliased_to = rhs.aliased_to;
00199             switch (rhs.type) {
00200             case Attr_unknown:
00201                 break;
00202             case Attr_container: {
00203                 if (rhs.is_alias)
00204                     attributes = rhs.attributes;
00205                 else
00206                     attributes = new AttrTable(*rhs.attributes);
00207                 break;
00208             }
00209             default: {
00210                 if (rhs.is_alias)
00211                     attr = rhs.attr;
00212                 else
00213                     attr = new std::vector<string>(*rhs.attr);
00214                 break;
00215             }
00216             }
00217         }
00218 
00219         entry &operator=(const entry &rhs)
00220         {
00221             if (this != &rhs) {
00222                 delete_entry();
00223                 clone(rhs);
00224             }
00225             return *this;
00226         }
00227     };
00228 
00229     typedef std::vector<entry *>::const_iterator Attr_citer ;
00230     typedef std::vector<entry *>::iterator Attr_iter ;
00231 
00232 private:
00233     string d_name;
00234     AttrTable *d_parent;
00235     std::vector<entry *> attr_map;
00236 
00237     void delete_attr_table();
00238 
00239     friend class AttrTableTest;
00240 
00241 protected:
00242     void clone(const AttrTable &at);
00243 
00244     void simple_print(FILE *out, string pad, Attr_iter i,
00245                       bool dereference);
00246     void simple_print(ostream &out, string pad, Attr_iter i,
00247                       bool dereference);
00248 
00249 public:
00250     AttrTable();
00251     AttrTable(const AttrTable &rhs);
00252     virtual ~AttrTable();
00253     AttrTable & operator=(const AttrTable &rhs);
00254 
00255     virtual void erase();
00256 
00257     virtual unsigned int get_size() const;
00258     virtual string get_name() const;
00259     virtual void set_name(const string &n);
00260 
00264     virtual AttrTable *get_parent() const
00265     {
00266         return d_parent;
00267     }
00268 
00269     virtual unsigned int append_attr(const string &name, const string &type,
00270                                      const string &value);
00271     virtual unsigned int append_attr(const string &name, const string &type,
00272                                      vector<string> *values);
00273 
00274     virtual AttrTable *append_container(const string &name);
00275     virtual AttrTable *append_container(AttrTable *at, const string &name);
00276 
00277     virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
00278     virtual AttrTable *find_container(const string &target);
00279     virtual AttrTable *recurrsive_find(const string &target,
00280                                        Attr_iter *location);
00281 
00282     Attr_iter simple_find(const string &target);
00283     AttrTable *simple_find_container(const string &target);
00284 
00285 
00286     virtual AttrTable *get_attr_table(const string &name);
00287     virtual string get_type(const string &name);
00288     virtual AttrType get_attr_type(const string &name);
00289     virtual unsigned int get_attr_num(const string &name);
00290     virtual string get_attr(const string &name, unsigned int i = 0);
00291     virtual vector<string> *get_attr_vector(const string &name);
00292     virtual void del_attr(const string &name, int i = -1);
00293 
00294     virtual Attr_iter attr_begin();
00295     virtual Attr_iter attr_end();
00296     virtual Attr_iter get_attr_iter(int i);
00297     virtual string get_name(Attr_iter iter);
00298     virtual bool is_container(Attr_iter iter);
00299     virtual AttrTable *get_attr_table(Attr_iter iter);
00300     virtual Attr_iter del_attr_table(Attr_iter iter);
00301     virtual string get_type(Attr_iter iter);
00302     virtual AttrType get_attr_type(Attr_iter iter);
00303     virtual unsigned int get_attr_num(Attr_iter iter);
00304     virtual string get_attr(Attr_iter iter, unsigned int i = 0);
00305     virtual std::vector<string> *get_attr_vector(Attr_iter iter);
00306 
00307     virtual void add_container_alias(const string &name, AttrTable *src);
00308     virtual void add_value_alias(AttrTable *at, const string &name,
00309                          const string &source);
00310     virtual bool attr_alias(const string &alias,
00311                             AttrTable *at,
00312                             const string &name);
00313     virtual bool attr_alias(const string &alias, const string &name);
00314 
00315     virtual void print(FILE *out, string pad = "    ",
00316                        bool dereference = false);
00317     virtual void print(ostream &out, string pad = "    ",
00318                        bool dereference = false);
00319 
00320     virtual void print_xml(FILE *out, string pad = "    ",
00321                            bool constrained = false);
00322     virtual void print_xml(ostream &out, string pad = "    ",
00323                            bool constrained = false);
00324 
00325     virtual void dump(ostream &strm) const ;
00326 };
00327 
00328 } // namespace libdap
00329 
00330 #endif // _attrtable_h

Generated on Wed Feb 10 16:08:01 2010 for libdap++ by  doxygen 1.4.7