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 
00073 enum AttrType {
00074     Attr_unknown,
00075     Attr_container,
00076     Attr_byte,
00077     Attr_int16,
00078     Attr_uint16,
00079     Attr_int32,
00080     Attr_uint32,
00081     Attr_float32,
00082     Attr_float64,
00083     Attr_string,
00084     Attr_url
00085 };
00086 
00087 string AttrType_to_String(const AttrType at);
00088 AttrType String_to_AttrType(const string &s);
00089 
00141 class AttrTable : public DapObj
00142 {
00143     // entry needs to be made public to make up for issues with this class'
00144     // design. It should probably be moved to it's own class. 05/22/03 jhrg
00145 public:
00150     struct entry
00151     {
00152         string name;
00153         AttrType type;
00154 
00155         bool is_alias;
00156         string aliased_to;
00157 
00158         // If type == Attr_container, use attributes to read the contained
00159         // table, otherwise use attr to read the vector of values.
00160         AttrTable *attributes;
00161         std::vector<string> *attr; // a vector of values. jhrg 12/5/94
00162 
00163         entry(): name(""), type(Attr_unknown), is_alias(false),
00164                 aliased_to("")
00165         {
00166             attributes = 0;
00167             attr = 0;
00168         }
00169 
00170         entry(const entry &rhs)
00171         {
00172             clone(rhs);
00173         }
00174 
00175         void delete_entry()
00176         {
00177             if (is_alias) // alias copies the pointers.
00178                 return;
00179             if (type == Attr_container) {
00180                 delete attributes; attributes = 0;
00181             }
00182             else {
00183                 delete attr; attr = 0;
00184             }
00185         }
00186 
00187         virtual ~entry()
00188         {
00189             delete_entry();
00190         }
00191 
00192         void clone(const entry &rhs)
00193         {
00194             name = rhs.name;
00195             type = rhs.type;
00196             is_alias = rhs.is_alias;
00197             aliased_to = rhs.aliased_to;
00198             switch (rhs.type) {
00199             case Attr_unknown:
00200                 break;
00201             case Attr_container: {
00202                     AttrTable *src_atp = rhs.attributes;
00203                     AttrTable *dest_atp = new AttrTable(*src_atp);
00204                     attributes = dest_atp;
00205                     break;
00206                 }
00207             default: {
00208                     std::vector<string> *src_attr = rhs.attr;
00209                     std::vector<string> *dest_attr = new std::vector<string>(*src_attr);
00210                     attr = dest_attr;
00211                     break;
00212                 }
00213             }
00214         }
00215 
00216         entry &operator=(const entry &rhs)
00217         {
00218             if (this != &rhs) {
00219                 delete_entry();
00220                 clone(rhs);
00221             }
00222             return *this;
00223         }
00224     };
00225 
00226     typedef std::vector<entry *>::const_iterator Attr_citer ;
00227     typedef std::vector<entry *>::iterator Attr_iter ;
00228 
00229 private:
00230     string d_name;
00231     AttrTable *d_parent;
00232     std::vector<entry *> attr_map;
00233 
00234     Attr_iter simple_find(const string &target);
00235     AttrTable *simple_find_container(const string &target);
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 
00247 public:
00248     AttrTable();
00249     AttrTable(const AttrTable &rhs);
00250     virtual ~AttrTable();
00251     AttrTable & operator=(const AttrTable &rhs);
00252 
00253     void erase();
00254 
00255     unsigned int get_size() const;
00256     string get_name() const;
00257     void set_name(const string &n);
00261     AttrTable *get_parent() const
00262     {
00263         return d_parent;
00264     }
00265 
00266     unsigned int append_attr(const string &name, const string &type,
00267                              const string &value);
00268     unsigned int append_attr(const string &name, const string &type,
00269                              vector<string> *values);
00270 
00271     AttrTable *append_container(const string &name);
00272     AttrTable *append_container(AttrTable *at, const string &name);
00273 
00274     void find(const string &target, AttrTable **at, Attr_iter *iter);
00275     AttrTable *find_container(const string &target);
00276     AttrTable *recurrsive_find(const string &target, Attr_iter *location);
00277 
00278     AttrTable *get_attr_table(const string &name);
00279     string get_type(const string &name);
00280     AttrType get_attr_type(const string &name);
00281     unsigned int get_attr_num(const string &name);
00282     string get_attr(const string &name, unsigned int i = 0);
00283     vector<string> *get_attr_vector(const string &name);
00284     void del_attr(const string &name, int i = -1);
00285 
00286     Attr_iter attr_begin();
00287     Attr_iter attr_end();
00288     Attr_iter get_attr_iter(int i);
00289     string get_name(Attr_iter iter);
00290     bool is_container(Attr_iter iter);
00291     AttrTable *get_attr_table(Attr_iter iter);
00292     Attr_iter del_attr_table(Attr_iter iter);
00293     string get_type(Attr_iter iter);
00294     AttrType get_attr_type(Attr_iter iter);
00295     unsigned int get_attr_num(Attr_iter iter);
00296     string get_attr(Attr_iter iter, unsigned int i = 0);
00297     std::vector<string> *get_attr_vector(Attr_iter iter);
00298 
00299     void add_container_alias(const string &name, AttrTable *src);
00300     void add_value_alias(AttrTable *das, const string &name,
00301                          const string &source);
00302     bool attr_alias(const string &alias, AttrTable *at, const string &name);
00303     bool attr_alias(const string &alias, const string &name);
00304 
00305     void print(FILE *out, string pad = "    ", bool dereference = false);
00306 
00307     void print_xml(FILE *out, string pad = "    ", bool constrained = false);
00308 
00309     virtual void dump(ostream &strm) const ;
00310 };
00311 
00312 #endif // _attrtable_h

Generated on Wed Jun 27 12:56:38 2007 for libdap++ by  doxygen 1.4.7