• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 INCLUDE_PERFETTO_BASE_LOOKUP_SET_H_
18 #define INCLUDE_PERFETTO_BASE_LOOKUP_SET_H_
19 
20 #include <set>
21 
22 namespace perfetto {
23 namespace base {
24 
25 // Set that allows lookup from const member of the object.
26 template <typename T, typename U, U T::*p>
27 class LookupSet {
28  public:
Get(const U & key)29   T* Get(const U& key) {
30     // This will be nicer with C++14 transparent comparators.
31     // Then we will be able to look up by just the key using a sutiable
32     // comparator.
33     //
34     // For now we need to allow to construct a T from the key.
35     T node(key);
36     auto it = set_.find(node);
37     if (it == set_.end())
38       return nullptr;
39     return const_cast<T*>(&(*it));
40   }
41 
42   template <typename... P>
Emplace(P &&...args)43   T* Emplace(P&&... args) {
44     auto r = set_.emplace(std::forward<P>(args)...);
45     return const_cast<T*>(&(*r.first));
46   }
47 
Remove(const T & child)48   bool Remove(const T& child) { return set_.erase(child); }
49 
50   static_assert(std::is_const<U>::value, "key must be const");
51 
52  private:
53   class Comparator {
54    public:
operator()55     bool operator()(const T& one, const T& other) const {
56       return (&one)->*p < (&other)->*p;
57     }
58   };
59 
60   std::set<T, Comparator> set_;
61 };
62 
63 }  // namespace base
64 }  // namespace perfetto
65 
66 #endif  // INCLUDE_PERFETTO_BASE_LOOKUP_SET_H_
67