00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _NAMED_ATTRIBUTE_H
00019 #define _NAMED_ATTRIBUTE_H
00020
00021 #include <string>
00022 #include <vector>
00023
00024 #include "oasys/debug/DebugUtils.h"
00025 #include "oasys/debug/Log.h"
00026 #include "oasys/serialize/Serialize.h"
00027
00028 namespace dtn {
00029
00030 class AttributeName {
00031 public:
00032 AttributeName(const std::string& name)
00033 : name_(name) {}
00034 AttributeName(const oasys::Builder&)
00035 : name_("") {}
00036
00037 const std::string& name() const { return name_; }
00038
00039 private:
00040
00041 std::string name_;
00042 };
00043
00048 class NamedAttribute: public oasys::Logger {
00049 public:
00053 typedef enum {
00054 ATTRIBUTE_TYPE_INVALID = 0,
00055 ATTRIBUTE_TYPE_INTEGER,
00056 ATTRIBUTE_TYPE_UNSIGNED_INTEGER,
00057 ATTRIBUTE_TYPE_BOOLEAN,
00058 ATTRIBUTE_TYPE_STRING
00059 } attribute_type_t;
00060
00064 static const char* type_to_str(int type) {
00065 switch(type) {
00066 case ATTRIBUTE_TYPE_INVALID: return "invalid";
00067 case ATTRIBUTE_TYPE_INTEGER: return "integer";
00068 case ATTRIBUTE_TYPE_UNSIGNED_INTEGER: return "unsigned integer";
00069 case ATTRIBUTE_TYPE_BOOLEAN: return "boolean";
00070 case ATTRIBUTE_TYPE_STRING: return "string";
00071 }
00072 NOTREACHED;
00073 }
00074
00075 NamedAttribute(const std::string& name, int v)
00076 : Logger("NamedAttribute", "/dtn/attribute/%s", name.c_str()),
00077 name_(name),
00078 type_(ATTRIBUTE_TYPE_INTEGER),
00079 ival_(v), uval_(0), bval_(false), sval_("") {}
00080 NamedAttribute(const std::string& name, u_int v)
00081 : Logger("NamedAttribute", "/dtn/attribute/%s", name.c_str()),
00082 name_(name),
00083 type_(ATTRIBUTE_TYPE_UNSIGNED_INTEGER),
00084 ival_(0), uval_(v), bval_(false), sval_("") {}
00085 NamedAttribute(const std::string& name, bool v)
00086 : Logger("NamedAttribute", "/dtn/attribute/%s", name.c_str()),
00087 name_(name),
00088 type_(ATTRIBUTE_TYPE_BOOLEAN),
00089 ival_(0), uval_(0), bval_(v), sval_("") {}
00090 NamedAttribute(const std::string& name, const std::string& v)
00091 : Logger("NamedAttribute", "/dtn/attribute/%s", name.c_str()),
00092 name_(name),
00093 type_(ATTRIBUTE_TYPE_STRING),
00094 ival_(0), uval_(0), bval_(false), sval_(v) {}
00095
00096 NamedAttribute(const oasys::Builder&)
00097 : Logger("NamedAttribute", "/dtn/attribute/UNKNOWN"),
00098 name_(""),
00099 type_(ATTRIBUTE_TYPE_INVALID),
00100 ival_(0), uval_(0), bval_(false), sval_("") {}
00101
00102 const std::string& name() const { return name_.name(); }
00103 attribute_type_t type() const { return type_; }
00104
00105 int int_val() const {
00106 if (type_ != ATTRIBUTE_TYPE_INTEGER) {
00107 log_debug("NamedAttribute::int_val: "
00108 "invalid type %s",
00109 type_to_str(type_));
00110 }
00111 return ival_;
00112 }
00113 u_int u_int_val() const {
00114 if (type_ != ATTRIBUTE_TYPE_UNSIGNED_INTEGER) {
00115 log_debug("NamedAttribute::u_int_val: "
00116 "invalid type %s",
00117 type_to_str(type_));
00118 }
00119 return uval_;
00120 }
00121 bool bool_val() const {
00122 if (type_ != ATTRIBUTE_TYPE_BOOLEAN) {
00123 log_debug("NamedAttribute::bool_val: "
00124 "invalid type %s",
00125 type_to_str(type_));
00126 }
00127 return bval_;
00128 }
00129 const std::string& string_val() const {
00130 if (type_ != ATTRIBUTE_TYPE_STRING) {
00131 log_debug("NamedAttribute::string_val: "
00132 "invalid type %s",
00133 type_to_str(type_));
00134 }
00135 return sval_;
00136 }
00137
00138 private:
00139 AttributeName name_;
00140
00141 attribute_type_t type_;
00142
00143 int ival_;
00144 u_int uval_;
00145 bool bval_;
00146 std::string sval_;
00147 };
00148
00149 typedef std::vector<AttributeName> AttributeNameVector;
00150 typedef std::vector<NamedAttribute> AttributeVector;
00151
00152 }
00153
00154 #endif