1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_FILTERFW_BASE_UTILITIES_H
18#define ANDROID_FILTERFW_BASE_UTILITIES_H
19
20#include <set>
21#include <utility>
22
23namespace android {
24namespace filterfw {
25
26// STLDeleteContainerPointers()
27//  For a range within a container of pointers, calls delete
28//  (non-array version) on these pointers.
29// NOTE: for these three functions, we could just implement a DeleteObject
30// functor and then call for_each() on the range and functor, but this
31// requires us to pull in all of algorithm.h, which seems expensive.
32// For hash_[multi]set, it is important that this deletes behind the iterator
33// because the hash_set may call the hash function on the iterator when it is
34// advanced, which could result in the hash function trying to deference a
35// stale pointer.
36template <class ForwardIterator>
37void STLDeleteContainerPointers(ForwardIterator begin,
38                                ForwardIterator end) {
39  while (begin != end) {
40    ForwardIterator temp = begin;
41    ++begin;
42    delete *temp;
43  }
44}
45
46// Given an STL container consisting of (key, value) pairs, STLDeleteValues
47// deletes all the "value" components and clears the container.  Does nothing
48// in the case it's given a NULL pointer.
49template <class T>
50void STLDeleteValues(T *v) {
51  if (!v) return;
52  for (typename T::iterator i = v->begin(); i != v->end(); ++i) {
53    delete i->second;
54  }
55  v->clear();
56}
57
58// Perform a lookup in a map or hash_map.
59// If the key is present a const pointer to the associated value is returned,
60// otherwise a NULL pointer is returned.
61template <class Collection>
62const typename Collection::value_type::second_type*
63FindOrNull(const Collection& collection,
64           const typename Collection::value_type::first_type& key) {
65  typename Collection::const_iterator it = collection.find(key);
66  if (it == collection.end()) {
67    return 0;
68  }
69  return &it->second;
70}
71
72// A simple class that gives checklist functionality: There are essemtially two
73// operations defined on a CheckList:
74//  - Adding a new (unchecked) item.
75//  - Checking off an item.
76// When checking off the last remaining item CheckItem() returns true.
77template<typename T>
78class CheckList {
79  public:
80    // Add a new unchecked item. Does nothing if item is already in checklist.
81    void AddItem(const T& item);
82
83    // Check off an item in the checklist. Returns true if all items have been
84    // checked.
85    bool CheckItem(const T& item);
86
87    // Clear the checklist.
88    void Clear() {
89      items_.clear();
90    }
91
92  private:
93    std::set<T> items_;
94};
95
96template<typename T>
97void CheckList<T>::AddItem(const T& item) {
98  if (!ContainsKey(items_, item))
99    items_.insert(item);
100}
101
102template<typename T>
103bool CheckList<T>::CheckItem(const T& item) {
104  typename std::set<T>::iterator iter = items_.find(item);
105  if (iter != items_.end())
106    items_.erase(iter);
107  return items_.empty();
108}
109
110// Perform a lookup in a map or hash_map whose values are pointers.
111// If the key is present a const pointer to the associated value is returned,
112// otherwise a NULL pointer is returned.
113// This function does not distinguish between a missing key and a key mapped
114// to a NULL value.
115template <class Collection>
116const typename Collection::value_type::second_type
117FindPtrOrNull(const Collection& collection,
118              const typename Collection::value_type::first_type& key) {
119  typename Collection::const_iterator it = collection.find(key);
120  if (it == collection.end()) {
121    return 0;
122  }
123  return it->second;
124}
125
126// Test to see if a set, map, hash_set or hash_map contains a particular key.
127// Returns true if the key is in the collection.
128template <typename Collection, typename Key>
129bool ContainsKey(const Collection& collection, const Key& key) {
130  return collection.find(key) != collection.end();
131}
132
133// Insert a new key and value into a map or hash_map.
134// If the key is not present in the map the key and value are
135// inserted, otherwise nothing happens. True indicates that an insert
136// took place, false indicates the key was already present.
137template <class Collection, class Key, class Value>
138bool InsertIfNotPresent(Collection * const collection,
139                        const Key& key, const Value& value) {
140  std::pair<typename Collection::iterator, bool> ret =
141    collection->insert(typename Collection::value_type(key, value));
142  return ret.second;
143}
144
145} // namespace filterfw
146} // namespace android
147
148#endif // ANDROID_FILTERFW_BASE_UTILITIES_H
149