]> git.stg.codes - stg.git/blob - include/stg/user_traff.h
b949da5fc2583534bc3d5fd89c8c0f16f2c3f95b
[stg.git] / include / stg / user_traff.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 #pragma once
22
23 #include "stg/optional.h"
24 #include "const.h"
25
26 #include <ostream>
27 #include <vector>
28 #include <cstdint>
29
30 namespace STG
31 {
32
33 enum TraffDirection {TRAFF_UPLOAD, TRAFF_DOWNLOAD};
34
35 class DirTraff
36 {
37     friend std::ostream& operator<< (std::ostream& stream, const DirTraff& traff);
38
39     public:
40         using ContainerType = std::vector<uint64_t>;
41         using IndexType = ContainerType::size_type;
42
43         DirTraff() noexcept : traff(DIR_NUM) {}
44         const uint64_t & operator[](IndexType idx) const noexcept { return traff[idx]; }
45         uint64_t & operator[](IndexType idx) noexcept { return traff[idx]; }
46         IndexType size() const noexcept { return traff.size(); }
47
48         void reset() noexcept
49         {
50             for (IndexType i = 0; i < traff.size(); ++i)
51                 traff[i] = 0;
52         }
53
54     private:
55         ContainerType traff;
56 };
57
58 //-----------------------------------------------------------------------------
59 inline
60 std::ostream& operator<<(std::ostream& stream, const DirTraff& traff)
61 {
62     bool first = true;
63     for (DirTraff::IndexType i = 0; i < traff.size(); ++i)
64     {
65         if (first)
66             first = false;
67         else
68             stream << ",";
69         stream << traff[i];
70     }
71     return stream;
72 }
73
74 class DirTraffOpt
75 {
76     public:
77         using ValueType = Optional<uint64_t>;
78         using ContainerType = std::vector<ValueType>;
79         using IndexType = ContainerType::size_type;
80
81         DirTraffOpt()  noexcept: traff(DIR_NUM) {}
82         explicit DirTraffOpt(const DirTraff & ts) noexcept
83             : traff(ts.size())
84         {
85             for (IndexType i = 0; i < ts.size(); ++i)
86                 traff[i] = ts[i];
87         }
88         DirTraffOpt& operator=(const DirTraff& ts) noexcept
89         {
90             for (IndexType i = 0; i < ts.size(); ++i)
91                 traff[i] = ts[i];
92             return *this;
93         }
94         const ValueType & operator[](IndexType idx) const noexcept { return traff[idx]; }
95         ValueType & operator[](IndexType idx) noexcept { return traff[idx]; }
96         IndexType size() const noexcept { return traff.size(); }
97
98     private:
99         ContainerType traff;
100 };
101
102 }