counter.cc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <vector>
  2. #include <boost/noncopyable.hpp>
  3. #include <statistics/counter.h>
  4. namespace {
  5. const unsigned int InitialValue = 0;
  6. } // namespace
  7. namespace isc {
  8. namespace statistics {
  9. class CounterImpl : boost::noncopyable {
  10. private:
  11. std::vector<Counter::Value> counters_;
  12. public:
  13. CounterImpl(const size_t nelements);
  14. ~CounterImpl();
  15. void inc(const Counter::Type&);
  16. const Counter::Value& get(const Counter::Type&) const;
  17. };
  18. CounterImpl::CounterImpl(const size_t items) :
  19. counters_(items, InitialValue)
  20. {
  21. if (items == 0) {
  22. isc_throw(isc::InvalidParameter, "Items must not be 0");
  23. }
  24. }
  25. CounterImpl::~CounterImpl() {}
  26. void
  27. CounterImpl::inc(const Counter::Type& type) {
  28. if(type >= counters_.size()) {
  29. isc_throw(isc::OutOfRange, "Counter type is out of range");
  30. }
  31. ++counters_.at(type);
  32. return;
  33. }
  34. const Counter::Value&
  35. CounterImpl::get(const Counter::Type& type) const {
  36. if(type >= counters_.size()) {
  37. isc_throw(isc::OutOfRange, "Counter type is out of range");
  38. }
  39. return (counters_.at(type));
  40. }
  41. Counter::Counter(const size_t items) : impl_(new CounterImpl(items))
  42. {}
  43. Counter::~Counter() {}
  44. void
  45. Counter::inc(const Type& type) {
  46. impl_->inc(type);
  47. return;
  48. }
  49. const Counter::Value&
  50. Counter::get(const Type& type) const {
  51. return (impl_->get(type));
  52. }
  53. } // namespace statistics
  54. } // namespace isc