X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_model.cpp;h=b083186b01bd94d26d437f359fb9df1bfd2eefe7;hb=a263fc592702c2906232810a2aaa11075cfcb9fb;hp=a10eef51e6353ca884ec8117565caf9ab9f17860;hpb=60ac78c465ecaf50ed8da304e90a6da6a3005e73;p=apitrace diff --git a/trace_model.cpp b/trace_model.cpp index a10eef5..b083186 100644 --- a/trace_model.cpp +++ b/trace_model.cpp @@ -62,7 +62,51 @@ Blob::~Blob() { } -// virtual Value::blob() +// bool cast +Null ::operator bool(void) const { return false; } +Bool ::operator bool(void) const { return value; } +SInt ::operator bool(void) const { return value != 0; } +UInt ::operator bool(void) const { return value != 0; } +Float ::operator bool(void) const { return value != 0; } +String ::operator bool(void) const { return true; } +Enum ::operator bool(void) const { return static_cast(*sig->second); } +Struct ::operator bool(void) const { return true; } +Array ::operator bool(void) const { return true; } +Blob ::operator bool(void) const { return true; } +Pointer::operator bool(void) const { return value != 0; } + + +// signed integer cast +Value ::operator signed long long (void) const { assert(0); return 0; } +Null ::operator signed long long (void) const { return 0; } +Bool ::operator signed long long (void) const { return static_cast(value); } +SInt ::operator signed long long (void) const { return value; } +UInt ::operator signed long long (void) const { assert(static_cast(value) >= 0); return static_cast(value); } +Float ::operator signed long long (void) const { return static_cast(value); } +Enum ::operator signed long long (void) const { return static_cast(*sig->second); } + + +// unsigned integer cast +Value ::operator unsigned long long (void) const { assert(0); return 0; } +Null ::operator unsigned long long (void) const { return 0; } +Bool ::operator unsigned long long (void) const { return static_cast(value); } +SInt ::operator unsigned long long (void) const { assert(value >= 0); return static_cast(value); } +UInt ::operator unsigned long long (void) const { return value; } +Float ::operator unsigned long long (void) const { return static_cast(value); } +Enum ::operator unsigned long long (void) const { return static_cast(*sig->second); } + + +// floating point cast +Value ::operator double (void) const { assert(0); return 0; } +Null ::operator double (void) const { return 0; } +Bool ::operator double (void) const { return static_cast(value); } +SInt ::operator double (void) const { return static_cast(value); } +UInt ::operator double (void) const { return static_cast(value); } +Float ::operator double (void) const { return value; } +Enum ::operator double (void) const { return static_cast(*sig->second); } + + +// blob cast void * Value ::blob(void) const { assert(0); return NULL; } void * Null ::blob(void) const { return NULL; } void * Blob ::blob(void) const { return buf; } @@ -90,8 +134,8 @@ void Visitor::visit(SInt *) { assert(0); } void Visitor::visit(UInt *) { assert(0); } void Visitor::visit(Float *) { assert(0); } void Visitor::visit(String *) { assert(0); } -void Visitor::visit(Enum *) { assert(0); } -void Visitor::visit(Bitmask *bitmask) { visit(static_cast(bitmask)); } +void Visitor::visit(Enum *node) { _visit(node->sig->second); } +void Visitor::visit(Bitmask *node) { visit(static_cast(node)); } void Visitor::visit(Struct *) { assert(0); } void Visitor::visit(Array *) { assert(0); } void Visitor::visit(Blob *) { assert(0); } @@ -152,7 +196,35 @@ public: } void visit(String *node) { - os << literal << '"' << node->value << '"' << normal; + os << literal << "\""; + for (std::string::const_iterator it = node->value.begin(); it != node->value.end(); ++it) { + unsigned char c = (unsigned char) *it; + if (c == '\"') + os << "\\\""; + else if (c == '\\') + os << "\\\\"; + else if (c >= 0x20 && c <= 0x7e) + os << c; + else if (c == '\t') { + os << "\t"; + } else if (c == '\r') { + // Ignore carriage-return + } else if (c == '\n') { + // Reset formatting so that it looks correct with 'less -R' + os << normal << '\n' << literal; + } else { + unsigned octal0 = c & 0x7; + unsigned octal1 = (c >> 3) & 0x7; + unsigned octal2 = (c >> 3) & 0x7; + os << "\\"; + if (octal2) + os << octal2; + if (octal1) + os << octal1; + os << octal0; + } + } + os << "\"" << normal; } void visit(Enum *node) { @@ -164,8 +236,8 @@ public: const Bitmask::Signature *sig = bitmask->sig; bool first = true; for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) { - assert(it->second); - if ((value & it->second) == it->second) { + if ((it->second && (value & it->second) == it->second) || + (!it->second && value == 0)) { if (!first) { os << " | "; } @@ -253,40 +325,6 @@ static inline const Value *unwrap(const Value *node) { } -Value::operator bool(void) const { - const Bool *b = dynamic_cast(unwrap(this)); - if (b) - return b->value; - assert(0); - return false; -} - -Value::operator signed long long(void) const { - const SInt *sint = dynamic_cast(unwrap(this)); - if (sint) - return sint->value; - const UInt *uint = dynamic_cast(unwrap(this)); - if (uint) - return uint->value; - assert(0); - return 0; -} - -Value::operator unsigned long long(void) const { - const UInt *uint = dynamic_cast(unwrap(this)); - if (uint) - return uint->value; - assert(0); - return 0; -} - - -Value::operator double(void) const { - const Float *fl = dynamic_cast(unwrap(this)); - assert(fl); - return fl->value; -} - static Null null; const Value & Value::operator[](size_t index) const {