changeset 4f887be9e1b6 in /z/repo/m5
details: http://repo.m5sim.org/m5?cmd=changeset;node=4f887be9e1b6
description:
        stats: clean up how templates are used on the data side.
        This basically works by taking advantage of the curiously recurring 
template
        pattern in an intelligent way so as to reduce the number of lines of 
code
        and hopefully make things a little bit clearer.

diffstat:

2 files changed, 326 insertions(+), 413 deletions(-)
src/base/statistics.cc |  100 +++----
src/base/statistics.hh |  639 ++++++++++++++++++++----------------------------

diffs (truncated from 1375 to 300 lines):

diff -r 3cf8e71257e0 -r 4f887be9e1b6 src/base/statistics.cc
--- a/src/base/statistics.cc    Thu Mar 05 19:09:53 2009 -0800
+++ b/src/base/statistics.cc    Thu Mar 05 19:09:53 2009 -0800
@@ -158,7 +158,7 @@
 bool
 Info::baseCheck() const
 {
-    if (!(flags & init)) {
+    if (!(flags & Stats::init)) {
 #ifdef DEBUG
         cprintf("this is stat number %d\n", id);
 #endif
@@ -175,55 +175,6 @@
 }
 
 
-void
-FormulaBase::result(VResult &vec) const
-{
-    if (root)
-        vec = root->result();
-}
-
-Result
-FormulaBase::total() const
-{
-    return root ? root->total() : 0.0;
-}
-
-size_type
-FormulaBase::size() const
-{
-    if (!root)
-        return 0;
-    else
-        return root->size();
-}
-
-void
-FormulaBase::reset()
-{
-}
-
-bool
-FormulaBase::zero() const
-{
-    VResult vec;
-    result(vec);
-    for (off_t i = 0; i < vec.size(); ++i)
-        if (vec[i] != 0.0)
-            return false;
-    return true;
-}
-
-void
-FormulaBase::update(Info *)
-{
-}
-
-string
-FormulaBase::str() const
-{
-    return root ? root->str() : "";
-}
-
 Formula::Formula()
 {
     setInit();
@@ -256,6 +207,55 @@
 }
 
 void
+Formula::result(VResult &vec) const
+{
+    if (root)
+        vec = root->result();
+}
+
+Result
+Formula::total() const
+{
+    return root ? root->total() : 0.0;
+}
+
+size_type
+Formula::size() const
+{
+    if (!root)
+        return 0;
+    else
+        return root->size();
+}
+
+void
+Formula::reset()
+{
+}
+
+bool
+Formula::zero() const
+{
+    VResult vec;
+    result(vec);
+    for (off_t i = 0; i < vec.size(); ++i)
+        if (vec[i] != 0.0)
+            return false;
+    return true;
+}
+
+void
+Formula::update()
+{
+}
+
+string
+Formula::str() const
+{
+    return root ? root->str() : "";
+}
+
+void
 check()
 {
     typedef list<Info *>::iterator iter_t;
diff -r 3cf8e71257e0 -r 4f887be9e1b6 src/base/statistics.hh
--- a/src/base/statistics.hh    Thu Mar 05 19:09:53 2009 -0800
+++ b/src/base/statistics.hh    Thu Mar 05 19:09:53 2009 -0800
@@ -115,6 +115,14 @@
     virtual ~Info();
 
     /**
+     * Check that this stat has been set up properly and is ready for
+     * use
+     * @return true for success
+     */
+    virtual bool check() const = 0;
+    bool baseCheck() const;
+
+    /**
      * Reset the stat to the default state.
      */
     virtual void reset() = 0;
@@ -126,14 +134,6 @@
     virtual bool zero() const = 0;
 
     /**
-     * Check that this stat has been set up properly and is ready for
-     * use
-     * @return true for success
-     */
-    virtual bool check() const = 0;
-    bool baseCheck() const;
-
-    /**
      * Visitor entry for outputing statistics data
      */
     virtual void visit(Visit &visitor) = 0;
@@ -187,8 +187,8 @@
 {
   public:
     /** Names and descriptions of subfields. */
-    mutable std::vector<std::string> subnames;
-    mutable std::vector<std::string> subdescs;
+    std::vector<std::string> subnames;
+    std::vector<std::string> subdescs;
 
   public:
     virtual size_type size() const = 0;
@@ -242,7 +242,7 @@
     visit(Visit &visitor)
     {
         this->update();
-        this->s.update(this);
+        this->s.update();
         visitor.visit(*this);
     }
 };
@@ -275,7 +275,7 @@
     void
     visit(Visit &visitor)
     {
-        this->s.update(this);
+        this->s.update();
         visitor.visit(*this);
     }
 };
@@ -285,9 +285,9 @@
   public:
     std::vector<DistData> data;
 
-   /** Names and descriptions of subfields. */
-    mutable std::vector<std::string> subnames;
-    mutable std::vector<std::string> subdescs;
+    /** Names and descriptions of subfields. */
+    std::vector<std::string> subnames;
+    std::vector<std::string> subdescs;
 
   protected:
     /** Local storage for the entry values, used for printing. */
@@ -320,7 +320,7 @@
     visit(Visit &visitor)
     {
         this->update();
-        this->s.update(this);
+        this->s.update();
         visitor.visit(*this);
     }
 };
@@ -333,10 +333,11 @@
     std::vector<std::string> subdescs;
     std::vector<std::string> y_subnames;
 
+    size_type x;
+    size_type y;
+
     /** Local storage for the entry values, used for printing. */
     mutable VCounter cvec;
-    mutable size_type x;
-    mutable size_type y;
 
   public:
     void
@@ -357,7 +358,7 @@
     visit(Visit &visitor)
     {
         this->update();
-        this->s.update(this);
+        this->s.update();
         visitor.visit(*this);
     }
 };
