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