From 052c4516d11fee162946422a9a491a153fdf20e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Sat, 26 Mar 2011 10:18:09 +0000 Subject: [PATCH] Make more value methods virtual. --- trace_model.cpp | 84 +++++++++++++++++++++++++++---------------------- trace_model.hpp | 37 +++++++++++++++++++--- 2 files changed, 80 insertions(+), 41 deletions(-) diff --git a/trace_model.cpp b/trace_model.cpp index a10eef5..7f2ef44 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 NULL; } +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 NULL; } +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 NULL; } +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); } @@ -253,40 +297,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 { diff --git a/trace_model.hpp b/trace_model.hpp index 62316e9..54c142e 100644 --- a/trace_model.hpp +++ b/trace_model.hpp @@ -54,10 +54,10 @@ public: virtual ~Value() {} virtual void visit(Visitor &visitor) = 0; - operator bool (void) const; - operator signed long long (void) const; - operator unsigned long long (void) const; - operator double (void) const; + virtual operator bool (void) const = 0; + virtual operator signed long long (void) const; + virtual operator unsigned long long (void) const; + virtual operator double (void) const; virtual void *blob(void) const; const char *string(void) const; @@ -105,6 +105,10 @@ public: class Null : public Value { public: + operator bool (void) const; + operator signed long long (void) const; + operator unsigned long long (void) const; + operator double (void) const; void *blob(void) const; void visit(Visitor &visitor); }; @@ -117,6 +121,10 @@ class Bool : public Value public: Bool(bool _value) : value(_value) {} + operator bool (void) const; + operator signed long long (void) const; + operator unsigned long long (void) const; + operator double (void) const; void visit(Visitor &visitor); bool value; @@ -128,6 +136,10 @@ class SInt : public Value public: SInt(signed long long _value) : value(_value) {} + operator bool (void) const; + operator signed long long (void) const; + operator unsigned long long (void) const; + operator double (void) const; void visit(Visitor &visitor); signed long long value; @@ -139,6 +151,10 @@ class UInt : public Value public: UInt(unsigned long long _value) : value(_value) {} + operator bool (void) const; + operator signed long long (void) const; + operator unsigned long long (void) const; + operator double (void) const; void visit(Visitor &visitor); unsigned long long value; @@ -150,6 +166,10 @@ class Float : public Value public: Float(double _value) : value(_value) {} + operator bool (void) const; + operator signed long long (void) const; + operator unsigned long long (void) const; + operator double (void) const; void visit(Visitor &visitor); double value; @@ -161,6 +181,7 @@ class String : public Value public: String(std::string _value) : value(_value) {} + operator bool (void) const; void visit(Visitor &visitor); std::string value; @@ -174,6 +195,10 @@ public: Enum(const Signature *_sig) : sig(_sig) {} + operator bool (void) const; + operator signed long long (void) const; + operator unsigned long long (void) const; + operator double (void) const; void visit(Visitor &visitor); const Signature *sig; @@ -205,6 +230,7 @@ public: Struct(Signature *_sig) : sig(_sig), members(_sig->member_names.size()) { } ~Struct(); + operator bool (void) const; void visit(Visitor &visitor); const Signature *sig; @@ -218,6 +244,7 @@ public: Array(size_t len) : values(len) {} ~Array(); + operator bool (void) const; void visit(Visitor &visitor); std::vector values; @@ -234,6 +261,7 @@ public: ~Blob(); + operator bool (void) const; void *blob(void) const; void visit(Visitor &visitor); @@ -247,6 +275,7 @@ class Pointer : public UInt public: Pointer(unsigned long long value) : UInt(value) {} + operator bool (void) const; void *blob(void) const; void visit(Visitor &visitor); }; -- 2.45.2