]> git.stg.codes - stg.git/blobdiff - include/stg/user_traff.h
Merge branch 'stg-2.409' into stg-2.409-radius
[stg.git] / include / stg / user_traff.h
index 7ed1636152a05a7abd13b2ee6b290889f9c0978e..d820588e3702d412436635474996da7d1a78b2c1 100644 (file)
 #ifndef USER_TRAFF_H
 #define USER_TRAFF_H
 
-#include <iostream>
-#include <vector>
-
+#include "resetable.h"
 #include "const.h"
 #include "os_int.h"
 
+#include <iostream>
+#include <vector>
+
 enum TRAFF_DIRECTION {TRAFF_UPLOAD, TRAFF_DOWNLOAD};
 
 class DIR_TRAFF
@@ -40,64 +41,30 @@ class DIR_TRAFF
     friend std::ostream & operator<< (std::ostream & o, const DIR_TRAFF & traff);
 
 public:
-    //-------------------------------------------------------------------------
-    DIR_TRAFF();
-    DIR_TRAFF(const DIR_TRAFF & ts);
-    DIR_TRAFF & operator=(const DIR_TRAFF & ts);
-    ~DIR_TRAFF();
-    uint64_t operator[](int idx) const;
-    uint64_t & operator[](int idx);
-    DIR_TRAFF operator+(const DIR_TRAFF & ts);
+    typedef std::vector<uint64_t> ContainerType;
+    typedef ContainerType::size_type IndexType;
 
-private:
-    std::vector<uint64_t> traff;
-};
-//-----------------------------------------------------------------------------
+    DIR_TRAFF() : traff(DIR_NUM) {}
+    const uint64_t & operator[](IndexType idx) const { return traff[idx]; }
+    uint64_t & operator[](IndexType idx) { return traff[idx]; }
+    IndexType size() const { return traff.size(); }
 
-//-----------------------------------------------------------------------------
-inline DIR_TRAFF::DIR_TRAFF()
-    : traff(DIR_NUM, 0)
-{
-}
-//-----------------------------------------------------------------------------
-inline DIR_TRAFF::DIR_TRAFF(const DIR_TRAFF & ts)
-    : traff(ts.traff)
-{
-}
-//-----------------------------------------------------------------------------
-inline DIR_TRAFF::~DIR_TRAFF()
-{
-}
-//-----------------------------------------------------------------------------
-inline DIR_TRAFF & DIR_TRAFF::operator=(const DIR_TRAFF & ts)
-{
-traff = ts.traff;
-return *this;
-}
-//-----------------------------------------------------------------------------
-inline uint64_t & DIR_TRAFF::operator[](int idx)
-{
-return traff[idx];
-}
-//-----------------------------------------------------------------------------
-inline uint64_t DIR_TRAFF::operator[](int idx) const
-{
-return traff[idx];
-}
-//-----------------------------------------------------------------------------
-inline DIR_TRAFF DIR_TRAFF::operator+(const DIR_TRAFF & ts)
-{
-for (int i = 0; i < DIR_NUM; i++)
+    void Reset()
     {
-    traff[i] = traff[i] + ts.traff[i];
+    for (IndexType i = 0; i < traff.size(); ++i)
+        traff[i] = 0;
     }
-return *this;
-}
+
+private:
+    ContainerType traff;
+};
+
 //-----------------------------------------------------------------------------
-inline std::ostream & operator<<(std::ostream & o, const DIR_TRAFF & traff)
+inline
+std::ostream & operator<<(std::ostream & o, const DIR_TRAFF & traff)
 {
 bool first = true;
-for (size_t i = 0; i < DIR_NUM; ++i)
+for (DIR_TRAFF::IndexType i = 0; i < traff.size(); ++i)
     {
     if (first)
         first = false;
@@ -107,5 +74,40 @@ for (size_t i = 0; i < DIR_NUM; ++i)
     }
 return o;
 }
-//-----------------------------------------------------------------------------
+
+class DIR_TRAFF_RES
+{
+public:
+    typedef RESETABLE<uint64_t> ValueType;
+    typedef std::vector<ValueType> ContainerType;
+    typedef ContainerType::size_type IndexType;
+
+    DIR_TRAFF_RES() : traff(DIR_NUM) {}
+    explicit DIR_TRAFF_RES(const DIR_TRAFF & ts)
+        : traff(ts.size())
+    {
+    for (IndexType i = 0; i < ts.size(); ++i)
+        traff[i] = ts[i];
+    }
+    DIR_TRAFF_RES & operator=(const DIR_TRAFF & ts)
+    {
+        for (IndexType i = 0; i < ts.size(); ++i)
+            traff[i] = ts[i];
+        return *this;
+    }
+    const ValueType & operator[](IndexType idx) const { return traff[idx]; }
+    ValueType & operator[](IndexType idx) { return traff[idx]; }
+    DIR_TRAFF GetData() const
+    {
+    DIR_TRAFF res;
+    for (IndexType i = 0; i < traff.size(); ++i)
+        if (!traff[i].empty())
+            res[i] = traff[i].data();
+    return res;
+    }
+
+private:
+    ContainerType traff;
+};
+
 #endif