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
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
00147
00148 public:
00153 struct entry
00154 {
00155 string name;
00156 AttrType type;
00157
00158 bool is_alias;
00159 string aliased_to;
00160
00161
00162
00163 AttrTable *attributes;
00164 std::vector<string> *attr;
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)
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 if (rhs.is_alias)
00206 attributes = rhs.attributes;
00207 else
00208 attributes = new AttrTable(*rhs.attributes);
00209 break;
00210 }
00211 default: {
00212 if (rhs.is_alias)
00213 attr = rhs.attr;
00214 else
00215 attr = new std::vector<string>(*rhs.attr);
00216 break;
00217 }
00218 }
00219 }
00220
00221 entry &operator=(const entry &rhs)
00222 {
00223 if (this != &rhs) {
00224 delete_entry();
00225 clone(rhs);
00226 }
00227 return *this;
00228 }
00229 };
00230
00231 typedef std::vector<entry *>::const_iterator Attr_citer ;
00232 typedef std::vector<entry *>::iterator Attr_iter ;
00233
00234 private:
00235 string d_name;
00236 AttrTable *d_parent;
00237 std::vector<entry *> attr_map;
00238
00239 Attr_iter simple_find(const string &target);
00240 AttrTable *simple_find_container(const string &target);
00241
00242 void delete_attr_table();
00243
00244 friend class AttrTableTest;
00245
00246 protected:
00247 void clone(const AttrTable &at);
00248
00249 void simple_print(FILE *out, string pad, Attr_iter i,
00250 bool dereference);
00251 void simple_print(ostream &out, string pad, Attr_iter i,
00252 bool dereference);
00253
00254 public:
00255 AttrTable();
00256 AttrTable(const AttrTable &rhs);
00257 virtual ~AttrTable();
00258 AttrTable & operator=(const AttrTable &rhs);
00259
00260 virtual void erase();
00261
00262 virtual unsigned int get_size() const;
00263 virtual string get_name() const;
00264 virtual void set_name(const string &n);
00265
00269 virtual AttrTable *get_parent() const
00270 {
00271 return d_parent;
00272 }
00273
00274 virtual unsigned int append_attr(const string &name, const string &type,
00275 const string &value);
00276 virtual unsigned int append_attr(const string &name, const string &type,
00277 vector<string> *values);
00278
00279 virtual AttrTable *append_container(const string &name);
00280 virtual AttrTable *append_container(AttrTable *at, const string &name);
00281
00282 virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
00283 virtual AttrTable *find_container(const string &target);
00284 virtual AttrTable *recurrsive_find(const string &target,
00285 Attr_iter *location);
00286
00287 virtual AttrTable *get_attr_table(const string &name);
00288 virtual string get_type(const string &name);
00289 virtual AttrType get_attr_type(const string &name);
00290 virtual unsigned int get_attr_num(const string &name);
00291 virtual string get_attr(const string &name, unsigned int i = 0);
00292 virtual vector<string> *get_attr_vector(const string &name);
00293 virtual void del_attr(const string &name, int i = -1);
00294
00295 virtual Attr_iter attr_begin();
00296 virtual Attr_iter attr_end();
00297 virtual Attr_iter get_attr_iter(int i);
00298 virtual string get_name(Attr_iter iter);
00299 virtual bool is_container(Attr_iter iter);
00300 virtual AttrTable *get_attr_table(Attr_iter iter);
00301 virtual Attr_iter del_attr_table(Attr_iter iter);
00302 virtual string get_type(Attr_iter iter);
00303 virtual AttrType get_attr_type(Attr_iter iter);
00304 virtual unsigned int get_attr_num(Attr_iter iter);
00305 virtual string get_attr(Attr_iter iter, unsigned int i = 0);
00306 virtual std::vector<string> *get_attr_vector(Attr_iter iter);
00307
00308 virtual void add_container_alias(const string &name, AttrTable *src);
00309 virtual void add_value_alias(AttrTable *at, const string &name,
00310 const string &source);
00311 virtual bool attr_alias(const string &alias,
00312 AttrTable *at,
00313 const string &name);
00314 virtual bool attr_alias(const string &alias, const string &name);
00315
00316 virtual void print(FILE *out, string pad = " ",
00317 bool dereference = false);
00318 virtual void print(ostream &out, string pad = " ",
00319 bool dereference = false);
00320
00321 virtual void print_xml(FILE *out, string pad = " ",
00322 bool constrained = false);
00323 virtual void print_xml(ostream &out, string pad = " ",
00324 bool constrained = false);
00325
00326 virtual void dump(ostream &strm) const ;
00327 };
00328
00329 }
00330
00331 #endif // _attrtable_h