source: trunk/lib/StatsCollection.cc @ 532

Last change on this file since 532 was 532, checked in by Peter Johansson, 14 years ago

Node holds a StatsCollection? rather than a Stats object.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: StatsCollection.cc 532 2007-12-26 01:47:03Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of svndigest, http://trac.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "StatsCollection.h"
25
26#include "ClassicStats.h"
27#include "Stats.h"
28
29#include <cassert>
30#include <map>
31#include <stdexcept>
32#include <string>
33
34#include <iostream>
35
36namespace theplu{
37namespace svndigest{
38
39
40  StatsCollection::StatsCollection(const std::string& path)
41    : path_(path)
42  {
43    stats_["classic"] = new ClassicStats(path);
44  }
45
46
47  StatsCollection::~StatsCollection(void)
48  {
49    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
50      assert(i->second);
51      delete i->second;
52    }
53
54  }
55
56
57  bool StatsCollection::load_cache(std::istream& is)
58  {
59    bool result = true;
60    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
61      if(!i->second->load_cache(is)){
62        result = false;
63        i->second->reset();
64        i->second->parse(path_);
65      }
66    }
67    return result;
68  }
69
70
71  const std::map<std::string, Stats*>& StatsCollection::stats(void) const
72  {
73    return stats_;
74  }
75
76
77  const StatsCollection& StatsCollection::parse(const std::string& path)
78  {
79    for (map::iterator i(stats_.begin()); i!=stats_.end(); ++i) {
80      i->second->parse(path);
81    }
82    return *this;
83  }
84
85
86  void StatsCollection::print(std::ostream& os)
87  {
88    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
89      i->second->print(os);
90    }
91  }
92
93
94  void StatsCollection::reset(void)
95  {
96    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
97      i->second->reset();
98    }
99  }
100
101
102  const StatsCollection& StatsCollection::operator+=(const StatsCollection& rhs)
103  {
104    assert(stats_.size()==rhs.stats_.size());
105    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
106      *(i->second) += rhs[i->first];
107    }
108    return *this;
109  }
110
111
112  const Stats& StatsCollection::operator[](const std::string& key) const
113  {
114    map::const_iterator iter = stats_.find(key);
115    if (iter==stats_.end())
116      throw std::runtime_error(key + 
117                               std::string(" not found in StatsCollection"));
118    return *(iter->second);
119  }
120
121
122  Stats& StatsCollection::operator[](const std::string& key)
123  {
124    assert(stats_.find(key)!=stats_.end());
125    return *(stats_[key]);
126  }
127
128
129}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.