From c9edb83044ae17a35c42c10504ffffd0f12f3598 Mon Sep 17 00:00:00 2001 From: José Fonseca Date: Sat, 20 Nov 2010 09:03:10 +0000 Subject: Make better use of C++ implicit casts. Support arrays. --- trace_model.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'trace_model.cpp') diff --git a/trace_model.cpp b/trace_model.cpp index 8f102247..b8325878 100644 --- a/trace_model.cpp +++ b/trace_model.cpp @@ -121,26 +121,27 @@ std::ostream & operator <<(std::ostream &os, Value *value) { } -static const Value *unwrap(const Value &node) { - const Const *c = dynamic_cast(&node); +static inline const Value *unwrap(const Value *node) { + const Const *c = dynamic_cast(node); if (c) return c->value; - return &node; + return node; } -signed long long asSInt(const Value &node) { - const SInt *sint = dynamic_cast(unwrap(node)); + +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(node)); + const UInt *uint = dynamic_cast(unwrap(this)); if (uint) return uint->value; assert(0); return 0; } -unsigned long long asUInt(const Value &node) { - const UInt *uint = dynamic_cast(unwrap(node)); +Value::operator unsigned long long(void) const { + const UInt *uint = dynamic_cast(unwrap(this)); if (uint) return uint->value; assert(0); @@ -148,14 +149,24 @@ unsigned long long asUInt(const Value &node) { } -double asFloat(const Value &node) { - const Float *fl = dynamic_cast(unwrap(node)); +Value::operator double(void) const { + const Float *fl = dynamic_cast(unwrap(this)); assert(fl); return fl->value; } static Void void_; +const Value & Value::operator[](size_t index) const { + const Array *array = dynamic_cast(unwrap(this)); + if (array) { + if (index < array->values.size()) { + return *array->values[index]; + } + } + return void_; +} + Value & Call::arg(const char *name) { for (std::list::iterator it = args.begin(); it != args.end(); ++it) { if (it->first == name) { -- cgit v1.2.3