@@ -397,29 +398,27 @@
     bool check() const { return true; }
 };
 
-template <class Derived, class Base, template <class> class Info>
-class DataWrap : public Base
+template <class Derived, template <class> class InfoType>
+class DataWrap : public InfoAccess
 {
   public:
-    typedef Derived DerivedType;
-    typedef Base BaseType;
-    typedef Info<Base> InfoType;
+    typedef InfoType<Derived> Info;
 
   protected:
-    Derived &self() { return *reinterpret_cast<Derived *>(this); }
+    Derived &self() { return *static_cast<Derived *>(this); }
 
   protected:
-    InfoType *
+    Info *
     info()
     {
-        return safe_cast<InfoType *>(InfoAccess::info());
+        return safe_cast<Info *>(InfoAccess::info());
     }
 
   public:
-    const InfoType *
+    const Info *
     info() const
     {
-        return safe_cast<const InfoType *>(InfoAccess::info());
+        return safe_cast<const Info *>(InfoAccess::info());
     }
 
   protected:
@@ -436,7 +435,7 @@
   public:
     DataWrap()
     {
-        this->setInfo(new InfoType(*this));
+        this->setInfo(new Info(self()));
     }
 
     /**
@@ -447,11 +446,12 @@
     Derived &
     name(const std::string &_name)
     {
-        InfoType *info = this->info();
+        Info *info = this->info();
         info->name = _name;
         info->flags |= print;
         return this->self();
     }
+    const std::string &name() const { return this->info()->name; }
 
     /**
      * Set the description and marks this stat to print at the end of
@@ -505,15 +505,12 @@
     }
 };
 
-template <class Derived, class Base, template <class Base> class Info>
-class DataWrapVec : public DataWrap<Derived, Base, Info>
+template <class Derived, template <class> class InfoType>
+class DataWrapVec : public DataWrap<Derived, InfoType>
_______________________________________________
m5-dev mailing list
m5-dev@m5sim.org
http://m5sim.org/mailman/listinfo/m5-dev

Reply via email to