]> git.cworth.org Git - apitrace/blobdiff - trace_model.hpp
More efficient enum representation.
[apitrace] / trace_model.hpp
index 9bb3e1d6e63930b07b31e36f2a0801cff86c2c05..5c162bc7f0da6ca76b276dbfd3bcf2a5b76755ef 100644 (file)
@@ -48,10 +48,56 @@ class Value
 {
 public:
    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;
+
+   void *blob(void) const;
+   const char *string(void) const;
+
+   inline operator signed char (void) const { 
+      return static_cast<signed long long>(*this);
+   }
+
+   inline operator unsigned char (void) const { 
+      return static_cast<signed long long>(*this);
+   }
+
+   inline operator signed short (void) const { 
+      return static_cast<signed long long>(*this);
+   }
+
+   inline operator unsigned short (void) const { 
+      return static_cast<unsigned long long>(*this);
+   }
+
+   inline operator signed (void) const { 
+      return static_cast<signed long long>(*this);
+   }
+
+   inline operator unsigned (void) const { 
+      return static_cast<unsigned long long>(*this);
+   }
+
+   inline operator signed long (void) const { 
+      return static_cast<signed long long>(*this);
+   }
+
+   inline operator unsigned long (void) const { 
+      return static_cast<unsigned long long>(*this);
+   }
+
+   inline operator float (void) const { 
+      return static_cast<double>(*this);
+   }
+
+   const Value & operator[](size_t index) const;
 };
 
 
-class Void : public Value
+class Null : public Value
 {
 public:
    void visit(Visitor &visitor);
@@ -113,10 +159,10 @@ public:
 };
 
 
-class Const : public Value
+class Enum : public Value
 {
 public:
-   Const(std::string _name, Value *_value) : name(_name), value(_value) {}
+   Enum(std::string &_name, Value *_value) : name(_name), value(_value) {}
 
    void visit(Visitor &visitor);
 
@@ -125,6 +171,20 @@ public:
 };
 
 
+class Bitmask : public UInt
+{
+public:
+   typedef std::pair<std::string, unsigned long long> Pair;
+   typedef std::vector<Pair> Signature;
+
+   Bitmask(const Signature *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {}
+
+   void visit(Visitor &visitor);
+
+   const Signature *sig;
+};
+
+
 class Array : public Value
 {
 public:
@@ -136,46 +196,77 @@ public:
 };
 
 
+class Blob : public Value
+{
+public:
+   Blob(size_t _size) {
+       size = _size;
+       buf = new char[_size];
+   }
+
+   ~Blob() {
+       delete [] buf;
+   }
+
+   void visit(Visitor &visitor);
+
+   size_t size;
+   char *buf;
+};
+
+
 class Visitor
 {
 public:
-   virtual void visit(Void *) {assert(0);}
+   virtual void visit(Null *) {assert(0);}
    virtual void visit(Bool *) {assert(0);}
    virtual void visit(SInt *) {assert(0);}
    virtual void visit(UInt *) {assert(0);}
    virtual void visit(Float *) {assert(0);}
    virtual void visit(String *) {assert(0);}
-   virtual void visit(Const *) {assert(0);}
+   virtual void visit(Enum *) {assert(0);}
+   virtual void visit(Bitmask *bitmask) {visit(static_cast<UInt *>(bitmask));}
    virtual void visit(Array *) {assert(0);}
+   virtual void visit(Blob *) {assert(0);}
+
+protected:
+   inline void _visit(Value *value) {
+      if (value) { 
+         value->visit(*this); 
+      }
+   }
 };
 
 
 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);
-
+signed long long asSInt(const Value &node);
+unsigned long long asUInt(const Value &node);
+double asFloat(const Value &node);
 
-typedef std::pair<std::string, Value *> Arg;
 
 class Call
 {
 public:
-   std::string name;
-   std::list<Arg> args;
+   struct Signature {
+      std::string name;
+      std::vector<std::string> arg_names;
+   };
+
+   unsigned no;
+   const Signature *sig;
+   std::vector<Value *> args;
    Value *ret;
 
-   Call() : ret(0) { }
+   Call(Signature *_sig) : sig(_sig), ret(0) { }
 
-   Value * get_arg(const char *name) {
-      for (std::list<Arg>::iterator it = args.begin(); it != args.end(); ++it) {
-         if (it->first == name) {
-            return it->second;
-         }
-      }
-      return NULL;
+   inline const std::string name(void) const {
+       return sig->name;
+   }
+
+   inline Value & arg(unsigned index) {
+       return *(args[index]);
    }
 };