File: registered_objects.h

package info (click to toggle)
chromium 145.0.7632.109-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,974,804 kB
  • sloc: cpp: 36,197,696; ansic: 7,602,761; javascript: 3,563,590; python: 1,649,324; xml: 838,427; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,022; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (89 lines) | stat: -rw-r--r-- 3,096 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PERFORMANCE_MANAGER_REGISTERED_OBJECTS_H_
#define COMPONENTS_PERFORMANCE_MANAGER_REGISTERED_OBJECTS_H_

#include <type_traits>

#include "base/check_op.h"
#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"

namespace performance_manager {

// Container for holding registered objects. The objects are stored as raw
// pointers. At most a single instance of an object of a given type may exist
// at a moment. It is expected that RegisteredType satisfies the following
// interface:
//
//   // Returns the type id of the derived type.
//   uintptr_t GetTypeId() const;
//
// The container is expected to be empty by the time of its destruction.
template <typename RegisteredType>
class RegisteredObjects {
 public:
  RegisteredObjects() = default;
  ~RegisteredObjects() { CHECK(objects_.empty()); }

  RegisteredObjects(const RegisteredObjects&) = delete;
  RegisteredObjects& operator=(const RegisteredObjects&) = delete;

  // Registers an object with this container. No more than one object of a given
  // type may be registered at once.
  void RegisterObject(RegisteredType* object) {
    CHECK_EQ(nullptr, GetRegisteredObject(object->GetTypeId()));
    objects_.insert(object);
    // If there are ever so many registered objects we should consider changing
    // data structures.
    CHECK_GT(100u, objects_.size());
  }

  // Unregisters an object from this container. The object must previously have
  // been registered.
  void UnregisterObject(RegisteredType* object) {
    CHECK_EQ(object, GetRegisteredObject(object->GetTypeId()));
    objects_.erase(object);
  }

  // Returns the object with the registered type, nullptr if none exists.
  RegisteredType* GetRegisteredObject(uintptr_t type_id) {
    auto it = objects_.find(type_id);
    if (it == objects_.end())
      return nullptr;
    CHECK_EQ((*it)->GetTypeId(), type_id);
    return *it;
  }

  // Returns the current size of this container.
  size_t size() const { return objects_.size(); }

  // Returns true if this container is empty.
  bool empty() const { return objects_.empty(); }

 private:
  // Comparator for registered objects. They are stored by raw pointers but
  // sorted by their type IDs. This is a transparent comparator that also allows
  // comparing with type IDs directly.
  struct RegisteredComparator {
    using is_transparent = void;
    bool operator()(const RegisteredType* r1, const RegisteredType* r2) const {
      return r1->GetTypeId() < r2->GetTypeId();
    }
    bool operator()(const RegisteredType* r1, uintptr_t type_id) const {
      return r1->GetTypeId() < type_id;
    }
    bool operator()(uintptr_t type_id, const RegisteredType* r2) const {
      return type_id < r2->GetTypeId();
    }
  };

  base::flat_set<raw_ptr<RegisteredType, CtnExperimental>, RegisteredComparator>
      objects_;
};

}  // namespace performance_manager

#endif  // COMPONENTS_PERFORMANCE_MANAGER_REGISTERED_OBJECTS_H_