X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/798b4c9ddc6425be51b1a70231f20d32254cede1..8272f5d139d947fdbe469dfd7afc3ddd6ea7a603:/include/stg/user_traff.h?ds=inline diff --git a/include/stg/user_traff.h b/include/stg/user_traff.h index 7ed16361..b949da5f 100644 --- a/include/stg/user_traff.h +++ b/include/stg/user_traff.h @@ -18,94 +18,85 @@ * Author : Boris Mikhailenko */ -/* - $Revision: 1.7 $ - $Date: 2010/10/07 19:48:52 $ - $Author: faust $ - */ +#pragma once -#ifndef USER_TRAFF_H -#define USER_TRAFF_H +#include "stg/optional.h" +#include "const.h" -#include +#include #include +#include -#include "const.h" -#include "os_int.h" +namespace STG +{ -enum TRAFF_DIRECTION {TRAFF_UPLOAD, TRAFF_DOWNLOAD}; +enum TraffDirection {TRAFF_UPLOAD, TRAFF_DOWNLOAD}; -class DIR_TRAFF +class DirTraff { - 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); - -private: - std::vector traff; + friend std::ostream& operator<< (std::ostream& stream, const DirTraff& traff); + + public: + using ContainerType = std::vector; + using IndexType = ContainerType::size_type; + + DirTraff() noexcept : traff(DIR_NUM) {} + const uint64_t & operator[](IndexType idx) const noexcept { return traff[idx]; } + uint64_t & operator[](IndexType idx) noexcept { return traff[idx]; } + IndexType size() const noexcept { return traff.size(); } + + void reset() noexcept + { + for (IndexType i = 0; i < traff.size(); ++i) + traff[i] = 0; + } + + private: + ContainerType traff; }; -//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -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) +inline +std::ostream& operator<<(std::ostream& stream, const DirTraff& traff) { -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++) + bool first = true; + for (DirTraff::IndexType i = 0; i < traff.size(); ++i) { - traff[i] = traff[i] + ts.traff[i]; + if (first) + first = false; + else + stream << ","; + stream << traff[i]; } -return *this; + return stream; } -//----------------------------------------------------------------------------- -inline std::ostream & operator<<(std::ostream & o, const DIR_TRAFF & traff) + +class DirTraffOpt { -bool first = true; -for (size_t i = 0; i < DIR_NUM; ++i) - { - if (first) - first = false; - else - o << ","; - o << traff[i]; - } -return o; + public: + using ValueType = Optional; + using ContainerType = std::vector; + using IndexType = ContainerType::size_type; + + DirTraffOpt() noexcept: traff(DIR_NUM) {} + explicit DirTraffOpt(const DirTraff & ts) noexcept + : traff(ts.size()) + { + for (IndexType i = 0; i < ts.size(); ++i) + traff[i] = ts[i]; + } + DirTraffOpt& operator=(const DirTraff& ts) noexcept + { + for (IndexType i = 0; i < ts.size(); ++i) + traff[i] = ts[i]; + return *this; + } + const ValueType & operator[](IndexType idx) const noexcept { return traff[idx]; } + ValueType & operator[](IndexType idx) noexcept { return traff[idx]; } + IndexType size() const noexcept { return traff.size(); } + + private: + ContainerType traff; +}; + } -//----------------------------------------------------------------------------- -#endif