X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_model.hpp;h=5c51bba7d06c345a541b8dca59cebe1291d546f6;hb=3c70dbfb4ca01d92172f87109ba995f6db5f7835;hp=8436fc1afac77bb7dd6ab8a08b5351a71d458376;hpb=4aed84a7b7dcec68a2ae0fce952cb8a189fc5c77;p=apitrace diff --git a/trace_model.hpp b/trace_model.hpp index 8436fc1..5c51bba 100644 --- a/trace_model.hpp +++ b/trace_model.hpp @@ -31,11 +31,9 @@ #define _TRACE_MODEL_HPP_ -#include +#include -#include #include -#include #include #include @@ -43,73 +41,81 @@ namespace Trace { -class Visitor; -class Dumper; -class UInt; +typedef unsigned Id; -class Value -{ -public: - virtual ~Value() {} - virtual void visit(Visitor &visitor) = 0; +struct FunctionSig { + Id id; + const char *name; + unsigned num_args; + const char **arg_names; +}; - 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; +struct StructSig { + Id id; + const char *name; + unsigned num_members; + const char **member_names; +}; - inline operator signed char (void) const { - return static_cast(*this); - } - inline operator unsigned char (void) const { - return static_cast(*this); - } +struct EnumSig { + Id id; + const char *name; + signed long long value; +}; - inline operator signed short (void) const { - return static_cast(*this); - } - inline operator unsigned short (void) const { - return static_cast(*this); - } +struct BitmaskFlag { + const char *name; + unsigned long long value; +}; - inline operator signed (void) const { - return static_cast(*this); - } - inline operator unsigned (void) const { - return static_cast(*this); - } +struct BitmaskSig { + Id id; + unsigned num_flags; + const BitmaskFlag *flags; +}; - inline operator signed long (void) const { - return static_cast(*this); - } - inline operator unsigned long (void) const { - return static_cast(*this); - } +class Visitor; - inline operator float (void) const { - return static_cast(*this); - } + +class Value +{ +public: + virtual ~Value() {} + virtual void visit(Visitor &visitor) = 0; + + virtual bool toBool(void) const = 0; + virtual signed long long toSInt(void) const; + virtual unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; + + virtual void *toPointer(void) const; + virtual unsigned long long toUIntPtr(void) const; + virtual const char *toString(void) const; const Value & operator[](size_t index) const; + + void dump(std::ostream &os, bool color=true); }; 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; + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; + void *toPointer(void) const; + unsigned long long toUIntPtr(void) const; + const char *toString(void) const; void visit(Visitor &visitor); }; @@ -119,10 +125,11 @@ 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; + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; void visit(Visitor &visitor); bool value; @@ -134,10 +141,11 @@ 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; + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; void visit(Visitor &visitor); signed long long value; @@ -149,10 +157,11 @@ 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; + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; void visit(Visitor &visitor); unsigned long long value; @@ -164,10 +173,11 @@ 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; + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; void visit(Visitor &visitor); double value; @@ -177,73 +187,53 @@ public: class String : public Value { public: - String(std::string _value) : value(_value) {} + String(const char * _value) : value(_value) {} - operator bool (void) const; + bool toBool(void) const; + const char *toString(void) const; void visit(Visitor &visitor); - std::string value; + const char * value; }; class Enum : public Value { public: - struct Signature : public std::pair - { - Signature() - : std::pair() - {} - Signature(const std::string &n, Trace::Value *val) - : std::pair(n, val) - {} - ~Signature() - { - delete second; - } - }; - - Enum(const Signature *_sig) : sig(_sig) {} + Enum(const EnumSig *_sig) : sig(_sig) {} - operator bool (void) const; - operator signed long long (void) const; - operator unsigned long long (void) const; - operator double (void) const; + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; void visit(Visitor &visitor); - const Signature *sig; + const EnumSig *sig; }; class Bitmask : public UInt { public: - typedef std::pair Pair; - typedef std::vector Signature; - - Bitmask(const Signature *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {} + Bitmask(const BitmaskSig *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {} void visit(Visitor &visitor); - const Signature *sig; + const BitmaskSig *sig; }; class Struct : public Value { public: - struct Signature { - std::string name; - std::vector member_names; - }; - - Struct(Signature *_sig) : sig(_sig), members(_sig->member_names.size()) { } + Struct(StructSig *_sig) : sig(_sig), members(_sig->num_members) { } ~Struct(); - operator bool (void) const; + bool toBool(void) const; void visit(Visitor &visitor); - const Signature *sig; + const StructSig *sig; std::vector members; }; @@ -254,7 +244,7 @@ public: Array(size_t len) : values(len) {} ~Array(); - operator bool (void) const; + bool toBool(void) const; void visit(Visitor &visitor); std::vector values; @@ -271,8 +261,8 @@ public: ~Blob(); - operator bool (void) const; - void *blob(void) const; + bool toBool(void) const; + void *toPointer(void) const; void visit(Visitor &visitor); size_t size; @@ -285,8 +275,9 @@ class Pointer : public UInt public: Pointer(unsigned long long value) : UInt(value) {} - operator bool (void) const; - void *blob(void) const; + bool toBool(void) const; + void *toPointer(void) const; + unsigned long long toUIntPtr(void) const; void visit(Visitor &visitor); }; @@ -316,41 +307,42 @@ protected: }; -std::ostream & operator <<(std::ostream &os, Value *value); - - -signed long long asSInt(const Value &node); -unsigned long long asUInt(const Value &node); -double asFloat(const Value &node); +inline std::ostream & operator <<(std::ostream &os, Value *value) { + if (value) { + value->dump(os); + } + return os; +} class Call { public: - struct Signature { - std::string name; - std::vector arg_names; - }; - unsigned no; - const Signature *sig; + const FunctionSig *sig; std::vector args; Value *ret; - Call(Signature *_sig) : sig(_sig), args(_sig->arg_names.size()), ret(0) { } + Call(FunctionSig *_sig) : sig(_sig), args(_sig->num_args), ret(0) { } ~Call(); - inline const std::string name(void) const { + inline const char * name(void) const { return sig->name; } inline Value & arg(unsigned index) { + assert(index < args.size()); return *(args[index]); } + + void dump(std::ostream &os, bool color=true); }; -std::ostream & operator <<(std::ostream &os, Call &call); +inline std::ostream & operator <<(std::ostream &os, Call &call) { + call.dump(os); + return os; +} } /* namespace Trace */