00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00149
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;
00164
00165
00166
00167 AttrTable *attributes;
00168 std::vector<string> *attr;
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)
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
00241
00242
00243
00244
00245
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 }
00345
00346 #endif // _attrtable_h