mukesh agrawal | 186af05 | 2012-01-11 15:07:34 -0800 | [diff] [blame] | 1 | // 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 | |
| 12 | using std::map; |
| 13 | using std::string; |
| 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | CallbackList::CallbackList() {} |
| 18 | |
| 19 | CallbackList::~CallbackList() { |
| 20 | for (CallbackMap::iterator it = callbacks_.begin(); |
| 21 | it != callbacks_.end(); |
| 22 | ++it) { |
| 23 | delete it->second; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | void CallbackList::Add(const string &name, Callback *callback) { |
| 28 | DCHECK(!ContainsKey(callbacks_, name)); |
| 29 | callbacks_[name] = callback; |
| 30 | } |
| 31 | |
| 32 | void 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 | |
| 41 | bool 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 |