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
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
00144
00145 public:
00150 struct entry
00151 {
00152 string name;
00153 AttrType type;
00154
00155 bool is_alias;
00156 string aliased_to;
00157
00158
00159
00160 AttrTable *attributes;
00161 std::vector<string> *attr;
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)
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 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 void erase();
00256
00257 unsigned int get_size() const;
00258 string get_name() const;
00259 void set_name(const string &n);
00263 AttrTable *get_parent() const
00264 {
00265 return d_parent;
00266 }
00267
00268 unsigned int append_attr(const string &name, const string &type,
00269 const string &value);
00270 unsigned int append_attr(const string &name, const string &type,
00271 vector<string> *values);
00272
00273 AttrTable *append_container(const string &name);
00274 AttrTable *append_container(AttrTable *at, const string &name);
00275
00276 void find(const string &target, AttrTable **at, Attr_iter *iter);
00277 AttrTable *find_container(const string &target);
00278 AttrTable *recurrsive_find(const string &target, Attr_iter *location);
00279
00280 AttrTable *get_attr_table(const string &name);
00281 string get_type(const string &name);
00282 AttrType get_attr_type(const string &name);
00283 unsigned int get_attr_num(const string &name);
00284 string get_attr(const string &name, unsigned int i = 0);
00285 vector<string> *get_attr_vector(const string &name);
00286 void del_attr(const string &name, int i = -1);
00287
00288 Attr_iter attr_begin();
00289 Attr_iter attr_end();
00290 Attr_iter get_attr_iter(int i);
00291 string get_name(Attr_iter iter);
00292 bool is_container(Attr_iter iter);
00293 AttrTable *get_attr_table(Attr_iter iter);
00294 Attr_iter del_attr_table(Attr_iter iter);
00295 string get_type(Attr_iter iter);
00296 AttrType get_attr_type(Attr_iter iter);
00297 unsigned int get_attr_num(Attr_iter iter);
00298 string get_attr(Attr_iter iter, unsigned int i = 0);
00299 std::vector<string> *get_attr_vector(Attr_iter iter);
00300
00301 void add_container_alias(const string &name, AttrTable *src);
00302 void add_value_alias(AttrTable *das, const string &name,
00303 const string &source);
00304 bool attr_alias(const string &alias, AttrTable *at, const string &name);
00305 bool attr_alias(const string &alias, const string &name);
00306
00307 void print(FILE *out, string pad = " ", bool dereference = false);
00308 void print(ostream &out, string pad = " ", bool dereference = false);
00309
00310 void print_xml(FILE *out, string pad = " ", bool constrained = false);
00311 void print_xml(ostream &out, string pad = " ", bool constrained = false);
00312
00313 virtual void dump(ostream &strm) const ;
00314 };
00315
00316 #endif // _attrtable_h