SystemC Library API Reference Manual
Reference documentation for the Simics SystemC Library.
 
Loading...
Searching...
No Matches
registry.h
Go to the documentation of this file.
1// -*- mode: C++; c-file-style: "virtutech-c++" -*-
2
3/*
4 © 2017 Intel Corporation
5
6 This software and the related documents are Intel copyrighted materials, and
7 your use of them is governed by the express license under which they were
8 provided to you ("License"). Unless the License provides otherwise, you may
9 not use, modify, copy, publish, distribute, disclose or transmit this software
10 or the related documents without Intel's prior written permission.
11
12 This software and the related documents are provided as is, with no express or
13 implied warranties, other than those that are expressly stated in the License.
14*/
15
16#ifndef SIMICS_SYSTEMC_REGISTRY_H
17#define SIMICS_SYSTEMC_REGISTRY_H
18
19#include <list>
20#include <map>
21#include <cassert>
22
23namespace simics {
24namespace systemc {
25
26template<class T>
27class Registrant;
28
29template<class T>
30class Registry {
31 public:
33 for (auto i = items_.begin(); i != items_.end(); ++i) {
34 Registrant<T> *r = dynamic_cast<Registrant<T> *>(*i);
35 // dynamic_cast to Registrant<T> from T will always work
36 assert(r != nullptr);
37 r->detach();
38 }
39 }
40 void add(T *impl) {
41 iterators_[impl] = items_.insert(items_.end(), impl);
42 }
43 void remove(T *impl) {
44 typename Iterators::iterator i = iterators_.find(impl);
45 if (i == iterators_.end())
46 return;
47
48 items_.erase(i->second);
49 iterators_.erase(i);
50 }
51 template<class F>
52 bool iterate(F *f) {
53 typename Items::iterator i = items_.begin();
54 while (i != items_.end())
55 if ((*f)(*i++))
56 return true;
57
58 return false;
59 }
60 template<class F>
61 bool reverseIterate(F *f) {
62 typename Items::reverse_iterator i;
63 for (i = items_.rbegin(); i != items_.rend(); ++i)
64 if ((*f)(*i))
65 return true;
66
67 return false;
68 }
70 static Registry<T> registry;
71 return &registry;
72 }
73
74 protected:
75 typedef std::list<T *> Items;
76 typedef std::map<T *, typename Items::iterator> Iterators;
77
80
81 private:
82 Registry() {}
83 Registry(const Registry &rhs);
84 Registry& operator=(const Registry &rhs);
85};
86
87template<class T>
88class Registrant : public T {
89 public:
90 Registrant() : detach_(false) {
91 Registry<T>::instance()->add(this);
92 }
93
94 virtual ~Registrant() {
95 if (!detach_)
96 Registry<T>::instance()->remove(this);
97 }
98 Registrant(const Registrant &rhs) : detach_(false) {
99 Registry<T>::instance()->add(this);
100 }
101 void detach() {
102 detach_ = true;
103 }
104 private:
105 Registrant& operator=(const Registrant &rhs);
106 bool detach_;
107};
108
109} // namespace systemc
110} // namespace simics
111
112#endif
Definition: registry.h:88
Registrant()
Definition: registry.h:90
virtual ~Registrant()
Definition: registry.h:94
Registrant(const Registrant &rhs)
Definition: registry.h:98
void detach()
Definition: registry.h:101
Definition: registry.h:30
std::map< T *, typename Items::iterator > Iterators
Definition: registry.h:76
std::list< T * > Items
Definition: registry.h:75
Iterators iterators_
Definition: registry.h:79
void add(T *impl)
Definition: registry.h:40
void remove(T *impl)
Definition: registry.h:43
bool iterate(F *f)
Definition: registry.h:52
static Registry< T > * instance()
Definition: registry.h:69
bool reverseIterate(F *f)
Definition: registry.h:61
~Registry()
Definition: registry.h:32
Items items_
Definition: registry.h:78
Definition: pci_bus_interface.h:24