OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CONTENT_RENDERER_MEDIA_TAGGED_LIST_H_ | |
6 #define CONTENT_RENDERER_MEDIA_TAGGED_LIST_H_ | |
7 | |
8 #include <algorithm> | |
9 #include <list> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted.h" | |
14 | |
15 namespace content { | |
16 | |
17 // Implements the pattern of a list of items, where added items are | |
18 // tagged, you can tag all items, and you can retrieve the list of | |
19 // items that are tagged, which removes the tag. | |
20 // | |
21 // For thread safety, operations on this object should be under an | |
22 // external lock. An internally-locked version could be created, but | |
23 // is not needed at the moment as users already lock. | |
24 template <class ItemType> | |
25 class TaggedList { | |
26 public: | |
27 typedef std::list<scoped_refptr<ItemType> > ItemList; | |
28 | |
29 TaggedList() {} | |
30 | |
31 void AddAndTag(ItemType* item) { | |
32 items_.push_back(item); | |
33 tagged_items_.push_back(item); | |
34 } | |
35 | |
36 void TagAll() { | |
37 tagged_items_ = items_; | |
38 } | |
39 | |
40 const ItemList& Items() const { | |
41 return items_; | |
42 } | |
43 | |
44 // Retrieves the list of items with tags, and removes their tags. | |
45 // | |
46 // |dest| should be empty. | |
47 void RetrieveAndClearTags(ItemList* dest) { | |
48 DCHECK(dest->empty()); | |
49 dest->swap(tagged_items_); | |
50 } | |
51 | |
52 // Remove an item that matches a predicate. Will return a reference | |
53 // to it if it is found. | |
54 template <class UnaryPredicate> | |
55 scoped_refptr<ItemType> Remove(UnaryPredicate predicate) { | |
56 tagged_items_.remove_if(predicate); | |
57 | |
58 typename ItemList::iterator it = std::find_if( | |
59 items_.begin(), items_.end(), predicate); | |
60 if (it != items_.end()) { | |
61 scoped_refptr<ItemType> removed_item = *it; | |
62 items_.erase(it); | |
63 return removed_item; | |
64 } | |
65 | |
66 return NULL; | |
67 } | |
68 | |
69 template <class UnaryPredicate> | |
70 bool Contains(UnaryPredicate predicate) const { | |
71 return std::find_if(items_.begin(), items_.end(), predicate) != | |
72 items_.end(); | |
73 } | |
74 | |
75 void Clear() { | |
76 items_.clear(); | |
77 tagged_items_.clear(); | |
78 } | |
79 | |
80 bool IsEmpty() const { | |
81 bool is_empty = items_.empty(); | |
82 DCHECK(!is_empty || tagged_items_.empty()); | |
83 return is_empty; | |
84 } | |
85 | |
86 private: | |
87 ItemList items_; | |
88 ItemList tagged_items_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(TaggedList); | |
91 }; | |
92 | |
93 } // namespace content | |
94 | |
95 #endif // CONTENT_RENDERER_MEDIA_TAGGED_LIST_H_ | |
OLD | NEW |