Reduce dependency of TiclInvalidationService on Profile
[chromium-blink-merge.git] / cc / base / ref_counted_managed.h
blob8bb836f015957ceecac9ee7d0e04193bb0ee3e91
1 // Copyright 2013 The Chromium 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.
5 #ifndef CC_BASE_REF_COUNTED_MANAGED_H_
6 #define CC_BASE_REF_COUNTED_MANAGED_H_
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "cc/base/cc_export.h"
12 namespace cc {
14 template <typename T> class RefCountedManaged;
16 template <typename T>
17 class CC_EXPORT RefCountedManager {
18 protected:
19 RefCountedManager() : live_object_count_(0) {}
20 ~RefCountedManager() {
21 CHECK_EQ(0, live_object_count_);
24 virtual void Release(T* object) = 0;
26 private:
27 friend class RefCountedManaged<T>;
28 int live_object_count_;
31 template <typename T>
32 class CC_EXPORT RefCountedManaged : public base::subtle::RefCountedBase {
33 public:
34 explicit RefCountedManaged(RefCountedManager<T>* manager)
35 : manager_(manager) {
36 manager_->live_object_count_++;
39 void AddRef() const {
40 base::subtle::RefCountedBase::AddRef();
43 void Release() {
44 if (base::subtle::RefCountedBase::Release()) {
45 DCHECK_GT(manager_->live_object_count_, 0);
46 manager_->live_object_count_--;
48 // This must be the last statement in case manager deletes
49 // the object immediately.
50 manager_->Release(static_cast<T*>(this));
54 protected:
55 ~RefCountedManaged() {}
57 private:
58 RefCountedManager<T>* manager_;
60 DISALLOW_COPY_AND_ASSIGN(RefCountedManaged<T>);
63 } // namespace cc
65 #endif // CC_BASE_REF_COUNTED_MANAGED_H_