blob: d4419896967d75dcc81000c1cff07e101317d210 [file] [log] [blame]
mukesh agrawal186af052012-01-11 15:07:34 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "callback_list.h"
6
7#include <string>
8
9#include <base/logging.h>
10#include <base/stl_util-inl.h>
11
12using std::map;
13using std::string;
14
15namespace shill {
16
17CallbackList::CallbackList() {}
18
19CallbackList::~CallbackList() {
20 for (CallbackMap::iterator it = callbacks_.begin();
21 it != callbacks_.end();
22 ++it) {
23 delete it->second;
24 }
25}
26
27void CallbackList::Add(const string &name, Callback *callback) {
28 DCHECK(!ContainsKey(callbacks_, name));
29 callbacks_[name] = callback;
30}
31
32void CallbackList::Remove(const string &name) {
33 DCHECK(ContainsKey(callbacks_, name));
34 CallbackMap::iterator it = callbacks_.find(name);
35 if (it != callbacks_.end()) {
36 delete it->second;
37 callbacks_.erase(it);
38 }
39}
40
41bool CallbackList::Run() const {
42 bool ret = true;
43 for (CallbackMap::const_iterator it = callbacks_.begin();
44 it != callbacks_.end();
45 ++it) {
46 VLOG(2) << "Running callback " << it->first;
47 bool res;
48 res = it->second->Run();
49 VLOG(2) << "Callback " << it->first << " returned " << res;
50 ret = ret && res;
51 }
52 return ret;
53}
54
55} // namespace shill