blob: ea2103ebeefba2ad1567659c8d60deb99f8af9a6 [file] [log] [blame]
$$ This is a pump file for generating file templates. Pump is a python
$$ script that is part of the Google Test suite of utilities. Description
$$ can be found here:
$$
$$ http://code.google.com/p/googletest/wiki/PumpManual
$$
$$ See comment for MAX_ARITY in base/bind.h.pump.
$var MAX_ARITY = 7
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_CALLBACK_LIST_H_
#define BASE_CALLBACK_LIST_H_
#include <list>
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/callback_internal.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
// OVERVIEW:
//
// A container for a list of callbacks. Unlike a normal STL vector or list,
// this container can be modified during iteration without invalidating the
// iterator. It safely handles the case of a callback removing itself
// or another callback from the list while callbacks are being run.
//
// TYPICAL USAGE:
//
// class MyWidget {
// public:
// ...
//
// typedef base::Callback<void(const Foo&)> OnFooCallback;
//
// scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription>
// RegisterCallback(const OnFooCallback& cb) {
// return callback_list_.Add(cb);
// }
//
// private:
// void NotifyFoo(const Foo& foo) {
// callback_list_.Notify(foo);
// }
//
// base::CallbackList<void(const Foo&)> callback_list_;
//
// DISALLOW_COPY_AND_ASSIGN(MyWidget);
// };
//
//
// class MyWidgetListener {
// public:
// MyWidgetListener::MyWidgetListener() {
// foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback(
// base::Bind(&MyWidgetListener::OnFoo, this)));
// }
//
// MyWidgetListener::~MyWidgetListener() {
// // Subscription gets deleted automatically and will deregister
// // the callback in the process.
// }
//
// private:
// void OnFoo(const Foo& foo) {
// // Do something.
// }
//
// scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription>
// foo_subscription_;
//
// DISALLOW_COPY_AND_ASSIGN(MyWidgetListener);
// };
namespace base {
namespace internal {
template <typename CallbackType>
class CallbackListBase {
public:
class Subscription {
public:
Subscription(CallbackListBase<CallbackType>* list,
typename std::list<CallbackType>::iterator iter)
: list_(list),
iter_(iter) {
}
~Subscription() {
if (list_->active_iterator_count_)
iter_->Reset();
else
list_->callbacks_.erase(iter_);
}
private:
CallbackListBase<CallbackType>* list_;
typename std::list<CallbackType>::iterator iter_;
DISALLOW_COPY_AND_ASSIGN(Subscription);
};
// Add a callback to the list. The callback will remain registered until the
// returned Subscription is destroyed, which must occur before the
// CallbackList is destroyed.
scoped_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT {
DCHECK(!cb.is_null());
return scoped_ptr<Subscription>(
new Subscription(this, callbacks_.insert(callbacks_.end(), cb)));
}
protected:
// An iterator class that can be used to access the list of callbacks.
class Iterator {
public:
explicit Iterator(CallbackListBase<CallbackType>* list)
: list_(list),
list_iter_(list_->callbacks_.begin()) {
++list_->active_iterator_count_;
}
Iterator(const Iterator& iter)
: list_(iter.list_),
list_iter_(iter.list_iter_) {
++list_->active_iterator_count_;
}
~Iterator() {
if (list_ && --list_->active_iterator_count_ == 0) {
list_->Compact();
}
}
CallbackType* GetNext() {
while ((list_iter_ != list_->callbacks_.end()) && list_iter_->is_null())
++list_iter_;
CallbackType* cb = NULL;
if (list_iter_ != list_->callbacks_.end()) {
cb = &(*list_iter_);
++list_iter_;
}
return cb;
}
private:
CallbackListBase<CallbackType>* list_;
typename std::list<CallbackType>::iterator list_iter_;
};
CallbackListBase() : active_iterator_count_(0) {}
~CallbackListBase() {
DCHECK_EQ(0, active_iterator_count_);
DCHECK_EQ(0U, callbacks_.size());
}
// Returns an instance of a CallbackListBase::Iterator which can be used
// to run callbacks.
Iterator GetIterator() {
return Iterator(this);
}
// Compact the list: remove any entries which were NULLed out during
// iteration.
void Compact() {
typename std::list<CallbackType>::iterator it = callbacks_.begin();
while (it != callbacks_.end()) {
if ((*it).is_null())
it = callbacks_.erase(it);
else
++it;
}
}
private:
std::list<CallbackType> callbacks_;
int active_iterator_count_;
DISALLOW_COPY_AND_ASSIGN(CallbackListBase);
};
} // namespace internal
template <typename Sig> class CallbackList;
$range ARITY 0..MAX_ARITY
$for ARITY [[
$range ARG 1..ARITY
$if ARITY == 0 [[
template <>
class CallbackList<void(void)>
: public internal::CallbackListBase<Callback<void(void)> > {
]] $else [[
template <$for ARG , [[typename A$(ARG)]]>
class CallbackList<void($for ARG , [[A$(ARG)]])>
: public internal::CallbackListBase<Callback<void($for ARG , [[A$(ARG)]])> > {
]]
public:
$if ARITY == 0 [[
typedef Callback<void(void)> CallbackType;
]] $else [[
typedef Callback<void($for ARG , [[A$(ARG)]])> CallbackType;
]]
CallbackList() {}
void Notify($for ARG ,
[[typename internal::CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) {
$if ARITY == 0 [[
internal::CallbackListBase<CallbackType>::Iterator it =
this->GetIterator();
]] $else [[
typename internal::CallbackListBase<CallbackType>::Iterator it =
this->GetIterator();
]]
CallbackType* cb;
while ((cb = it.GetNext()) != NULL) {
cb->Run($for ARG , [[a$(ARG)]]);
}
}
private:
DISALLOW_COPY_AND_ASSIGN(CallbackList);
};
]] $$ for ARITY
} // namespace base
#endif // BASE_CALLBACK_LIST_H_