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 
00076 enum AttrType {
00077     Attr_unknown,
00078     Attr_container,
00079     Attr_byte,
00080     Attr_int16,
00081     Attr_uint16,
00082     Attr_int32,
00083     Attr_uint32,
00084     Attr_float32,
00085     Attr_float64,
00086     Attr_string,
00087     Attr_url
00088 };
00089 
00090 string AttrType_to_String(const AttrType at);
00091 AttrType String_to_AttrType(const string &s);
00092 
00144 class AttrTable : public DapObj
00145 {
00146     // entry needs to be made public to make up for issues with this class'
00147     // design. It should probably be moved to it's own class. 05/22/03 jhrg
00148 public:
00153     struct entry
00154     {
00155         string name;
00156         AttrType type;
00157 
00158         bool is_alias;
00159         string aliased_to;
00160 
00161         // If type == Attr_container, use attributes to read the contained
00162         // table, otherwise use attr to read the vector of values.
00163         AttrTable *attributes;
00164         std::vector<string> *attr; // a vector of values. jhrg 12/5/94
00165 
00166         entry(): name(""), type(Attr_unknown), is_alias(false),
00167                 aliased_to("")
00168         {
00169             attributes = 0;
00170             attr = 0;
00171         }
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             switch (rhs.type) {
00202             case Attr_unknown:
00203                 break;
00204             case Attr_container: {
00205                     AttrTable *src_atp = rhs.attributes;
00206                     AttrTable *dest_atp = new AttrTable(*src_atp);
00207                     attributes = dest_atp;
00208                     break;
00209                 }
00210             default: {
00211                     std::vector<string> *src_attr = rhs.attr;
00212                     std::vector<string> *dest_attr = new std::vector<string>(*src_attr);
00213                     attr = dest_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     Attr_iter simple_find(const string &target);
00238     AttrTable *simple_find_container(const string &target);
00239 
00240     void delete_attr_table();
00241 
00242     friend class AttrTableTest;
00243 
00244 protected:
00245     void clone(const AttrTable &at);
00246 
00247     void simple_print(FILE *out, string pad, Attr_iter i,
00248                       bool dereference);
00249     void simple_print(ostream &out, string pad, Attr_iter i,
00250                       bool dereference);
00251 
00252 public:
00253     AttrTable();
00254     AttrTable(const AttrTable &rhs);
00255     virtual ~AttrTable();
00256     AttrTable & operator=(const AttrTable &rhs);
00257 
00258     void erase();
00259 
00260     unsigned int get_size() const;
00261     string get_name() const;
00262     void set_name(const string &n);
00266     AttrTable *get_parent() const
00267     {
00268         return d_parent;
00269     }
00270 
00271     unsigned int append_attr(const string &name, const string &type,
00272                              const string &value);
00273     unsigned int append_attr(const string &name, const string &type,
00274                              vector<string> *values);
00275 
00276     AttrTable *append_container(const string &name);
00277     AttrTable *append_container(AttrTable *at, const string &name);
00278 
00279     void find(const string &target, AttrTable **at, Attr_iter *iter);
00280     AttrTable *find_container(const string &target);
00281     AttrTable *recurrsive_find(const string &target, Attr_iter *location);
00282 
00283     AttrTable *get_attr_table(const string &name);
00284     string get_type(const string &name);
00285     AttrType get_attr_type(const string &name);
00286     unsigned int get_attr_num(const string &name);
00287     string get_attr(const string &name, unsigned int i = 0);
00288     vector<string> *get_attr_vector(const string &name);
00289     void del_attr(const string &name, int i = -1);
00290 
00291     Attr_iter attr_begin();
00292     Attr_iter attr_end();
00293     Attr_iter get_attr_iter(int i);
00294     string get_name(Attr_iter iter);
00295     bool is_container(Attr_iter iter);
00296     AttrTable *get_attr_table(Attr_iter iter);
00297     Attr_iter del_attr_table(Attr_iter iter);
00298     string get_type(Attr_iter iter);
00299     AttrType get_attr_type(Attr_iter iter);
00300     unsigned int get_attr_num(Attr_iter iter);
00301     string get_attr(Attr_iter iter, unsigned int i = 0);
00302     std::vector<string> *get_attr_vector(Attr_iter iter);
00303 
00304     void add_container_alias(const string &name, AttrTable *src);
00305     void add_value_alias(AttrTable *das, const string &name,
00306                          const string &source);
00307     bool attr_alias(const string &alias, AttrTable *at, const string &name);
00308     bool attr_alias(const string &alias, const string &name);
00309 
00310     void print(FILE *out, string pad = "    ", bool dereference = false);
00311     void print(ostream &out, string pad = "    ", bool dereference = false);
00312 
00313     void print_xml(FILE *out, string pad = "    ", bool constrained = false);
00314     void print_xml(ostream &out, string pad = "    ", bool constrained = false);
00315 
00316     virtual void dump(ostream &strm) const ;
00317 };
00318 
00319 } // namespace libdap
00320 
00321 #endif // _attrtable_h

Generated on Tue Mar 4 18:01:54 2008 for libdap++ by  doxygen 1.5.1