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 <simics/util/help-macros.h>
20
21#include <list>
22#include <map>
23#include <cassert>
24
25namespace simics {
26namespace systemc {
27
28template<class T>
29class Registrant;
30
31template<class T>
32class Registry {
33 public:
35 for (auto i = items_.begin(); i != items_.end(); ++i) {
36 Registrant<T> *r = dynamic_cast<Registrant<T> *>(*i);
37 FATAL_ERROR_IF(!r, "Registry: unexpected non-Registrant stored");
38 r->detach();
39 }
40 }
41 void add(T *impl) {
42 iterators_[impl] = items_.insert(items_.end(), impl);
43 }
44 void remove(T *impl) {
45 typename Iterators::iterator i = iterators_.find(impl);
46 if (i == iterators_.end())
47 return;
48
49 items_.erase(i->second);
50 iterators_.erase(i);
51 }
52 template<class F>
53 bool iterate(F *f) {
54 typename Items::iterator i = items_.begin();
55 while (i != items_.end())
56 if ((*f)(*i++))
57 return true;
58
59 return false;
60 }
61 template<class F>
62 bool reverseIterate(F *f) {
63 typename Items::reverse_iterator i;
64 for (i = items_.rbegin(); i != items_.rend(); ++i)
65 if ((*f)(*i))
66 return true;
67
68 return false;
69 }
71 static Registry<T> registry;
72 return &registry;
73 }
74
75 protected:
76 typedef std::list<T *> Items;
77 typedef std::map<T *, typename Items::iterator> Iterators;
78
81
82 private:
83 Registry() {}
84 Registry(const Registry &rhs);
85 Registry& operator=(const Registry &rhs);
86};
87
88template<class T>
89class Registrant : public T {
90 public:
91 Registrant() : detach_(false) {
92 Registry<T>::instance()->add(this);
93 }
94
95 virtual ~Registrant() {
96 if (!detach_)
97 Registry<T>::instance()->remove(this);
98 }
99 Registrant(const Registrant &rhs) : detach_(false) {
100 Registry<T>::instance()->add(this);
101 }
102 void detach() {
103 detach_ = true;
104 }
105 private:
106 Registrant& operator=(const Registrant &rhs);
107 bool detach_;
108};
109
110} // namespace systemc
111} // namespace simics
112
113#endif
Definition: registry.h:89
Registrant()
Definition: registry.h:91
virtual ~Registrant()
Definition: registry.h:95
Registrant(const Registrant &rhs)
Definition: registry.h:99
void detach()
Definition: registry.h:102
Definition: registry.h:32
std::map< T *, typename Items::iterator > Iterators
Definition: registry.h:77
std::list< T * > Items
Definition: registry.h:76
Iterators iterators_
Definition: registry.h:80
void add(T *impl)
Definition: registry.h:41
void remove(T *impl)
Definition: registry.h:44
bool iterate(F *f)
Definition: registry.h:53
static Registry< T > * instance()
Definition: registry.h:70
bool reverseIterate(F *f)
Definition: registry.h:62
~Registry()
Definition: registry.h:34
Items items_
Definition: registry.h:79
Definition: adapter.h:81