source: branches/0.7-stable/lib/StatsCollection.cc @ 1075

Last change on this file since 1075 was 1075, checked in by Peter Johansson, 11 years ago

Fixes severe bug that we allowed copy construction of StatsCollection?
(compiler generated), which involved copying of pointers, which were
deleted in destructor, i.e., we allowed multiple deletion.

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